]> jfr.im git - irc/borknet/trunk.git/blob - core/CoreDBControl.java
14/03/2011 REV 89
[irc/borknet/trunk.git] / core / CoreDBControl.java
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.sql.*;
26 import java.util.*;
27 import java.io.*;
28 import java.security.*;
29 import borknet_services.core.*;
30
31 /**
32 * The database communication class of the Q IRC Bot.
33 * @author Ozafy - ozafy@borknet.org - http://www.borknet.org
34 */
35 public class CoreDBControl
36 {
37 /** Database server */
38 private String server;
39 /** Database user */
40 private String user;
41 /** Database password */
42 private String password;
43 /** Database */
44 private String db;
45 /** Database connection */
46 private Connection con;
47 /** Main bot */
48 private Core C;
49
50 private HashMap<String,User> usersByNumeric = new HashMap<String,User>();
51 private HashMap<String,User> usersByNick = new HashMap<String,User>();
52 private HashMap<String,ArrayList<User>> usersByAuth = new HashMap<String,ArrayList<User>>();
53 private HashMap<String,ArrayList<User>> usersByHost = new HashMap<String,ArrayList<User>>();
54 private HashMap<String,ArrayList<User>> usersByIP = new HashMap<String,ArrayList<User>>();
55
56 private HashMap<String,Auth> auths = new HashMap<String,Auth>();
57
58 private HashMap<String,Server> serversByNumeric = new HashMap<String,Server>();
59 private HashMap<String,ArrayList<Server>> serversByHub = new HashMap<String,ArrayList<Server>>();
60 private HashMap<String,Server> serversByHost = new HashMap<String,Server>();
61
62 private HashMap<String,Channel> channels = new HashMap<String,Channel>();
63
64 /**
65 * Constructs a Database connection.
66 * @param server Database server
67 * @param user Database user
68 * @param pass Database password
69 * @param db Database
70 * @param debug Are we debugging?
71 * @param B Main bot
72 */
73 public CoreDBControl(String server, String user, String password, String db, Core C)
74 {
75 try
76 {
77 this.server = server;
78 this.user = user;
79 this.password = password;
80 this.db = db;
81 this.C = C;
82 testDriver();
83 con = getConnection ( server, user, password, db);
84 C.printDebug( "[>---<] >> *** Truncating userchans..." );
85 PreparedStatement pstmt;
86 pstmt = con.prepareStatement("TRUNCATE TABLE `userchans`");
87 pstmt.execute();
88 C.printDebug( "[>---<] >> *** Done." );
89 C.printDebug( "[>---<] >> *** Loading Auth data..." );
90 loadAuths();
91 C.printDebug( "[>---<] >> *** Done." );
92 }
93 catch(Exception e)
94 {
95 C.printDebug("Database error!");
96 C.die("SQL error, trying to die gracefully.");
97 }
98 }
99
100 /**
101 * Close the Database connection.
102 */
103 public void close_mysql()
104 {
105 try
106 {
107 con.close();
108 C.printDebug( "[>---<] >> *** MySQL connection closed clean." );
109 }
110 catch(Exception e)
111 {
112 C.printDebug("MySQL connection failed to close!");
113 C.die("SQL error, trying to die gracefully.");
114 }
115 }
116
117 /**
118 * Test the Database driver
119 */
120 protected void testDriver ( )
121 {
122 try
123 {
124 Class.forName ( "org.gjt.mm.mysql.Driver" );
125 C.printDebug( "[>---<] >> *** MySQL Driver Found" );
126 }
127 catch ( java.lang.ClassNotFoundException e )
128 {
129 C.printDebug("MySQL JDBC Driver not found!");
130 C.die("SQL error, trying to die gracefully.");
131 }
132 }
133
134 /**
135 * Creates a Database connection.
136 * @param server Database server
137 * @param user Database user
138 * @param password Database password
139 * @param db Database table
140 *
141 * @return a Database connection.
142 */
143 protected Connection getConnection ( String server, String user, String pass, String db ) throws Exception
144 {
145 String url = "";
146 try
147 {
148 url = "jdbc:mysql://"+server+"/"+db+"?user="+user+"&password="+pass;
149 Connection con = DriverManager.getConnection(url);
150 C.printDebug("[>---<] >> *** Connection established to MySQL server...");
151 return con;
152 }
153 catch ( java.sql.SQLException e )
154 {
155 C.printDebug("Connection couldn't be established to " + url);
156 C.debug(e);
157 throw e;
158 }
159 }
160
161 public Connection getCon()
162 {
163 return con;
164 }
165 /**
166 * Cleans all tables that arn't permanent
167 */
168 public void cleanDB()
169 {
170 try
171 {
172 PreparedStatement pstmt;
173 pstmt = con.prepareStatement("TRUNCATE TABLE `userchans`");
174 pstmt.execute();
175 usersByNumeric = new HashMap<String,User>();
176 usersByNick = new HashMap<String,User>();
177 usersByAuth = new HashMap<String,ArrayList<User>>();
178 usersByHost = new HashMap<String,ArrayList<User>>();
179 usersByIP = new HashMap<String,ArrayList<User>>();
180 }
181 catch(Exception e)
182 {
183 System.out.println ( "Error executing sql statement" );
184 C.debug(e);
185 C.die("SQL error, trying to die gracefully.");
186 }
187 }
188
189 public void loadAuths()
190 {
191 try
192 {
193 PreparedStatement pstmt;
194 pstmt = con.prepareStatement("SELECT * FROM auths");
195 ResultSet rs = pstmt.executeQuery();
196 while(rs.next())
197 {
198 String authnick = rs.getString("authnick");
199 Auth a = new Auth(authnick);
200 a.setPassword(rs.getString("pass"));
201 a.setMail(rs.getString("mail"));
202 a.setLevel(rs.getInt("level"));
203 a.setSuspended((rs.getBoolean("suspended") ? 1 : 0));
204 a.setLast(rs.getLong("last"));
205 a.setInfo(rs.getString("info"));
206 a.setUserflags(rs.getString("userflags"));
207 a.setVHost(rs.getString("vhost"));
208 auths.put(authnick.toLowerCase(),a);
209 }
210 }
211 catch(Exception e)
212 {
213 System.out.println ( "Error executing sql statement" );
214 C.debug(e);
215 C.die("SQL error, trying to die gracefully.");
216 }
217 }
218
219 /**
220 * Check if a channel exists.
221 * @param chan channel to check
222 *
223 * @return true or false
224 */
225 public boolean chanExists(String chan)
226 {
227 try
228 {
229 PreparedStatement pstmt;
230 pstmt = con.prepareStatement("SELECT channel FROM userchans WHERE channel = ?");
231 pstmt.setString(1,chan);
232 ResultSet rs = pstmt.executeQuery();
233 rs.first();
234 String channel = rs.getString("channel");
235 return true;
236 }
237 catch(Exception e)
238 {
239 return false;
240 }
241 }
242
243 /**
244 * Check if an auth exists.
245 * @param auth auth to check
246 *
247 * @return true or false
248 */
249 public boolean authExists(String auth)
250 {
251 return auths.containsKey(auth.toLowerCase());
252 }
253
254 /**
255 * Check if an auth is online
256 * @param auth auth to check
257 *
258 * @return true or false
259 */
260 public boolean authOnline(String auth)
261 {
262 return usersByAuth.containsKey(auth.toLowerCase());
263 }
264
265 public String getNumViaAuth(String auth)
266 {
267 User u = usersByAuth.get(auth.toLowerCase()).get(0);
268 if(u instanceof User)
269 {
270 return u.getNumeric();
271 }
272 else
273 {
274 return "0";
275 }
276 }
277
278 /**
279 * Get a numeric's user row
280 * @param numer numeric of the user to fetch
281 *
282 * @return an array of all fields
283 */
284 public int getAuthLev(String numer)
285 {
286 Auth a = auths.get(usersByNumeric.get(numer).getAuth().toLowerCase());
287 if(a instanceof Auth)
288 {
289 return a.getLevel();
290 }
291 else
292 {
293 return 0;
294 }
295 }
296
297 /**
298 * Check if a nick is reserved.
299 * @param auth nick to check
300 *
301 * @return true or false
302 */
303 public boolean isReservedNick(String auth)
304 {
305 Auth a = auths.get(auth.toLowerCase());
306 if(a instanceof Auth)
307 {
308 Integer lev = a.getLevel();
309 if(lev>1)
310 {
311 if(a.getUserflags().contains("k"))
312 {
313 return true;
314 }
315 else
316 {
317 return false;
318 }
319 }
320 else
321 {
322 return false;
323 }
324 }
325 else
326 {
327 return false;
328 }
329 }
330
331 /**
332 * Check if a nick is reserved.
333 * @param auth nick to check
334 *
335 * @return true or false
336 */
337 public boolean isService(String numeric)
338 {
339 Server s = serversByNumeric.get(numeric.substring(0,2));
340 if(s instanceof Server)
341 {
342 return s.getService();
343 }
344 else
345 {
346 return false;
347 }
348 }
349
350 /**
351 * Check if a nick is reserved.
352 * @param auth nick to check
353 *
354 * @return true or false
355 */
356 public String getServer(String numeric)
357 {
358 Server s = serversByNumeric.get(numeric.substring(0,2));
359 if(s instanceof Server)
360 {
361 return s.getHost();
362 }
363 else
364 {
365 return "unknown";
366 }
367 }
368
369 public int getServerCount()
370 {
371 return serversByNumeric.size();
372 }
373
374 /**
375 * Check if a snumeric exists.
376 * @param numer numeric to check
377 *
378 * @return true or false
379 */
380 public boolean isServerNumeric(String numer)
381 {
382 Server s = serversByNumeric.get(numer);
383 if(s instanceof Server)
384 {
385 return true;
386 }
387 else
388 {
389 return false;
390 }
391 }
392
393 /**
394 * Check if a numeric has op on a channel
395 * @param user numeric to check
396 * @param channel channel to check
397 *
398 * @return true or false
399 */
400 public boolean isOpChan(String user, String channel)
401 {
402 try
403 {
404 PreparedStatement pstmt;
405 pstmt = con.prepareStatement("SELECT modes FROM userchans WHERE BINARY user = ? AND channel = ?");
406 pstmt.setString(1,user);
407 pstmt.setString(2,channel);
408 ResultSet rs = pstmt.executeQuery();
409 rs.first();
410 String mode = rs.getString("modes");
411 if(mode.equals("o"))
412 {
413 return true;
414 }
415 else
416 {
417 return false;
418 }
419 }
420 catch(Exception e)
421 {
422 return false;
423 }
424 }
425
426 /**
427 * Check if a numeric exists.
428 * @param numer numeric to check
429 *
430 * @return true or false
431 */
432 public boolean isNickUsed(String nick)
433 {
434 return usersByNick.containsKey(nick.toLowerCase());
435 }
436
437 /**
438 * Check if a numeric is on a channel
439 * @param user numeric to check
440 * @param channel channel to check
441 *
442 * @return true or false
443 */
444 public boolean isOnChan(String user, String channel)
445 {
446 try
447 {
448 PreparedStatement pstmt;
449 pstmt = con.prepareStatement("SELECT user FROM userchans WHERE BINARY user = ? AND channel = ?");
450 pstmt.setString(1,user);
451 pstmt.setString(2,channel);
452 ResultSet rs = pstmt.executeQuery();
453 rs.first();
454 String mode = rs.getString("user");
455 return true;
456 }
457 catch(Exception e)
458 {
459 return false;
460 }
461 }
462
463 /**
464 * Check if a host is a known op on a channel
465 * @param host host to check
466 * @param channel channel to check
467 *
468 * @return true or false
469 */
470 public boolean isKnownOpChan(String host, String channel)
471 {
472 try
473 {
474 PreparedStatement pstmt;
475 pstmt = con.prepareStatement("SELECT points FROM chanfix WHERE host = ? AND channel = ?");
476 pstmt.setString(1,host);
477 pstmt.setString(2,channel);
478 ResultSet rs = pstmt.executeQuery();
479 rs.first();
480 int points = rs.getInt("points");
481 if(points > 25)
482 {
483 return true;
484 }
485 return true;
486 }
487 catch(Exception e)
488 {
489 return false;
490 }
491 }
492
493 /**
494 * Check if a host has a chanfix level
495 * @param user numeric to check
496 * @param channel channel to check
497 *
498 * @return true or false
499 */
500 public boolean hasChanfix(String user, String channel)
501 {
502 try
503 {
504 PreparedStatement pstmt;
505 pstmt = con.prepareStatement("SELECT channel FROM chanfix WHERE host = ? AND channel = ?");
506 pstmt.setString(1,user);
507 pstmt.setString(2,channel);
508 ResultSet rs = pstmt.executeQuery();
509 rs.first();
510 String chan = rs.getString("channel");
511 return true;
512 }
513 catch(Exception e)
514 {
515 return false;
516 }
517 }
518
519 /**
520 * Check if a channel has ops
521 * @param channel channel to check
522 *
523 * @return true or false
524 */
525 public boolean chanHasOps(String channel)
526 {
527 try
528 {
529 PreparedStatement pstmt;
530 pstmt = con.prepareStatement("SELECT modes FROM userchans WHERE channel = ?");
531 pstmt.setString(1,channel);
532 ResultSet rs = pstmt.executeQuery();
533 while(rs.next())
534 {
535 String mode = rs.getString("modes");
536 if(mode.equals("o"))
537 {
538 return true;
539 }
540 }
541 return false;
542 }
543 catch(Exception e)
544 {
545 return false;
546 }
547 }
548
549 /**
550 * Check if a channel has known ops
551 * @param channel channel to check
552 *
553 * @return true or false
554 */
555 public boolean chanfixHasOps(String channel)
556 {
557 try
558 {
559 PreparedStatement pstmt;
560 pstmt = con.prepareStatement("SELECT points FROM chanfix WHERE channel = ?");
561 pstmt.setString(1,channel);
562 ResultSet rs = pstmt.executeQuery();
563 while(rs.next())
564 {
565 int points = rs.getInt("points");
566 if(points > 25)
567 {
568 return true;
569 }
570 }
571 return false;
572 }
573 catch(Exception e)
574 {
575 return false;
576 }
577 }
578
579 /**
580 * Get the number of users on a channel
581 * @param channel channel to check
582 *
583 * @return the number of users on a channel
584 */
585 public int getChanUsers(String channel)
586 {
587 try
588 {
589 PreparedStatement pstmt;
590 pstmt = con.prepareStatement("SELECT SQL_CALC_FOUND_ROWS * FROM userchans WHERE channel = ?");
591 pstmt.setString(1,channel);
592 ResultSet rs = pstmt.executeQuery();
593 pstmt = con.prepareStatement("SELECT FOUND_ROWS();");
594 rs = pstmt.executeQuery();
595 rs.first();
596 int users = rs.getInt(1);
597 rs.close();
598 return users;
599 }
600 catch(Exception e)
601 {
602 return 0;
603 }
604 }
605
606 /**
607 * Get the number of users authed
608 * @param auth auth to check
609 *
610 * @return the number of users authed
611 */
612 public int getAuthUsers(String auth)
613 {
614 try
615 {
616 return usersByAuth.get(auth.toLowerCase()).size();
617 }
618 catch(Exception e)
619 {
620 return 0;
621 }
622 }
623
624 /**
625 * Get the number of users connected from the same host
626 * @param host host to check
627 *
628 * @return the number of users connected from the same host
629 */
630 public int getHostCount(String host)
631 {
632 try
633 {
634 return usersByHost.get(host.toLowerCase()).size();
635 }
636 catch(Exception e)
637 {
638 return 0;
639 }
640 }
641
642 /**
643 * Get the number of users connected from the same host
644 * @param host host to check
645 *
646 * @return the number of users connected from the same host
647 */
648 public int getIpCount(String ip)
649 {
650 try
651 {
652 return usersByIP.get(ip).size();
653 }
654 catch(Exception e)
655 {
656 return 0;
657 }
658 }
659
660 public HashMap<String,User> getUsers()
661 {
662 return usersByNumeric;
663 }
664
665 /**
666 * Get a numeric's user row
667 * @param numer numeric of the user to fetch
668 *
669 * @return an array of all fields
670 */
671 public String[] getUserRow(String numer)
672 {
673 try
674 {
675 User u = usersByNumeric.get(numer);
676 return new String[]{ u.getNumeric(), u.getNick(), u.getIdent()+"@"+u.getHost(), u.getModes(), u.getAuth(), u.getOperator()+"", u.getServer(), u.getIp(), u.getFakehost()};
677 }
678 catch(Exception e)
679 {
680 return new String[]{"0","0","0","0","0","0","0","0","0","0"};
681 }
682 }
683
684 /**
685 * Get an auth's user row
686 * @param auth auth of the user to fetch
687 *
688 * @return an array of all fields
689 */
690 public String[] getUserRowViaAuth(String auth)
691 {
692 try
693 {
694 User u = usersByAuth.get(auth.toLowerCase()).get(0);
695 return new String[]{ u.getNumeric(), u.getNick(), u.getIdent()+"@"+u.getHost(), u.getModes(), u.getAuth(), u.getOperator()+"", u.getServer(), u.getIp(), u.getFakehost()};
696
697 }
698 catch(Exception e)
699 {
700 return new String[]{"0","0","0","0","0","0","0","0","0","0"};
701 }
702 }
703
704 /**
705 * Get a hosts's user row
706 * @param host host of the user to fetch
707 *
708 * @return an array of all fields
709 */
710 public String[] getUserRowViaHost(String host)
711 {
712 try
713 {
714 User u = usersByHost.get(host.toLowerCase()).get(0);
715 return new String[]{ u.getNumeric(), u.getNick(), u.getIdent()+"@"+u.getHost(), u.getModes(), u.getAuth(), u.getOperator()+"", u.getServer(), u.getIp(), u.getFakehost()};
716
717 }
718 catch(Exception e)
719 {
720 return new String[]{"0","0","0","0","0","0","0","0","0","0"};
721 }
722 }
723
724 /**
725 * Get a nick's user row
726 * @param nick nick of the user to fetch
727 *
728 * @return an array of all fields
729 */
730 public String[] getNickRow(String nick)
731 {
732 try
733 {
734 User u = usersByNick.get(nick.toLowerCase());
735 return new String[]{ u.getNumeric(), u.getNick(), u.getIdent()+"@"+u.getHost(), u.getModes(), u.getAuth(), u.getOperator()+"", u.getServer(), u.getIp(), u.getFakehost()};
736
737 }
738 catch(Exception e)
739 {
740 return new String[]{"0","0","0","0","0","0","0","0","0","0"};
741 }
742 }
743
744 /**
745 * Get an auth's row
746 * @param nick auth to fetch
747 *
748 * @return an array of all fields
749 */
750 public String[] getAuthRow(String nick)
751 {
752 try
753 {
754 Auth a = auths.get(nick.toLowerCase());
755 return new String[]{a.getAuthnick(),a.getPassword(),a.getMail(),a.getLevel()+"",a.getSuspended()+"",a.getLast()+"",a.getInfo(),a.getUserflags(),a.getVHost()};
756
757 }
758 catch(Exception e)
759 {
760 return new String[]{"0","0","0","0","0","0","0","0","0","0"};
761 }
762 }
763
764 /**
765 * Get a user's channels
766 * @param user user's numeric
767 *
768 * @return an array of all channels
769 */
770 public String[] getUserChans(String user)
771 {
772 try
773 {
774 PreparedStatement pstmt;
775 pstmt = con.prepareStatement("SELECT * FROM userchans WHERE BINARY user = ?");
776 pstmt.setString(1,user);
777 ResultSet rs = pstmt.executeQuery();
778 ArrayList<String> a = new ArrayList<String>();
779 while(rs.next())
780 {
781 a.add(rs.getString("channel"));
782 }
783 if(a.size()>0)
784 {
785 String[] r = (String[]) a.toArray(new String[ a.size() ]);
786 return r;
787 }
788 else
789 {
790 return new String[]{"0","0","0","0","0","0","0","0","0","0"};
791 }
792 }
793 catch(Exception e)
794 {
795 return new String[]{"0","0","0","0","0","0","0","0","0","0"};
796 }
797 }
798
799 /**
800 * Get a channel's users
801 * @param chan channel to fetch
802 *
803 * @return an array of all users
804 */
805 public String[] getChannelUsers(String chan)
806 {
807 try
808 {
809 PreparedStatement pstmt;
810 pstmt = con.prepareStatement("SELECT * FROM userchans WHERE channel = ?");
811 pstmt.setString(1,chan);
812 ResultSet rs = pstmt.executeQuery();
813 ArrayList<String> a = new ArrayList<String>();
814 while(rs.next())
815 {
816 a.add(rs.getString("user"));
817 }
818 if(a.size()>0)
819 {
820 String[] r = (String[]) a.toArray(new String[ a.size() ]);
821 return r;
822 }
823 else
824 {
825 return new String[]{"0","0","0","0","0","0","0","0","0","0"};
826 }
827 }
828 catch(Exception e)
829 {
830 return new String[]{"0","0","0","0","0","0","0","0","0","0"};
831 }
832 }
833
834 /**
835 * Get all channels
836 * @return an array of all channels
837 */
838 public String[] getUserChanTable()
839 {
840 try
841 {
842 PreparedStatement pstmt;
843 pstmt = con.prepareStatement("SELECT DISTINCT channel FROM userchans");
844 ResultSet rs = pstmt.executeQuery();
845 ArrayList<String> a = new ArrayList<String>();
846 while(rs.next())
847 {
848 a.add(rs.getString(1));
849 }
850 if(a.size()>0)
851 {
852 String[] r = (String[]) a.toArray(new String[ a.size() ]);
853 return r;
854 }
855 else
856 {
857 return new String[]{"0","0","0","0","0","0","0","0","0","0"};
858 }
859 }
860 catch(Exception e)
861 {
862 return new String[]{"0","0","0","0","0","0","0","0","0","0"};
863 }
864 }
865
866 /**
867 * Get all numerics
868 * @return an array of all numerics
869 */
870 public String[] getNumericTable()
871 {
872 try
873 {
874 ArrayList<String> a = new ArrayList<String>(usersByNumeric.keySet());
875 if(a.size()>0)
876 {
877 String[] r = (String[]) a.toArray(new String[ a.size() ]);
878 return r;
879 }
880 else
881 {
882 return new String[]{"0","0","0","0","0","0","0","0","0","0"};
883 }
884 }
885 catch(Exception e)
886 {
887 return new String[]{"0","0","0","0","0","0","0","0","0","0"};
888 }
889 }
890
891 /**
892 * Get all numerics
893 * @return an array of all numerics
894 */
895 public String[] getNumericTable(String server)
896 {
897 try
898 {
899 Server s = serversByHost.get(server.toLowerCase());
900 String numer = "";
901 if(s instanceof Server)
902 {
903 numer = s.getNumeric();
904 }
905 else
906 {
907 return new String[]{"0","0","0","0","0","0","0","0","0","0"};
908 }
909 ArrayList<String> numerics = new ArrayList<String>(usersByNumeric.keySet());
910 ArrayList<String> a = new ArrayList<String>();
911 for(String n : numerics)
912 {
913 if(n.startsWith(numer))
914 {
915 a.add(n);
916 }
917 }
918 if(a.size()>0)
919 {
920 String[] r = (String[]) a.toArray(new String[ a.size() ]);
921 return r;
922 }
923 else
924 {
925 return new String[]{"0","0","0","0","0","0","0","0","0","0"};
926 }
927 }
928 catch(Exception e)
929 {
930 return new String[]{"0","0","0","0","0","0","0","0","0","0"};
931 }
932 }
933
934 /**
935 * Get all numerics
936 * @return an array of all numerics
937 */
938 public String[] getNumericTableUniqueHosts()
939 {
940 try
941 {
942 ArrayList<String> hosts = new ArrayList<String>(usersByHost.keySet());
943 ArrayList<String> a = new ArrayList<String>();
944 for(String s : hosts)
945 {
946 a.add(usersByHost.get(s).get(0).getNumeric());
947 }
948 if(a.size()>0)
949 {
950 String[] r = (String[]) a.toArray(new String[ a.size() ]);
951 return r;
952 }
953 else
954 {
955
956 return new String[]{"0","0","0","0","0","0","0","0","0","0"};
957 }
958 }
959 catch(Exception e)
960 {
961 return new String[]{"0","0","0","0","0","0","0","0","0","0"};
962 }
963 }
964
965 public String[][] getServerList()
966 {
967 try
968 {
969
970 ArrayList<String> servers = new ArrayList<String>(serversByNumeric.keySet());
971 ArrayList<String> a = new ArrayList<String>();
972 ArrayList<String> b = new ArrayList<String>();
973 ArrayList<String> c = new ArrayList<String>();
974 ArrayList<String> d = new ArrayList<String>();
975 for(String numer : servers)
976 {
977 Server s = serversByNumeric.get(numer);
978 if(s instanceof Server)
979 {
980 a.add(numer);
981 b.add(s.getHost());
982 c.add(s.getHub());
983 d.add(s.getService().toString());
984 }
985 }
986 String[][] r = new String[a.size()][4];
987 if(a.size()>0)
988 {
989 for(int n=0; n<r.length; n++)
990 {
991 r[n][0] = a.get(n);
992 r[n][1] = b.get(n);
993 r[n][2] = c.get(n);
994 r[n][3] = d.get(n);
995 }
996 return r;
997 }
998 else
999 {
1000 return new String[][] {{"0","0"},{"0","0"}};
1001 }
1002 }
1003 catch(Exception e)
1004 {
1005 return new String[][] {{"0","0"},{"0","0"}};
1006 }
1007 }
1008
1009 public String[][] getServerTable()
1010 {
1011 try
1012 {
1013
1014 ArrayList<String> servers = new ArrayList<String>(serversByHost.keySet());
1015 ArrayList<String> a = new ArrayList<String>();
1016 ArrayList<String> b = new ArrayList<String>();
1017 ArrayList<String> c = new ArrayList<String>();
1018 for(String host : servers)
1019 {
1020 Server s = serversByHost.get(host);
1021 String numer = "";
1022 if(s instanceof Server)
1023 {
1024 if(!s.getService())
1025 {
1026 a.add(host);
1027 numer = s.getNumeric();
1028 ArrayList<String> numerics = new ArrayList<String>(usersByNumeric.keySet());
1029 int users = 0;
1030 int opers = 0;
1031 for(String n : numerics)
1032 {
1033 if(n.startsWith(numer))
1034 {
1035 users++;
1036 User u = usersByNumeric.get(n);
1037 if(u.getModes().contains("o"))
1038 {
1039 opers++;
1040 }
1041 }
1042 }
1043 b.add(users+"");
1044 c.add(opers+"");
1045 }
1046 }
1047 }
1048 String[][] r = new String[a.size()][3];
1049 if(a.size()>0)
1050 {
1051 for(int n=0; n<r.length; n++)
1052 {
1053 r[n][0] = a.get(n);
1054 r[n][1] = b.get(n);
1055 r[n][2] = c.get(n);
1056 }
1057 return r;
1058 }
1059 else
1060 {
1061 return new String[][] {{"0","0"},{"0","0"}};
1062 }
1063 }
1064 catch(Exception e)
1065 {
1066 return new String[][] {{"0","0"},{"0","0"}};
1067 }
1068 }
1069
1070 /**
1071 * Get all staff members
1072 * @return an array of all staff members
1073 */
1074 public ArrayList<String> getStaffList()
1075 {
1076 try
1077 {
1078 PreparedStatement pstmt;
1079 pstmt = con.prepareStatement("SELECT level,authnick FROM auths WHERE level > 1 AND level < 5000 ORDER BY level ASC;");
1080 ResultSet rs = pstmt.executeQuery();
1081 ArrayList<String> staff = new ArrayList<String>();
1082 while(rs.next())
1083 {
1084 int lev = rs.getInt("level");
1085 String a = rs.getString("authnick") + " (";
1086 String x = "Helper";
1087 if(lev>99)
1088 {
1089 x = "IRC Operator";
1090 }
1091 if(lev>949)
1092 {
1093 x = "IRC Administrator";
1094 }
1095 if(lev>=999)
1096 {
1097 x = "Services Developer";
1098 }
1099 a += x+").";
1100 staff.add(a);
1101 }
1102 return staff;
1103 }
1104 catch(Exception e)
1105 {
1106 return new ArrayList<String>();
1107 }
1108 }
1109
1110 /**
1111 * Get all user rows connected to an auth
1112 * @param auth auth to fetch
1113 *
1114 * @return a double array of all users
1115 */
1116 public ArrayList<String[]> getUserRowsViaAuth(String auth)
1117 {
1118 try
1119 {
1120 ArrayList<User> users = usersByAuth.get(auth.toLowerCase());
1121 ArrayList<String[]> a = new ArrayList<String[]>();
1122 for(User u : users)
1123 {
1124 String[] user = new String[]{u.getNumeric(),u.getNick(),u.getIdent()+"@"+u.getHost(),u.getModes(),u.getAuth(),u.getOperator()+"",u.getServer(),u.getIp(),u.getFakehost()};
1125 a.add(user);
1126 }
1127 return a;
1128 }
1129 catch(Exception e)
1130 {
1131 return new ArrayList<String[]>();
1132 }
1133 }
1134
1135 /**
1136 * Set a userline field to a new value
1137 * @param numer numeric of user to adapt
1138 * @param colum colum to change
1139 * @param info new info to insert
1140 */
1141 public void setUserField(String numer, int colum, String info)
1142 {
1143 User u = usersByNumeric.get(numer);
1144 if(u instanceof User)
1145 {
1146 try
1147 {
1148 switch(colum)
1149 {
1150 case 0:
1151 u.setNumeric(info);
1152 usersByNumeric.remove(numer);
1153 usersByNumeric.put(info,u);
1154 break;
1155 case 1:
1156 String oldkey = u.getNick();
1157 u.setNick(info);
1158 if(usersByNick.containsKey(oldkey.toLowerCase()))
1159 {
1160 usersByNick.remove(oldkey.toLowerCase());
1161 }
1162 usersByNick.put(info.toLowerCase(),u);
1163 break;
1164 case 2:
1165 String[] splithost = info.split("@");
1166 u.setIdent(splithost[0]);
1167 u.setHost(splithost[1]);
1168 if(usersByHost.containsKey(info.toLowerCase()))
1169 {
1170 ArrayList<User> users = usersByHost.get(info.toLowerCase());
1171 users.add(u);
1172 usersByHost.put(info.toLowerCase(),users);
1173 }
1174 else
1175 {
1176 ArrayList<User> users = new ArrayList<User>();
1177 users.add(u);
1178 usersByHost.put(info.toLowerCase(),users);
1179 }
1180 break;
1181 case 3:
1182 u.setModes(info);
1183 break;
1184 case 4:
1185 u.setAuth(info);
1186 if(usersByAuth.containsKey(info.toLowerCase()))
1187 {
1188 ArrayList<User> users = usersByAuth.get(info.toLowerCase());
1189 users.add(u);
1190 usersByAuth.put(info.toLowerCase(),users);
1191 }
1192 else
1193 {
1194 ArrayList<User> users = new ArrayList<User>();
1195 users.add(u);
1196 usersByAuth.put(info.toLowerCase(),users);
1197 }
1198 break;
1199 case 5:
1200 if(Boolean.parseBoolean(info))
1201 {
1202 u.setOperator(1);
1203 }
1204 else
1205 {
1206 u.setOperator(0);
1207 }
1208 break;
1209 case 6:
1210 u.setServer(info);
1211 break;
1212 case 7:
1213 u.setIp(info);
1214 if(usersByIP.containsKey(info))
1215 {
1216 ArrayList<User> users = usersByIP.get(info);
1217 users.add(u);
1218 usersByIP.put(info.toLowerCase(),users);
1219 }
1220 else
1221 {
1222 ArrayList<User> users = new ArrayList<User>();
1223 users.add(u);
1224 usersByIP.put(info,users);
1225 }
1226 break;
1227 case 8:
1228 u.setFakehost(info);
1229 break;
1230 }
1231 }
1232 catch ( Exception e )
1233 {
1234 System.out.println( "Error finding user." );
1235 C.debug(e);
1236 C.die("SQL error, trying to die gracefully.");
1237 }
1238 }
1239 else
1240 {
1241 System.out.println( "Error finding user." );
1242 C.printDebug("Error finding user.");
1243 }
1244 }
1245
1246 /**
1247 * Set an authline field to a new value
1248 * @param auth auth of user to adapt
1249 * @param colum colum to change
1250 * @param info new info to insert
1251 */
1252 public void setAuthField(String auth, int colum, String info)
1253 {
1254 try
1255 {
1256 String set[] = new String[]{"authnick", "pass", "mail","level","suspended","last","info","userflags","vhost"};
1257 PreparedStatement pstmt;
1258 pstmt = con.prepareStatement("UPDATE auths SET "+set[colum]+" = ? WHERE authnick = ?");
1259 if(colum==3)
1260 {
1261 pstmt.setInt(1,Integer.parseInt(info));
1262 }
1263 else if(colum==4)
1264 {
1265 pstmt.setBoolean(1,Boolean.parseBoolean(info));
1266 }
1267 else if(colum==5)
1268 {
1269 pstmt.setLong(1,Long.parseLong(info));
1270 }
1271 else
1272 {
1273 pstmt.setString(1,info);
1274 }
1275 pstmt.setString(2,auth);
1276 pstmt.executeUpdate();
1277 Auth a = auths.get(auth.toLowerCase());
1278 if(a instanceof Auth)
1279 {
1280 switch(colum)
1281 {
1282 case 0:
1283 a.setAuthnick(info);
1284 break;
1285 case 1:
1286 a.setPassword(info);
1287 break;
1288 case 2:
1289 a.setMail(info);
1290 break;
1291 case 3:
1292 a.setLevel(Integer.parseInt(info));
1293 break;
1294 case 4:
1295 a.setSuspended((Boolean.parseBoolean(info) ? 1 : 0));
1296 break;
1297 case 5:
1298 a.setLast(Long.parseLong(info));
1299 break;
1300 case 6:
1301 a.setInfo(info);
1302 break;
1303 case 7:
1304 a.setUserflags(info);
1305 break;
1306 case 8:
1307 a.setVHost(info);
1308 break;
1309 }
1310 }
1311 }
1312 catch ( SQLException e )
1313 {
1314 System.out.println ( "Error executing sql statement" );
1315 C.debug(e);
1316 C.die("SQL error, trying to die gracefully.");
1317 }
1318 }
1319
1320 /**
1321 * Set a user's chanmode
1322 * @param user numeric of user to adapt
1323 * @param chan channel where the mode changed
1324 * @param mode new mode
1325 */
1326 public void setUserChanMode(String user, String chan, String mode)
1327 {
1328 try
1329 {
1330 if(mode.contains("o"))
1331 {
1332 PreparedStatement pstmt;
1333 String change = "0";
1334 if(mode.contains("+"))
1335 {
1336 change = "o";
1337 }
1338 pstmt = con.prepareStatement("UPDATE userchans SET modes = ? WHERE BINARY user = ? AND channel = ?");
1339 pstmt.setString(1,change);
1340 pstmt.setString(2,user);
1341 pstmt.setString(3,chan);
1342 pstmt.executeUpdate();
1343 }
1344 }
1345 catch ( SQLException e )
1346 {
1347 System.out.println ( "Error executing sql statement" );
1348 C.debug(e);
1349 C.die("SQL error, trying to die gracefully.");
1350 }
1351
1352
1353 Channel c = channels.get(chan.toLowerCase());
1354 if(c instanceof Channel)
1355 {
1356 c.setUserChanMode(user,mode);
1357 }
1358 }
1359
1360 /**
1361 * Remove all ops from a channel
1362 * @param chan channel to change
1363 */
1364 public void setClearMode(String chan, String modes)
1365 {
1366 try
1367 {
1368 PreparedStatement pstmt;
1369 pstmt = con.prepareStatement("UPDATE userchans SET modes = '0' WHERE channel = ?");
1370 pstmt.setString(1,chan);
1371 pstmt.executeUpdate();
1372 }
1373 catch ( SQLException e )
1374 {
1375 System.out.println ( "Error executing sql statement" );
1376 C.debug(e);
1377 C.die("SQL error, trying to die gracefully.");
1378 }
1379
1380
1381
1382 Channel c = channels.get(chan.toLowerCase());
1383 if(c instanceof Channel)
1384 {
1385 c.setClearMode(modes);
1386 }
1387 }
1388
1389 /**
1390 * Delete an auth
1391 * @param auth auth to delete
1392 */
1393 public void delAuth(String auth)
1394 {
1395 try
1396 {
1397 PreparedStatement pstmt;
1398 pstmt = con.prepareStatement("DELETE FROM auths WHERE authnick = ? LIMIT 1");
1399 pstmt.setString(1,auth);
1400 pstmt.executeUpdate();
1401 auths.remove(auth.toLowerCase());
1402 }
1403 catch ( SQLException e )
1404 {
1405 System.out.println ( "Error executing sql statement" );
1406 C.debug(e);
1407 C.die("SQL error, trying to die gracefully.");
1408 }
1409 }
1410
1411 /**
1412 * Delete a user
1413 * @param numer numeric of user to delete
1414 */
1415 public void delUser(String numer)
1416 {
1417 try
1418 {
1419 User u = usersByNumeric.get(numer);
1420 usersByNick.remove(u.getNick().toLowerCase());
1421 ArrayList<User> users = usersByAuth.get(u.getAuth().toLowerCase());
1422 if(users instanceof ArrayList)
1423 {
1424 users.remove(u);
1425 if(users.size()>0)
1426 {
1427 usersByAuth.put(u.getAuth(),users);
1428 }
1429 else
1430 {
1431 usersByAuth.remove(u.getAuth());
1432 }
1433 }
1434 users = usersByHost.get(u.getHost());
1435 users.remove(u);
1436 if(users.size()>0)
1437 {
1438 usersByHost.put(u.getHost(),users);
1439 }
1440 else
1441 {
1442 usersByHost.remove(u.getHost());
1443 }
1444 users = usersByIP.get(u.getIp());
1445 users.remove(u);
1446 if(users.size()>0)
1447 {
1448 usersByIP.put(u.getIp(),users);
1449 }
1450 else
1451 {
1452 usersByIP.remove(u.getIp());
1453 }
1454 usersByNumeric.remove(numer);
1455 System.gc();
1456 PreparedStatement pstmt;
1457 pstmt = con.prepareStatement("DELETE FROM userchans WHERE BINARY user = ?");
1458 pstmt.setString(1,numer);
1459 pstmt.executeUpdate();
1460
1461 ArrayList<String> userchannels = u.getChannels();
1462 for(String channel: userchannels)
1463 {
1464 delUser(numer);
1465 }
1466 }
1467 catch ( Exception e )
1468 {
1469 }
1470 }
1471
1472 /**
1473 * Delete a server
1474 * @param host host of server to delete
1475 */
1476 public void delServer(String host)
1477 {
1478 Server s = serversByHost.get(host.toLowerCase());
1479 if(s instanceof Server)
1480 {
1481 delChildren(host);
1482 String numer = s.getNumeric();
1483 String hub = s.getHub();
1484 ArrayList<Server> servers = serversByHub.get(hub);
1485 if(servers instanceof ArrayList)
1486 {
1487 for(int i=0; i<servers.size(); i++)
1488 {
1489 if(servers.get(i).equals(s))
1490 {
1491 servers.remove(i);
1492 break;
1493 }
1494 }
1495 }
1496 }
1497 else
1498 {
1499 System.out.println ( "Error Removing server: "+host );
1500 C.die("SQL error, trying to die gracefully.");
1501 }
1502 }
1503
1504 /**
1505 * Delete a Server's children
1506 * @param host host of the server that has to remove his children
1507 */
1508
1509 public void delChildren(String host)
1510 {
1511 Server s = serversByHost.get(host.toLowerCase());
1512 if(s instanceof Server)
1513 {
1514 String numer = s.getNumeric();
1515 ArrayList<Server> servers = serversByHub.get(numer);
1516 if(servers instanceof ArrayList)
1517 {
1518 for(Server ser : servers)
1519 {
1520 delChildren(ser.getHost());
1521 }
1522 }
1523 ArrayList<String> numerics = new ArrayList<String>(usersByNumeric.keySet());
1524 for(String n : numerics)
1525 {
1526 if(n.startsWith(numer))
1527 {
1528 delUser(n);
1529 }
1530 }
1531 serversByHub.remove(numer);
1532 serversByNumeric.remove(numer);
1533 serversByHost.remove(host.toLowerCase());
1534 }
1535 else
1536 {
1537 System.out.println ( "Error Removing server: "+host );
1538 C.die("SQL error, trying to die gracefully.");
1539 }
1540 }
1541
1542 /**
1543 * Delete a user from a channel
1544 * @param chan channel where user should be removed
1545 * @param user numeric of user to remove
1546 */
1547 public void delUserChan(String chan, String user)
1548 {
1549 try
1550 {
1551 PreparedStatement pstmt;
1552 pstmt = con.prepareStatement("DELETE FROM userchans WHERE channel = ? AND BINARY user = ?");
1553 pstmt.setString(1,chan);
1554 pstmt.setString(2,user);
1555 pstmt.executeUpdate();
1556 }
1557 catch ( SQLException e )
1558 {
1559 System.out.println ( "Error executing sql statement" );
1560 C.debug(e);
1561 C.die("SQL error, trying to die gracefully.");
1562 }
1563
1564 Channel c = channels.get(chan.toLowerCase());
1565 if(c instanceof Channel)
1566 {
1567 c.delUser(user);
1568 }
1569 User u = usersByNumeric.get(user);
1570 if(u instanceof User)
1571 {
1572 u.partChannel(chan);
1573 }
1574 }
1575
1576 public void addUser(String nume,String nick, String host, String mode, String auth, boolean isop, String server, String ip, String fake)
1577 {
1578 try
1579 {
1580 User u = new User(nume);
1581 u.setNick(nick);
1582 String[] splithost = host.split("@");
1583 u.setIdent(splithost[0]);
1584 u.setHost(splithost[1]);
1585 u.setModes(mode);
1586 u.setAuth(auth);
1587 if(isop)
1588 {
1589 u.setOperator(1);
1590 }
1591 else
1592 {
1593 u.setOperator(0);
1594 }
1595 u.setServer(server);
1596 u.setIp(ip);
1597 u.setFakehost(fake);
1598 usersByNumeric.put(nume,u);
1599 usersByNick.put(nick.toLowerCase(),u);
1600 if(!auth.equals("0"))
1601 {
1602 if(usersByAuth.containsKey(auth.toLowerCase()))
1603 {
1604 ArrayList<User> users = usersByAuth.get(auth.toLowerCase());
1605 users.add(u);
1606 usersByAuth.put(auth.toLowerCase(),users);
1607 }
1608 else
1609 {
1610 ArrayList<User> users = new ArrayList<User>();
1611 users.add(u);
1612 usersByAuth.put(auth.toLowerCase(),users);
1613 }
1614 }
1615 if(usersByHost.containsKey(splithost[1]))
1616 {
1617 ArrayList<User> users = usersByHost.get(splithost[1]);
1618 users.add(u);
1619 usersByHost.put(splithost[1],users);
1620 }
1621 else
1622 {
1623 ArrayList<User> users = new ArrayList<User>();
1624 users.add(u);
1625 usersByHost.put(splithost[1],users);
1626 }
1627 if(usersByIP.containsKey(ip))
1628 {
1629 ArrayList<User> users = usersByIP.get(ip);
1630 users.add(u);
1631 usersByIP.put(ip,users);
1632 }
1633 else
1634 {
1635 ArrayList<User> users = new ArrayList<User>();
1636 users.add(u);
1637 usersByIP.put(ip,users);
1638 }
1639 }
1640 catch ( Exception e )
1641 {
1642 System.out.println ( "Error executing statement" );
1643 C.debug(e);
1644 C.die("SQL error, trying to die gracefully.");
1645 }
1646 }
1647
1648 public void addAuth(String auth,String pass, String mail1, int lev, boolean suspended, Long time, String info,String userflags, String vhost)
1649 {
1650 try
1651 {
1652 PreparedStatement pstmt;
1653 pstmt = con.prepareStatement("INSERT INTO auths VALUES (?,?,?,?,?,?,?,?,?)");
1654 pstmt.setString(1,auth);
1655 pstmt.setString(2,pass);
1656 pstmt.setString(3,mail1);
1657 pstmt.setInt(4,lev);
1658 pstmt.setBoolean(5,suspended);
1659 pstmt.setLong(6,time);
1660 pstmt.setString(7,info);
1661 pstmt.setString(8,userflags);
1662 pstmt.setString(9,vhost);
1663 pstmt.executeUpdate();
1664 Auth a = new Auth(auth);
1665 a.setPassword(pass);
1666 a.setMail(mail1);
1667 a.setLevel(lev);
1668 a.setSuspended(suspended ? 1 : 0);
1669 a.setLast(time);
1670 a.setInfo(info);
1671 a.setUserflags(userflags);
1672 auths.put(auth.toLowerCase(),a);
1673 }
1674 catch ( SQLException e )
1675 {
1676 System.out.println ( "Error executing sql statement" );
1677 C.debug(e);
1678 C.die("SQL error, trying to die gracefully.");
1679 }
1680 }
1681
1682 public void addServer(String numer, String host ,String hub, boolean service)
1683 {
1684 try
1685 {
1686 Server s = new Server(numer);
1687 s.setHost(host.toLowerCase());
1688 s.setHub(hub);
1689 s.setService(service);
1690 serversByHost.put(host.toLowerCase(),s);
1691 serversByNumeric.put(numer,s);
1692 if(serversByHub.containsKey(hub))
1693 {
1694 ArrayList<Server> servers = serversByHub.get(hub);
1695 servers.add(s);
1696 serversByHub.put(hub,servers);
1697 }
1698 else
1699 {
1700 ArrayList<Server> servers = new ArrayList<Server>();
1701 servers.add(s);
1702 serversByHub.put(hub,servers);
1703 }
1704 }
1705 catch (Exception e)
1706 {
1707 System.out.println ( "Error executing statement" );
1708 C.debug(e);
1709 C.die("SQL error, trying to die gracefully.");
1710 }
1711 }
1712
1713 public void addUserChan(String channel,String user,String modes, String timestamp)
1714 {
1715 try
1716 {
1717 PreparedStatement pstmt;
1718 pstmt = con.prepareStatement("INSERT INTO userchans VALUES (?,?,?)");
1719 pstmt.setString(1,channel);
1720 pstmt.setString(2,user);
1721 pstmt.setString(3,modes);
1722 pstmt.executeUpdate();
1723 }
1724 catch ( SQLException e )
1725 {
1726 System.out.println ( "Error executing sql statement" );
1727 C.debug(e);
1728 C.die("SQL error, trying to die gracefully.");
1729 }
1730
1731
1732 Channel c = channels.get(channel.toLowerCase());
1733 if(c instanceof Channel)
1734 {
1735 c.addUser(user);
1736 }
1737 else
1738 {
1739 if(modes.equals("o"))
1740 {
1741 c = new Channel(channel,timestamp,user,true);
1742 }
1743 else
1744 {
1745 c = new Channel(channel,timestamp,user,false);
1746 }
1747 channels.put(channel.toLowerCase(),c);
1748 }
1749 User u = usersByNumeric.get(user);
1750 if(u instanceof User)
1751 {
1752 u.joinChannel(channel);
1753 }
1754 }
1755
1756 public void save()
1757 {
1758 try
1759 {
1760 File file=new File("backup/");
1761 if(file.exists())
1762 {
1763 PreparedStatement pstmt;
1764 C.report("Creating DB Backup...");
1765
1766 Calendar cal = Calendar.getInstance();
1767 long now = (cal.getTimeInMillis() / 1000);
1768
1769 Runtime rt = Runtime.getRuntime();
1770 File backup=new File("backup/"+now+".sql");
1771 PrintStream ps;
1772
1773 Process child = rt.exec("mysqldump -u"+user+" -p"+password+" "+db);
1774 ps=new PrintStream(backup);
1775 InputStream in = child.getInputStream();
1776 int ch;
1777 while ((ch = in.read()) != -1)
1778 {
1779 ps.write(ch);
1780 }
1781
1782 InputStream err = child.getErrorStream();
1783 while ((ch = err.read()) != -1)
1784 {
1785 System.out.write(ch);
1786 }
1787 C.report("Done.");
1788 }
1789 else
1790 {
1791 C.report("Could not access backup directory.");
1792 }
1793 }
1794 catch(Exception e)
1795 {
1796 System.out.println ( "Error executing sql statement" );
1797 C.debug(e);
1798 C.die("SQL error, trying to die gracefully.");
1799 }
1800 }
1801
1802 public void clean()
1803 {
1804 Calendar cal = Calendar.getInstance();
1805 long now = (cal.getTimeInMillis() / 1000);
1806 try
1807 {
1808 C.report("Cleaning Core DB...");
1809 ArrayList<String> authlist = new ArrayList<String>(auths.keySet());
1810 ArrayList<String> delete = new ArrayList<String>();
1811 for(String authnick : authlist)
1812 {
1813 Auth auth = auths.get(authnick);
1814 boolean online = authOnline(authnick);
1815 if(!online && auth.getLevel() < 2 && auth.getSuspended()==0 && auth.getLast() < now-3456000)
1816 {
1817 delete.add(authnick);
1818 }
1819 }
1820 if(delete.size()>0)
1821 {
1822 for(String auth : delete)
1823 {
1824 C.report("Deleting AUTH: '" + auth + "'");
1825 delAuth(auth);
1826 }
1827 }
1828 C.report("Core Cleanup complete!");
1829 }
1830 catch(Exception e)
1831 {
1832 System.out.println ( "Error executing sql statement" );
1833 e.printStackTrace();
1834 C.die("SQL error, trying to die gracefully.");
1835 }
1836 }
1837
1838 public String encrypt(String plaintext)
1839 {
1840 byte[] defaultBytes = plaintext.getBytes();
1841 try
1842 {
1843 MessageDigest algorithm = MessageDigest.getInstance("MD5");
1844 algorithm.reset();
1845 algorithm.update(defaultBytes);
1846 byte messageDigest[] = algorithm.digest();
1847 StringBuffer hexString = new StringBuffer();
1848 for (int i=0;i<messageDigest.length;i++)
1849 {
1850 String hex = Integer.toHexString(0xFF & messageDigest[i]);
1851 if(hex.length()==1) hexString.append('0');
1852 hexString.append(hex);
1853 }
1854 return hexString.toString();
1855 }
1856 catch(NoSuchAlgorithmException e)
1857 {
1858 System.out.println ( "Error encrypting password." );
1859 C.debug(e);
1860 C.die("SQL error, trying to die gracefully.");
1861 return "0";
1862 }
1863 }
1864
1865 public void chanfix()
1866 {
1867 String users[] = getNumericTableUniqueHosts();
1868 for(int n=0; n<users.length; n++)
1869 {
1870 if(!isService(users[n]))
1871 {
1872 String channels[] = getUserChans(users[n]);;
1873 for(int p=0; p<channels.length; p++)
1874 {
1875 if(isOpChan(users[n], channels[p]))
1876 {
1877 String user[] = getUserRow(users[n]);
1878 if(!user[2].equalsIgnoreCase(C.get_ident() + "@" + C.get_host()))
1879 {
1880 String userid = user[2];
1881 if(!user[4].equalsIgnoreCase("0"))
1882 {
1883 userid = user[4];
1884 }
1885 if(hasChanfix(userid, channels[p]))
1886 {
1887 chanfix_addpoint(userid, channels[p]);
1888 }
1889 else
1890 {
1891 if(getChanUsers(channels[p]) > 2)
1892 {
1893 add_chanfix(userid, channels[p]);
1894 }
1895 }
1896 }
1897 }
1898 }
1899 }
1900 }
1901 try
1902 {
1903 PreparedStatement pstmt;
1904 pstmt = con.prepareStatement("SELECT * FROM chanfix");
1905 ResultSet rs = pstmt.executeQuery();
1906 while(rs.next())
1907 {
1908 if(rs.getString("host").contains("@"))
1909 {
1910 String user[] = getUserRowViaHost(rs.getString("host"));
1911 if(!isOpChan(user[0],rs.getString("channel")))
1912 {
1913 chanfix_delpoint(rs.getString("channel"),rs.getString("host"));
1914 }
1915 }
1916 else
1917 {
1918 String user[] = getUserRowViaAuth(rs.getString("host"));
1919 if(!isOpChan(user[0],rs.getString("channel")))
1920 {
1921 chanfix_delpoint(rs.getString("channel"),rs.getString("host"));
1922 }
1923 }
1924 }
1925 }
1926 catch ( SQLException e )
1927 {
1928 System.out.println ( "Error executing sql statement" );
1929 C.debug(e);
1930 C.die("SQL error, trying to die gracefully.");
1931 }
1932 }
1933
1934 public void chanfix_addpoint(String host, String chan)
1935 {
1936 try
1937 {
1938 PreparedStatement pstmt;
1939 pstmt = con.prepareStatement("SELECT * FROM chanfix WHERE host = ? AND channel = ?");
1940 pstmt.setString(1,host);
1941 pstmt.setString(2,chan);
1942 ResultSet rs = pstmt.executeQuery();
1943 rs.first();
1944 if(Integer.parseInt(rs.getString("points"))<4033)
1945 {
1946 String points = ""+(Integer.parseInt(rs.getString("points"))+1);
1947 pstmt = con.prepareStatement("UPDATE chanfix SET points = ? WHERE host = ? AND channel = ?");
1948 pstmt.setInt(1,Integer.parseInt(points));
1949 pstmt.setString(2,host);
1950 pstmt.setString(3,chan);
1951 pstmt.executeUpdate();
1952 }
1953 pstmt = con.prepareStatement("UPDATE chanfix SET last = ? WHERE host = ? AND channel = ?");
1954 pstmt.setLong(1,Long.parseLong(C.get_time()));
1955 pstmt.setString(2,host);
1956 pstmt.setString(3,chan);
1957 pstmt.executeUpdate();
1958 }
1959 catch ( SQLException e )
1960 {
1961 System.out.println ( "Error executing sql statement" );
1962 C.debug(e);
1963 C.die("SQL error, trying to die gracefully.");
1964 }
1965 }
1966
1967 public void chanfix_delpoint(String chan, String host)
1968 {
1969 try
1970 {
1971 PreparedStatement pstmt;
1972 pstmt = con.prepareStatement("SELECT * FROM chanfix WHERE host = ? AND channel = ?");
1973 pstmt.setString(1,host);
1974 pstmt.setString(2,chan);
1975 ResultSet rs = pstmt.executeQuery();
1976 rs.first();
1977 if(rs.getInt("points")>1)
1978 {
1979 int points = rs.getInt("points")-1;
1980 pstmt = con.prepareStatement("UPDATE chanfix SET points = ? WHERE host = ? AND channel = ?");
1981 pstmt.setInt(1,points);
1982 pstmt.setString(2,host);
1983 pstmt.setString(3,chan);
1984 pstmt.executeUpdate();
1985 }
1986 else
1987 {
1988 del_chanfix(chan, host);
1989 }
1990 }
1991 catch ( SQLException e )
1992 {
1993 System.out.println ( "Error executing sql statement" );
1994 C.debug(e);
1995 C.die("SQL error, trying to die gracefully.");
1996 }
1997 }
1998
1999 public void add_chanfix(String host, String chan)
2000 {
2001 try
2002 {
2003 PreparedStatement pstmt;
2004 pstmt = con.prepareStatement("INSERT INTO chanfix VALUES (?,?,?,?)");
2005 pstmt.setString(1,chan);
2006 pstmt.setString(2,host);
2007 pstmt.setInt(3,1);
2008 pstmt.setLong(4,Long.parseLong(C.get_time()));
2009 pstmt.executeUpdate();
2010 }
2011 catch ( SQLException e )
2012 {
2013 System.out.println ( "Error executing sql statement" );
2014 C.debug(e);
2015 C.die("SQL error, trying to die gracefully.");
2016 }
2017 }
2018
2019 public void del_chanfix(String channel, String host)
2020 {
2021 try
2022 {
2023 PreparedStatement pstmt;
2024 pstmt = con.prepareStatement("DELETE FROM chanfix WHERE host = ? AND channel = ?");
2025 pstmt.setString(1,host);
2026 pstmt.setString(2,channel);
2027 pstmt.executeUpdate();
2028 }
2029 catch ( SQLException e )
2030 {
2031 System.out.println ( "Error executing sql statement" );
2032 C.debug(e);
2033 C.die("SQL error, trying to die gracefully.");
2034 }
2035 }
2036
2037 public ArrayList<String> checkUserChans()
2038 {
2039 ArrayList<String> info = new ArrayList<String>();
2040 /*
2041 chanExists
2042 -isOpChan
2043 -isOnChan
2044 -chanHasOps
2045 getChanUsers
2046 -getUserChans
2047 getChannelUsers
2048 -getUserChanTable
2049 */
2050 info.add("MYSQL vs Memory");
2051 Channel c = channels.get("#azefezfzefzefzfzefzefzf");
2052 info.add(chanExists("#azefezfzefzefzfzefzefzf")+" "+(c instanceof Channel));
2053 c = channels.get("#borknet");
2054 if(c instanceof Channel)
2055 {
2056 info.add(chanExists("#BorkNet")+" true");
2057 info.add(getChanUsers("#BorkNet")+" "+c.getUsercount());
2058 info.add(Arrays.toString(getChannelUsers("#BorkNet"))+" "+Arrays.toString(c.getUserlist()));
2059 }
2060 else
2061 {
2062 info.add("#BorkNet not in memory");
2063 }
2064 return info;
2065 }
2066 }