-
Notifications
You must be signed in to change notification settings - Fork 6
/
mysql.c
1991 lines (1504 loc) · 52.1 KB
/
mysql.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SQL Routines to load/save Chan/NickServ data db.
**
** Chatnet K9 Channel Services
** Based on ircdservices ver. by Andy Church
** Parts copyright (c) 1999-2000 Andrew Kempe and others.
** Chatnet modifications (c) 2001-2003
**
** E-mail: routing@lists.chatnet.org
** Authors:
**
** Vampire (vampire@alias.chatnet.org)
** sloth (sloth@nopninjas.com)
**
** *** DO NOT DISTRIBUTE ***
**/
/*************************************************************************/
/*************************************************************************/
/*************************************************************************
* MYSQL database code section
*
* + 09.18.2002 - sloth@nopninjas.com
* all core MYSQL commands to load and save to the DB
* specific commands other than ADDCHAN are not working
*
* db_escape_string(char *dest, char *string, unsigned int len)
* dest should be double the length + 1 of string.
*
* db_connect(int init_db)
* connect to the database and initialize the db structure if init_db
*
* coming soon: more comments
*
* mysql_real_query still needs a wrapper to try to reconnect
* if disconnected from the DB
*
*************************************************************************/
#ifdef USE_MYSQL
#include "services.h"
#include "pseudo.h"
#define DB_LEVEL_FIELDS 1
#define DB_ACCESS_FIELDS 2
#define DB_AKICK_FIELDS 4
#define DB_ENTRYMSG_FIELDS 1
#define DB_AUTOOP_FIELDS 2
#define DB_COMMENT_FIELDS 2
#define DB_CHAN_FIELDS 27
/* START SQL PROTOTYPES */
void db_remove_autoop(ChannelInfo *ci, NickInfo *ni);
void db_autoop(ChannelInfo *ci, NickInfo *ni, char param);
void db_comment(ChannelInfo *ci, NickInfo *ni, char *comment);
void db_akick_del(char *chan, char *mask);
void db_load_cs(void);
static ChannelInfo *db_load_channel(char **crow);
void db_load_comments(ChannelInfo *ci);
void db_load_autoop(ChannelInfo *ci);
void db_load_entrymsg(ChannelInfo *ci);
void db_load_akicks(ChannelInfo *ci);
void db_load_access(ChannelInfo *ci);
void db_load_levels(ChannelInfo *ci, uint16 n_levels);
int db_add_channel(ChannelInfo *ci);
int db_queryadd_chan(ChannelInfo *ci);
int db_queryadd_comment(ChannelInfo *ci, int n, int a);
int db_queryadd_autoop(ChannelInfo *ci, int n, int a);
int db_queryadd_entrymsg(ChannelInfo *ci, int n);
int db_queryadd_akick(ChannelInfo *ci, int n, int a);
int db_queryadd_access(ChannelInfo *ci, int n, int a);
int db_test_chan(char *channame, uint32 n);
void db_cs_expire_nick(char *usernick);
void db_remuser(char *channame, char *usernick);
int db_remove_nick_in(char *e_usernick, char *dbtable);
int db_remove_nick_from(char *e_usernick, char *channame, char *dbtable);
/* END SQL PROTOTYPES */
extern void alpha_insert_chan(ChannelInfo *ci);
extern void count_chan(ChannelInfo *ci);
extern void reset_levels(ChannelInfo *ci);
extern void alpha_insert_chan(ChannelInfo *ci);
extern void alpha_insert_nick(NickInfo *ni);
char *db_escape_string(char *dest, char *string, unsigned int len) {
if (!string || !len) {
dest[0] = 0;
return(NULL);
}
len = len / 2 - 1;
if(strlen(string) < len)
len = strlen(string);
mysql_real_escape_string((MYSQL *)&mysqldb, dest, string, len);
return(dest);
}
int db_connect(int init_db) {
if(init_db)
if (mysql_init((MYSQL *)&mysqldb) == NULL) {
log("dberror: could not init MYSQL data");
exit(1);
}
if (debug)
log("debug: connecting to database [%s@%s:%d/%s]",
DBuser, DBhost, DBport, DBname);
if (mysql_real_connect((MYSQL *)&mysqldb, DBhost, DBuser, DBpass, DBname,
DBport, NULL, 0) == NULL) {
log("dberror: could not connect to database: %s",
mysql_error((MYSQL *)&mysqldb));
return(0);
}
return(1);
}
int db_delete_chan(char *channame) {
char e_channame[CHANMAX*2+1],
tmpquery[2048],
dbtable[][32] = {
"chan_access",
"chan_akick",
"chan_autoop",
"chan_entrymsg",
"chan_comments",
"chan_levels",
"channel"
};
uint16 i, r = 0;
if (db_escape_string(e_channame, channame, sizeof(e_channame)) == NULL)
return(-1);
for( i = 0; i < 7; i++) {
snprintf(tmpquery, sizeof(tmpquery) - 1, "DELETE FROM %s WHERE "
"channame=\'%s\'", dbtable[i], e_channame);
if(mysql_real_query((MYSQL *)&mysqldb, tmpquery, strlen(tmpquery)) != 0) {
log("dberror: deleting [%s] from [%s]: %s", channame, dbtable[i],
mysql_error((MYSQL *)&mysqldb));
r = DB_ERROR;
}
}
return(r);
}
void db_simple_update(char *dbtable, char *field, char *value,
char *cfield, char *cvalue) {
char e_value[512],
e_cvalue[512];
if (db_escape_string(e_value, value, sizeof(e_value)) == NULL)
return;
if (db_escape_string(e_cvalue, cvalue, sizeof(e_cvalue)) == NULL)
return;
db_queryf("UPDATE %s SET %s='%s' WHERE %s='%s'", dbtable, field, value,
cfield, cvalue);
}
void db_chan_seti(ChannelInfo *ci, char *field, int value) {
char e_channame[CHANMAX*2+1];
if (db_escape_string(e_channame, ci->name, sizeof(e_channame)) == NULL)
return;
db_queryf("UPDATE channel SET %s='%d' WHERE channame='%s'", field, value,
e_channame);
}
void db_chan_set(ChannelInfo *ci, char *field, char *value) {
char e_channame[CHANMAX*2+1],
e_value[2048];
if (db_escape_string(e_channame, ci->name, sizeof(e_channame)) == NULL)
return;
if (value) {
if (db_escape_string(e_value, value, sizeof(e_value)) == NULL)
return;
} else
strcpy(e_value, "");
db_queryf("UPDATE channel SET %s='%s' WHERE channame='%s'", field, e_value,
e_channame);
}
/* db_update_number
* send the SQL command to update the number.
* used in db_update_numbers()
*/
void db_update_number(MYSQL_ROW row, uint32 number, char *dbtable,
char *e_channame) {
db_queryf("UPDATE %s SET number=%d WHERE channame='%s' && number=%s",
dbtable, number, e_channame, row[1]);
}
/* always escape user input string arguments to this! */
int db_update_numbers(char *e_channame, char *dbtable) {
MYSQL_ROW row;
MYSQL_RES *mr;
uint32 i;
if ((mr = db_request("SELECT channame,number FROM %s WHERE channame='%s' "
"ORDER BY number", dbtable, e_channame)) == NULL)
return(DB_ERROR);
if(mysql_num_rows(mr) == 0) {
mysql_free_result(mr);
return(0);
}
for( i = 0; (row = mysql_fetch_row(mr)); i++)
db_update_number(row, i, dbtable, e_channame);
mysql_free_result(mr);
return(i);
}
/*
* db_remove_nick_from
* remove a user from specified table and returns the number of remaining
* entries or negative on error. used for cleaning up expired users in the
* channel database.
*/
int db_remove_nick_from(char *e_usernick, char *channame, char *dbtable) {
char table[][32] = {
"chan_comment",
"chan_akick",
"chan_levels",
"chan_autoop",
"chan_access",
"chan_entrymsg",
"\0"
},
e_channame[CHANMAX*2+1],
tmpquery[2048];
int r, i;
if (db_escape_string(e_channame, channame, sizeof(e_channame)) == NULL)
return(-1);
snprintf(tmpquery, sizeof(tmpquery) - 1, "DELETE FROM %s WHERE "
"channame=\'%s\' && nick=\'%s\'", dbtable, e_channame, e_usernick);
if (mysql_real_query((MYSQL *)&mysqldb, tmpquery, strlen(tmpquery)) != 0) {
log("dberror: deleting [%s@%s] in [%s]: %s", e_usernick, channame, dbtable,
mysql_error((MYSQL *)&mysqldb));
return(DB_ERROR);
}
if(mysql_affected_rows((MYSQL *)&mysqldb) < 1) {
if (debug)
log("dberror: entry not found while deleting [%s@%s] in [%s]",
e_usernick, channame, dbtable);
return(-1);
}
if ((r = db_update_numbers(e_channame, dbtable)) < 0)
return(-1);
for( i = 0; table[i] && i<6; i++) {
if (!strcmp(table[i], dbtable)) {
db_update_count(i, channame, r);
break;
}
}
return(r);
}
int db_remove_nick_in(char *e_usernick, char *dbtable) {
MYSQL_ROW row;
MYSQL_RES *mr;
char tmpquery[2048],
*last = "\0";
uint16 i;
snprintf(tmpquery, sizeof(tmpquery) - 1, "SELECT channame FROM %s "
"WHERE nick=\'%s\'", dbtable, e_usernick);
if (mysql_real_query((MYSQL *)&mysqldb, tmpquery, strlen(tmpquery)) != 0) {
log("dberror: selecting [nick=%s] in [%s]: %s", e_usernick, dbtable,
mysql_error((MYSQL *)&mysqldb));
return(DB_ERROR);
}
if((mr = mysql_store_result((MYSQL *)&mysqldb)) == NULL) {
log("dberror: getting results from [%s] for [%s]: %s", dbtable, e_usernick,
mysql_error((MYSQL *)&mysqldb));
return(DB_ERROR);
}
if(mysql_num_rows(mr) == 0)
return(0);
for( i = 0; (row = mysql_fetch_row(mr)); i++) {
if(strcmp(last, row[0])) {
db_remove_nick_from(e_usernick, row[0], dbtable);
last = row[0];
}
}
return(i);
}
void db_remuser(char *channame, char *usernick) {
char e_usernick[NICKMAX*2+1],
dbtable[][32] = {
"chan_access",
"chan_autoop",
"chan_comments"
};
uint16 i;
if (debug > 2)
log("debug db_remuser(%s, %s)", channame, usernick);
if (db_escape_string(e_usernick, usernick, sizeof(e_usernick)) == NULL)
return;
for( i = 0; i < 3; i++)
db_remove_nick_from(e_usernick, channame, dbtable[i]);
}
void db_cs_expire_nick(char *usernick) {
char e_usernick[NICKMAX*2+1],
dbtable[][32] = {
"chan_access",
"chan_autoop",
"chan_comments"
};
uint16 i;
if (db_escape_string(e_usernick, usernick, sizeof(e_usernick)) == NULL)
return;
for( i = 0; i < 3; i++)
db_remove_nick_in(e_usernick, dbtable[i]);
}
int db_test_chan(char *channame, uint32 n) {
MYSQL_RES *mr;
char tmpquery[2048], e_channame[CHANMAX*2+1];
int32 i, a, r;
char tabname[][32] = {
"channel",
"chan_akick",
"chan_levels",
"chan_autoop",
"chan_access",
"chan_entrymsg",
"chan_comments",
"channel"
};
if (db_escape_string(e_channame, channame, sizeof(e_channame)) == NULL)
return(0);
a = (n < 8) ? n : 0;
for( i = a; i < (a ? (a + 1) : 7); i++) {
memset(tmpquery, 0, sizeof(tmpquery));
snprintf(tmpquery, sizeof(tmpquery) - 1, "SELECT channame FROM %s"
" WHERE channame=\'%s\'", tabname[i], e_channame);
if(mysql_real_query((MYSQL *)&mysqldb, tmpquery, strlen(tmpquery)) != 0) {
log("dberror: querying [%s] for [%s]: %s", tabname[i], channame,
mysql_error((MYSQL *)&mysqldb));
return(DB_ERROR);
}
if((mr = mysql_store_result((MYSQL *)&mysqldb)) == NULL) {
log("dberror: getting results from [%s] for: [%s]: %s", tabname[i],
channame, mysql_error((MYSQL *)&mysqldb));
return(DB_ERROR);
} else if((r = mysql_num_rows(mr)) > 0) {
if (debug)
log("dberror: channel [%s] already exists in [%s]",
channame, tabname[i]);
mysql_free_result(mr);
return(DB_EXIST);
}
mysql_free_result(mr);
}
return(DB_NOEXIST);
}
int db_queryadd_access(ChannelInfo *ci, int n, int a) {
char e_channame[CHANMAX*2+1],
e_nick[NICKMAX*2+1],
tmpquery[2048];
if(db_escape_string(e_channame, ci->name, sizeof(e_channame)) == NULL)
return(0);
if(db_escape_string(e_nick, ci->access[n].ni->nick, sizeof(e_nick)) == NULL)
return(0);
memset(tmpquery, 0, sizeof(tmpquery));
snprintf(tmpquery, sizeof(tmpquery) - 1, "INSERT INTO chan_access "
"VALUES (\'%s\',%d,%d,\'%s\')", e_channame, a, ci->access[n].level,
e_nick);
if(mysql_real_query((MYSQL *)&mysqldb, tmpquery, strlen(tmpquery)) != 0) {
log("dberror: loading access for [%s]! %s", ci->name,
mysql_error((MYSQL *)&mysqldb));
return(DB_ERROR);
}
return(1);
}
int db_queryadd_akick(ChannelInfo *ci, int n, int a) {
char e_channame[CHANMAX*2+1],
e_nickmask[512],
e_reason[512],
e_who[NICKMAX*2+1],
tmpquery[2048];
if (db_escape_string(e_channame, ci->name, sizeof(e_channame)) == NULL)
return(0);
if (db_escape_string(e_nickmask, ci->akick[n].is_nick ?
ci->akick[n].u.ni->nick : ci->akick[n].u.mask,
sizeof(e_nickmask)) == NULL)
return(0);
if (db_escape_string(e_reason, ci->akick[n].reason, sizeof(e_reason)) == NULL)
return(0);
if (db_escape_string(e_who, ci->akick[n].who, sizeof(e_who)) == NULL)
return(0);
memset(tmpquery, 0, sizeof(tmpquery));
snprintf(tmpquery, sizeof(tmpquery) - 1, "INSERT INTO chan_akick VALUES"
" (\'%s\',%d,%d,\'%s\',\'%s\',\'%s\')", e_channame, a,
ci->akick[n].is_nick, e_nickmask, e_reason, e_who);
if (mysql_real_query((MYSQL *)&mysqldb, tmpquery, strlen(tmpquery)) != 0) {
log("dberror: loading akicks for [%s]: %s", ci->name,
mysql_error((MYSQL *)&mysqldb));
return(DB_ERROR);
}
return(1);
}
int db_queryadd_entrymsg(ChannelInfo *ci, int n) {
char e_channame[CHANMAX*2+1],
e_entrymsg[2048],
tmpquery[2048];
if (db_escape_string(e_channame, ci->name, sizeof(e_channame)) == NULL)
return(0);
if (db_escape_string(e_entrymsg, ci->entrymsg[n], sizeof(e_entrymsg)) == NULL)
return(0);
memset(tmpquery, 0, sizeof(tmpquery));
snprintf(tmpquery, sizeof(tmpquery) - 1, "INSERT INTO chan_entrymsg "
"VALUES (\'%s\',%d,\'%s\')", e_channame, n, e_entrymsg);
if (mysql_real_query((MYSQL *)&mysqldb, tmpquery, strlen(tmpquery)) != 0) {
log("dberror: loading onjoins for [%s]: %s", ci->name,
mysql_error((MYSQL *)&mysqldb));
return(DB_ERROR);
}
return(1);
}
int db_queryadd_autoop(ChannelInfo *ci, int n, int a) {
char e_channame[CHANMAX*2+1],
e_nick[NICKMAX*2+1],
tmpquery[2048];
if (db_escape_string(e_channame, ci->name, sizeof(e_channame)) == NULL)
return(0);
if (db_escape_string(e_nick, ci->autoop[n].ni->nick, sizeof(e_nick)) == NULL)
return(0);
memset(tmpquery, 0, sizeof(tmpquery));
snprintf(tmpquery, sizeof(tmpquery) - 1, "INSERT INTO chan_autoop "
"VALUES (\'%s\',%d,\'%s\',\'%c\')", e_channame, a,
e_nick, ci->autoop[n].flag);
if (mysql_real_query((MYSQL *)&mysqldb, tmpquery, strlen(tmpquery)) != 0) {
log("dberror: loading autoops for [%s]: %s", ci->name,
mysql_error((MYSQL *)&mysqldb));
return(DB_ERROR);
}
return(1);
}
int db_queryadd_comment(ChannelInfo *ci, int n, int a) {
char e_channame[CHANMAX*2+1],
e_nick[NICKMAX*2+1],
e_msg[COMMENTMAX*2+1],
tmpquery[2048];
if (db_escape_string(e_channame, ci->name, sizeof(e_channame)) == NULL)
return(0);
if (db_escape_string(e_nick, ci->comment[n].ni->nick, sizeof(e_nick)) == NULL)
return(0);
if (db_escape_string(e_msg, ci->comment[n].msg, sizeof(e_msg)) == NULL)
return(0);
memset(tmpquery, 0, sizeof(tmpquery));
snprintf(tmpquery, sizeof(tmpquery) - 1, "INSERT INTO chan_comments "
"VALUES (\'%s\',%d,\'%s\',\'%s\')", e_channame, a,
e_nick, e_msg);
if (mysql_real_query((MYSQL *)&mysqldb, tmpquery, strlen(tmpquery)) != 0) {
log("dberror: loading user greets for [%s]: %s", ci->name,
mysql_error((MYSQL *)&mysqldb));
return(DB_ERROR);
}
return(1);
}
int db_queryadd_chan(ChannelInfo *ci) {
/* escape string buffers [buf * 2 + 1]*/
char e_channame[CHANMAX*2+1],
e_foundernick[NICKMAX*2+1],
e_successornick[NICKMAX*2+1],
e_founderpass[PASSMAX*2+1],
e_chandesc[512],
e_chanurl[512],
e_email[512],
e_last_topic[800],
e_last_topic_setter[NICKMAX*2+1],
e_swho[NICKMAX*2+1],
e_sreason[512],
e_mlock_key[512],
tmpquery[4096];
if (db_escape_string(e_channame, ci->name, sizeof(e_channame)) == NULL)
return(0);
if (db_escape_string(e_foundernick, ci->founder->nick,
sizeof(e_foundernick)) == NULL)
return(0);
if (ci->successor)
db_escape_string(e_successornick, ci->successor->nick,
sizeof(e_successornick));
db_escape_string(e_founderpass, ci->founderpass, sizeof(e_founderpass));
db_escape_string(e_chandesc, ci->desc, sizeof(e_chandesc));
db_escape_string(e_chanurl, ci->url, sizeof(e_chanurl));
db_escape_string(e_email, ci->email, sizeof(e_email));
db_escape_string(e_last_topic, ci->last_topic, sizeof(e_last_topic));
db_escape_string(e_last_topic_setter, ci->last_topic_setter,
sizeof(e_last_topic_setter));
db_escape_string(e_mlock_key, ci->mlock_key, sizeof(e_mlock_key));
if(ci->suspendinfo) {
db_escape_string(e_swho, ci->suspendinfo->who, sizeof(e_swho));
db_escape_string(e_sreason, ci->suspendinfo->reason, sizeof(e_sreason));
}
memset(tmpquery, 0, sizeof(tmpquery));
snprintf(tmpquery, sizeof(tmpquery) - 1, "INSERT INTO channel VALUES ("
"\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',%d,%d,"
"\'%s\',\'%s\',%d,%d,\'%s\',\'%s\',%d,%d,%d,"
"%d,%d,%d,%d,%d,\'%s\',%d,%d,%d)",
e_channame,
e_foundernick,
e_successornick,
e_founderpass,
e_chandesc,
e_chanurl,
e_email,
ci->time_registered,
ci->last_used,
e_last_topic,
e_last_topic_setter,
ci->last_topic_time,
ci->flags,
ci->suspendinfo ? e_swho : "",
ci->suspendinfo ? e_sreason : "",
ci->suspendinfo ? ci->suspendinfo->suspended : 0,
ci->suspendinfo ? ci->suspendinfo->expires : 0,
CA_SIZE,
ci->accesscount,
ci->akickcount,
ci->mlock_on,
ci->mlock_off,
ci->mlock_limit,
e_mlock_key,
ci->entrycount,
ci->autoopcount,
ci->commentcount
);
if(mysql_real_query((MYSQL *)&mysqldb, tmpquery, strlen(tmpquery)) != 0) {
log("dberror: loading channel for [%s]: %s", ci->name,
mysql_error((MYSQL *)&mysqldb));
return(DB_ERROR);
}
return(1);
}
int db_add_channel(ChannelInfo *ci) {
char escapebuf[2050];
int i, r, a;
if (debug)
log("db_add_channel() adding channel [%s]", ci->name);
if((r = db_test_chan(ci->name, DB_TEST_ALL)) != DB_NOEXIST)
return(r);
/* load command levels into the db */
for( i = 0; i < CA_SIZE; i++)
db_queryf("INSERT INTO chan_levels VALUES ('%s','%d','%d')",
db_escape_string(escapebuf, ci->name, sizeof(escapebuf)),
i, ci->levels[i]);
/* load access list into the db */
if(ci->accesscount) {
for( i = 0, a = 0; i < ci->accesscount; i++)
if(ci->access[i].in_use) {
db_queryadd_access(ci, i, a);
a++;
}
if(a < ci->accesscount)
ci->accesscount = a;
}
/* load banlist into the db */
if(ci->akickcount) {
for( i = 0, a = 0; i < ci->akickcount; i++)
if(ci->akick[i].in_use) {
db_queryadd_akick(ci, i, a);
a++;
}
if(a < ci->akickcount)
ci->akickcount = a;
}
/* load onjoin messages into the db */
if(ci->entrycount)
for( i = 0; i < ci->entrycount; i++)
db_queryadd_entrymsg(ci, i);
/* load autoop list into the db */
if(ci->autoopcount) {
for( i = 0, a = 0; i < ci->autoopcount; i++)
if(ci->autoop[i].in_use) {
db_queryadd_autoop(ci, i, a);
a++;
}
if(a < ci->autoopcount)
ci->autoopcount = a;
}
/* load user greet messages into the db */
if(ci->commentcount) {
for( i = 0, a = 0; i < ci->commentcount; i++)
if(ci->comment[i].in_use) {
db_queryadd_comment(ci, i, a);
a++;
}
if(a < ci->commentcount)
ci->commentcount = a;
}
/* finish adding the rest */
db_queryadd_chan(ci);
return(DB_SUCCESS);
}
void db_load_levels(ChannelInfo *ci, uint16 n_levels) {
MYSQL_ROW row;
MYSQL_RES *res;
char tmpquery[256],
e_channame[CHANMAX*2+1];
uint16 fields, i, r;
if (db_escape_string(e_channame, ci->name, sizeof(e_channame)) == NULL)
fatal("error: could not escape string [%s]", ci->name);
snprintf(tmpquery, sizeof(tmpquery) - 1, "SELECT level FROM chan_levels "
"WHERE channame='%s' ORDER BY number", e_channame);
if(mysql_real_query((MYSQL *)&mysqldb, tmpquery, strlen(tmpquery)) != 0) {
fatal("dberror: querying [chan_levels] for [%s]: %s",
ci->name, mysql_error((MYSQL *)&mysqldb));
}
if((res = mysql_store_result((MYSQL *)&mysqldb)) == NULL) {
fatal("dberror: getting results from [chan_levels] for [%s]: %s",
ci->name, mysql_error((MYSQL *)&mysqldb));
}
if((r = mysql_num_rows(res)) != n_levels) {
fatal("dberror: [chan_levels] table has an invalid number or rows for [%s]!"
"%d rows != %d n_levels", ci->name, r, n_levels);
mysql_free_result(res);
}
if((fields = mysql_num_fields(res)) < DB_LEVEL_FIELDS) {
fatal("dberror: less than [%d] fields in table [chan_levels] "
"for [%s]", DB_LEVEL_FIELDS, ci->name);
mysql_free_result(res);
}
for( i = 0; (row = mysql_fetch_row(res)); i++)
ci->levels[i] = atoi(row[0]);
mysql_free_result(res);
return;
}
void db_load_access(ChannelInfo *ci) {
MYSQL_ROW row;
MYSQL_RES *res;
char tmpquery[256],
e_channame[CHANMAX*2+1];
uint16 fields, i;
if (db_escape_string(e_channame, ci->name, sizeof(e_channame)) == NULL)
fatal("error: could not escape string [%s]", ci->name);
snprintf(tmpquery, sizeof(tmpquery) - 1, "SELECT level,nick FROM chan_access "
"WHERE channame='%s' ORDER BY number", e_channame);
if(mysql_real_query((MYSQL *)&mysqldb, tmpquery, strlen(tmpquery)) != 0) {
fatal("dberror: querying [chan_access] for [%s]: %s",
ci->name, mysql_error((MYSQL *)&mysqldb));
}
if((res = mysql_store_result((MYSQL *)&mysqldb)) == NULL) {
fatal("dberror: getting results from [chan_access] for [%s]: %s",
ci->name, mysql_error((MYSQL *)&mysqldb));
}
ci->accesscount = mysql_num_rows(res);
/*
if(mysql_num_rows(res) != ci->accesscount) {
mysql_free_result(res);
fatal("dberror: [chan_access] table has invalid number or rows for [%s]!",
ci->name);
}
*/
if((fields = mysql_num_fields(res)) < DB_ACCESS_FIELDS) {
mysql_free_result(res);
fatal("dberror: less than [%d] fields in table [chan_access] "
"for [%s]", DB_ACCESS_FIELDS, ci->name);
}
ci->access = scalloc(sizeof(ChanAccess), ci->accesscount);
for( i = 0; (row = mysql_fetch_row(res)); i++) {
ci->access[i].level = atoi(row[0]);
if(!(ci->access[i].ni = findnick(row[1]))) {
if (debug > 2)
log("debug: db_load_access() couldn't find nick [%s]", row[1]);
ci->access[i].in_use = 0;
} else
ci->access[i].in_use = 1;
}
mysql_free_result(res);
return;
}
void db_load_akicks(ChannelInfo *ci) {
MYSQL_ROW row;
MYSQL_RES *res;
char tmpquery[256],
e_channame[CHANMAX*2+1];
uint16 fields, i;
if (db_escape_string(e_channame, ci->name, sizeof(e_channame)) == NULL)
fatal("error: could not escape string [%s]", ci->name);
snprintf(tmpquery, sizeof(tmpquery) - 1, "SELECT is_nick,mask,reason,who "
"FROM chan_akick WHERE channame='%s' ORDER BY number", e_channame);
if(mysql_real_query((MYSQL *)&mysqldb, tmpquery, strlen(tmpquery)) != 0) {
fatal("dberror: querying [chan_akick] for [%s]: %s",
ci->name, mysql_error((MYSQL *)&mysqldb));
}
if((res = mysql_store_result((MYSQL *)&mysqldb)) == NULL) {
fatal("dberror: getting results from [chan_akick] for [%s]: %s",
ci->name, mysql_error((MYSQL *)&mysqldb));
}
ci->akickcount = mysql_num_rows(res);
/*
if(mysql_num_rows(res) != ci->akickcount) {
mysql_free_result(res);
fatal("dberror: [chan_akick] table has an invalid number or rows for [%s]!",
ci->name);
}
*/
if((fields = mysql_num_fields(res)) < DB_AKICK_FIELDS) {
mysql_free_result(res);
fatal("dberror: less than [%d] fields in table [chan_akick] "
"for [%s]", DB_AKICK_FIELDS, ci->name);
}
ci->akick = scalloc(sizeof(AutoKick), ci->akickcount);
for( i = 0; (row = mysql_fetch_row(res)); i++) {
ci->akick[i].is_nick = atoi(row[0]);
if (ci->akick[i].is_nick)
ci->akick[i].u.ni = findnick(row[1]);
else {
ci->akick[i].u.mask = smalloc(strlen(row[1]) + 1);
strcpy(ci->akick[i].u.mask, row[1]);
}
ci->akick[i].reason = smalloc(strlen(row[2]) + 1);
strcpy(ci->akick[i].reason, row[2]);
copy_buffer(ci->akick[i].who, row[3]);
ci->akick[i].in_use = 1;
}
mysql_free_result(res);
return;
}
void db_load_entrymsg(ChannelInfo *ci) {
MYSQL_ROW row;
MYSQL_RES *res;
char tmpquery[256],
e_channame[CHANMAX*2+1],
**greet;
uint16 fields, i;
if (db_escape_string(e_channame, ci->name, sizeof(e_channame)) == NULL)
fatal("error: could not escape string [%s]", ci->name);
snprintf(tmpquery, sizeof(tmpquery) - 1, "SELECT entrymsg FROM chan_entrymsg " "WHERE channame='%s' ORDER BY number", e_channame);
if(mysql_real_query((MYSQL *)&mysqldb, tmpquery, strlen(tmpquery)) != 0) {
fatal("dberror: querying [chan_entrymsg] for [%s]: %s",
ci->name, mysql_error((MYSQL *)&mysqldb));
}
if((res = mysql_store_result((MYSQL *)&mysqldb)) == NULL) {
fatal("dberror: getting results from [chan_entrymsg] for [%s]: %s",
ci->name, mysql_error((MYSQL *)&mysqldb));
}
ci->entrycount = mysql_num_rows(res);
/*
if(mysql_num_rows(res) != ci->entrycount) {
mysql_free_result(res);
fatal("dberror: [chan_entrymsg] table has an invalid number or rows "
"for [%s]!", ci->name);
}
*/
if((fields = mysql_num_fields(res)) < DB_ENTRYMSG_FIELDS) {
mysql_free_result(res);
fatal("dberror: less than [%d] fields in table [chan_entrymsg] "
"for [%s]", DB_ENTRYMSG_FIELDS, ci->name);
}
greet = smalloc(sizeof(char *) * ci->entrycount);
ci->entrymsg = greet;
for( i = 0; (row = mysql_fetch_row(res)); i++, greet++)
copy_string(greet, row[0]);
mysql_free_result(res);
return;
}
void db_load_autoop(ChannelInfo *ci) {
MYSQL_ROW row;
MYSQL_RES *res;
char tmpquery[256],
e_channame[CHANMAX*2+1];
uint16 fields, i;
if (db_escape_string(e_channame, ci->name, sizeof(e_channame)) == NULL)
fatal("error: could not escape string [%s]", ci->name);
snprintf(tmpquery, sizeof(tmpquery) - 1, "SELECT nick,flag FROM chan_autoop "
"WHERE channame='%s' ORDER BY number", e_channame);
if(mysql_real_query((MYSQL *)&mysqldb, tmpquery, strlen(tmpquery)) != 0) {
fatal("error querying [chan_autoop] for [%s]: %s",
ci->name, mysql_error((MYSQL *)&mysqldb));
}
if((res = mysql_store_result((MYSQL *)&mysqldb)) == NULL) {
fatal("dberror: getting results from [chan_autoop] for [%s]: %s",
ci->name, mysql_error((MYSQL *)&mysqldb));
}
ci->autoopcount = mysql_num_rows(res);
/*
if(mysql_num_rows(res) != ci->autoopcount) {
mysql_free_result(res);
fatal("dberror: [chan_autoop] table has an invalid number or rows "
"for [%s]!", ci->name);
}
*/
if((fields = mysql_num_fields(res)) < DB_AUTOOP_FIELDS) {
mysql_free_result(res);
log("dberror: less than [%d] fields in table [chan_autoop] "
"for [%s]", DB_AUTOOP_FIELDS, ci->name);
}
ci->autoop = scalloc(sizeof(ChannelAutoop), ci->autoopcount);
for( i = 0; (row = mysql_fetch_row(res)); i++) {
ci->autoop[i].ni = findnick(row[0]);
ci->autoop[i].flag = row[1][0];