From 8ec2a46c0a0a9e6577eddb7cdd130946e4d188e6 Mon Sep 17 00:00:00 2001 From: cnaude Date: Fri, 6 Nov 2015 15:08:22 -0700 Subject: [PATCH] Add zero-width-space option for bots. --- .../purpleirc/IRCMessageQueueWatcher.java | 33 +++++++++++++++++-- .../java/com/cnaude/purpleirc/PurpleBot.java | 2 ++ .../purpleirc/Utilities/ChatTokenizer.java | 3 +- src/main/resources/SampleBot.yml | 2 ++ 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/cnaude/purpleirc/IRCMessageQueueWatcher.java b/src/main/java/com/cnaude/purpleirc/IRCMessageQueueWatcher.java index 9bca4fd..ef5b655 100644 --- a/src/main/java/com/cnaude/purpleirc/IRCMessageQueueWatcher.java +++ b/src/main/java/com/cnaude/purpleirc/IRCMessageQueueWatcher.java @@ -16,10 +16,13 @@ */ package com.cnaude.purpleirc; +import java.util.ArrayList; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; +import org.pircbotx.Channel; +import org.pircbotx.User; /** * @@ -62,14 +65,14 @@ public class IRCMessageQueueWatcher { if (ircMessage != null) { plugin.logDebug("[" + queue.size() + "]: queueAndSend message detected"); for (String s : cleanupAndSplitMessage(ircMessage.message)) { - if (ircMessage.ctcpResponse) { + if (ircMessage.ctcpResponse) { blockingCTCPMessage(ircMessage.target, s); - } else { + } else { blockingIRCMessage(ircMessage.target, s); + } } } } - } private void blockingIRCMessage(final String target, final String message) { if (!ircBot.isConnected()) { @@ -89,7 +92,31 @@ public class IRCMessageQueueWatcher { plugin.logDebug("[blockingCTCPMessage] Message sent to " + target + ": " + message); } + private String addZeroWidthSpace(String s) { + if (s.length() > 1) { + String a = s.substring(0, 1); + String b = s.substring(1); + return a + "\u200B" + b; + } + return s; + } + + private String pingFix(String message) { + for (Channel channel : ircBot.bot.getUserBot().getChannels()) { + for (User user : channel.getUsers()) { + if (message.toLowerCase().contains(user.getNick().toLowerCase())) { + message = message.replaceAll("(?i)" + user.getNick(), addZeroWidthSpace(user.getNick())); + plugin.logDebug("Adding ZWS to " + user.getNick()); + } + } + } + return message; + } + private String[] cleanupAndSplitMessage(String message) { + if (ircBot.pingFix) { + message = pingFix(message); + } return message.replaceAll(REGEX_CLEAN, "").replaceAll(REGEX_CRLF, "\n").split(LF); } diff --git a/src/main/java/com/cnaude/purpleirc/PurpleBot.java b/src/main/java/com/cnaude/purpleirc/PurpleBot.java index 5bd765c..561d1bf 100644 --- a/src/main/java/com/cnaude/purpleirc/PurpleBot.java +++ b/src/main/java/com/cnaude/purpleirc/PurpleBot.java @@ -98,6 +98,7 @@ public final class PurpleBot { public boolean channelCmdNotifyEnabled; public boolean relayPrivateChat; public boolean partInvalidChannels; + public boolean pingFix; public int botServerPort; public long chatDelay; public String botServer; @@ -667,6 +668,7 @@ public final class PurpleBot { rawMessage = config.getString("raw-message", ""); relayPrivateChat = config.getBoolean("relay-private-chat", false); partInvalidChannels = config.getBoolean("part-invalid-channels", false); + pingFix = config.getBoolean("zero-width-space", false); partInvalidChannelsMsg = config.getString("part-invalid-channels-message", ""); nick = config.getString("nick", ""); botNick = nick; diff --git a/src/main/java/com/cnaude/purpleirc/Utilities/ChatTokenizer.java b/src/main/java/com/cnaude/purpleirc/Utilities/ChatTokenizer.java index 625df4c..8900f4c 100644 --- a/src/main/java/com/cnaude/purpleirc/Utilities/ChatTokenizer.java +++ b/src/main/java/com/cnaude/purpleirc/Utilities/ChatTokenizer.java @@ -653,7 +653,7 @@ public class ChatTokenizer { String displayName = player.getDisplayName(); String playerIP = ""; try { - player.getAddress().getAddress().getHostAddress(); + playerIP = player.getAddress().getAddress().getHostAddress(); } catch (Exception ex) { plugin.logDebug(ex.getMessage()); } @@ -873,6 +873,7 @@ public class ChatTokenizer { /** * + * @param sender * @param targetPlayer * @param message * @param template diff --git a/src/main/resources/SampleBot.yml b/src/main/resources/SampleBot.yml index be55ccb..16b34c7 100644 --- a/src/main/resources/SampleBot.yml +++ b/src/main/resources/SampleBot.yml @@ -80,6 +80,8 @@ flood-control: part-invalid-channels: false # Message when leaving invalid channel part-invalid-channels-message: 'I should not be here! Bye!' +# Insert zero width space into nicks of IRC output to prevent client pings +zero-width-space: false # Channel auto join delay in server ticks (20 ticks = 1 second) channel-auto-join-delay: 20 # If your irc-chat message has a %CUSTOMPREFIX% then these custom prefixes can replace them.