diff --git a/src/main/java/com/cnaude/purpleirc/IRCListeners/MessageListener.java b/src/main/java/com/cnaude/purpleirc/IRCListeners/MessageListener.java index b7d8aa0..34abf50 100644 --- a/src/main/java/com/cnaude/purpleirc/IRCListeners/MessageListener.java +++ b/src/main/java/com/cnaude/purpleirc/IRCListeners/MessageListener.java @@ -54,19 +54,22 @@ public class MessageListener extends ListenerAdapter { User user = event.getUser(); plugin.logDebug("Message caught <" + user.getNick() + ">: " + message); - - if (plugin.shortifyHook != null && ircBot.isShortifyEnabled(channel.getName())) { - plugin.logDebug("Shortifying message (before): " + message); - message = plugin.shortifyHook.shorten(message); - plugin.logDebug("Shortifying message (after): " + message); - } else { - plugin.logDebug("Shortify: false"); - } + try { + if (plugin.shortifyHook != null && ircBot.isShortifyEnabled(channel.getName())) { + plugin.logDebug("Shortifying message (before): " + message); + message = plugin.shortifyHook.shorten(message); + plugin.logDebug("Shortifying message (after): " + message); + } else { + plugin.logDebug("Shortify: false"); + } - if (ircBot.isValidChannel(channel.getName())) { - plugin.ircMessageHandler.processMessage(ircBot, user, channel, message, false); - } else { - plugin.logDebug("Channel " + channel.getName() + " is not valid."); + if (ircBot.isValidChannel(channel.getName())) { + plugin.ircMessageHandler.processMessage(ircBot, user, channel, message, false); + } else { + plugin.logDebug("Channel " + channel.getName() + " is not valid."); + } + } catch (Exception ex) { + plugin.logError("onMessage: " + ex.getMessage()); } } } diff --git a/src/main/java/com/cnaude/purpleirc/PurpleBot.java b/src/main/java/com/cnaude/purpleirc/PurpleBot.java index a612766..8d091dc 100644 --- a/src/main/java/com/cnaude/purpleirc/PurpleBot.java +++ b/src/main/java/com/cnaude/purpleirc/PurpleBot.java @@ -139,6 +139,8 @@ public final class PurpleBot { public CaseInsensitiveMap> worldList; public CaseInsensitiveMap> muteList; public CaseInsensitiveMap> enabledMessages; + public CaseInsensitiveMap userPrefixes; + public String defaultCustomPrefix; public CaseInsensitiveMap>> commandMap; public CaseInsensitiveMap>> extraCommandMap; public CaseInsensitiveMap joinNoticeCooldownMap; @@ -174,6 +176,7 @@ public final class PurpleBot { this.extraCommandMap = new CaseInsensitiveMap<>(); this.joinNoticeCooldownMap = new CaseInsensitiveMap<>(); this.enabledMessages = new CaseInsensitiveMap<>(); + this.userPrefixes = new CaseInsensitiveMap<>(); this.muteList = new CaseInsensitiveMap<>(); this.worldList = new CaseInsensitiveMap<>(); this.opsList = new CaseInsensitiveMap<>(); @@ -315,21 +318,6 @@ public final class PurpleBot { ircListeners.add(new ServerResponseListener(plugin, this)); } - /* - private void addAutoJoinChannels(Configuration.Builder configBuilder) { - for (String channelName : botChannels) { - if (channelAutoJoin.containsKey(channelName)) { - if (channelAutoJoin.get(channelName)) { - if (channelPassword.get(channelName).isEmpty()) { - configBuilder.addAutoJoinChannel(channelName); - } else { - configBuilder.addAutoJoinChannel(channelName, channelPassword.get(channelName)); - } - } - } - } - } - */ public void autoJoinChannels() { plugin.getServer().getScheduler().runTaskLaterAsynchronously(plugin, new Runnable() { @Override @@ -680,6 +668,7 @@ public final class PurpleBot { voicesList.clear(); muteList.clear(); enabledMessages.clear(); + userPrefixes.clear(); worldList.clear(); commandMap.clear(); extraCommandMap.clear(); @@ -689,6 +678,15 @@ public final class PurpleBot { channelCmdNotifyMode = config.getString("command-notify.mode", "msg"); plugin.logDebug(" channelCmdNotifyMode => " + channelCmdNotifyMode); + + for (String s : config.getStringList("custom-prefixes")) { + String pair[] = s.split(" ", 2); + if (pair.length > 0) { + userPrefixes.put(pair[0], pair[1]); + plugin.logDebug("CustomPrefix: " + pair[0] + " => " + pair[1]); + } + } + defaultCustomPrefix = config.getString("custom-prefix-deault", "[IRC]"); // build command notify recipient list for (String recipient : config.getStringList("command-notify.recipients")) { @@ -831,7 +829,7 @@ public final class PurpleBot { enabledMessages.put(channelName, c); if (enabledMessages.isEmpty()) { plugin.logInfo("There are no enabled messages!"); - } + } // build valid world list Collection w = new ArrayList<>(); diff --git a/src/main/java/com/cnaude/purpleirc/Utilities/ChatTokenizer.java b/src/main/java/com/cnaude/purpleirc/Utilities/ChatTokenizer.java index 9228b7e..4b52f79 100644 --- a/src/main/java/com/cnaude/purpleirc/Utilities/ChatTokenizer.java +++ b/src/main/java/com/cnaude/purpleirc/Utilities/ChatTokenizer.java @@ -55,16 +55,17 @@ public class ChatTokenizer { * @return */ public String chatIRCTokenizer(PurpleBot ircBot, User user, org.pircbotx.Channel channel, String template) { - return plugin.colorConverter.ircColorsToGame(ircUserTokenizer(template, user) + return plugin.colorConverter.ircColorsToGame(ircUserTokenizer(template, user, ircBot) .replace("%NICKPREFIX%", ircBot.getNickPrefix(user, channel)) .replace("%CHANNEL%", channel.getName())); } - public String ircUserTokenizer(String template, User user) { + public String ircUserTokenizer(String template, User user, PurpleBot ircBot) { String host = user.getHostmask(); String server = user.getServer(); String away = user.getAwayMessage(); String ircNick = user.getNick(); + String customPrefix = ircBot.defaultCustomPrefix; if (host == null) { host = ""; } @@ -74,13 +75,35 @@ public class ChatTokenizer { if (away == null) { away = ""; } + if (!ircBot.userPrefixes.isEmpty()) { + for (String key : ircBot.userPrefixes.keySet()) { + plugin.logDebug("Does " + key + " match " + user.getNick() + "?"); + if (ircBot.userPrefixes.containsKey(user.getNick())) { + plugin.logDebug("YES"); + customPrefix = ircBot.userPrefixes.get(key); + break; + } else { + plugin.logDebug("NO"); + } + plugin.logDebug("Does " + key + " match " + user.getHostmask() + "?"); + if (ircBot.checkUserMask(user, key)) { + customPrefix = ircBot.userPrefixes.get(key); + plugin.logDebug("YES"); + break; + } else { + plugin.logDebug("NO"); + } + } + } + plugin.logDebug("customPrefix: " + customPrefix); return template.replace("%HOST%", host) + .replace("%CUSTOMPREFIX%", customPrefix) .replace("%NAME%", ircNick) .replace("%SERVER%", server) .replace("%AWAY%", away); } - public String ircUserTokenizer(String template, User recipient, User kicker) { + public String ircUserTokenizer(String template, User recipient, User kicker, PurpleBot ircBot) { String host = kicker.getHostmask(); String server = kicker.getServer(); String away = kicker.getAwayMessage(); @@ -94,7 +117,7 @@ public class ChatTokenizer { if (away == null) { away = ""; } - return ircUserTokenizer(template, recipient) + return ircUserTokenizer(template, recipient, ircBot) .replace("%KICKERHOST%", host) .replace("%KICKER%", ircNick) .replace("%KICKERSERVER%", server) @@ -122,7 +145,7 @@ public class ChatTokenizer { } else { tmpl = playerTokenizer(ircNick, template); } - return plugin.colorConverter.ircColorsToGame(ircUserTokenizer(tmpl, user) + return plugin.colorConverter.ircColorsToGame(ircUserTokenizer(tmpl, user, ircBot) .replace("%HEROCHANNEL%", hChannel) .replace("%HERONICK%", channelManager.getChannel(hChannel).getNick()) .replace("%HEROCOLOR%", channelManager.getChannel(hChannel).getColor().toString()) @@ -150,7 +173,7 @@ public class ChatTokenizer { plugin.logDebug("ircChatToGameTokenizer: null player: " + ircNick); tmpl = playerTokenizer(ircNick, template); } - return plugin.colorConverter.ircColorsToGame(ircUserTokenizer(tmpl, user) + return plugin.colorConverter.ircColorsToGame(ircUserTokenizer(tmpl, user, ircBot) .replace("%NICKPREFIX%", ircBot.getNickPrefix(user, channel)) .replace("%MESSAGE%", message) .replace("%CHANNEL%", channel.getName())); @@ -177,7 +200,7 @@ public class ChatTokenizer { } else { tmpl = playerTokenizer(ircNick, template); } - return plugin.colorConverter.ircColorsToGame(ircUserTokenizer(tmpl, user) + return plugin.colorConverter.ircColorsToGame(ircUserTokenizer(tmpl, user, ircBot) .replace("%HEROCHANNEL%", hChannel) .replace("%HERONICK%", channelManager.getChannel(hChannel).getNick()) .replace("%HEROCOLOR%", channelManager.getChannel(hChannel).getColor().toString()) @@ -206,7 +229,7 @@ public class ChatTokenizer { } else { tmpl = playerTokenizer(ircNick, template); } - return plugin.colorConverter.ircColorsToGame(ircUserTokenizer(tmpl, user) + return plugin.colorConverter.ircColorsToGame(ircUserTokenizer(tmpl, user, ircBot) .replace("%TOWNYCHANNEL%", tChannel) .replace("%NICKPREFIX%", ircBot.getNickPrefix(user, channel)) .replace("%MESSAGE%", message) @@ -225,7 +248,7 @@ public class ChatTokenizer { * @return */ public String ircKickTokenizer(PurpleBot ircBot, User recipient, User kicker, String reason, org.pircbotx.Channel channel, String template) { - return plugin.colorConverter.ircColorsToGame(ircUserTokenizer(template, recipient, kicker) + return plugin.colorConverter.ircColorsToGame(ircUserTokenizer(template, recipient, kicker, ircBot) .replace("%NICKPREFIX%", ircBot.getNickPrefix(kicker, channel)) .replace("%REASON%", reason) .replace("%CHANNEL%", channel.getName())); @@ -245,7 +268,7 @@ public class ChatTokenizer { * @return */ public String ircKickToHeroChatTokenizer(PurpleBot ircBot, User recipient, User kicker, String reason, org.pircbotx.Channel channel, String template, ChannelManager channelManager, String hChannel) { - return plugin.colorConverter.ircColorsToGame(ircUserTokenizer(template, recipient, kicker) + return plugin.colorConverter.ircColorsToGame(ircUserTokenizer(template, recipient, kicker, ircBot) .replace("%HEROCHANNEL%", hChannel) .replace("%HERONICK%", channelManager.getChannel(hChannel).getNick()) .replace("%HEROCOLOR%", channelManager.getChannel(hChannel).getColor().toString()) @@ -265,7 +288,7 @@ public class ChatTokenizer { * @return */ public String ircModeTokenizer(PurpleBot ircBot, User user, String mode, org.pircbotx.Channel channel, String template) { - return plugin.colorConverter.ircColorsToGame(ircUserTokenizer(template, user) + return plugin.colorConverter.ircColorsToGame(ircUserTokenizer(template, user, ircBot) .replace("%MODE%", mode) .replace("%NICKPREFIX%", ircBot.getNickPrefix(user, channel)) .replace("%CHANNEL%", channel.getName())); @@ -283,7 +306,7 @@ public class ChatTokenizer { * @return */ public String ircNoticeTokenizer(PurpleBot ircBot, User user, String message, String notice, org.pircbotx.Channel channel, String template) { - return plugin.colorConverter.ircColorsToGame(ircUserTokenizer(template, user) + return plugin.colorConverter.ircColorsToGame(ircUserTokenizer(template, user, ircBot) .replace("%NICKPREFIX%", ircBot.getNickPrefix(user, channel)) .replace("%MESSAGE%", message) .replace("%NOTICE%", notice) diff --git a/src/main/resources/SampleBot.yml b/src/main/resources/SampleBot.yml index d1d8202..a7ef596 100644 --- a/src/main/resources/SampleBot.yml +++ b/src/main/resources/SampleBot.yml @@ -66,6 +66,13 @@ part-invalid-channels: false part-invalid-channels-message: 'I should not be here! Bye!' # Channel auto join delay in server ticks (20 ticks = 1 second) channel-auto-join-delay: 20 +# If your irc-chat message has a %CUSTOMPREFIX% the these custom prefixes can replace them. +# Can match either nick or hostmask +custom-prefixes: +- 'AwesomeNick [AwesomePrefix]' +- '*!*sarah@example.com [Owner]' +# Default if no match is found +custom-prefix-deault: '[IRC]' # channels - List the channels your bot will join here channels: # Channel name must be surrounded by sing quotes to be YAML compliant.