]>
Commit | Line | Data |
---|---|---|
431b229b LP |
1 | /** |
2 | # | |
3 | # BorkNet Services Core | |
4 | # | |
5 | ||
6 | # | |
7 | # Copyright (C) 2004 Ozafy - ozafy@borknet.org - http://www.borknet.org | |
8 | # | |
9 | # This program is free software; you can redistribute it and/or | |
10 | # modify it under the terms of the GNU General Public License | |
11 | # as published by the Free Software Foundation; either version 2 | |
12 | # of the License, or (at your option) any later version. | |
13 | # | |
14 | # This program is distributed in the hope that it will be useful, | |
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | # GNU General Public License for more details. | |
18 | # | |
19 | # You should have received a copy of the GNU General Public License | |
20 | # along with this program; if not, write to the Free Software | |
21 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
22 | # | |
23 | */ | |
24 | package borknet_services.core; | |
25 | import java.io.*; | |
26 | import java.net.*; | |
27 | import java.util.*; | |
28 | import java.text.*; | |
29 | import java.util.logging.*; | |
30 | import java.util.regex.*; | |
31 | import java.sql.*; | |
32 | ||
33 | /** | |
34 | * The main Bot Class. | |
35 | * This class creates and manages the connection between the IRC Server and The Bot. | |
36 | * @author Ozafy - ozafy@borknet.org - http://www.borknet.org | |
37 | */ | |
38 | public class Core | |
39 | { | |
40 | /** Version reply sent to users */ | |
41 | private String version = "The BorkNet Services Core (C) Laurens Panier (Ozafy) & BorkNet Dev-Com - http://www.borknet.org"; | |
42 | ||
43 | /** Reads the IRC Server */ | |
44 | private BufferedReader IRCir; | |
45 | /** Writes to the IRC Server */ | |
46 | private BufferedWriter IRCor; | |
47 | /** Socket to connect to the IRC Server*/ | |
48 | private Socket IRCServerS; | |
49 | /** Variable to keep the bot alive */ | |
50 | public boolean running; | |
51 | /** Internal Timer */ | |
52 | private CoreTimer timer; | |
53 | /** Create mail daemon */ | |
54 | private Thread timerThread; | |
55 | ||
56 | /** Bot Description */ | |
57 | private String description = ""; | |
58 | /** Bot Nick */ | |
59 | private String nick = ""; | |
60 | /** Bot Ident */ | |
61 | private String ident = ""; | |
62 | /** Bot Host */ | |
63 | private String host = ""; | |
64 | /** Server to connect to */ | |
65 | private String server = ""; | |
66 | /** Port to connect on */ | |
67 | private int port = 0; | |
68 | /** Password to connect */ | |
69 | private String pass = ""; | |
70 | /** Server numeric */ | |
71 | private String numeric = ""; | |
72 | /** Channel to report to */ | |
73 | private String reportchan = ""; | |
74 | /** Report connections? */ | |
75 | private boolean reportconn = false; | |
76 | /** ip's to ignore */ | |
77 | private String reportignore = ""; | |
78 | /** Name of the network the bot is connecting to */ | |
79 | private String network = ""; | |
80 | ||
81 | private int ticks = 0; | |
82 | ||
83 | /** Controls all data received from the IRC Server */ | |
84 | private CoreServer ser; | |
85 | /** Controls all communication to the Database */ | |
86 | private CoreDBControl dbc; | |
87 | private CoreModControl mod; | |
88 | private ArrayList<String> modules = new ArrayList<String>(); | |
89 | private ArrayList<String> developers = new ArrayList<String>(); | |
90 | ||
91 | /** active netsplits */ | |
92 | private boolean split = false; | |
93 | /** list of splitted servers */ | |
94 | private ArrayList<String> splits = new ArrayList<String>(); | |
95 | /** Core's numeric */ | |
96 | private String corenum = "AAA"; | |
97 | ||
98 | private boolean debug = false; | |
99 | ||
100 | /** logging stuff */ | |
101 | private Logger logger = Logger.getLogger(""); | |
102 | private FileHandler fh; | |
103 | private SimpleDateFormat format = new SimpleDateFormat("EEE dd/MM/yyyy HH:mm:ss"); | |
104 | /** | |
105 | * Constructs an IRCClient. | |
106 | * @param dataSrc Holds all the configuration file settings. | |
107 | * @param debug If we're running in debug. | |
108 | */ | |
109 | public Core(boolean debug) | |
110 | { | |
111 | this.debug = debug; | |
112 | if(debug) | |
113 | { | |
114 | try | |
115 | { | |
116 | fh = new FileHandler("debug.%g", 1000000, 10, true); | |
117 | fh.setFormatter(new ShortFormatter()); | |
118 | logger.addHandler(fh); | |
119 | Handler handlers[] = logger.getHandlers(); | |
120 | for (int i = 0; i < handlers.length; i++) | |
121 | { | |
122 | if(handlers[i] instanceof ConsoleHandler) | |
123 | { | |
124 | logger.removeHandler(handlers[i]); | |
125 | } | |
126 | } | |
127 | logger.setLevel(Level.ALL); | |
128 | } | |
129 | catch(Exception e) | |
130 | { | |
131 | System.out.println("Error creating logfile!"); | |
132 | System.exit(1); | |
133 | } | |
134 | } | |
135 | load_conf(); | |
136 | create_coremodules(); | |
137 | //connect to the irc server | |
138 | connect(server, port); | |
139 | logon(); | |
140 | ||
141 | //keep running till we're told otherwise | |
142 | running = true; | |
143 | while(running) | |
144 | { | |
145 | service(); | |
146 | } | |
147 | ||
148 | //disconnect | |
149 | logoff(); | |
150 | disconnect(); | |
151 | } | |
152 | ||
153 | public void printDebug(String s) | |
154 | { | |
155 | if(debug) | |
156 | { | |
157 | java.util.Date now = new java.util.Date(); | |
158 | logger.info("["+format.format(now)+"]"+s); | |
159 | } | |
160 | } | |
161 | ||
162 | public void debug(Exception e) | |
163 | { | |
164 | if(debug) | |
165 | { | |
166 | StackTraceElement[] te = e.getStackTrace(); | |
167 | logger.info(e.toString()); | |
168 | for(StackTraceElement el : te) | |
169 | { | |
170 | logger.info("\tat "+el.getClassName()+"."+el.getMethodName()+"("+el.getFileName()+":"+el.getLineNumber()+")"); | |
171 | } | |
172 | } | |
173 | } | |
174 | ||
175 | /** | |
176 | * Load the config file | |
177 | */ | |
178 | private void load_conf() | |
179 | { | |
180 | ConfLoader loader = new ConfLoader(this); | |
181 | try | |
182 | { | |
183 | loader.load(); | |
184 | } | |
185 | catch(Exception e) | |
186 | { | |
187 | debug(e); | |
188 | System.exit(1); | |
189 | } | |
190 | Properties dataSrc = loader.getVars(); | |
191 | try | |
192 | { | |
193 | //set all the config file vars | |
194 | description = dataSrc.getProperty("description"); | |
195 | nick = dataSrc.getProperty("nick"); | |
196 | ident = dataSrc.getProperty("ident"); | |
197 | host = dataSrc.getProperty("host"); | |
198 | server = dataSrc.getProperty("server"); | |
199 | port = Integer.parseInt(dataSrc.getProperty("port")); | |
200 | pass = dataSrc.getProperty("pass"); | |
201 | numeric = dataSrc.getProperty("numeric"); | |
202 | reportchan = dataSrc.getProperty("reportchan"); | |
203 | reportconn = Boolean.parseBoolean(dataSrc.getProperty("reportconn")); | |
204 | reportignore = dataSrc.getProperty("reportignore"); | |
205 | network = dataSrc.getProperty("network"); | |
206 | String mods[] = dataSrc.getProperty("modules").split(","); | |
207 | for(int n=0; n<mods.length; n++) | |
208 | { | |
209 | modules.add(mods[n]); | |
210 | } | |
211 | String devs[] = dataSrc.getProperty("developers").split(","); | |
212 | for(int n=0; n<devs.length; n++) | |
213 | { | |
214 | developers.add(devs[n].toLowerCase()); | |
215 | } | |
216 | } | |
217 | catch(Exception e) | |
218 | { | |
219 | printDebug("Error loading configfile."); | |
220 | debug(e); | |
221 | System.exit(1); | |
222 | } | |
223 | } | |
224 | ||
225 | /** | |
226 | * Create the modules | |
227 | */ | |
228 | private void create_coremodules() | |
229 | { | |
230 | //create the db control class | |
231 | dbc = new CoreDBControl(this); | |
232 | //create the server communication class | |
233 | ser = new CoreServer(this, dbc); | |
234 | timer = new CoreTimer(this); | |
235 | Thread timerThread = new Thread(timer); | |
236 | timerThread.setDaemon(true); | |
237 | timerThread.start(); | |
238 | } | |
239 | ||
240 | /** | |
241 | * Connects the bot to the given IRC Server | |
242 | * @param serverHostname IP/host to connect to. | |
243 | * @param serverPort Port to connect on. | |
244 | */ | |
245 | private void connect(String serverHostname, int serverPort) | |
246 | { | |
247 | InputStream IRCis = null; | |
248 | OutputStream IRCos = null; | |
249 | //check for input output streams | |
250 | try | |
251 | { | |
252 | IRCServerS = new Socket(serverHostname, serverPort); | |
253 | IRCis = IRCServerS.getInputStream(); | |
254 | IRCos = IRCServerS.getOutputStream(); | |
255 | //make the buffers | |
256 | IRCir = new BufferedReader(new InputStreamReader(IRCis,"ISO-8859-1")); | |
257 | IRCor = new BufferedWriter(new OutputStreamWriter(IRCos,"ISO-8859-1")); | |
258 | } | |
259 | catch(Exception e) | |
260 | { | |
261 | printDebug("error opening streams to IRC server"); | |
262 | debug(e); | |
263 | System.exit(1); | |
264 | } | |
265 | return; | |
266 | } | |
267 | ||
268 | /** | |
269 | * Kill the connection to the server. | |
270 | */ | |
271 | private void disconnect() | |
272 | { | |
273 | try | |
274 | { | |
275 | IRCir.close(); | |
276 | IRCor.close(); | |
277 | } | |
278 | catch(IOException e) | |
279 | { | |
280 | printDebug("Error disconnecting from IRC server"); | |
281 | debug(e); | |
282 | } | |
283 | } | |
284 | ||
285 | /** | |
286 | * Log off clean. | |
287 | */ | |
288 | private void logoff() | |
289 | { | |
290 | BufferedReader br = IRCir; | |
291 | BufferedWriter bw = IRCor; | |
292 | try | |
293 | { | |
294 | if(!ircsend("quit :Shutting down.")); | |
295 | bw.write("quit :Shutting down."); | |
296 | bw.newLine(); | |
297 | bw.flush(); | |
298 | } | |
299 | catch(Exception e) | |
300 | { | |
301 | printDebug("logoff error: " + e); | |
302 | System.exit(1); | |
303 | } | |
304 | } | |
305 | ||
306 | /** | |
307 | * Start our connection burst | |
308 | */ | |
309 | private void logon() | |
310 | { | |
311 | BufferedReader br = IRCir; | |
312 | BufferedWriter bw = IRCor; | |
313 | try | |
314 | { | |
315 | // send user info | |
316 | printDebug("[>---<] >> *** Connecting to IRC server..."); | |
317 | printDebug("[>---<] >> *** Sending password..."); | |
318 | printDebug("[>out<] >> PASS " + pass); | |
319 | bw.write("PASS " + pass); | |
320 | bw.newLine(); | |
321 | printDebug("[>---<] >> *** Identify the Service..."); | |
322 | String time = get_time(); | |
323 | //itroduce myself properly | |
324 | printDebug("[>out<] >> SERVER " + host + " 1 " + time + " " + time + " J10 " + numeric + "]]] +s :" + description); | |
325 | bw.write("SERVER " + host + " 1 " + time + " " + time + " J10 " + numeric + "]]] +s :" + description); | |
326 | bw.newLine(); | |
327 | dbc.addServer(numeric,host,"0",true); | |
328 | printDebug("[>---<] >> *** Sending EB"); | |
329 | printDebug("[>out<] >> " + numeric + " EB"); | |
330 | bw.write(numeric + " EB"); | |
331 | bw.newLine(); | |
332 | bw.flush(); | |
333 | } | |
334 | catch(Exception e) | |
335 | { | |
336 | printDebug("logon error: " + e); | |
337 | System.exit(1); | |
338 | } | |
339 | return; | |
340 | } | |
341 | ||
342 | /** | |
343 | * Send raw data to the IRC Server | |
344 | */ | |
345 | public boolean ircsend(String message) | |
346 | { | |
347 | printDebug("[>out<] >> " + message); | |
348 | try | |
349 | { | |
350 | IRCor.write(message); | |
351 | IRCor.newLine(); | |
352 | IRCor.flush(); | |
353 | } | |
354 | catch(IOException e) | |
355 | { | |
356 | return false; | |
357 | } | |
358 | return true; | |
359 | } | |
360 | ||
361 | /** | |
362 | * Parse raw server data. | |
363 | */ | |
364 | private void service() | |
365 | { | |
366 | try | |
367 | { | |
368 | if(IRCir.ready()) | |
369 | { | |
370 | String msg = IRCir.readLine(); | |
371 | printDebug("[>in <] >> " + msg); | |
372 | ser.parseLine(msg); | |
373 | } | |
374 | //nothing to do, nap | |
375 | else | |
376 | { | |
377 | try | |
378 | { | |
379 | Thread.sleep(100); | |
380 | } | |
381 | catch(InterruptedException e) | |
382 | { | |
383 | } | |
384 | } | |
385 | } | |
386 | //dun dun dun | |
387 | catch(IOException e) | |
388 | { | |
389 | debug(e); | |
390 | System.exit(1); | |
391 | } | |
392 | } | |
393 | ||
394 | /** | |
395 | * rehash & reconnect to our server. | |
396 | */ | |
397 | public void rehash() | |
398 | { | |
399 | mod.stop(); | |
400 | timer.stop(); | |
401 | logoff(); | |
402 | disconnect(); | |
403 | ser.setEA(false); | |
404 | ser.setEB(false); | |
405 | modules.clear(); | |
406 | load_conf(); | |
407 | create_coremodules(); | |
408 | connect(server,port); | |
409 | logon(); | |
410 | } | |
411 | ||
412 | public void die(String quit) | |
413 | { | |
414 | mod.stop(); | |
415 | cmd_quit(corenum, quit); | |
416 | running = false; | |
417 | } | |
418 | ||
419 | ||
420 | /** | |
421 | * Get the bot's nick | |
422 | * @return bot's nick | |
423 | */ | |
424 | public String get_nick() | |
425 | { | |
426 | return nick; | |
427 | } | |
428 | ||
429 | /** | |
430 | * Get the bot's host | |
431 | * @return bot's host | |
432 | */ | |
433 | public String get_host() | |
434 | { | |
435 | return host; | |
436 | } | |
437 | ||
438 | /** | |
439 | * Get the bot's ident | |
440 | * @return bot's ident | |
441 | */ | |
442 | public String get_ident() | |
443 | { | |
444 | return ident; | |
445 | } | |
446 | ||
447 | /** | |
448 | * Get the bot's numeric | |
449 | * @return bot's numeric | |
450 | */ | |
451 | public String get_numeric() | |
452 | { | |
453 | return numeric; | |
454 | } | |
455 | ||
456 | /** | |
457 | * Get the bot's numeric | |
458 | * @return bot's numeric | |
459 | */ | |
460 | public String get_corenum() | |
461 | { | |
462 | return corenum; | |
463 | } | |
464 | ||
465 | /** | |
466 | * Get the version reply | |
467 | * @return the version reply | |
468 | */ | |
469 | public String get_version() | |
470 | { | |
471 | return version; | |
472 | } | |
473 | ||
474 | /** | |
475 | * Get the reportchannel | |
476 | * @return reportchannel | |
477 | */ | |
478 | public String get_reportchan() | |
479 | { | |
480 | return reportchan; | |
481 | } | |
482 | ||
483 | /** | |
484 | * Get the reportchannel | |
485 | * @return reportchannel | |
486 | */ | |
487 | public boolean get_reportconn() | |
488 | { | |
489 | return reportconn; | |
490 | } | |
491 | ||
492 | /** | |
493 | * Get the reportchannel | |
494 | * @return reportchannel | |
495 | */ | |
496 | public String get_reportignore() | |
497 | { | |
498 | return reportignore; | |
499 | } | |
500 | ||
501 | /** | |
502 | * Get the network name | |
503 | * @return network name | |
504 | */ | |
505 | public String get_net() | |
506 | { | |
507 | return network; | |
508 | } | |
509 | ||
510 | /** | |
511 | * Get the current netsplit status | |
512 | * @return if the net's split or not | |
513 | */ | |
514 | public boolean get_split() | |
515 | { | |
516 | return split; | |
517 | } | |
518 | ||
519 | /** | |
520 | * Get the current netsplit servers | |
521 | * @return list of servers | |
522 | */ | |
523 | public ArrayList<String> get_splitList() | |
524 | { | |
525 | return splits; | |
526 | } | |
527 | ||
528 | /** | |
529 | * Set the current netsplit status | |
530 | * @param s if the net's split or not | |
531 | */ | |
532 | public void set_split(boolean s) | |
533 | { | |
534 | split = s; | |
535 | } | |
536 | ||
537 | /** | |
538 | * Add a splitted server | |
539 | * @param host splitted server's host | |
540 | */ | |
541 | public void add_split(String host) | |
542 | { | |
543 | splits.add(host); | |
544 | set_split(true); | |
545 | } | |
546 | ||
547 | /** | |
548 | * Delete a splitted server | |
549 | * @param host joined server's host | |
550 | */ | |
551 | public void del_split(String host) | |
552 | { | |
553 | if(splits.indexOf(host) != -1) | |
554 | { | |
555 | splits.remove(splits.indexOf(host)); | |
556 | } | |
557 | if(splits.size()<1) | |
558 | { | |
559 | set_split(false); | |
560 | } | |
561 | } | |
562 | ||
563 | /** | |
564 | * Get the current defcon level | |
565 | * @return current defcon level | |
566 | */ | |
567 | public CoreDBControl get_dbc() | |
568 | { | |
569 | return dbc; | |
570 | } | |
571 | ||
572 | public CoreModControl getCoreModControl() | |
573 | { | |
574 | return mod; | |
575 | } | |
576 | ||
577 | public boolean get_debug() | |
578 | { | |
579 | return debug; | |
580 | } | |
581 | ||
582 | public boolean isDeveloper(String auth) | |
583 | { | |
584 | return developers.contains(auth.toLowerCase()); | |
585 | } | |
586 | ||
587 | /** | |
588 | * Report directly to the reportchan | |
589 | * @param s what to report | |
590 | */ | |
591 | public void report(String s) | |
592 | { | |
593 | cmd_privmsg(corenum, reportchan, s); | |
594 | } | |
595 | ||
596 | public void cmd_pong() | |
597 | { | |
598 | ircsend(numeric + " Z :" + host); | |
599 | } | |
600 | ||
601 | /** | |
602 | * Make the bot send <raw> | |
603 | * @param raw string to send | |
604 | */ | |
605 | public void cmd_raw(String raw) | |
606 | { | |
607 | ircsend(raw); | |
608 | } | |
609 | ||
610 | /** | |
611 | * Make the bot join a channel | |
612 | * @param channel channel to join | |
613 | */ | |
614 | public void cmd_join(String num, String channel) | |
615 | { | |
616 | if(dbc.chanExists(channel)) | |
617 | { | |
618 | ircsend(numeric + num + " J " + channel); | |
619 | cmd_mode(numeric + num , channel , "+o"); | |
620 | dbc.addUserChan(channel, numeric + num, "0", true, false); | |
621 | } | |
622 | else | |
623 | { | |
624 | ircsend(numeric + num + " C " + channel + " " + get_time()); | |
625 | dbc.addUserChan(channel, numeric + num, get_time(), true, false); | |
626 | } | |
627 | } | |
628 | ||
629 | /** | |
630 | * Make the bot join a channel | |
631 | * @param channel channel to join | |
632 | */ | |
633 | public void cmd_join(String numeric, String num, String channel) | |
634 | { | |
635 | if(dbc.chanExists(channel)) | |
636 | { | |
637 | ircsend(numeric + num + " J " + channel); | |
638 | cmd_mode(numeric + num , channel , "+o"); | |
639 | dbc.addUserChan(channel, numeric + num, "0", true, false); | |
640 | } | |
641 | else | |
642 | { | |
643 | ircsend(numeric + num + " C " + channel + " " + get_time()); | |
644 | dbc.addUserChan(channel, numeric + num, get_time(), true, false); | |
645 | } | |
646 | } | |
647 | ||
648 | /** | |
649 | * Make the bot join a channel | |
650 | * @param channel channel to join | |
651 | */ | |
652 | public void cmd_join(String numeric, String num, String channel, boolean noop) | |
653 | { | |
654 | if(dbc.chanExists(channel)) | |
655 | { | |
656 | ircsend(numeric + num + " J " + channel); | |
657 | dbc.addUserChan(channel, numeric + num, "0", false, false); | |
658 | } | |
659 | else | |
660 | { | |
661 | ircsend(numeric + num + " C " + channel + " " + get_time()); | |
662 | dbc.addUserChan(channel, numeric + num, get_time(), false, false); | |
663 | } | |
664 | } | |
665 | ||
666 | /** | |
667 | * Make the server change a user's mode | |
668 | * @param user user's numeric | |
669 | * @param channel channel to change modes on | |
670 | * @param mode mode to change | |
671 | */ | |
672 | public void cmd_mode(String user , String channel , String mode) | |
673 | { | |
674 | ircsend(numeric + " OM " + channel + " " + mode + " " + user); | |
675 | dbc.setUserChanMode(user, channel, mode); | |
676 | } | |
677 | ||
678 | /** | |
679 | * Make the server change a user's mode | |
680 | * @param user user's numeric | |
681 | * @param channel channel to change modes on | |
682 | * @param mode mode to change | |
683 | */ | |
684 | public void cmd_mode(String numeric, String user , String channel , String mode) | |
685 | { | |
686 | ircsend(numeric + " OM " + channel + " " + mode + " " + user); | |
687 | dbc.setUserChanMode(user, channel, mode); | |
688 | } | |
689 | ||
690 | /** | |
691 | * Make the bot change a user's mode | |
692 | * @param user user's numeric | |
693 | * @param channel channel to change modes on | |
694 | * @param mode mode to change | |
695 | */ | |
696 | public void cmd_mode_me(String num, String user, String channel, String mode) | |
697 | { | |
698 | ircsend(numeric + num + " M " + channel + " " + mode + " " + user); | |
699 | dbc.setUserChanMode(user, channel, mode); | |
700 | } | |
701 | ||
702 | /** | |
703 | * Make the bot change a user's mode | |
704 | * @param user user's numeric | |
705 | * @param channel channel to change modes on | |
706 | * @param mode mode to change | |
707 | */ | |
708 | public void cmd_mode_me(String numeric, String num, String user, String channel, String mode) | |
709 | { | |
710 | ircsend(numeric + num + " M " + channel + " " + mode + " " + user); | |
711 | dbc.setUserChanMode(user, channel, mode); | |
712 | } | |
713 | ||
714 | /** | |
715 | * Make the bot part a channel | |
716 | * @param channel channel to part | |
717 | * @param reason say why we're leaving | |
718 | */ | |
719 | public void cmd_part(String num, String channel, String reason) | |
720 | { | |
721 | ircsend(numeric + num + " L " + channel + " :" + reason); | |
722 | dbc.delUserChan(channel, numeric + num); | |
723 | } | |
724 | ||
725 | /** | |
726 | * Make the bot part a channel | |
727 | * @param channel channel to part | |
728 | * @param reason say why we're leaving | |
729 | */ | |
730 | public void cmd_part(String numeric, String num, String channel, String reason) | |
731 | { | |
732 | ircsend(numeric + num + " L " + channel + " :" + reason); | |
733 | dbc.delUserChan(channel, numeric + num); | |
734 | } | |
735 | ||
736 | /** | |
737 | * Make the server send a privmsg | |
738 | * @param user user's numeric (or channel) where to privmsg to | |
739 | * @param msg what to say | |
740 | */ | |
741 | public void cmd_sprivmsg(String user, String msg) | |
742 | { | |
743 | ircsend(numeric + " P " + user + " :" + msg); | |
744 | } | |
745 | ||
746 | /** | |
747 | * Make the server send a privmsg | |
748 | * @param user user's numeric (or channel) where to privmsg to | |
749 | * @param msg what to say | |
750 | */ | |
751 | public void cmd_sprivmsg(String numeric, String user, String msg) | |
752 | { | |
753 | ircsend(numeric + " P " + user + " :" + msg); | |
754 | } | |
755 | ||
756 | /** | |
757 | * Make the bot send a privmsg | |
758 | * @param user user (or channel) where to privmsg to | |
759 | * @param msg what to say | |
760 | */ | |
761 | public void cmd_privmsg(String num, String user, String msg) | |
762 | { | |
763 | ircsend(numeric + num + " P " + user + " :" + msg); | |
764 | } | |
765 | ||
766 | /** | |
767 | * Make the bot send a privmsg | |
768 | * @param user user (or channel) where to privmsg to | |
769 | * @param msg what to say | |
770 | */ | |
771 | public void cmd_privmsg(String numeric, String num, String user, String msg) | |
772 | { | |
773 | ircsend(numeric + num + " P " + user + " :" + msg); | |
774 | } | |
775 | ||
776 | /** | |
777 | * Make the bot invite a user to a channel | |
778 | * @param user nick of user to invite | |
779 | * @param chan channel where we're inviting him to (we need op there) | |
780 | */ | |
781 | public void cmd_invite(String num, String user, String chan) | |
782 | { | |
783 | ircsend(numeric + num + " I " + user + " :" + chan); | |
784 | } | |
785 | ||
786 | /** | |
787 | * Make the bot invite a user to a channel | |
788 | * @param user nick of user to invite | |
789 | * @param chan channel where we're inviting him to (we need op there) | |
790 | */ | |
791 | public void cmd_invite(String numeric, String num, String user, String chan) | |
792 | { | |
793 | ircsend(numeric + num + " I " + user + " :" + chan); | |
794 | } | |
795 | ||
796 | /** | |
797 | * send a notice as bot | |
798 | * @param user user's numeric to notice | |
799 | * @param msg what to say | |
800 | */ | |
801 | public void cmd_notice(String num, String user, String msg) | |
802 | { | |
803 | ircsend(numeric + num + " O " + user + " :" + msg); | |
804 | } | |
805 | ||
806 | /** | |
807 | * send a notice as bot | |
808 | * @param user user's numeric to notice | |
809 | * @param msg what to say | |
810 | */ | |
811 | public void cmd_notice(String numeric, String num, String user, String msg) | |
812 | { | |
813 | ircsend(numeric + num + " O " + user + " :" + msg); | |
814 | } | |
815 | ||
816 | /** | |
817 | * set a G-Line | |
818 | * @param host host to ban | |
819 | * @param duration duration of the ban, in seconds | |
820 | * @param reason why we're banning him/her/it | |
821 | */ | |
822 | public void cmd_gline(String host, String duration, String reason) | |
823 | { | |
824 | ircsend(numeric + " GL * +" + host + " " + duration + " :" + reason); | |
825 | } | |
826 | ||
827 | /** | |
828 | * set a G-Line | |
829 | * @param host host to ban | |
830 | * @param duration duration of the ban, in seconds | |
831 | * @param reason why we're banning him/her/it | |
832 | */ | |
833 | public void cmd_gline(String numeric, String host, String duration, String reason) | |
834 | { | |
835 | ircsend(numeric + " GL * +" + host + " " + duration + " :" + reason); | |
836 | } | |
837 | ||
838 | /** | |
839 | * remove a G-Line | |
840 | * @param host host to unban | |
841 | */ | |
842 | public void cmd_ungline(String host) | |
843 | { | |
844 | ircsend(numeric + " GL * -" + host); | |
845 | } | |
846 | ||
847 | /** | |
848 | * remove a G-Line | |
849 | * @param host host to unban | |
850 | */ | |
851 | public void cmd_ungline(String numeric, String host) | |
852 | { | |
853 | ircsend(numeric + " GL * -" + host); | |
854 | } | |
855 | ||
856 | /** | |
857 | * Kill a user | |
858 | * @param user user's numeric | |
859 | * @param why reason why we're killing him/her/it | |
860 | */ | |
861 | public void cmd_dis(String user, String why) | |
862 | { | |
863 | ircsend(numeric + " D " + user + " : (" + why + ")"); | |
864 | //dbc.delUser(user); | |
865 | } | |
866 | ||
867 | /** | |
868 | * Kill a user | |
869 | * @param user user's numeric | |
870 | * @param why reason why we're killing him/her/it | |
871 | */ | |
872 | public void cmd_dis(String numeric, String user, String why) | |
873 | { | |
874 | ircsend(numeric + " D " + user + " : (" + why + ")"); | |
875 | //dbc.delUser(user); | |
876 | } | |
877 | ||
878 | /** | |
879 | * Make the bot quit IRC | |
880 | * @param quit message to give when quitting | |
881 | */ | |
882 | public void cmd_quit(String num,String quit) | |
883 | { | |
884 | ircsend(numeric + num + " Q :Quit: " + quit); | |
885 | dbc.delUser(numeric + num); | |
886 | } | |
887 | ||
888 | /** | |
889 | * Make the bot quit IRC | |
890 | * @param quit message to give when quitting | |
891 | */ | |
892 | public void cmd_quit(String numeric, String num,String quit) | |
893 | { | |
894 | ircsend(numeric + num + " Q :Quit: " + quit); | |
895 | dbc.delUser(numeric + num); | |
896 | } | |
897 | ||
898 | /** | |
899 | * set the topic as bot | |
900 | * @param chan channel to change topic on | |
901 | * @param topic new topic | |
902 | */ | |
903 | public void cmd_topic(String num,String chan, String topic) | |
904 | { | |
905 | //ircsend(numeric + "AAA T " + chan + " " + 0 + " " + get_time() + " :" + topic); | |
906 | ircsend(numeric + num + " T " + chan + " :" + topic); | |
907 | } | |
908 | ||
909 | /** | |
910 | * set the topic as bot | |
911 | * @param chan channel to change topic on | |
912 | * @param topic new topic | |
913 | */ | |
914 | public void cmd_topic(String numeric, String num,String chan, String topic) | |
915 | { | |
916 | //ircsend(numeric + "AAA T " + chan + " " + 0 + " " + get_time() + " :" + topic); | |
917 | ircsend(numeric + num + " T " + chan + " :" + topic); | |
918 | } | |
919 | ||
920 | /** | |
921 | * kick as bot | |
922 | * @param chan channel to kick from | |
923 | * @param user user's numeric to kick | |
924 | * @param msg reason why we're kicking | |
925 | */ | |
926 | public void cmd_kick_me(String num,String chan, String user, String msg) | |
927 | { | |
928 | ircsend(numeric + num + " K " + chan + " " + user + " :" + msg); | |
929 | dbc.delUserChan(chan, user); | |
930 | } | |
931 | ||
932 | /** | |
933 | * kick as bot | |
934 | * @param chan channel to kick from | |
935 | * @param user user's numeric to kick | |
936 | * @param msg reason why we're kicking | |
937 | */ | |
938 | public void cmd_kick_me(String numeric,String num,String chan, String user, String msg) | |
939 | { | |
940 | ircsend(numeric + num + " K " + chan + " " + user + " :" + msg); | |
941 | dbc.delUserChan(chan, user); | |
942 | } | |
943 | ||
944 | /** | |
945 | * kick as server | |
946 | * @param chan channel to kick from | |
947 | * @param user user's numeric to kick | |
948 | * @param msg reason why we're kicking | |
949 | */ | |
950 | public void cmd_kick(String chan, String user, String msg) | |
951 | { | |
952 | ircsend(numeric + " K " + chan + " " + user + " :" + msg); | |
953 | dbc.delUserChan(chan, user); | |
954 | } | |
955 | ||
956 | /** | |
957 | * kick as server | |
958 | * @param chan channel to kick from | |
959 | * @param user user's numeric to kick | |
960 | * @param msg reason why we're kicking | |
961 | */ | |
962 | public void cmd_kick(String numeric, String chan, String user, String msg) | |
963 | { | |
964 | ircsend(numeric + " K " + chan + " " + user + " :" + msg); | |
965 | dbc.delUserChan(chan, user); | |
966 | } | |
967 | ||
968 | /** | |
969 | * change the channel limit | |
970 | * @param chan channel to set a new limit | |
971 | * @param lim new limit | |
972 | */ | |
973 | public void cmd_limit(String num, String chan, int lim) | |
974 | { | |
975 | ircsend(numeric + num + " M " + chan + " +l " + lim); | |
976 | } | |
977 | ||
978 | /** | |
979 | * change the channel limit | |
980 | * @param chan channel to set a new limit | |
981 | * @param lim new limit | |
982 | */ | |
983 | public void cmd_limit(String numeric, String num, String chan, int lim) | |
984 | { | |
985 | ircsend(numeric + num + " M " + chan + " +l " + lim); | |
986 | } | |
987 | ||
988 | /** | |
989 | * change the channel key | |
990 | * @param chan channel to set a new key | |
991 | * @param key new key | |
992 | */ | |
993 | public void cmd_key(String num, String chan, String key) | |
994 | { | |
995 | ircsend(numeric + num + " M " + chan + " +k " + key); | |
996 | } | |
997 | ||
998 | /** | |
999 | * change the channel key | |
1000 | * @param chan channel to set a new key | |
1001 | * @param key new key | |
1002 | */ | |
1003 | public void cmd_key(String numeric, String num, String chan, String key) | |
1004 | { | |
1005 | ircsend(numeric + num + " M " + chan + " +k " + key); | |
1006 | } | |
1007 | ||
1008 | /** | |
1009 | * create a fake user | |
1010 | * @param nick fake nickname | |
1011 | * @param ident fake ident | |
1012 | * @param host fake host | |
1013 | * @param desc fake description | |
1014 | */ | |
1015 | public void cmd_create_service(String num, String nick, String ident, String host, String modes, String desc) | |
1016 | { | |
1017 | String time = get_time(); | |
1018 | ircsend(numeric + " N " + nick + " 1 " + time + " " + ident + " " + host + " "+modes+" " + nick + " B]AAAB " + numeric+num+" :" + desc); | |
1019 | dbc.addUser(numeric+num,nick,ident+"@"+host,modes,nick,true,numeric,"0.0.0.0","0"); | |
1020 | } | |
1021 | ||
1022 | public void cmd_create_service(String num, String nick, String ident, String host, String modes, String auth, String desc, boolean customauth) | |
1023 | { | |
1024 | String time = get_time(); | |
1025 | ircsend(numeric + " N " + nick + " 1 " + time + " " + ident + " " + host + " "+modes+" " + auth + " B]AAAB " + numeric+num+" :" + desc); | |
1026 | dbc.addUser(numeric+num,nick,ident+"@"+host,modes,auth,true,numeric,"0.0.0.0","0"); | |
1027 | } | |
1028 | ||
1029 | /** | |
1030 | * create a fake user | |
1031 | * @param nick fake nickname | |
1032 | * @param ident fake ident | |
1033 | * @param host fake host | |
1034 | * @param desc fake description | |
1035 | */ | |
1036 | public void cmd_create_service(String numeric, String num, String nick, String ident, String host, String modes, String desc) | |
1037 | { | |
1038 | String time = get_time(); | |
1039 | ircsend(numeric + " N " + nick + " 1 " + time + " " + ident + " " + host + " "+modes+" " + nick + " B]AAAB " + numeric+num+" :" + desc); | |
1040 | dbc.addUser(numeric+num,nick,ident+"@"+host,modes,nick,true,numeric,"0.0.0.0","0"); | |
1041 | } | |
1042 | ||
1043 | ||
1044 | /** | |
1045 | * create a fake user | |
1046 | * @param nick fake nickname | |
1047 | * @param ident fake ident | |
1048 | * @param host fake host | |
1049 | * @param desc fake description | |
1050 | */ | |
1051 | public void cmd_create_service(String numeric, String num, String nick, String ident, String host, String ip, String modes, String desc) | |
1052 | { | |
1053 | String time = get_time(); | |
1054 | ircsend(numeric + " N " + nick + " 1 " + time + " " + ident + " " + host + " "+modes+" " + nick + " "+base64Encode(ipToLong(ip))+" " + numeric+num+" :" + desc); | |
1055 | dbc.addUser(numeric+num,nick,ident+"@"+host,modes,nick,true,numeric,"0.0.0.0","0"); | |
1056 | } | |
1057 | ||
1058 | /** | |
1059 | * kill a fake user | |
1060 | * @param nume fake numeric to kill | |
1061 | */ | |
1062 | public void cmd_kill_service(String nume, String msg) | |
1063 | { | |
1064 | ircsend(nume + " Q :"+msg); | |
1065 | dbc.delUser(nume); | |
1066 | } | |
1067 | ||
1068 | /** | |
1069 | * create a fake user | |
1070 | [>in <] >> AB SQ spamscan.borknet.org 1135697097 :EOF from client | |
1071 | [>in <] >> AB S spamscan.borknet.org 2 0 1135956212 J10 ]S]]] +s :BorkNet Spamscan | |
1072 | [>in <] >> ]S EB | |
1073 | [>in <] >> ]S N S 2 1135956212 TheSBot spamscan.borknet.org +owkgrX S B]AAAB ]SAAA :BorkNet Spamscan | |
1074 | [>in <] >> ]SAAA J #coder-com 949217470 | |
1075 | [>out<] >> ]QAAA M #coder-com +v ]SAAA | |
1076 | [>in <] >> ]S OM #coder-com +ov ]SAAA ]SAAA | |
1077 | [>in <] >> ]S EA | |
1078 | */ | |
1079 | public void cmd_create_server(String host, String num, String desc) | |
1080 | { | |
1081 | String time = get_time(); | |
1082 | ircsend(numeric + " S " + host + " 2 0 " + time + " J10 " + num + "]]] +s :"+desc); | |
1083 | dbc.addServer(num,host,numeric,true); | |
1084 | } | |
1085 | ||
1086 | /** | |
1087 | * kill a fake user | |
1088 | * @param nume fake numeric to kill | |
1089 | */ | |
1090 | public void cmd_kill_server(String host, String msg) | |
1091 | { | |
1092 | String time = get_time(); | |
1093 | ircsend(numeric + " SQ "+host+ " 0 :"+msg); | |
1094 | dbc.delServer(host); | |
1095 | } | |
1096 | ||
1097 | public void cmd_sethost(String num, String ident, String host) | |
1098 | { | |
1099 | ircsend(numeric + " SH " + num + " " + ident + " " + host); | |
1100 | User user = dbc.getUser(num); | |
1101 | String modes = user.getModes(); | |
1102 | if (!modes.contains("h")) | |
1103 | { | |
1104 | dbc.setUserField(num, 3, modes + "h"); | |
1105 | } | |
1106 | dbc.setUserField(num, 8, ident + "@" + host); | |
1107 | } | |
1108 | ||
1109 | /** | |
1110 | * get the system time | |
1111 | * | |
1112 | * @return the system time | |
1113 | */ | |
1114 | public String get_time() | |
1115 | { | |
1116 | Calendar cal = Calendar.getInstance(); | |
1117 | long l = (cal.getTimeInMillis() / 1000); | |
1118 | return l+""; | |
1119 | } | |
1120 | ||
1121 | public void timerTick() | |
1122 | { | |
1123 | //ticks every minute | |
1124 | ticks++; | |
1125 | if(ticks>=1440) | |
1126 | { | |
1127 | mod.clean(); | |
1128 | ticks = 0; | |
1129 | } | |
1130 | } | |
1131 | ||
1132 | /** | |
1133 | * Base64 decoding | |
1134 | */ | |
1135 | public long base64Decode(String numer) | |
1136 | { | |
1137 | char num[] = numer.toCharArray(); | |
1138 | long base64n = 0; | |
1139 | int pwr = num.length-1; | |
1140 | for(char c : num) | |
1141 | { | |
1142 | int d = 0; | |
1143 | switch (c) { | |
1144 | case 'A': d = 0; break; case 'B': d = 1; break; | |
1145 | case 'C': d = 2; break; case 'D': d = 3; break; | |
1146 | case 'E': d = 4; break; case 'F': d = 5; break; | |
1147 | case 'G': d = 6; break; case 'H': d = 7; break; | |
1148 | case 'I': d = 8; break; case 'J': d = 9; break; | |
1149 | case 'K': d = 10; break; case 'L': d = 11; break; | |
1150 | case 'M': d = 12; break; case 'N': d = 13; break; | |
1151 | case 'O': d = 14; break; case 'P': d = 15; break; | |
1152 | case 'Q': d = 16; break; case 'R': d = 17; break; | |
1153 | case 'S': d = 18; break; case 'T': d = 19; break; | |
1154 | case 'U': d = 20; break; case 'V': d = 21; break; | |
1155 | case 'W': d = 22; break; case 'X': d = 23; break; | |
1156 | case 'Y': d = 24; break; case 'Z': d = 25; break; | |
1157 | case 'a': d = 26; break; case 'b': d = 27; break; | |
1158 | case 'c': d = 28; break; case 'd': d = 29; break; | |
1159 | case 'e': d = 30; break; case 'f': d = 31; break; | |
1160 | case 'g': d = 32; break; case 'h': d = 33; break; | |
1161 | case 'i': d = 34; break; case 'j': d = 35; break; | |
1162 | case 'k': d = 36; break; case 'l': d = 37; break; | |
1163 | case 'm': d = 38; break; case 'n': d = 39; break; | |
1164 | case 'o': d = 40; break; case 'p': d = 41; break; | |
1165 | case 'q': d = 42; break; case 'r': d = 43; break; | |
1166 | case 's': d = 44; break; case 't': d = 45; break; | |
1167 | case 'u': d = 46; break; case 'v': d = 47; break; | |
1168 | case 'w': d = 48; break; case 'x': d = 49; break; | |
1169 | case 'y': d = 50; break; case 'z': d = 51; break; | |
1170 | case '0': d = 52; break; case '1': d = 53; break; | |
1171 | case '2': d = 54; break; case '3': d = 55; break; | |
1172 | case '4': d = 56; break; case '5': d = 57; break; | |
1173 | case '6': d = 58; break; case '7': d = 59; break; | |
1174 | case '8': d = 60; break; case '9': d = 61; break; | |
1175 | case '[': d = 62; break; case ']': d = 63; break; | |
1176 | default: break; | |
1177 | } | |
1178 | if(pwr != 0) | |
1179 | { | |
1180 | base64n += (d*Math.pow(64,pwr)); | |
1181 | } | |
1182 | else | |
1183 | { | |
1184 | base64n += d; | |
1185 | } | |
1186 | pwr--; | |
1187 | } | |
1188 | return base64n; | |
1189 | } | |
1190 | ||
1191 | public String base64Encode(long numer) | |
1192 | { | |
1193 | String base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789[]"; | |
1194 | char base[] = base64.toCharArray(); | |
1195 | String encoded = ""; | |
1196 | while(numer>0) | |
1197 | { | |
1198 | long i = numer%64; | |
1199 | int index = (int) i; | |
1200 | encoded = base[index] + encoded; | |
1201 | numer = (int) Math.ceil(numer/64); | |
1202 | } | |
1203 | return encoded; | |
1204 | } | |
1205 | ||
1206 | public long ipToLong(String addr) | |
1207 | { | |
1208 | String[] addrArray = addr.split("\\."); | |
1209 | long num = 0; | |
1210 | for (int i=0;i<addrArray.length;i++) | |
1211 | { | |
1212 | int power = 3-i; | |
1213 | num += ((Long.parseLong(addrArray[i])%256 * Math.pow(256,power))); | |
1214 | } | |
1215 | return num; | |
1216 | } | |
1217 | ||
1218 | public String longToIp(long i) | |
1219 | { | |
1220 | return ((i >> 24 ) & 0xFF) + "." + ((i >> 16 ) & 0xFF) + "." + ((i >> 8 ) & 0xFF) + "." + (i & 0xFF); | |
1221 | } | |
1222 | ||
1223 | public String longToHost(long ip) | |
1224 | { | |
1225 | String dotIp = longToIp(ip); | |
1226 | try | |
1227 | { | |
1228 | InetAddress ia = InetAddress.getByName(dotIp); | |
1229 | return ia.getCanonicalHostName(); | |
1230 | } | |
1231 | catch(Exception ex) | |
1232 | { | |
1233 | return dotIp; | |
1234 | } | |
1235 | } | |
1236 | ||
1237 | public void cmd_EB() | |
1238 | { | |
1239 | //get the time | |
1240 | String time = get_time(); | |
1241 | //create myself | |
1242 | ircsend(numeric + " N " + nick + " 1 " + time + " " + ident + " " + host + " +oXwkgdr " + nick + " B]AAAB " + numeric+corenum+" :" + description); | |
1243 | dbc.addUser(numeric+corenum, nick,ident+"@"+host,"+oXwkgdr",nick,true,numeric,"127.0.0.1","0"); | |
1244 | //join my debugchannel | |
1245 | cmd_join(corenum,reportchan); | |
1246 | //i'm done | |
1247 | ircsend(numeric + " EA"); | |
1248 | mod = new CoreModControl(this, modules); | |
1249 | } | |
1250 | } |