Anope IRC Services  Version 1.8
rdb.c
Go to the documentation of this file.
1 /* RDB functions.
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  *
11  *
12  */
13 #include "services.h"
14 
15 /*************************************************************************/
16 
17 /* Initialize the current RDB database engine */
18 int rdb_init()
19 {
20 
21 #ifdef USE_MYSQL
22  return db_mysql_init();
23 #endif
24 
25  return 0;
26 }
27 
28 /*************************************************************************/
29 
30 /* Check if RDB can be used to load/save data */
31 int rdb_open()
32 {
33 
34 #ifdef USE_MYSQL
35  return db_mysql_open(); /* db_mysql_open(); */
36 #endif
37 
38  return 0;
39 }
40 
41 /*************************************************************************/
42 
43 /* Strictly spoken this should close the database. However, it's not too
44  * efficient to close it every time after a write or so, so we just
45  * pretend we closed it while in reality it's still open.
46  */
47 int rdb_close()
48 {
49 
50 #ifdef USE_MYSQL
51  return 1; /* db_mysql_close(); */
52 #endif
53 
54  return 1;
55 }
56 
57 /*************************************************************************/
58 
59 /* Quote the string to be used in inclused for the current RDB database */
60 char *rdb_quote(char *str)
61 {
62 #ifdef USE_MYSQL
63  return db_mysql_quote(str);
64 #endif
65 
66  return sstrdup(str);
67 }
68 
69 /*************************************************************************/
70 
71 /* Tag a table by setting the 'active' field to 0 for all rows. After an
72  * update, all rows with active still 0 will be deleted; this is done to
73  * easily delete old entries from the database.
74  */
75 int rdb_tag_table(char *table)
76 {
77 #ifdef USE_MYSQL
78  return db_mysql_try("UPDATE %s SET active = 0", table);
79 #endif
80 
81  return 0;
82 
83 }
84 
85 /* Be sure to quote all user input in the clause! */
86 int rdb_tag_table_where(char *table, char *clause)
87 {
88 #ifdef USE_MYSQL
89  return db_mysql_try("UPDATE %s SET active = 0 WHERE %s", table,
90  clause);
91 #endif
92 
93  return 0;
94 
95 }
96 
97 /*************************************************************************/
98 
99 /* Empty an entire database table */
100 int rdb_empty_table(char *table)
101 {
102 #ifdef USE_MYSQL
103  return db_mysql_try("TRUNCATE TABLE %s", table);
104 #endif
105 
106  return 0;
107 
108 }
109 
110 /*************************************************************************/
111 
112 /* Clean up a table with 'dirty' records (active = 0) */
113 int rdb_clean_table(char *table)
114 {
115 #ifdef USE_MYSQL
116  return db_mysql_try("DELETE FROM %s WHERE active = 0", table);
117 #endif
118 
119  return 0;
120 }
121 
122 /* Be sure to quote user input in the clause! */
123 int rdb_clean_table_where(char *table, char *clause)
124 {
125 #ifdef USE_MYSQL
126  return db_mysql_try("DELETE FROM %s WHERE active = 0 AND (%s)", table,
127  clause);
128 #endif
129 
130  return 0;
131 }
132 
133 /*************************************************************************/
134 
135 /* Delete specific records from a table. The clause is MySQL syntax, and
136  * should be all quoted up nicely in the calling code.
137  */
138 int rdb_scrub_table(char *table, char *clause)
139 {
140 #ifdef USE_MYSQL
141  return db_mysql_try("DELETE FROM %s WHERE %s", table, clause);
142 #endif
143 
144  return 0;
145 
146 }
147 
148 /*************************************************************************/
149 
150 /* Execute a direct MySQL query. Do NOT forget to quote all user input!
151  * NOTE: this ideally shouldn't be used, but that's probably a phase3 utopia
152  */
153 int rdb_direct_query(char *query)
154 {
155 
156 #ifdef USE_MYSQL
157  alog("Direct Query: %s", query);
158  return db_mysql_query(query);
159 #endif
160 
161  return 0;
162 
163 }
164 
165 /*************************************************************************/
166 
167 /* Update the needed tables when someone changes their display.
168  * The original author didn't even like this (claimed it should be in
169  * mysql.c), and i do agree muchly.
170  */
171 int rdb_ns_set_display(char *newnick, char *oldnick)
172 {
173  int ret = 0;
174  char *q_newnick;
175  char *q_oldnick;
176 
177  q_newnick = rdb_quote(newnick);
178  q_oldnick = rdb_quote(oldnick);
179 
180 #ifdef USE_MYSQL
181  /* Change the display on NS_CORE */
182  ret =
184  ("UPDATE anope_ns_core SET display = '%s' WHERE display = '%s'",
185  q_newnick, q_oldnick);
186 
187  /* Change the display on NS_ALIAS for all grouped nicks */
188  if (ret)
189  ret =
191  ("UPDATE anope_ns_alias SET display='%s' WHERE display='%s'",
192  q_newnick, q_oldnick);
193 
194  /* Change the display on ChanServ ACCESS list */
195  if (ret)
196  ret =
198  ("UPDATE anope_cs_access SET display='%s' WHERE display='%s'",
199  q_newnick, q_oldnick);
200 
201  /* Change the display on ChanServ AKICK list */
202  if (ret)
203  ret =
205  ("UPDATE anope_cs_akicks SET creator='%s' WHERE creator='%s'",
206  q_newnick, q_oldnick);
207 
208  /* Change the display on MemoServ sent memos -- is it required? */
209  if (ret)
210  ret =
212  ("UPDATE anope_ms_info SET sender='%s' WHERE sender='%s'",
213  q_newnick, q_oldnick);
214 
215  /* Change the display on MemoServ received memos -- is it required? */
216  if (ret)
217  ret =
219  ("UPDATE anope_ms_info SET receiver='%s' WHERE receiver='%s'",
220  q_newnick, q_oldnick);
221 
222  /* Change the akills set on the person's nick */
223  if (ret)
224  ret =
226  ("UPDATE anope_cs_akicks SET dmask='%s' WHERE dmask='%s' AND flags & %d",
227  q_newnick, q_oldnick, AK_ISNICK);
228 
229  /* Change the display on NickServ ACCESS list */
230  if (ret)
231  ret =
233  ("UPDATE anope_ns_access SET display='%s' WHERE display='%s'",
234  q_newnick, q_oldnick);
235 
236  /* No need to update anope_cs_info here as it is updated when we
237  * save the database.
238  *
239  * anope_hs_core is per nick, not per display; a changed display
240  * won't change anything there
241  */
242 
243 #endif
244 
245  free(q_newnick);
246  free(q_oldnick);
247 
248  return ret;
249 }
250 
251 /*************************************************************************/
252 
254 {
255 
256 #ifdef USE_MYSQL
257  return db_mysql_save_ns_core(nc);
258 #endif
259 
260  return 0;
261 }
262 
263 /*************************************************************************/
264 
266 {
267 
268 #ifdef USE_MYSQL
269  return db_mysql_save_ns_alias(na);
270 #endif
271 
272  return 0;
273 }
274 
275 /*************************************************************************/
276 
278 {
279 
280 #ifdef USE_MYSQL
281  return db_mysql_save_ns_req(nr);
282 #endif
283 
284  return 0;
285 }
286 
287 /*************************************************************************/
288 
290 {
291 
292 #ifdef USE_MYSQL
293  return db_mysql_save_cs_info(ci);
294 #endif
295 
296  return 0;
297 }
298 
299 /*************************************************************************/
300 
302 {
303 
304 #ifdef USE_MYSQL
305  return db_mysql_save_bs_core(bi);
306 #endif
307 
308  return 0;
309 }
310 
311 /*************************************************************************/
312 
314 {
315 
316 #ifdef USE_MYSQL
317  return db_mysql_save_hs_core(hc);
318 #endif
319 
320  return 0;
321 }
322 
323 /*************************************************************************/
324 
325 int rdb_save_os_db(unsigned int maxucnt, unsigned int maxutime,
326  SList * ak, SList * sgl, SList * sql, SList * szl)
327 {
328 
329 #ifdef USE_MYSQL
330  return db_mysql_save_os_db(maxusercnt, maxusertime, ak, sgl, sql, szl);
331 #endif
332 
333  return 0;
334 }
335 
336 /*************************************************************************/
337 
339 {
340 
341 #ifdef USE_MYSQL
342  return db_mysql_save_news(ni);
343 #endif
344 
345  return 0;
346 }
347 
348 /*************************************************************************/
349 
351 {
352 
353 #ifdef USE_MYSQL
354  return db_mysql_load_bs_dbase();
355 #endif
356 
357  return 0;
358 }
359 
360 /*************************************************************************/
361 
363 {
364 
365 #ifdef USE_MYSQL
366  return db_mysql_load_hs_dbase();
367 #endif
368 
369  return 0;
370 }
371 
372 /*************************************************************************/
373 
375 {
376 
377 #ifdef USE_MYSQL
378  return db_mysql_load_ns_dbase();
379 #endif
380 
381  return 0;
382 }
383 
384 /*************************************************************************/
385 
386 int rdb_load_news(void)
387 {
388 
389 #ifdef USE_MYSQL
390  return db_mysql_load_news();
391 #endif
392 
393  return 0;
394 }
395 
396 /*************************************************************************/
397 
399 {
400 
401 #ifdef USE_MYSQL
402  return db_mysql_load_exceptions();
403 #endif
404 
405  return 0;
406 }
407 
408 /*************************************************************************/
409 
411 {
412 
413 #ifdef USE_MYSQL
414  return db_mysql_load_cs_dbase();
415 #endif
416 
417  return 0;
418 }
419 
420 /*************************************************************************/
421 
423 {
424 
425 #ifdef USE_MYSQL
426  return db_mysql_load_os_dbase();
427 #endif
428 
429  return 0;
430 }
431 
432 /*************************************************************************/
433 
435 {
436 
437 #ifdef USE_MYSQL
439 #endif
440 
441  return 0;
442 }
443 
444 /*************************************************************************/
445 
446 #define LOAD_DBASE(num, name, func) {\
447  if (!func) {\
448  alog("RDB unable to load %s database (%d/8) !!!", name, num);\
449  return 0;\
450  }\
451  if (debug)\
452  alog("debug: RDB Loaded %s DataBase (%d/8)", name, num);\
453 }
454 
456 {
457  if (!skeleton) {
458  LOAD_DBASE(1, "NickServ", rdb_load_ns_dbase());
459 
460  if (s_HostServ) {
461  LOAD_DBASE(2, "HostServ", rdb_load_hs_dbase());
462  }
463 
464  if (s_BotServ) {
465  LOAD_DBASE(3, "BotServ", rdb_load_bs_dbase());
466  }
467 
468  LOAD_DBASE(4, "ChanServ", rdb_load_cs_dbase());
469  }
470 
471  LOAD_DBASE(5, "OperServ", rdb_load_os_dbase());
472  LOAD_DBASE(6, "News", rdb_load_news());
473  LOAD_DBASE(7, "Exception", rdb_load_exceptions());
474 
475  if (PreNickDBName) {
476  LOAD_DBASE(8, "PreNick", rdb_load_ns_req_dbase());
477  } else if (debug) {
478  alog("debug: RDB No need to load PreNickDB (8/8)");
479  }
480 
481  alog("RDB: All DataBases loaded.");
482 
483  return 0;
484 }
485 
486 /*************************************************************************/
487 
489 {
490 
491 #ifdef USE_MYSQL
492  return db_mysql_save_exceptions(e);
493 #endif
494 
495  return 0;
496 }
497 
498 /* EOF */
#define LOAD_DBASE(num, name, func)
Definition: rdb.c:446
E time_t maxusertime
Definition: extern.h:1142
int rdb_save_exceptions(Exception *e)
Definition: rdb.c:488
int db_mysql_save_ns_alias(NickAlias *na)
Definition: mysql.c:462
int rdb_load_exceptions(void)
Definition: rdb.c:398
int rdb_load_os_dbase(void)
Definition: rdb.c:422
int db_mysql_save_exceptions(Exception *e)
Definition: mysql.c:991
E char * s_BotServ
Definition: extern.h:287
E uint32 maxusercnt
Definition: extern.h:1141
int rdb_clean_table_where(char *table, char *clause)
Definition: rdb.c:123
E char * PreNickDBName
Definition: extern.h:333
int rdb_save_ns_req(NickRequest *nr)
Definition: rdb.c:277
int db_mysql_load_bs_dbase(void)
Definition: mysql.c:1122
int db_mysql_init()
Definition: mysql.c:56
int db_mysql_load_os_dbase(void)
Definition: mysql.c:1267
char * rdb_quote(char *str)
Definition: rdb.c:60
int rdb_save_os_db(unsigned int maxucnt, unsigned int maxutime, SList *ak, SList *sgl, SList *sql, SList *szl)
Definition: rdb.c:325
int rdb_load_ns_dbase(void)
Definition: rdb.c:374
int rdb_load_dbases(void)
Definition: rdb.c:455
#define AK_ISNICK
Definition: services.h:635
int rdb_save_news(NewsItem *ni)
Definition: rdb.c:338
E char * sstrdup(const char *s)
Definition: memory.c:105
int rdb_load_cs_dbase(void)
Definition: rdb.c:410
int rdb_close()
Definition: rdb.c:47
int db_mysql_save_news(NewsItem *ni)
Definition: mysql.c:957
int rdb_save_ns_core(NickCore *nc)
Definition: rdb.c:253
int db_mysql_save_os_db(unsigned int maxucnt, unsigned int maxutime, SList *ak, SList *sgl, SList *sql, SList *szl)
Definition: mysql.c:819
int db_mysql_load_cs_dbase(void)
Definition: mysql.c:1413
int rdb_save_ns_alias(NickAlias *na)
Definition: rdb.c:265
int rdb_load_news(void)
Definition: rdb.c:386
int rdb_save_bs_core(BotInfo *bi)
Definition: rdb.c:301
int rdb_save_hs_core(HostCore *hc)
Definition: rdb.c:313
int rdb_ns_set_display(char *newnick, char *oldnick)
Definition: rdb.c:171
int rdb_scrub_table(char *table, char *clause)
Definition: rdb.c:138
int rdb_load_bs_dbase(void)
Definition: rdb.c:350
int db_mysql_open()
Definition: mysql.c:94
int rdb_init()
Definition: rdb.c:18
E void alog(const char *fmt,...) FORMAT(printf
int db_mysql_query(char *sql)
Definition: mysql.c:135
int db_mysql_load_ns_req_dbase(void)
Definition: mysql.c:1718
int db_mysql_save_cs_info(ChannelInfo *ci)
Definition: mysql.c:520
int rdb_direct_query(char *query)
Definition: rdb.c:153
int db_mysql_try(const char *fmt,...)
Definition: mysql.c:218
int db_mysql_load_news(void)
Definition: mysql.c:1190
E int debug
Definition: extern.h:775
char * db_mysql_quote(char *sql)
Definition: mysql.c:169
int db_mysql_save_hs_core(HostCore *hc)
Definition: mysql.c:1037
E char * s_HostServ
Definition: extern.h:303
E int skeleton
Definition: extern.h:778
int rdb_load_hs_dbase(void)
Definition: rdb.c:362
int db_mysql_save_ns_core(NickCore *nc)
Definition: mysql.c:347
int rdb_clean_table(char *table)
Definition: rdb.c:113
Definition: slist.h:20
int db_mysql_load_ns_dbase(void)
Definition: mysql.c:1763
int db_mysql_load_exceptions(void)
Definition: mysql.c:1232
int rdb_open()
Definition: rdb.c:31
int rdb_tag_table_where(char *table, char *clause)
Definition: rdb.c:86
int rdb_load_ns_req_dbase(void)
Definition: rdb.c:434
int rdb_save_cs_info(ChannelInfo *ci)
Definition: rdb.c:289
int rdb_empty_table(char *table)
Definition: rdb.c:100
int rdb_tag_table(char *table)
Definition: rdb.c:75
int db_mysql_save_ns_req(NickRequest *nr)
Definition: mysql.c:304
int db_mysql_save_bs_core(BotInfo *bi)
Definition: mysql.c:1079
int db_mysql_load_hs_dbase(void)
Definition: mysql.c:1154