|
@@ -0,0 +1,349 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+ * SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+package com.dmdirc.addons.relaybot;
|
|
24
|
+
|
|
25
|
+import com.dmdirc.parser.interfaces.callbacks.ChannelMessageListener;
|
|
26
|
+import com.dmdirc.Channel;
|
|
27
|
+import com.dmdirc.ChannelEventHandler;
|
|
28
|
+import com.dmdirc.config.IdentityManager;
|
|
29
|
+import com.dmdirc.parser.interfaces.ChannelClientInfo;
|
|
30
|
+import com.dmdirc.parser.interfaces.ChannelInfo;
|
|
31
|
+import com.dmdirc.parser.interfaces.Parser;
|
|
32
|
+import com.dmdirc.parser.irc.IRCCallbackManager;
|
|
33
|
+import com.dmdirc.parser.irc.IRCChannelClientInfo;
|
|
34
|
+import com.dmdirc.parser.irc.IRCParser;
|
|
35
|
+import com.dmdirc.plugins.PluginClassLoader;
|
|
36
|
+import com.dmdirc.plugins.PluginInfo;
|
|
37
|
+import com.dmdirc.plugins.PluginManager;
|
|
38
|
+import com.dmdirc.ui.messages.Styliser;
|
|
39
|
+import java.lang.reflect.Field;
|
|
40
|
+import java.lang.reflect.Method;
|
|
41
|
+import java.util.HashMap;
|
|
42
|
+import java.util.Map;
|
|
43
|
+
|
|
44
|
+/**
|
|
45
|
+ * This class replaces the ChannelHandler in the core to allow intercepting
|
|
46
|
+ * callbacks from the parser.
|
|
47
|
+ *
|
|
48
|
+ * This allows us to hide them from the core and send faked ones instead.
|
|
49
|
+ *
|
|
50
|
+ * @author shane
|
|
51
|
+ */
|
|
52
|
+public class RelayChannelHandler implements ChannelMessageListener {
|
|
53
|
+ /** My Channel. */
|
|
54
|
+ private final Channel myChannel;
|
|
55
|
+
|
|
56
|
+ /** Core channel handler. */
|
|
57
|
+ private final ChannelEventHandler coreChannelHandler;
|
|
58
|
+
|
|
59
|
+ /** Plugin that owns this RelayChannelHandler. */
|
|
60
|
+ private final RelayBotPlugin myPlugin;
|
|
61
|
+
|
|
62
|
+ /** Known ChannelClients */
|
|
63
|
+ private final Map<String, IRCChannelClientInfo> channelClients = new HashMap<String, IRCChannelClientInfo>();
|
|
64
|
+
|
|
65
|
+ /**
|
|
66
|
+ * Create a new RelayChannelHandler.
|
|
67
|
+ *
|
|
68
|
+ * @param myPlugin
|
|
69
|
+ * @param myChannel channel to hax!
|
|
70
|
+ */
|
|
71
|
+ public RelayChannelHandler(final RelayBotPlugin myPlugin, final Channel myChannel) {
|
|
72
|
+ this.myChannel = myChannel;
|
|
73
|
+ this.myPlugin = myPlugin;
|
|
74
|
+ ChannelEventHandler ceh = null;
|
|
75
|
+ try {
|
|
76
|
+ // Get the core Channel handler.
|
|
77
|
+ final Field field = myChannel.getClass().getDeclaredField("eventHandler");
|
|
78
|
+ field.setAccessible(true);
|
|
79
|
+ ceh = (ChannelEventHandler) field.get(myChannel);
|
|
80
|
+ } catch (IllegalArgumentException ex) {
|
|
81
|
+ ex.printStackTrace();
|
|
82
|
+ } catch (IllegalAccessException ex) {
|
|
83
|
+ ex.printStackTrace();
|
|
84
|
+ } catch (NoSuchFieldException ex) {
|
|
85
|
+ ex.printStackTrace();
|
|
86
|
+ } catch (SecurityException ex) {
|
|
87
|
+ ex.printStackTrace();
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ coreChannelHandler = ceh;
|
|
91
|
+
|
|
92
|
+ if (coreChannelHandler != null) {
|
|
93
|
+ final IRCCallbackManager cbm = (IRCCallbackManager) myChannel.getServer().getParser().getCallbackManager();
|
|
94
|
+ cbm.delCallback(ChannelMessageListener.class, coreChannelHandler);
|
|
95
|
+ cbm.addCallback(ChannelMessageListener.class, this, myChannel.getName());
|
|
96
|
+ }
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+ /**
|
|
101
|
+ * Get a ChannelClient object for the given nick@server.
|
|
102
|
+ *
|
|
103
|
+ * @param channel Channel
|
|
104
|
+ * @param nick Nickname to get channel client for.
|
|
105
|
+ * @param sendJoinIfNew
|
|
106
|
+ * @return Requested ChannelClient Info.
|
|
107
|
+ */
|
|
108
|
+ private IRCChannelClientInfo getChannelClient(final ChannelInfo channel, final String nick, final boolean sendJoinIfNew) {
|
|
109
|
+ final Parser parser = channel.getParser();
|
|
110
|
+ final String storeName = parser.getStringConverter().toLowerCase(nick);
|
|
111
|
+ synchronized (channelClients) {
|
|
112
|
+ if (!channelClients.containsKey(storeName)) {
|
|
113
|
+ final RelayClientInfo client = new RelayClientInfo(channel.getParser(), nick);
|
|
114
|
+ final IRCChannelClientInfo newChannelClient = new IRCChannelClientInfo((IRCParser)channel.getParser(), client, channel);
|
|
115
|
+ colourClient(newChannelClient);
|
|
116
|
+
|
|
117
|
+ channelClients.put(storeName, newChannelClient);
|
|
118
|
+ if (sendJoinIfNew && !client.isServer()) {
|
|
119
|
+ coreChannelHandler.onChannelJoin(parser, channel, newChannelClient);
|
|
120
|
+ // The nickcolour plugin colours the nicknames on join
|
|
121
|
+ // and uses nickname@server when colouring rather than
|
|
122
|
+ // the setting the user wanted, we can recolour here to
|
|
123
|
+ // fix that.
|
|
124
|
+ colourClient(newChannelClient);
|
|
125
|
+ }
|
|
126
|
+ }
|
|
127
|
+
|
|
128
|
+ return channelClients.get(storeName);
|
|
129
|
+ }
|
|
130
|
+ }
|
|
131
|
+
|
|
132
|
+ /**
|
|
133
|
+ * Remove a stored ChannelClient
|
|
134
|
+ *
|
|
135
|
+ * @param channel Channel
|
|
136
|
+ * @param nick Nickname to get channel client for.
|
|
137
|
+ */
|
|
138
|
+ private void removeChannelClient(final ChannelInfo channel, final String nick) {
|
|
139
|
+ final Parser parser = channel.getParser();
|
|
140
|
+ final String storeName = parser.getStringConverter().toLowerCase(nick);
|
|
141
|
+ synchronized (channelClients) {
|
|
142
|
+ channelClients.remove(storeName);
|
|
143
|
+ }
|
|
144
|
+ }
|
|
145
|
+
|
|
146
|
+ /**
|
|
147
|
+ * Rename a stored ChannelClient
|
|
148
|
+ *
|
|
149
|
+ * @param channelClient ChannelClient
|
|
150
|
+ * @param newNick new Nickname
|
|
151
|
+ */
|
|
152
|
+ private void renameChannelClient(final IRCChannelClientInfo channelClient, final String newNick) {
|
|
153
|
+ final Parser parser = channelClient.getChannel().getParser();
|
|
154
|
+ final String storeName = parser.getStringConverter().toLowerCase(newNick);
|
|
155
|
+
|
|
156
|
+ synchronized (channelClients) {
|
|
157
|
+ channelClients.remove(channelClient.getClient().toString());
|
|
158
|
+ ((RelayClientInfo) channelClient.getClient()).changeNickname(newNick);
|
|
159
|
+ channelClients.put(storeName,channelClient);
|
|
160
|
+ }
|
|
161
|
+ }
|
|
162
|
+
|
|
163
|
+ /**
|
|
164
|
+ * Restore the callback handling to the coreChannelHandler.
|
|
165
|
+ */
|
|
166
|
+ public void restoreCoreChannelHandler() {
|
|
167
|
+ if (coreChannelHandler != null) {
|
|
168
|
+ final IRCCallbackManager cbm = (IRCCallbackManager) myChannel.getServer().getParser().getCallbackManager();
|
|
169
|
+
|
|
170
|
+ // Force adding this callback to the CBM.
|
|
171
|
+ if (cbm instanceof RelayCallbackManager) {
|
|
172
|
+ ((RelayCallbackManager) cbm).forceAddCallback(ChannelMessageListener.class, coreChannelHandler, myChannel.getName());
|
|
173
|
+ } else {
|
|
174
|
+ cbm.addCallback(ChannelMessageListener.class, coreChannelHandler, myChannel.getName());
|
|
175
|
+ }
|
|
176
|
+ unset();
|
|
177
|
+ updateNames();
|
|
178
|
+ }
|
|
179
|
+ }
|
|
180
|
+
|
|
181
|
+ /**
|
|
182
|
+ * Remove channel message handling from this ChannelHandler
|
|
183
|
+ */
|
|
184
|
+ public void unset() {
|
|
185
|
+ myChannel.getServer().getParser().getCallbackManager().delCallback(ChannelMessageListener.class, this);
|
|
186
|
+ }
|
|
187
|
+
|
|
188
|
+ /**
|
|
189
|
+ * Colour a client as needed.
|
|
190
|
+ *
|
|
191
|
+ * @param channelClient Client to colour
|
|
192
|
+ */
|
|
193
|
+ private void colourClient(final ChannelClientInfo channelClient) {
|
|
194
|
+ // Use nick colour plugin to colour the client if available.
|
|
195
|
+ final PluginInfo nickColour = PluginManager.getPluginManager().getPluginInfoByName("nickcolour");
|
|
196
|
+
|
|
197
|
+ final boolean fullColour = IdentityManager.getGlobalConfig().getOptionBool(myPlugin.getDomain(), "colourFullName");
|
|
198
|
+ final RelayClientInfo client = (RelayClientInfo)channelClient.getClient();
|
|
199
|
+ final boolean oldValue = client.getShowFullNickname();
|
|
200
|
+ client.setShowFullNickname(fullColour);
|
|
201
|
+
|
|
202
|
+ if (nickColour != null && nickColour.isLoaded()) {
|
|
203
|
+ try {
|
|
204
|
+ final Method gpcl = PluginInfo.class.getDeclaredMethod("getPluginClassLoader");
|
|
205
|
+ gpcl.setAccessible(true);
|
|
206
|
+ final PluginClassLoader pcl = (PluginClassLoader) gpcl.invoke(nickColour);
|
|
207
|
+
|
|
208
|
+ final Class<?> nc = nickColour.getPlugin().getClass();
|
|
209
|
+ final Method colourClient = nc.getDeclaredMethod("colourClient", new Class<?>[]{String.class, ChannelClientInfo.class});
|
|
210
|
+ colourClient.setAccessible(true);
|
|
211
|
+ colourClient.invoke(nickColour.getPlugin(), myChannel.getServer().getNetwork(), channelClient);
|
|
212
|
+ } catch (Throwable t) { /* If it can't colour then oh well. */ }
|
|
213
|
+ }
|
|
214
|
+
|
|
215
|
+ client.setShowFullNickname(oldValue);
|
|
216
|
+ }
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+ /**
|
|
220
|
+ * Handle the IRCParsers incoming message.
|
|
221
|
+ *
|
|
222
|
+ * @param parser Parser that sent the message
|
|
223
|
+ * @param channel Channel the message went to
|
|
224
|
+ * @param channelClient Client who send the message
|
|
225
|
+ * @param message Message content
|
|
226
|
+ * @param host Host of client
|
|
227
|
+ */
|
|
228
|
+ @Override
|
|
229
|
+ public void onChannelMessage(final Parser parser, final ChannelInfo channel, final ChannelClientInfo channelClient, final String message, final String host) {
|
|
230
|
+ final String channelName = parser.getStringConverter().toLowerCase(channel.getName());
|
|
231
|
+ String botName;
|
|
232
|
+ try {
|
|
233
|
+ botName = IdentityManager.getGlobalConfig().getOption(myPlugin.getDomain(), channelName);
|
|
234
|
+ } catch (IllegalArgumentException iae) {
|
|
235
|
+ botName = "";
|
|
236
|
+ }
|
|
237
|
+ final boolean isBot = parser.getStringConverter().equalsIgnoreCase(botName, channelClient.getClient().getNickname());
|
|
238
|
+
|
|
239
|
+ final boolean joinNew = IdentityManager.getGlobalConfig().getOptionBool(myPlugin.getDomain(), "joinOnDiscover");
|
|
240
|
+
|
|
241
|
+ // See if we need to modify this message
|
|
242
|
+ if (channelClient instanceof IRCChannelClientInfo && !botName.isEmpty() && isBot) {
|
|
243
|
+ final String[] bits = message.split(" ", 2);
|
|
244
|
+ final String initial = Styliser.stipControlCodes(bits[0]);
|
|
245
|
+
|
|
246
|
+ if (initial.charAt(0) == '+') {
|
|
247
|
+ // Channel Message
|
|
248
|
+ final IRCChannelClientInfo newChannelClient = getChannelClient(channel, bits[0].substring(2, bits[0].length() - 1), joinNew);
|
|
249
|
+
|
|
250
|
+ coreChannelHandler.onChannelMessage(parser, channel, newChannelClient, bits[1], host);
|
|
251
|
+ return;
|
|
252
|
+ } else if (initial.equalsIgnoreCase("***")) {
|
|
253
|
+ // Some kind of state-changing action
|
|
254
|
+ final String[] newBits = bits[1].split(" ");
|
|
255
|
+
|
|
256
|
+ if (newBits.length > 2) {
|
|
257
|
+ final IRCChannelClientInfo newChannelClient = getChannelClient(channel, newBits[0], joinNew);
|
|
258
|
+
|
|
259
|
+ if (newBits[2].equalsIgnoreCase("joined")) {
|
|
260
|
+ // User joined a relayed channel.
|
|
261
|
+ if (!joinNew) {
|
|
262
|
+ // If auto join on discover isn't enabled, we will
|
|
263
|
+ // need to send this join, else it will already
|
|
264
|
+ // have been sent.
|
|
265
|
+ coreChannelHandler.onChannelJoin(parser, channel, newChannelClient);
|
|
266
|
+ // And recolour to combat the nickcolour plugin
|
|
267
|
+ // changing the colour on join.
|
|
268
|
+ colourClient(newChannelClient);
|
|
269
|
+ }
|
|
270
|
+ return;
|
|
271
|
+ } else if (newBits[2].equalsIgnoreCase("left")) {
|
|
272
|
+ // User left a relayed channel.
|
|
273
|
+ String reason = (newBits.length > 4) ? mergeBits(newBits, 4, newBits.length - 1, " ") : "()";
|
|
274
|
+ reason = reason.substring(1, reason.length() - 1);
|
|
275
|
+
|
|
276
|
+ coreChannelHandler.onChannelPart(parser, channel, newChannelClient, reason);
|
|
277
|
+ removeChannelClient(channel, newBits[0]);
|
|
278
|
+ return;
|
|
279
|
+ } else if (newBits[2].equalsIgnoreCase("quit")) {
|
|
280
|
+ // User quit a relayed channel.
|
|
281
|
+ String reason = (newBits.length > 4) ? mergeBits(newBits, 4, newBits.length - 1, " ") : "()";
|
|
282
|
+ reason = reason.substring(1, reason.length() - 1);
|
|
283
|
+
|
|
284
|
+ coreChannelHandler.onChannelQuit(parser, channel, newChannelClient, reason);
|
|
285
|
+ removeChannelClient(channel, newBits[0]);
|
|
286
|
+ return;
|
|
287
|
+ } else if (newBits[2].equalsIgnoreCase("kicked")) {
|
|
288
|
+ // User was kicked from a relayed channel.
|
|
289
|
+ String reason = (newBits.length > 7) ? mergeBits(newBits, 7, newBits.length - 1, " ") : "()";
|
|
290
|
+ reason = reason.substring(1, reason.length() - 1);
|
|
291
|
+
|
|
292
|
+ final IRCChannelClientInfo kickingChannelClient = (newBits.length > 6) ? getChannelClient(channel, newBits[6], joinNew) : null;
|
|
293
|
+
|
|
294
|
+ coreChannelHandler.onChannelKick(parser, channel, newChannelClient, kickingChannelClient, reason, "");
|
|
295
|
+ removeChannelClient(channel, newBits[0]);
|
|
296
|
+ return;
|
|
297
|
+ } else if (newBits[2].equalsIgnoreCase("now")) {
|
|
298
|
+ // User changed their nickname in a relayed channel.
|
|
299
|
+ if (newBits.length > 3) {
|
|
300
|
+ renameChannelClient(newChannelClient, newBits[3]);
|
|
301
|
+
|
|
302
|
+ coreChannelHandler.onChannelNickChanged(parser, channel, newChannelClient, newBits[0]);
|
|
303
|
+ return;
|
|
304
|
+ }
|
|
305
|
+ }
|
|
306
|
+ }
|
|
307
|
+ } else if (initial.charAt(0) == '*') {
|
|
308
|
+ // Channel Action
|
|
309
|
+ final String[] newBits = bits[1].split(" ", 2);
|
|
310
|
+
|
|
311
|
+ final IRCChannelClientInfo newChannelClient = getChannelClient(channel, newBits[0], joinNew);
|
|
312
|
+
|
|
313
|
+ coreChannelHandler.onChannelAction(parser, channel, newChannelClient, newBits[1], "");
|
|
314
|
+ return;
|
|
315
|
+ }
|
|
316
|
+ }
|
|
317
|
+
|
|
318
|
+ // Pass it on unchanged.
|
|
319
|
+ coreChannelHandler.onChannelMessage(parser, channel, channelClient, message, host);
|
|
320
|
+ }
|
|
321
|
+
|
|
322
|
+ /**
|
|
323
|
+ * Merge the given bits.
|
|
324
|
+ *and
|
|
325
|
+ * @param bits Bits to merge
|
|
326
|
+ * @param start Start
|
|
327
|
+ * @param end end
|
|
328
|
+ * @param joiner What to use to join them
|
|
329
|
+ * @return Joined bits.
|
|
330
|
+ */
|
|
331
|
+ private String mergeBits(final String[] bits, final int start, final int end, final String joiner) {
|
|
332
|
+ final StringBuilder builder = new StringBuilder();
|
|
333
|
+ for (int i = start; i <= end; i++) {
|
|
334
|
+ if (bits.length < i) { break; }
|
|
335
|
+ if (i != start) { builder.append(joiner); }
|
|
336
|
+ builder.append(bits[i]);
|
|
337
|
+ }
|
|
338
|
+
|
|
339
|
+ return builder.toString();
|
|
340
|
+ }
|
|
341
|
+
|
|
342
|
+ /**
|
|
343
|
+ * Send an onChannelNames() event for this channel.
|
|
344
|
+ * This will cause all remote clients to vanish from the nicklist.
|
|
345
|
+ */
|
|
346
|
+ public void updateNames() {
|
|
347
|
+ coreChannelHandler.onChannelGotNames(myChannel.getServer().getParser(), myChannel.getChannelInfo());
|
|
348
|
+ }
|
|
349
|
+}
|