]>
Commit | Line | Data |
---|---|---|
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 | import java.sql.*; | |
25 | import java.util.*; | |
26 | import java.io.*; | |
27 | import java.security.*; | |
28 | import borknet_services.core.*; | |
29 | ||
30 | /** | |
31 | * The database communication class of the Q IRC C. | |
32 | * @author Ozafy - ozafy@borknet.org - http://www.borknet.org | |
33 | */ | |
34 | public class DBControl | |
35 | { | |
36 | /** Main bot */ | |
37 | private Core C; | |
38 | ||
39 | private String dbname = "services.db"; | |
40 | ||
41 | private CoreDBControl dbc; | |
42 | ||
43 | private S Bot; | |
44 | ||
45 | private HashMap<String,String> channels = new HashMap<String,String>(); | |
46 | private HashMap<String,String> userMsg = new HashMap<String,String>(); | |
47 | private HashMap<String,Integer> userPoints = new HashMap<String,Integer>(); | |
48 | private int kills = 1; | |
49 | ||
50 | /** | |
51 | * Constructs a Database connection. | |
52 | * @param server Database server | |
53 | * @param user Database user | |
54 | * @param pass Database password | |
55 | * @param db Database | |
56 | * @param debug Are we debugging? | |
57 | * @param B Main bot | |
58 | */ | |
59 | public DBControl(Core C, S Bot) | |
60 | { | |
61 | try | |
62 | { | |
63 | this.C = C; | |
64 | this.Bot = Bot; | |
65 | this.dbc = C.get_dbc(); | |
66 | testDriver(); | |
67 | C.printDebug("[>---<] >> *** Checking SQL db..."); | |
68 | createTables(); | |
69 | C.printDebug( "[>---<] >> *** Done." ); | |
70 | load(); | |
71 | } | |
72 | catch(Exception e) | |
73 | { | |
74 | C.printDebug("Database error!"); | |
75 | C.debug(e); | |
76 | C.die("SQL error, trying to die gracefully."); | |
77 | } | |
78 | } | |
79 | ||
80 | private void testDriver ( ) | |
81 | { | |
82 | try | |
83 | { | |
84 | Class.forName ( "org.sqlite.JDBC" ); | |
85 | C.printDebug( "[>---<] >> *** SQL Driver Found" ); | |
86 | } | |
87 | catch ( java.lang.ClassNotFoundException e ) | |
88 | { | |
89 | C.printDebug("SQL JDBC Driver not found!"); | |
90 | C.die("SQL error, trying to die gracefully."); | |
91 | } | |
92 | } | |
93 | ||
94 | private void createTables() | |
95 | { | |
96 | Connection con = null; | |
97 | try | |
98 | { | |
99 | con = DriverManager.getConnection("jdbc:sqlite:"+dbname); | |
100 | Statement statement = con.createStatement(); | |
101 | statement.setQueryTimeout(30); | |
102 | statement.executeUpdate("CREATE TABLE IF NOT EXISTS s_channels (name string, flags string)"); | |
103 | } | |
104 | catch ( Exception e ) | |
105 | { | |
106 | C.printDebug("Database error!"); | |
107 | C.debug(e); | |
108 | C.die("SQL error, trying to die gracefully."); | |
109 | } | |
110 | finally | |
111 | { | |
112 | try | |
113 | { | |
114 | if(con != null) | |
115 | { | |
116 | con.close(); | |
117 | } | |
118 | } | |
119 | catch(Exception e) | |
120 | { | |
121 | C.printDebug("Database error!"); | |
122 | C.debug(e); | |
123 | C.die("SQL error, trying to die gracefully."); | |
124 | } | |
125 | } | |
126 | } | |
127 | ||
128 | private void executeUpdate(String query, String[] params) | |
129 | { | |
130 | Connection con = null; | |
131 | try | |
132 | { | |
133 | con = DriverManager.getConnection("jdbc:sqlite:"+dbname); | |
134 | PreparedStatement pstmt = con.prepareStatement(query); | |
135 | for(int i=0;i<params.length;i++) | |
136 | { | |
137 | pstmt.setString((i+1),params[i]); | |
138 | } | |
139 | pstmt.executeUpdate(); | |
140 | } | |
141 | catch ( Exception e ) | |
142 | { | |
143 | C.printDebug("Database error!"); | |
144 | C.debug(e); | |
145 | C.die("SQL error, trying to die gracefully."); | |
146 | } | |
147 | finally | |
148 | { | |
149 | try | |
150 | { | |
151 | if(con != null) | |
152 | { | |
153 | con.close(); | |
154 | } | |
155 | } | |
156 | catch(Exception e) | |
157 | { | |
158 | C.printDebug("Database error!"); | |
159 | C.debug(e); | |
160 | C.die("SQL error, trying to die gracefully."); | |
161 | } | |
162 | } | |
163 | } | |
164 | ||
165 | public boolean chanExists(String chan) | |
166 | { | |
167 | return dbc.chanExists(chan); | |
168 | } | |
169 | ||
170 | public boolean SchanExists(String chan) | |
171 | { | |
172 | return channels.containsKey(chan.toLowerCase()); | |
173 | } | |
174 | ||
175 | public List<String> getChanTable() | |
176 | { | |
177 | List<String> keys = new ArrayList<String>(channels.keySet()); | |
178 | return keys; | |
179 | } | |
180 | ||
181 | public void load() | |
182 | { | |
183 | Connection con = null; | |
184 | try | |
185 | { | |
186 | con = DriverManager.getConnection("jdbc:sqlite:"+dbname); | |
187 | Statement statement = con.createStatement(); | |
188 | statement.setQueryTimeout(30); | |
189 | ResultSet rs = statement.executeQuery("SELECT name,flags FROM s_channels"); | |
190 | ArrayList<String> a = new ArrayList<String>(); | |
191 | while(rs.next()) | |
192 | { | |
193 | channels.put(rs.getString("name").toLowerCase(),rs.getString("flags")); | |
194 | } | |
195 | } | |
196 | catch ( Exception e ) | |
197 | { | |
198 | C.printDebug("Database error!"); | |
199 | C.debug(e); | |
200 | C.die("SQL error, trying to die gracefully."); | |
201 | } | |
202 | finally | |
203 | { | |
204 | try | |
205 | { | |
206 | if(con != null) | |
207 | { | |
208 | con.close(); | |
209 | } | |
210 | } | |
211 | catch(Exception e) | |
212 | { | |
213 | C.printDebug("Database error!"); | |
214 | C.debug(e); | |
215 | C.die("SQL error, trying to die gracefully."); | |
216 | } | |
217 | } | |
218 | } | |
219 | ||
220 | public String getChanFlags(String channel) | |
221 | { | |
222 | return channels.get(channel.toLowerCase()); | |
223 | } | |
224 | ||
225 | public int getPoints(String user) | |
226 | { | |
227 | return userPoints.get(user); | |
228 | } | |
229 | ||
230 | public int getID() | |
231 | { | |
232 | return kills++; | |
233 | } | |
234 | ||
235 | public boolean isService(String numeric) | |
236 | { | |
237 | return dbc.isService(numeric); | |
238 | } | |
239 | ||
240 | public boolean repeat(String user, String msg) | |
241 | { | |
242 | try | |
243 | { | |
244 | String m = userMsg.get(user); | |
245 | return m.equals(msg); | |
246 | } | |
247 | catch(Exception e) | |
248 | { | |
249 | return false; | |
250 | } | |
251 | } | |
252 | ||
253 | public void delChan(String chan) | |
254 | { | |
255 | executeUpdate("DELETE FROM s_channels WHERE name = ?", new String[] {chan}); | |
256 | channels.remove(chan.toLowerCase()); | |
257 | } | |
258 | ||
259 | public void delPoints(int points) | |
260 | { | |
261 | List<String> users = new ArrayList<String>(userPoints.keySet()); | |
262 | for(String user : users) | |
263 | { | |
264 | int p = userPoints.get(user); | |
265 | p -= points; | |
266 | if(p<0) | |
267 | { | |
268 | userPoints.remove(user); | |
269 | userMsg.remove(user); | |
270 | } | |
271 | else | |
272 | { | |
273 | userPoints.put(user,p); | |
274 | } | |
275 | } | |
276 | } | |
277 | ||
278 | public void addPoints(String user, int points) | |
279 | { | |
280 | Integer p = userPoints.get(user); | |
281 | if(p instanceof Integer) | |
282 | { | |
283 | userPoints.put(user,(points+p)); | |
284 | } | |
285 | else | |
286 | { | |
287 | userPoints.put(user,points); | |
288 | } | |
289 | } | |
290 | ||
291 | public void addChan(String chan, String flags) | |
292 | { | |
293 | executeUpdate("INSERT INTO s_channels VALUES (?,?)", new String[] {chan, flags}); | |
294 | channels.put(chan.toLowerCase(),flags); | |
295 | } | |
296 | ||
297 | public boolean setChanFlags(String chan, String flags) | |
298 | { | |
299 | try | |
300 | { | |
301 | executeUpdate("UPDATE s_channels SET flags = ? WHERE name = ?", new String[] {flags, chan}); | |
302 | channels.put(chan.toLowerCase(),flags); | |
303 | return true; | |
304 | } | |
305 | catch ( Exception e ) | |
306 | { | |
307 | return false; | |
308 | } | |
309 | } | |
310 | ||
311 | public void setMsg(String user, String msg) | |
312 | { | |
313 | userMsg.put(user,msg); | |
314 | } | |
315 | ||
316 | public int getChanUsers(String channel) | |
317 | { | |
318 | return dbc.getChanUsers(channel); | |
319 | } | |
320 | ||
321 | public void clean() | |
322 | { | |
323 | List<String> channelkeys = new ArrayList<String>(channels.keySet()); | |
324 | for(String channel : channelkeys) | |
325 | { | |
326 | if(getChanUsers(channel) < 1) | |
327 | { | |
328 | delChan(channel); | |
329 | } | |
330 | } | |
331 | } | |
332 | ||
333 | public User getUser(String numer) | |
334 | { | |
335 | return dbc.getUser(numer); | |
336 | } | |
337 | } |