diff --git a/src/main/java/com/cnaude/purpleirc/IRCListeners/NoticeListener.java b/src/main/java/com/cnaude/purpleirc/IRCListeners/NoticeListener.java index 0d0e1bd..947f936 100644 --- a/src/main/java/com/cnaude/purpleirc/IRCListeners/NoticeListener.java +++ b/src/main/java/com/cnaude/purpleirc/IRCListeners/NoticeListener.java @@ -18,6 +18,9 @@ import com.cnaude.purpleirc.PurpleBot; import com.cnaude.purpleirc.PurpleIRC; +import com.cnaude.purpleirc.Utilities.CaseInsensitiveMap; +import java.util.ArrayList; +import org.apache.commons.codec.binary.Base64; import org.pircbotx.Channel; import org.pircbotx.User; import org.pircbotx.hooks.ListenerAdapter; @@ -49,14 +52,88 @@ public NoticeListener(PurpleIRC plugin, PurpleBot ircBot) { @Override public void onNotice(NoticeEvent event) { Channel channel = event.getChannel(); - String message = event.getMessage(); + String message = event.getMessage().trim(); String notice = event.getNotice(); User user = event.getUser(); + String nick = user.getNick(); - plugin.logInfo("-" + user.getNick() + "-" + message); - if (message.contains("You are connected using SSL cipher ")) { - ircBot.sslInfo = message.split(" SSL cipher ")[1]; + if (message.startsWith(PurpleIRC.LINK_CMD) && ircBot.botLinkingEnabled) { + String encodedText = message.replace(PurpleIRC.LINK_CMD, ""); + String decodedText = new String(Base64.decodeBase64(encodedText.getBytes())); + String splitMsg[] = decodedText.split(":"); + + plugin.logDebug("REMOTE LINK COMMAND: " + encodedText + "(" + decodedText + ")"); + + if (splitMsg.length >= 2) { + String command = splitMsg[0]; + String code = splitMsg[1]; + + if (command.equals("LINK_REQUEST")) { + ircBot.linkRequests.put(user.getNick(), code); + plugin.logInfo("PurpleIRC bot link request from " + user.getNick()); + plugin.logInfo("To accept: /irc linkaccept " + + ircBot.getFileName().replace(".yml", "") + " " + user.getNick()); + return; + } + + plugin.logDebug("Are we linked to " + user.getNick() + "?"); + if (ircBot.botLinks.containsKey(nick)) { + plugin.logDebug("Yes we are linked. Is the code correct?"); + if (ircBot.botLinks.get(nick).equals(code)) { + plugin.logDebug("Yes the code is correct!"); + plugin.logDebug(" [COMMAND: " + command + "]"); + plugin.logDebug(" [CODE: " + code + "]"); + + if (command.equals("PRIVATE_MSG") && splitMsg.length >= 5) { + String from = splitMsg[2]; + String target = splitMsg[3]; + String sMessage = decodedText.split(":", 5)[4]; + + plugin.logDebug(" [FROM:" + from + "]"); + plugin.logDebug(" [TO:" + target + "]"); + plugin.logDebug(" [MSG: " + sMessage + "]"); + ircBot.playerCrossChat(user, from, target, sMessage); + } else if (command.equals("PLAYER_INFO") && splitMsg.length >= 4) { + String curCount = splitMsg[2]; + String maxCount = splitMsg[3]; + String players = ""; + if (splitMsg.length == 5) { + players = splitMsg[4]; + } + plugin.logDebug(" [CUR_COUNT:" + curCount + "]"); + plugin.logDebug(" [MAX_COUNT:" + maxCount + "]"); + plugin.logDebug(" [PLAYERS:" + players + "]"); + if (!ircBot.remoteServerInfo.containsKey(nick)) { + ircBot.remoteServerInfo.put(nick, new CaseInsensitiveMap()); + } + if (ircBot.remoteServerInfo.containsKey(nick)) { + ircBot.remoteServerInfo.get(nick).put("CUR_COUNT", curCount); + ircBot.remoteServerInfo.get(nick).put("MAX_COUNT", maxCount); + } + if (!ircBot.remotePlayers.containsKey(nick)) { + ircBot.remotePlayers.put(nick, new ArrayList()); + } + if (ircBot.remotePlayers.containsKey(nick)) { + ircBot.remotePlayers.get(nick).clear(); + if (!players.isEmpty()) { + for (String s : players.split(",")) { + plugin.logDebug(" [ADDING:" + s + "]"); + ircBot.remotePlayers.get(nick).add(s); + } + } + } + } + } else { + plugin.logDebug("Invalid code from " + nick + "!"); + } + } else { + plugin.logDebug("We are not linked to " + nick + "!"); + } + } + return; } + + plugin.logInfo("-" + user.getNick() + "-" + message); if (channel != null) { if (ircBot.isValidChannel(channel.getName())) { ircBot.broadcastIRCNotice(user, message, notice, channel);