3 # BorkNet Services Core
7 # Copyright (C) 2004 Ozafy - ozafy@borknet.org - http://www.borknet.org
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.
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.
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.
24 package borknet_services
.core
;
28 import java
.security
.*;
29 import borknet_services
.core
.*;
32 * The database communication class of the Q IRC Bot.
33 * @author Ozafy - ozafy@borknet.org - http://www.borknet.org
35 public class CoreDBControl
37 /** Database server */
38 private String server
;
41 /** Database password */
42 private String password
;
45 /** Database connection */
46 private Connection con
;
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
>>();
56 private HashMap
<String
,Auth
> auths
= new HashMap
<String
,Auth
>();
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
>();
62 private HashMap
<String
,Channel
> channels
= new HashMap
<String
,Channel
>();
65 * Constructs a Database connection.
66 * @param server Database server
67 * @param user Database user
68 * @param pass Database password
70 * @param debug Are we debugging?
73 public CoreDBControl(String server
, String user
, String password
, String db
, Core C
)
79 this.password
= password
;
83 con
= getConnection ( server
, user
, password
, db
);
84 C
.printDebug( "[>---<] >> *** Truncating userchans..." );
85 PreparedStatement pstmt
;
86 pstmt
= con
.prepareStatement("TRUNCATE TABLE `userchans`");
88 C
.printDebug( "[>---<] >> *** Done." );
89 C
.printDebug( "[>---<] >> *** Loading Auth data..." );
91 C
.printDebug( "[>---<] >> *** Done." );
95 C
.printDebug("Database error!");
96 C
.die("SQL error, trying to die gracefully.");
101 * Close the Database connection.
103 public void close_mysql()
108 C
.printDebug( "[>---<] >> *** MySQL connection closed clean." );
112 C
.printDebug("MySQL connection failed to close!");
113 C
.die("SQL error, trying to die gracefully.");
118 * Test the Database driver
120 protected void testDriver ( )
124 Class
.forName ( "org.gjt.mm.mysql.Driver" );
125 C
.printDebug( "[>---<] >> *** MySQL Driver Found" );
127 catch ( java
.lang
.ClassNotFoundException e
)
129 C
.printDebug("MySQL JDBC Driver not found!");
130 C
.die("SQL error, trying to die gracefully.");
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
141 * @return a Database connection.
143 protected Connection
getConnection ( String server
, String user
, String pass
, String db
) throws Exception
148 url
= "jdbc:mysql://"+server
+"/"+db
+"?user="+user
+"&password="+pass
;
149 Connection con
= DriverManager
.getConnection(url
);
150 C
.printDebug("[>---<] >> *** Connection established to MySQL server...");
153 catch ( java
.sql
.SQLException e
)
155 C
.printDebug("Connection couldn't be established to " + url
);
161 public Connection
getCon()
166 * Cleans all tables that arn't permanent
168 public void cleanDB()
172 PreparedStatement pstmt
;
173 pstmt
= con
.prepareStatement("TRUNCATE TABLE `userchans`");
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
>>();
183 System
.out
.println ( "Error executing sql statement" );
185 C
.die("SQL error, trying to die gracefully.");
189 public void loadAuths()
193 PreparedStatement pstmt
;
194 pstmt
= con
.prepareStatement("SELECT * FROM auths");
195 ResultSet rs
= pstmt
.executeQuery();
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
);
213 System
.out
.println ( "Error executing sql statement" );
215 C
.die("SQL error, trying to die gracefully.");
220 * Check if a channel exists.
221 * @param chan channel to check
223 * @return true or false
225 public boolean chanExists(String chan
)
229 PreparedStatement pstmt
;
230 pstmt
= con
.prepareStatement("SELECT channel FROM userchans WHERE channel = ?");
231 pstmt
.setString(1,chan
);
232 ResultSet rs
= pstmt
.executeQuery();
234 String channel
= rs
.getString("channel");
244 * Check if an auth exists.
245 * @param auth auth to check
247 * @return true or false
249 public boolean authExists(String auth
)
251 return auths
.containsKey(auth
.toLowerCase());
255 * Check if an auth is online
256 * @param auth auth to check
258 * @return true or false
260 public boolean authOnline(String auth
)
262 return usersByAuth
.containsKey(auth
.toLowerCase());
265 public String
getNumViaAuth(String auth
)
267 User u
= usersByAuth
.get(auth
.toLowerCase()).get(0);
268 if(u
instanceof User
)
270 return u
.getNumeric();
279 * Get a numeric's user row
280 * @param numer numeric of the user to fetch
282 * @return an array of all fields
284 public int getAuthLev(String numer
)
286 Auth a
= auths
.get(usersByNumeric
.get(numer
).getAuth().toLowerCase());
287 if(a
instanceof Auth
)
298 * Check if a nick is reserved.
299 * @param auth nick to check
301 * @return true or false
303 public boolean isReservedNick(String auth
)
305 Auth a
= auths
.get(auth
.toLowerCase());
306 if(a
instanceof Auth
)
308 Integer lev
= a
.getLevel();
311 if(a
.getUserflags().contains("k"))
332 * Check if a nick is reserved.
333 * @param auth nick to check
335 * @return true or false
337 public boolean isService(String numeric
)
339 Server s
= serversByNumeric
.get(numeric
.substring(0,2));
340 if(s
instanceof Server
)
342 return s
.getService();
351 * Check if a nick is reserved.
352 * @param auth nick to check
354 * @return true or false
356 public String
getServer(String numeric
)
358 Server s
= serversByNumeric
.get(numeric
.substring(0,2));
359 if(s
instanceof Server
)
369 public int getServerCount()
371 return serversByNumeric
.size();
375 * Check if a snumeric exists.
376 * @param numer numeric to check
378 * @return true or false
380 public boolean isServerNumeric(String numer
)
382 Server s
= serversByNumeric
.get(numer
);
383 if(s
instanceof Server
)
394 * Check if a numeric has op on a channel
395 * @param user numeric to check
396 * @param channel channel to check
398 * @return true or false
400 public boolean isOpChan(String user
, String channel
)
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();
410 String mode
= rs
.getString("modes");
427 * Check if a numeric exists.
428 * @param numer numeric to check
430 * @return true or false
432 public boolean isNickUsed(String nick
)
434 return usersByNick
.containsKey(nick
.toLowerCase());
438 * Check if a numeric is on a channel
439 * @param user numeric to check
440 * @param channel channel to check
442 * @return true or false
444 public boolean isOnChan(String user
, String channel
)
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();
454 String mode
= rs
.getString("user");
464 * Check if a host is a known op on a channel
465 * @param host host to check
466 * @param channel channel to check
468 * @return true or false
470 public boolean isKnownOpChan(String host
, String channel
)
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();
480 int points
= rs
.getInt("points");
494 * Check if a host has a chanfix level
495 * @param user numeric to check
496 * @param channel channel to check
498 * @return true or false
500 public boolean hasChanfix(String user
, String channel
)
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();
510 String chan
= rs
.getString("channel");
520 * Check if a channel has ops
521 * @param channel channel to check
523 * @return true or false
525 public boolean chanHasOps(String channel
)
529 PreparedStatement pstmt
;
530 pstmt
= con
.prepareStatement("SELECT modes FROM userchans WHERE channel = ?");
531 pstmt
.setString(1,channel
);
532 ResultSet rs
= pstmt
.executeQuery();
535 String mode
= rs
.getString("modes");
550 * Check if a channel has known ops
551 * @param channel channel to check
553 * @return true or false
555 public boolean chanfixHasOps(String channel
)
559 PreparedStatement pstmt
;
560 pstmt
= con
.prepareStatement("SELECT points FROM chanfix WHERE channel = ?");
561 pstmt
.setString(1,channel
);
562 ResultSet rs
= pstmt
.executeQuery();
565 int points
= rs
.getInt("points");
580 * Get the number of users on a channel
581 * @param channel channel to check
583 * @return the number of users on a channel
585 public int getChanUsers(String channel
)
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();
596 int users
= rs
.getInt(1);
607 * Get the number of users authed
608 * @param auth auth to check
610 * @return the number of users authed
612 public int getAuthUsers(String auth
)
616 return usersByAuth
.get(auth
.toLowerCase()).size();
625 * Get the number of users connected from the same host
626 * @param host host to check
628 * @return the number of users connected from the same host
630 public int getHostCount(String host
)
634 return usersByHost
.get(host
.toLowerCase()).size();
643 * Get the number of users connected from the same host
644 * @param host host to check
646 * @return the number of users connected from the same host
648 public int getIpCount(String ip
)
652 return usersByIP
.get(ip
).size();
660 public HashMap
<String
,User
> getUsers()
662 return usersByNumeric
;
666 * Get a numeric's user row
667 * @param numer numeric of the user to fetch
669 * @return an array of all fields
671 public String
[] getUserRow(String numer
)
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()};
680 return new String
[]{"0","0","0","0","0","0","0","0","0","0"};
685 * Get an auth's user row
686 * @param auth auth of the user to fetch
688 * @return an array of all fields
690 public String
[] getUserRowViaAuth(String auth
)
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()};
700 return new String
[]{"0","0","0","0","0","0","0","0","0","0"};
705 * Get a hosts's user row
706 * @param host host of the user to fetch
708 * @return an array of all fields
710 public String
[] getUserRowViaHost(String host
)
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()};
720 return new String
[]{"0","0","0","0","0","0","0","0","0","0"};
725 * Get a nick's user row
726 * @param nick nick of the user to fetch
728 * @return an array of all fields
730 public String
[] getNickRow(String nick
)
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()};
740 return new String
[]{"0","0","0","0","0","0","0","0","0","0"};
746 * @param nick auth to fetch
748 * @return an array of all fields
750 public String
[] getAuthRow(String nick
)
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()};
760 return new String
[]{"0","0","0","0","0","0","0","0","0","0"};
765 * Get a user's channels
766 * @param user user's numeric
768 * @return an array of all channels
770 public String
[] getUserChans(String user
)
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
>();
781 a
.add(rs
.getString("channel"));
785 String
[] r
= (String
[]) a
.toArray(new String
[ a
.size() ]);
790 return new String
[]{"0","0","0","0","0","0","0","0","0","0"};
795 return new String
[]{"0","0","0","0","0","0","0","0","0","0"};
800 * Get a channel's users
801 * @param chan channel to fetch
803 * @return an array of all users
805 public String
[] getChannelUsers(String chan
)
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
>();
816 a
.add(rs
.getString("user"));
820 String
[] r
= (String
[]) a
.toArray(new String
[ a
.size() ]);
825 return new String
[]{"0","0","0","0","0","0","0","0","0","0"};
830 return new String
[]{"0","0","0","0","0","0","0","0","0","0"};
836 * @return an array of all channels
838 public String
[] getUserChanTable()
842 PreparedStatement pstmt
;
843 pstmt
= con
.prepareStatement("SELECT DISTINCT channel FROM userchans");
844 ResultSet rs
= pstmt
.executeQuery();
845 ArrayList
<String
> a
= new ArrayList
<String
>();
848 a
.add(rs
.getString(1));
852 String
[] r
= (String
[]) a
.toArray(new String
[ a
.size() ]);
857 return new String
[]{"0","0","0","0","0","0","0","0","0","0"};
862 return new String
[]{"0","0","0","0","0","0","0","0","0","0"};
868 * @return an array of all numerics
870 public String
[] getNumericTable()
874 ArrayList
<String
> a
= new ArrayList
<String
>(usersByNumeric
.keySet());
877 String
[] r
= (String
[]) a
.toArray(new String
[ a
.size() ]);
882 return new String
[]{"0","0","0","0","0","0","0","0","0","0"};
887 return new String
[]{"0","0","0","0","0","0","0","0","0","0"};
893 * @return an array of all numerics
895 public String
[] getNumericTable(String server
)
899 Server s
= serversByHost
.get(server
.toLowerCase());
901 if(s
instanceof Server
)
903 numer
= s
.getNumeric();
907 return new String
[]{"0","0","0","0","0","0","0","0","0","0"};
909 ArrayList
<String
> numerics
= new ArrayList
<String
>(usersByNumeric
.keySet());
910 ArrayList
<String
> a
= new ArrayList
<String
>();
911 for(String n
: numerics
)
913 if(n
.startsWith(numer
))
920 String
[] r
= (String
[]) a
.toArray(new String
[ a
.size() ]);
925 return new String
[]{"0","0","0","0","0","0","0","0","0","0"};
930 return new String
[]{"0","0","0","0","0","0","0","0","0","0"};
936 * @return an array of all numerics
938 public String
[] getNumericTableUniqueHosts()
942 ArrayList
<String
> hosts
= new ArrayList
<String
>(usersByHost
.keySet());
943 ArrayList
<String
> a
= new ArrayList
<String
>();
944 for(String s
: hosts
)
946 a
.add(usersByHost
.get(s
).get(0).getNumeric());
950 String
[] r
= (String
[]) a
.toArray(new String
[ a
.size() ]);
956 return new String
[]{"0","0","0","0","0","0","0","0","0","0"};
961 return new String
[]{"0","0","0","0","0","0","0","0","0","0"};
965 public String
[][] getServerList()
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
)
977 Server s
= serversByNumeric
.get(numer
);
978 if(s
instanceof Server
)
983 d
.add(s
.getService().toString());
986 String
[][] r
= new String
[a
.size()][4];
989 for(int n
=0; n
<r
.length
; n
++)
1000 return new String
[][] {{"0","0"},{"0","0"}};
1005 return new String
[][] {{"0","0"},{"0","0"}};
1009 public String
[][] getServerTable()
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
)
1020 Server s
= serversByHost
.get(host
);
1022 if(s
instanceof Server
)
1027 numer
= s
.getNumeric();
1028 ArrayList
<String
> numerics
= new ArrayList
<String
>(usersByNumeric
.keySet());
1031 for(String n
: numerics
)
1033 if(n
.startsWith(numer
))
1036 User u
= usersByNumeric
.get(n
);
1037 if(u
.getModes().contains("o"))
1048 String
[][] r
= new String
[a
.size()][3];
1051 for(int n
=0; n
<r
.length
; n
++)
1061 return new String
[][] {{"0","0"},{"0","0"}};
1066 return new String
[][] {{"0","0"},{"0","0"}};
1071 * Get all staff members
1072 * @return an array of all staff members
1074 public ArrayList
<String
> getStaffList()
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
>();
1084 int lev
= rs
.getInt("level");
1085 String a
= rs
.getString("authnick") + " (";
1086 String x
= "Helper";
1093 x
= "IRC Administrator";
1097 x
= "Services Developer";
1106 return new ArrayList
<String
>();
1111 * Get all user rows connected to an auth
1112 * @param auth auth to fetch
1114 * @return a double array of all users
1116 public ArrayList
<String
[]> getUserRowsViaAuth(String auth
)
1120 ArrayList
<User
> users
= usersByAuth
.get(auth
.toLowerCase());
1121 ArrayList
<String
[]> a
= new ArrayList
<String
[]>();
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()};
1131 return new ArrayList
<String
[]>();
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
1141 public void setUserField(String numer
, int colum
, String info
)
1143 User u
= usersByNumeric
.get(numer
);
1144 if(u
instanceof User
)
1152 usersByNumeric
.remove(numer
);
1153 usersByNumeric
.put(info
,u
);
1156 String oldkey
= u
.getNick();
1158 if(usersByNick
.containsKey(oldkey
.toLowerCase()))
1160 usersByNick
.remove(oldkey
.toLowerCase());
1162 usersByNick
.put(info
.toLowerCase(),u
);
1165 String
[] splithost
= info
.split("@");
1166 u
.setIdent(splithost
[0]);
1167 u
.setHost(splithost
[1]);
1168 if(usersByHost
.containsKey(info
.toLowerCase()))
1170 ArrayList
<User
> users
= usersByHost
.get(info
.toLowerCase());
1172 usersByHost
.put(info
.toLowerCase(),users
);
1176 ArrayList
<User
> users
= new ArrayList
<User
>();
1178 usersByHost
.put(info
.toLowerCase(),users
);
1186 if(usersByAuth
.containsKey(info
.toLowerCase()))
1188 ArrayList
<User
> users
= usersByAuth
.get(info
.toLowerCase());
1190 usersByAuth
.put(info
.toLowerCase(),users
);
1194 ArrayList
<User
> users
= new ArrayList
<User
>();
1196 usersByAuth
.put(info
.toLowerCase(),users
);
1200 if(Boolean
.parseBoolean(info
))
1214 if(usersByIP
.containsKey(info
))
1216 ArrayList
<User
> users
= usersByIP
.get(info
);
1218 usersByIP
.put(info
.toLowerCase(),users
);
1222 ArrayList
<User
> users
= new ArrayList
<User
>();
1224 usersByIP
.put(info
,users
);
1228 u
.setFakehost(info
);
1232 catch ( Exception e
)
1234 System
.out
.println( "Error finding user." );
1236 C
.die("SQL error, trying to die gracefully.");
1241 System
.out
.println( "Error finding user." );
1242 C
.printDebug("Error finding user.");
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
1252 public void setAuthField(String auth
, int colum
, String info
)
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 = ?");
1261 pstmt
.setInt(1,Integer
.parseInt(info
));
1265 pstmt
.setBoolean(1,Boolean
.parseBoolean(info
));
1269 pstmt
.setLong(1,Long
.parseLong(info
));
1273 pstmt
.setString(1,info
);
1275 pstmt
.setString(2,auth
);
1276 pstmt
.executeUpdate();
1277 Auth a
= auths
.get(auth
.toLowerCase());
1278 if(a
instanceof Auth
)
1283 a
.setAuthnick(info
);
1286 a
.setPassword(info
);
1292 a
.setLevel(Integer
.parseInt(info
));
1295 a
.setSuspended((Boolean
.parseBoolean(info
) ?
1 : 0));
1298 a
.setLast(Long
.parseLong(info
));
1304 a
.setUserflags(info
);
1312 catch ( SQLException e
)
1314 System
.out
.println ( "Error executing sql statement" );
1316 C
.die("SQL error, trying to die gracefully.");
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
1326 public void setUserChanMode(String user
, String chan
, String mode
)
1330 if(mode
.contains("o"))
1332 PreparedStatement pstmt
;
1333 String change
= "0";
1334 if(mode
.contains("+"))
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();
1345 catch ( SQLException e
)
1347 System
.out
.println ( "Error executing sql statement" );
1349 C
.die("SQL error, trying to die gracefully.");
1353 Channel c
= channels
.get(chan
.toLowerCase());
1354 if(c
instanceof Channel
)
1356 c
.setUserChanMode(user
,mode
);
1361 * Remove all ops from a channel
1362 * @param chan channel to change
1364 public void setClearMode(String chan
, String modes
)
1368 PreparedStatement pstmt
;
1369 pstmt
= con
.prepareStatement("UPDATE userchans SET modes = '0' WHERE channel = ?");
1370 pstmt
.setString(1,chan
);
1371 pstmt
.executeUpdate();
1373 catch ( SQLException e
)
1375 System
.out
.println ( "Error executing sql statement" );
1377 C
.die("SQL error, trying to die gracefully.");
1382 Channel c
= channels
.get(chan
.toLowerCase());
1383 if(c
instanceof Channel
)
1385 c
.setClearMode(modes
);
1391 * @param auth auth to delete
1393 public void delAuth(String auth
)
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());
1403 catch ( SQLException e
)
1405 System
.out
.println ( "Error executing sql statement" );
1407 C
.die("SQL error, trying to die gracefully.");
1413 * @param numer numeric of user to delete
1415 public void delUser(String numer
)
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
)
1427 usersByAuth
.put(u
.getAuth(),users
);
1431 usersByAuth
.remove(u
.getAuth());
1434 users
= usersByHost
.get(u
.getHost());
1438 usersByHost
.put(u
.getHost(),users
);
1442 usersByHost
.remove(u
.getHost());
1444 users
= usersByIP
.get(u
.getIp());
1448 usersByIP
.put(u
.getIp(),users
);
1452 usersByIP
.remove(u
.getIp());
1454 usersByNumeric
.remove(numer
);
1456 PreparedStatement pstmt
;
1457 pstmt
= con
.prepareStatement("DELETE FROM userchans WHERE BINARY user = ?");
1458 pstmt
.setString(1,numer
);
1459 pstmt
.executeUpdate();
1461 ArrayList
<String
> userchannels
= u
.getChannels();
1462 for(String channel
: userchannels
)
1467 catch ( Exception e
)
1474 * @param host host of server to delete
1476 public void delServer(String host
)
1478 Server s
= serversByHost
.get(host
.toLowerCase());
1479 if(s
instanceof Server
)
1482 String numer
= s
.getNumeric();
1483 String hub
= s
.getHub();
1484 ArrayList
<Server
> servers
= serversByHub
.get(hub
);
1485 if(servers
instanceof ArrayList
)
1487 for(int i
=0; i
<servers
.size(); i
++)
1489 if(servers
.get(i
).equals(s
))
1499 System
.out
.println ( "Error Removing server: "+host
);
1500 C
.die("SQL error, trying to die gracefully.");
1505 * Delete a Server's children
1506 * @param host host of the server that has to remove his children
1509 public void delChildren(String host
)
1511 Server s
= serversByHost
.get(host
.toLowerCase());
1512 if(s
instanceof Server
)
1514 String numer
= s
.getNumeric();
1515 ArrayList
<Server
> servers
= serversByHub
.get(numer
);
1516 if(servers
instanceof ArrayList
)
1518 for(Server ser
: servers
)
1520 delChildren(ser
.getHost());
1523 ArrayList
<String
> numerics
= new ArrayList
<String
>(usersByNumeric
.keySet());
1524 for(String n
: numerics
)
1526 if(n
.startsWith(numer
))
1531 serversByHub
.remove(numer
);
1532 serversByNumeric
.remove(numer
);
1533 serversByHost
.remove(host
.toLowerCase());
1537 System
.out
.println ( "Error Removing server: "+host
);
1538 C
.die("SQL error, trying to die gracefully.");
1543 * Delete a user from a channel
1544 * @param chan channel where user should be removed
1545 * @param user numeric of user to remove
1547 public void delUserChan(String chan
, String user
)
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();
1557 catch ( SQLException e
)
1559 System
.out
.println ( "Error executing sql statement" );
1561 C
.die("SQL error, trying to die gracefully.");
1564 Channel c
= channels
.get(chan
.toLowerCase());
1565 if(c
instanceof Channel
)
1569 User u
= usersByNumeric
.get(user
);
1570 if(u
instanceof User
)
1572 u
.partChannel(chan
);
1576 public void addUser(String nume
,String nick
, String host
, String mode
, String auth
, boolean isop
, String server
, String ip
, String fake
)
1580 User u
= new User(nume
);
1582 String
[] splithost
= host
.split("@");
1583 u
.setIdent(splithost
[0]);
1584 u
.setHost(splithost
[1]);
1595 u
.setServer(server
);
1597 u
.setFakehost(fake
);
1598 usersByNumeric
.put(nume
,u
);
1599 usersByNick
.put(nick
.toLowerCase(),u
);
1600 if(!auth
.equals("0"))
1602 if(usersByAuth
.containsKey(auth
.toLowerCase()))
1604 ArrayList
<User
> users
= usersByAuth
.get(auth
.toLowerCase());
1606 usersByAuth
.put(auth
.toLowerCase(),users
);
1610 ArrayList
<User
> users
= new ArrayList
<User
>();
1612 usersByAuth
.put(auth
.toLowerCase(),users
);
1615 if(usersByHost
.containsKey(splithost
[1]))
1617 ArrayList
<User
> users
= usersByHost
.get(splithost
[1]);
1619 usersByHost
.put(splithost
[1],users
);
1623 ArrayList
<User
> users
= new ArrayList
<User
>();
1625 usersByHost
.put(splithost
[1],users
);
1627 if(usersByIP
.containsKey(ip
))
1629 ArrayList
<User
> users
= usersByIP
.get(ip
);
1631 usersByIP
.put(ip
,users
);
1635 ArrayList
<User
> users
= new ArrayList
<User
>();
1637 usersByIP
.put(ip
,users
);
1640 catch ( Exception e
)
1642 System
.out
.println ( "Error executing statement" );
1644 C
.die("SQL error, trying to die gracefully.");
1648 public void addAuth(String auth
,String pass
, String mail1
, int lev
, boolean suspended
, Long time
, String info
,String userflags
, String vhost
)
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
);
1668 a
.setSuspended(suspended ?
1 : 0);
1671 a
.setUserflags(userflags
);
1672 auths
.put(auth
.toLowerCase(),a
);
1674 catch ( SQLException e
)
1676 System
.out
.println ( "Error executing sql statement" );
1678 C
.die("SQL error, trying to die gracefully.");
1682 public void addServer(String numer
, String host
,String hub
, boolean service
)
1686 Server s
= new Server(numer
);
1687 s
.setHost(host
.toLowerCase());
1689 s
.setService(service
);
1690 serversByHost
.put(host
.toLowerCase(),s
);
1691 serversByNumeric
.put(numer
,s
);
1692 if(serversByHub
.containsKey(hub
))
1694 ArrayList
<Server
> servers
= serversByHub
.get(hub
);
1696 serversByHub
.put(hub
,servers
);
1700 ArrayList
<Server
> servers
= new ArrayList
<Server
>();
1702 serversByHub
.put(hub
,servers
);
1707 System
.out
.println ( "Error executing statement" );
1709 C
.die("SQL error, trying to die gracefully.");
1713 public void addUserChan(String channel
,String user
,String modes
, String timestamp
)
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();
1724 catch ( SQLException e
)
1726 System
.out
.println ( "Error executing sql statement" );
1728 C
.die("SQL error, trying to die gracefully.");
1732 Channel c
= channels
.get(channel
.toLowerCase());
1733 if(c
instanceof Channel
)
1739 if(modes
.equals("o"))
1741 c
= new Channel(channel
,timestamp
,user
,true);
1745 c
= new Channel(channel
,timestamp
,user
,false);
1747 channels
.put(channel
.toLowerCase(),c
);
1749 User u
= usersByNumeric
.get(user
);
1750 if(u
instanceof User
)
1752 u
.joinChannel(channel
);
1760 File file
=new File("backup/");
1763 PreparedStatement pstmt
;
1764 C
.report("Creating DB Backup...");
1766 Calendar cal
= Calendar
.getInstance();
1767 long now
= (cal
.getTimeInMillis() / 1000);
1769 Runtime rt
= Runtime
.getRuntime();
1770 File backup
=new File("backup/"+now
+".sql");
1773 Process child
= rt
.exec("mysqldump -u"+user
+" -p"+password
+" "+db
);
1774 ps
=new PrintStream(backup
);
1775 InputStream in
= child
.getInputStream();
1777 while ((ch
= in
.read()) != -1)
1782 InputStream err
= child
.getErrorStream();
1783 while ((ch
= err
.read()) != -1)
1785 System
.out
.write(ch
);
1791 C
.report("Could not access backup directory.");
1796 System
.out
.println ( "Error executing sql statement" );
1798 C
.die("SQL error, trying to die gracefully.");
1804 Calendar cal
= Calendar
.getInstance();
1805 long now
= (cal
.getTimeInMillis() / 1000);
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
)
1813 Auth auth
= auths
.get(authnick
);
1814 boolean online
= authOnline(authnick
);
1815 if(!online
&& auth
.getLevel() < 2 && auth
.getSuspended()==0 && auth
.getLast() < now
-3456000)
1817 delete
.add(authnick
);
1822 for(String auth
: delete
)
1824 C
.report("Deleting AUTH: '" + auth
+ "'");
1828 C
.report("Core Cleanup complete!");
1832 System
.out
.println ( "Error executing sql statement" );
1833 e
.printStackTrace();
1834 C
.die("SQL error, trying to die gracefully.");
1838 public String
encrypt(String plaintext
)
1840 byte[] defaultBytes
= plaintext
.getBytes();
1843 MessageDigest algorithm
= MessageDigest
.getInstance("MD5");
1845 algorithm
.update(defaultBytes
);
1846 byte messageDigest
[] = algorithm
.digest();
1847 StringBuffer hexString
= new StringBuffer();
1848 for (int i
=0;i
<messageDigest
.length
;i
++)
1850 String hex
= Integer
.toHexString(0xFF & messageDigest
[i
]);
1851 if(hex
.length()==1) hexString
.append('0');
1852 hexString
.append(hex
);
1854 return hexString
.toString();
1856 catch(NoSuchAlgorithmException e
)
1858 System
.out
.println ( "Error encrypting password." );
1860 C
.die("SQL error, trying to die gracefully.");
1865 public void chanfix()
1867 String users
[] = getNumericTableUniqueHosts();
1868 for(int n
=0; n
<users
.length
; n
++)
1870 if(!isService(users
[n
]))
1872 String channels
[] = getUserChans(users
[n
]);;
1873 for(int p
=0; p
<channels
.length
; p
++)
1875 if(isOpChan(users
[n
], channels
[p
]))
1877 String user
[] = getUserRow(users
[n
]);
1878 if(!user
[2].equalsIgnoreCase(C
.get_ident() + "@" + C
.get_host()))
1880 String userid
= user
[2];
1881 if(!user
[4].equalsIgnoreCase("0"))
1885 if(hasChanfix(userid
, channels
[p
]))
1887 chanfix_addpoint(userid
, channels
[p
]);
1891 if(getChanUsers(channels
[p
]) > 2)
1893 add_chanfix(userid
, channels
[p
]);
1903 PreparedStatement pstmt
;
1904 pstmt
= con
.prepareStatement("SELECT * FROM chanfix");
1905 ResultSet rs
= pstmt
.executeQuery();
1908 if(rs
.getString("host").contains("@"))
1910 String user
[] = getUserRowViaHost(rs
.getString("host"));
1911 if(!isOpChan(user
[0],rs
.getString("channel")))
1913 chanfix_delpoint(rs
.getString("channel"),rs
.getString("host"));
1918 String user
[] = getUserRowViaAuth(rs
.getString("host"));
1919 if(!isOpChan(user
[0],rs
.getString("channel")))
1921 chanfix_delpoint(rs
.getString("channel"),rs
.getString("host"));
1926 catch ( SQLException e
)
1928 System
.out
.println ( "Error executing sql statement" );
1930 C
.die("SQL error, trying to die gracefully.");
1934 public void chanfix_addpoint(String host
, String chan
)
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();
1944 if(Integer
.parseInt(rs
.getString("points"))<4033)
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();
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();
1959 catch ( SQLException e
)
1961 System
.out
.println ( "Error executing sql statement" );
1963 C
.die("SQL error, trying to die gracefully.");
1967 public void chanfix_delpoint(String chan
, String host
)
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();
1977 if(rs
.getInt("points")>1)
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();
1988 del_chanfix(chan
, host
);
1991 catch ( SQLException e
)
1993 System
.out
.println ( "Error executing sql statement" );
1995 C
.die("SQL error, trying to die gracefully.");
1999 public void add_chanfix(String host
, String chan
)
2003 PreparedStatement pstmt
;
2004 pstmt
= con
.prepareStatement("INSERT INTO chanfix VALUES (?,?,?,?)");
2005 pstmt
.setString(1,chan
);
2006 pstmt
.setString(2,host
);
2008 pstmt
.setLong(4,Long
.parseLong(C
.get_time()));
2009 pstmt
.executeUpdate();
2011 catch ( SQLException e
)
2013 System
.out
.println ( "Error executing sql statement" );
2015 C
.die("SQL error, trying to die gracefully.");
2019 public void del_chanfix(String channel
, String host
)
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();
2029 catch ( SQLException e
)
2031 System
.out
.println ( "Error executing sql statement" );
2033 C
.die("SQL error, trying to die gracefully.");
2037 public ArrayList
<String
> checkUserChans()
2039 ArrayList
<String
> info
= new ArrayList
<String
>();
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
)
2056 info
.add(chanExists("#BorkNet")+" true");
2057 info
.add(getChanUsers("#BorkNet")+" "+c
.getUsercount());
2058 info
.add(Arrays
.toString(getChannelUsers("#BorkNet"))+" "+Arrays
.toString(c
.getUserlist()));
2062 info
.add("#BorkNet not in memory");