This repository has been archived by the owner on Jul 27, 2019. It is now read-only.
/
IRCMessageHandler.java
210 lines (194 loc) · 10.1 KB
/
IRCMessageHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.cnaude.purpleirc.Utilities;
import com.cnaude.purpleirc.IRCCommand;
import com.cnaude.purpleirc.IRCCommandSender;
import com.cnaude.purpleirc.TemplateName;
import com.cnaude.purpleirc.PurpleBot;
import com.cnaude.purpleirc.PurpleIRC;
import com.google.common.base.Joiner;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.pircbotx.Channel;
import org.pircbotx.User;
/**
*
* @author cnaude
*/
public class IRCMessageHandler {
PurpleIRC plugin;
/**
*
* @param plugin
*/
public IRCMessageHandler(PurpleIRC plugin) {
this.plugin = plugin;
}
/**
*
* @param ircBot
* @param user
* @param channel
* @param message
* @param privateMessage
*/
public void processMessage(PurpleBot ircBot, User user, Channel channel, String message, boolean privateMessage) {
plugin.logDebug("processMessage: " + message);
String myChannel = channel.getName();
if (ircBot.muteList.get(myChannel).contains(user.getNick())) {
plugin.logDebug("User is muted. Ignoring message from " + user.getNick() + ": " + message);
return;
}
if (message.startsWith(ircBot.commandPrefix) && (!message.startsWith(ircBot.commandPrefix + ircBot.commandPrefix))) {
String command = message.split(" ")[0].substring(ircBot.commandPrefix.length());
String commandArgs = null;
if (message.contains(" ")) {
commandArgs = message.split(" ", 2)[1];
}
plugin.logDebug("IRC command detected: " + command);
plugin.logDebug(message);
String target = channel.getName();
if (ircBot.commandMap.get(myChannel).containsKey(command)) {
boolean privateListen = Boolean.parseBoolean(ircBot.commandMap
.get(myChannel).get(command).get("private_listen"));
boolean channelListen = Boolean.parseBoolean(ircBot.commandMap
.get(myChannel).get(command).get("channel_listen"));
plugin.logDebug("privateListen: " + privateListen);
plugin.logDebug("channelListen: " + channelListen);
if (privateMessage && !privateListen) {
plugin.logDebug("This is a private message but privateListen is false. Ignoring...");
return;
}
if (!privateMessage && !channelListen) {
plugin.logDebug("This is a channel message but channelListen is false. Ignoring...");
return;
}
String gameCommand = (String) ircBot.commandMap.get(myChannel).get(command).get("game_command");
String modes = (String) ircBot.commandMap.get(myChannel).get(command).get("modes");
boolean privateCommand = Boolean.parseBoolean(ircBot.commandMap.get(myChannel).get(command).get("private"));
boolean ctcpResponse = Boolean.parseBoolean(ircBot.commandMap.get(myChannel).get(command).get("ctcp"));
plugin.logDebug(gameCommand + ":" + modes + ":" + privateCommand);
if (privateCommand || privateMessage) {
target = user.getNick();
}
plugin.logDebug("Target: " + target);
boolean modeOkay = false;
if (modes.equals("*")) {
modeOkay = true;
}
if (modes.contains("o") && !modeOkay) {
modeOkay = user.getChannelsOpIn().contains(channel);
}
if (modes.contains("v") && !modeOkay) {
modeOkay = user.getChannelsVoiceIn().contains(channel);
}
if (modes.contains("h") && !modeOkay) {
modeOkay = user.getChannelsHalfOpIn().contains(channel);
}
if (modes.contains("q") && !modeOkay) {
modeOkay = user.getChannelsOwnerIn().contains(channel);
}
if (modes.contains("s") && !modeOkay) {
modeOkay = user.getChannelsSuperOpIn().contains(channel);
}
if (modeOkay) {
if (gameCommand.equals("@list")) {
sendMessage(ircBot, target, plugin.getMCPlayers(ircBot, myChannel), ctcpResponse);
} else if (gameCommand.equals("@uptime")) {
sendMessage(ircBot, target, plugin.getMCUptime(), ctcpResponse);
} else if (gameCommand.equals("@help")) {
sendMessage(ircBot, target, getCommands(ircBot.commandMap, myChannel), ctcpResponse);
} else if (gameCommand.equals("@chat")) {
ircBot.broadcastChat(user.getNick(), myChannel, commandArgs, false);
} else if (gameCommand.equals("@ochat")) {
ircBot.broadcastChat(user.getNick(), myChannel, commandArgs, true);
} else if (gameCommand.equals("@hchat")) {
ircBot.broadcastHeroChat(user.getNick(), myChannel, target, commandArgs);
} else if (gameCommand.equals("@motd")) {
sendMessage(ircBot, target, plugin.getServerMotd(), ctcpResponse);
} else if (gameCommand.equals("@msg")) {
ircBot.playerChat(user.getNick(), myChannel, target, commandArgs);
} else if (gameCommand.equals("@clearqueue")) {
sendMessage(ircBot, target, plugin.commandQueue.clearQueue(), ctcpResponse);
sendMessage(ircBot, target, ircBot.messageQueue.clearQueue(), ctcpResponse);
} else if (gameCommand.equals("@query")) {
sendMessage(ircBot, target, plugin.getRemotePlayers(commandArgs), ctcpResponse);
} else {
if (commandArgs == null) {
commandArgs = "";
}
if (gameCommand.contains("%ARGS%")) {
gameCommand = gameCommand.replace("%ARGS%", commandArgs);
}
if (gameCommand.contains("%NAME%")) {
gameCommand = gameCommand.replace("%NAME%", user.getNick());
}
plugin.logDebug("GM: \"" + gameCommand.trim() + "\"");
try {
plugin.commandQueue.add(new IRCCommand(new IRCCommandSender(ircBot, target, plugin, ctcpResponse), gameCommand.trim()));
} catch (Exception ex) {
plugin.logError(ex.getMessage());
}
}
} else {
plugin.logDebug("User '" + user.getNick() + "' mode not okay.");
ircBot.asyncIRCMessage(target, plugin.getMsgTemplate(
ircBot.botNick, TemplateName.NO_PERM_FOR_IRC_COMMAND)
.replace("%NICK%", user.getNick())
.replace("%CMDPREFIX%", ircBot.commandPrefix));
}
} else {
if (privateMessage || ircBot.invalidCommandPrivate.get(myChannel)) {
target = user.getNick();
}
plugin.logDebug("Invalid command: " + command);
if (ircBot.invalidCommandCTCP.get(myChannel)) {
ircBot.blockingCTCPMessage(target, plugin.getMsgTemplate(
ircBot.botNick, TemplateName.INVALID_IRC_COMMAND)
.replace("%NICK%", user.getNick())
.replace("%CMDPREFIX%", ircBot.commandPrefix));
} else {
ircBot.asyncIRCMessage(target, plugin.getMsgTemplate(
ircBot.botNick, TemplateName.INVALID_IRC_COMMAND)
.replace("%NICK%", user.getNick())
.replace("%CMDPREFIX%", ircBot.commandPrefix));
}
}
} else {
if (ircBot.ignoreIRCChat.get(myChannel)) {
plugin.logDebug("Message NOT dispatched for broadcast due to \"ignore-irc-chat\" being true ...");
return;
}
if (privateMessage && !ircBot.relayPrivateChat) {
plugin.logDebug("Message NOT dispatched for broadcast due to \"relay-private-chat\" being false and this is a private message ...");
return;
}
plugin.logDebug("Message dispatched for broadcast...");
ircBot.broadcastChat(user.getNick(), myChannel, message, false);
}
}
private void sendMessage(PurpleBot ircBot, String target, String message, boolean ctcpResponse) {
if (ctcpResponse) {
plugin.logDebug("Sending message to target: " + target + " => " + message);
ircBot.asyncCTCPMessage(target, message);
} else {
plugin.logDebug("Sending message to target: " + target + " => " + message);
ircBot.asyncIRCMessage(target, message);
}
}
private String getCommands(CaseInsensitiveMap<CaseInsensitiveMap<CaseInsensitiveMap<String>>> commandMap, String myChannel) {
if (commandMap.containsKey(myChannel)) {
List<String> sortedCommands = new ArrayList<String>();
for (String command : commandMap.get(myChannel).keySet()) {
sortedCommands.add(command);
}
Collections.sort(sortedCommands, Collator.getInstance());
return "Valid commands: " + Joiner.on(", ").join(sortedCommands);
}
return "";
}
}