From 8466a84b1d128f1c2222678653765ad9f97f66af Mon Sep 17 00:00:00 2001 From: cnaude Date: Sat, 9 Jun 2018 10:07:46 -0700 Subject: [PATCH] Add nick timer to try and switch back to primary nick. --- .../com/cnaude/purpleirc/NickWatcher.java | 70 +++++++++++++++++++ .../java/com/cnaude/purpleirc/PurpleBot.java | 12 +++- src/main/resources/SampleBot.yml | 2 + 3 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/cnaude/purpleirc/NickWatcher.java diff --git a/src/main/java/com/cnaude/purpleirc/NickWatcher.java b/src/main/java/com/cnaude/purpleirc/NickWatcher.java new file mode 100644 index 0000000..17dbe1b --- /dev/null +++ b/src/main/java/com/cnaude/purpleirc/NickWatcher.java @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2014 cnaude + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.cnaude.purpleirc; +import org.bukkit.scheduler.BukkitTask; + +/** + * + * @author Chris Naude Poll the command queue and dispatch to Bukkit + */ +public class NickWatcher { + + private final PurpleIRC plugin; + private final PurpleBot ircBot; + private BukkitTask bt = null; + + /** + * + * @param ircBot + * @param plugin + */ + public NickWatcher(final PurpleBot ircBot, final PurpleIRC plugin) { + this.ircBot = ircBot; + this.plugin = plugin; + startWatcher(); + } + + private void startWatcher() { + plugin.logDebug("Starting nick watcher"); + bt = this.plugin.getServer().getScheduler().runTaskTimerAsynchronously(this.plugin, new Runnable() { + @Override + public void run() { + checkNick(); + } + }, ircBot.nickTimer, ircBot.nickTimer); + } + + private void checkNick() { + if (ircBot.bot == null) { + return; + } + if (!ircBot.bot.isConnected()) { + return; + } + if (!ircBot.bot.getNick().equals(ircBot.nick)) { + plugin.logDebug("My nick is wrong. Attempting to change..."); + // we don't want to continusouly cycle thorugh our nicks + ircBot.nickIndex = 0; + ircBot.bot.sendIRC().changeNick(ircBot.nick); + } + } + + public void cancel() { + this.bt.cancel(); + } + +} diff --git a/src/main/java/com/cnaude/purpleirc/PurpleBot.java b/src/main/java/com/cnaude/purpleirc/PurpleBot.java index 722c8db..34a6cba 100644 --- a/src/main/java/com/cnaude/purpleirc/PurpleBot.java +++ b/src/main/java/com/cnaude/purpleirc/PurpleBot.java @@ -119,7 +119,8 @@ public final class PurpleBot { public String nick; public String botNick; public List altNicks; - private int nickIndex = 0; + public int nickTimer; + public int nickIndex = 0; public String botLogin; public String botRealName; public int ircMaxLineLength; @@ -186,6 +187,7 @@ public final class PurpleBot { private final ArrayList ircListeners; public IRCMessageQueueWatcher messageQueue; public FloodChecker floodChecker; + public NickWatcher nickWatcher; protected boolean floodControlEnabled; protected int floodControlMaxMessages; protected long floodControlTimeInterval; @@ -316,6 +318,9 @@ public final class PurpleBot { messageQueue = new IRCMessageQueueWatcher(this, plugin); floodChecker = new FloodChecker(this, plugin); + if (nickTimer > 0) { + nickWatcher = new NickWatcher(this, plugin); + } } @@ -792,6 +797,7 @@ public final class PurpleBot { nick = config.getString("nick", ""); botNick = nick; altNicks = config.getStringList("alt-nicks"); + nickTimer = config.getInt("alt-nick-timer", 1200); plugin.loadTemplates(config, botNick, "message-format"); botLogin = config.getString("login", "PircBot"); botRealName = config.getString("realname", ""); @@ -3357,7 +3363,7 @@ public final class PurpleBot { if (isMessageEnabled(channel, TemplateName.IRC_HERO_PART)) { String hChannel = heroChannelMap.get(channel.getName()); String template = plugin.getMessageTemplate(botNick, channelName, TemplateName.IRC_HERO_PART); - String message = plugin.tokenizer.ircChatToHeroChatTokenizer(this, user, channel, template,hChannel); + String message = plugin.tokenizer.ircChatToHeroChatTokenizer(this, user, channel, template, hChannel); plugin.herochatHook.sendHeroMessage(hChannel, message); } @@ -3435,7 +3441,7 @@ public final class PurpleBot { if (isMessageEnabled(channel, TemplateName.IRC_HERO_TOPIC)) { String hChannel = heroChannelMap.get(channel.getName()); String template = plugin.getMessageTemplate(botNick, channelName, TemplateName.IRC_HERO_TOPIC); - String message = plugin.tokenizer.ircChatToHeroChatTokenizer(this, user, channel, template, topic,hChannel); + String message = plugin.tokenizer.ircChatToHeroChatTokenizer(this, user, channel, template, topic, hChannel); plugin.herochatHook.sendHeroMessage(hChannel, message); } diff --git a/src/main/resources/SampleBot.yml b/src/main/resources/SampleBot.yml index 1385796..3797071 100644 --- a/src/main/resources/SampleBot.yml +++ b/src/main/resources/SampleBot.yml @@ -9,6 +9,8 @@ nick: AwesomeBot alt-nicks: - '%NICK%_' - '%NICK%__' +# tick interval for attempting to switch back to primary nick. set to 0 to disable. +alt-nick-timer: 1200 # login - Your bot's login name login: AwesomeName # realname