/
IRCLib.java
436 lines (381 loc) · 15.5 KB
/
IRCLib.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package irclib;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class IRCLib extends Thread {
public BufferedWriter out;
public BufferedReader in;
protected boolean bConnected;
protected List<String> lChannel;
protected String sServer;
protected String sNick;
protected String sName;
protected String sUser;
protected String sKey;
protected String SASLUser;
protected String SASLPass;
protected Integer iPort;
protected Socket socket;
private Map<String, IRCCommand> commands = Maps.newHashMap();
private ArrayList<Coloring> colorings = Lists.newArrayList();
public IRCLib () {
super();
this.lChannel = Lists.newArrayList();
this.sName = "sdIRC";
this.sUser = "sdIRC";
this.sKey = "";
this.SASLUser = "";
this.SASLPass = "";
}
public boolean connect (final String s, final Integer p) throws IOException {
if (this.sNick == null) {
return false;
}
if (this.bConnected) {
return false;
}
this.socket = new Socket(s, p);
this.bConnected = true;
this.out = new BufferedWriter(new OutputStreamWriter(this.socket.getOutputStream()));
this.in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
if (!this.isAlive()) {
this.start();
}
if (!this.SASLUser.isEmpty() && !this.SASLPass.isEmpty()) {
this.out.write("CAP REQ :sasl\n");
}
if (!this.sKey.isEmpty()) {
this.out.write("PASS " + this.sKey + "\n");
}
this.out.write("USER " + this.sUser + " * * :" + this.sName + "\n");
this.out.write("NICK " + this.sNick + "\n");
this.out.flush();
return true;
}
public boolean connect (final String s, final Integer p, final String n) throws IOException {
this.sNick = n;
return this.connect(s, p);
}
public boolean connect (final String s, final Integer p, final String n, final String k) throws IOException {
this.sNick = n;
this.sKey = k;
return this.connect(s, p);
}
public void close () throws IOException {
this.bConnected = false;
this.socket.close();
this.in = null;
}
public String getServer () {
return this.bConnected ? this.sServer : null;
}
public String getNick () {
return this.sNick;
}
public boolean setNick (final String n) {
if (this.bConnected) {
this.sNick = n;
this.sendRaw("NICK :" + n);
return true;
}
this.sNick = n;
return true;
}
public String getUser () {
return this.sUser;
}
public boolean setUser (final String u) {
if (this.bConnected) {
return false;
}
this.sUser = u;
return true;
}
public boolean setInfo (final String i) {
if (this.bConnected) {
return false;
}
this.sName = i;
return true;
}
public void setSASLUser (final String s) {
this.SASLUser = s;
}
public void setSASLPass (final String s) {
this.SASLPass = s;
}
public List<String> getChannels () {
return this.lChannel;
}
public boolean sendRaw (final String s) {
if (this.bConnected) {
try {
this.out.write(s + "\n");
this.out.flush();
return true;
} catch (IOException e) {
e.printStackTrace();
return this.bConnected = false;
}
}
return false;
}
private static String validNick = "[A-}][0-9A-}-]*";
private static String validUser = "(?:(?=[\u0001-\u00ff])[^\\s@])+";
private static Pattern mask = Pattern.compile(":("+validNick+")!("+validUser+")@("+validUser+')');
protected Matcher parseMask (final String m) {
final Matcher iUser = mask.matcher(m);
return iUser.matches() ? iUser : null;
}
protected void process (final String line) {
if (line.toUpperCase().startsWith("PING ")) {
this.sendRaw("PONG " + line.substring(5));
} else if (line.toUpperCase().startsWith("AUTHENTICATE +")) {
this.sendRaw("AUTHENTICATE " + this.getSASL(this.SASLUser, this.SASLPass));
} else {
final String[] aLine = line.split(" ", 3);
this.processCommand(aLine[1], line);
}
}
protected void processCommand (final String sCommand, final String sLine) {
final String[] sParsed = sLine.split(" ");
if (sCommand.toUpperCase().trim().equals("004")) {
this.onConnected();
} else if (sCommand.toUpperCase().trim().equals("433")) {
this.setNick(this.sNick + "_");
} else if (sCommand.toUpperCase().equals("CAP")) {
if (sParsed[3].equals("ACK")) {
this.sendRaw("AUTHENTICATE PLAIN");
}
} else if (sCommand.toUpperCase().trim().equals("903") || sCommand.toUpperCase().trim().equals("904") || sCommand.toUpperCase().trim().equals("905")
|| sCommand.toUpperCase().trim().equals("906") || sCommand.toUpperCase().trim().equals("907")) {
this.sendRaw("CAP END");
} else if (!sCommand.toUpperCase().trim().equals("432")) {
if (sCommand.toUpperCase().trim().equals("NICK")) {
Matcher iUser = parseMask(sParsed[0]);
String snNick = sParsed[0].split("!")[0].replaceFirst(":", "");
if (iUser != null && iUser.group(1).equals(this.sNick)) {
this.sNick = sParsed[2].replaceFirst(":", "");
onNick(snNick, this.sNick);
}
this.onNick(snNick, sParsed[2].replaceFirst(":", ""));
} else if (sCommand.toUpperCase().trim().equals("JOIN")) {
final Matcher iUser = this.parseMask(sParsed[0]);
final String sOrigin = (iUser == null) ? sParsed[0].replaceFirst(":", "") : iUser.group(1);
if (iUser == null) {
this.onJoin(sOrigin, sParsed[2]);
} else {
if (iUser.group(1).equals(this.sNick)) {
this.lChannel.add(sParsed[2]);
}
this.onJoin(iUser.group(1), iUser.group(2), iUser.group(3), sParsed[2]);
}
} else if (sCommand.toUpperCase().trim().equals("PART")) {
final Matcher iUser = this.parseMask(sParsed[0]);
if (iUser.group(1).equals(this.sNick)) {
this.lChannel.remove(sParsed[2]);
}
final String sPart = (sLine.split(" ", 4).length > 3) ? sLine.split(" ", 4)[3].replaceFirst(":", "") : null;
this.onPart(iUser.group(1), iUser.group(2), iUser.group(3), sParsed[2], sPart);
} else if (sCommand.toUpperCase().trim().equals("KICK")) {
final Matcher iUser = this.parseMask(sParsed[0]);
final String sOrigin = (iUser == null) ? sParsed[0].replaceFirst(":", "") : iUser.group(1);
if (iUser == null) {
this.onKick(sOrigin, sParsed[3], sParsed[2], sParsed[4]);
} else {
if (iUser.group(1).equals(this.sNick)) {
this.lChannel.remove(sParsed[2]);
}
this.onKick(iUser.group(1), iUser.group(2), iUser.group(3), sParsed[3], sParsed[2], sParsed[4]);
}
} else if (sCommand.toUpperCase().trim().equals("QUIT")) {
final Matcher iUser = this.parseMask(sParsed[0]);
this.onQuit(iUser.group(1), iUser.group(2), iUser.group(3), sLine.split(" ", 3)[2].replaceFirst(":", ""));
} else if (sCommand.toUpperCase().trim().equals("PRIVMSG")) {
final Matcher iUser = this.parseMask(sParsed[0]);
final String sOrigin = (iUser == null) ? sParsed[0].replaceFirst(":", "") : iUser.group(1);
if (sLine.split(" ", 4)[3].replaceFirst(":", "").startsWith(Character.toString('\u0001'))) {
if (iUser == null) {
this.processCTCP(sOrigin, sParsed[2], sLine.split(" ", 4)[3].replaceFirst(":", "").substring(1, sLine.split(" ", 4)[3].replaceFirst(":", "").length() - 1));
} else {
this.processCTCP(iUser.group(1), iUser.group(2), iUser.group(3), sParsed[2],
sLine.split(" ", 4)[3].replaceFirst(":", "").substring(1, sLine.split(" ", 4)[3].replaceFirst(":", "").length() - 1));
}
} else if (iUser == null) {
this.onMessage(sOrigin, sParsed[2], sLine.split(" ", 4)[3].replaceFirst(":", ""));
} else {
String m = sLine.split(" ", 4)[3].replaceFirst(":", "");
String[] v = m.split(" ");
List<String> temp = null;
if (commands.containsKey(v[0])) {
temp = commands.get(v[0]).onCommand(m);
}
if (temp != null) {
this.onMessage(iUser.group(1), iUser.group(2), iUser.group(3), sParsed[2], m);
} else {
for (String s : temp) {
sendMessage(sParsed[2], s);
}
}
}
} else if (sCommand.toUpperCase().trim().equals("NOTICE")) {
final Matcher iUser = this.parseMask(sParsed[0]);
final String sOrigin = (iUser == null) ? sParsed[0].replaceFirst(":", "") : iUser.group(1);
if (iUser == null) {
this.onNotice(sOrigin, sParsed[2], sLine.split(" ", 4)[3].replaceFirst(":", ""));
} else {
this.onNotice(iUser.group(1), iUser.group(2), iUser.group(3), sParsed[2], sLine.split(" ", 4)[3].replaceFirst(":", ""));
}
}
}
}
public void processCTCP (final String n, final String u, final String h, final String d, final String m) {
final String[] mParts = m.split(" ", 2);
if (mParts[0].equals("ACTION")) {
this.onAction(n, u, h, d, mParts[1]);
} else {
this.onCTCP(n, u, h, d, m);
}
}
public void processCTCP (final String s, final String d, final String m) {
final String[] mParts = m.split(" ", 2);
if (mParts[0].equals("ACTION")) {
this.onAction(s, d, mParts[1]);
} else {
this.onCTCP(s, d, m);
}
}
public String getSASL (final String u, final String p) {
return Base64.encode(u + Character.toString('\0') + u + Character.toString('\0') + p);
}
public void run () {
try {
String line = null;
while (this.in != null) {
if ((line = this.in.readLine()) != null) {
process(line);
} else if (!bConnected || !socket.isConnected()) {
socket.close();
connect(socket.getInetAddress().getHostAddress(), socket.getPort());
}
}
} catch (IOException ex) {
System.out.println("[IRCLIB]" + ex.getMessage() == null ? "null" : ex.getMessage());
}
}
public void joinChannel (final String c) {
this.sendRaw("JOIN " + c);
}
public void joinChannel (final String c, final String k) {
this.sendRaw("JOIN " + c + " " + k);
}
public void partChannel (final String c) {
this.sendRaw("PART " + c);
}
public void sendMessage (final String d, final String m) {
this.sendRaw("PRIVMSG " + d + " :" + m);
}
public void sendAction (final String d, final String m) {
this.sendCTCP(d, "ACTION " + m);
}
public void sendCTCP (final String d, final String m) {
this.sendRaw("PRIVMSG " + d + " :" + Character.toString('\u0001') + m + Character.toString('\u0001'));
}
public void sendCTCPReply (final String d, final String m) {
this.sendRaw("NOTICE " + d + " :" + Character.toString('\u0001') + m + Character.toString('\u0001'));
}
public void onConnected () {
}
public void onNick (final String oldNick, final String newNick) {
}
public void onJoin (final String n, final String u, final String h, final String c) {
}
public void onJoin (final String s, final String c) {
}
public void onPart (final String n, final String u, final String h, final String c, final String r) {
}
public void onPart (final String s, final String c, final String r) {
}
public void onKick (final String n, final String kn, final String u, final String h, final String c, final String r) {
}
public void onKick (final String s, final String kn, final String c, final String r) {
}
public void onQuit (final String n, final String u, final String h, final String r) {
}
public void onCTCP (final String n, final String u, final String h, final String d, final String m) {
}
public void onCTCP (final String s, final String d, final String m) {
}
public void onAction (final String s, final String d, final String m) {
}
public void onAction (final String n, final String u, final String h, final String d, final String m) {
}
public void onMessage (final String n, final String u, final String h, final String d, final String m) {
}
public void onMessage (final String s, final String d, final String m) {
}
public void onNotice (final String n, final String u, final String h, final String d, final String m) {
}
public void onNotice (final String s, final String d, final String m) {
}
/**
*
* @param command the command to register ex: !players or !tps
* @param callback the IRCCommand to run when this command is entered
* @return true if the command is successfully registered, false if it is not registered
*/
public boolean registerCommand (String command, IRCCommand callback) {
if (commands.containsKey(command)) {
return false;
}
commands.put(command, callback);
return true;
}
/**
*
* @param command the command to check
* @return true if the command is registered, false otherwise
*/
public boolean isCommandRegistered (String command) {
return commands.containsKey(command);
}
/**
* removes a command if it exists
* @param command command to remove
*/
public void removeCommand (String command) {
commands.remove(command);
}
/**
*
* @return Immutable Map of commands and their callbacks
*/
public ImmutableMap getCommands () {
return ImmutableMap.builder().putAll(commands).build();
}
/**
*
* @param c coloring to process during chat events
*/
public void addColoring (Coloring c) {
colorings.add(c);
}
/**
* WARNING: DO NOT MODIFY THIS LIST!!!!
* @return list of colorings
*/
public List<Coloring> getColorings () {
return colorings;
}
}