]> jfr.im git - irc/quakenet/newserv.git/blob - helpmod2/hconf.c
Initial Import
[irc/quakenet/newserv.git] / helpmod2 / hconf.c
1
2 #include <stdio.h>
3 #include <ctype.h>
4 #include <string.h>
5 #include <time.h>
6 #include <unistd.h>
7
8 #include "hconf.h"
9 #include "helpmod.h"
10 #include "hterm.h"
11
12
13 static void helpmod_line_fix(char **ptr)
14 {
15 while (isspace(**ptr))
16 (*ptr)++;
17
18 if (strchr(*ptr, '\n') != NULL)
19 *strchr(*ptr, '\n') = '\0';
20
21 if (strchr(*ptr, '\r') != NULL)
22 *strchr(*ptr, '\r') = '\0';
23 }
24
25
26 int helpmod_config_read(const char *fname)
27 {
28 FILE *in;
29 char buf[512], *ptr;
30
31 if ((in = fopen(fname,"rt")) == NULL)
32 return -1;
33
34 while (!feof(in))
35 {
36 ptr = (char*)buf;
37
38 fgets(buf, 512, in);
39 if (feof(in))
40 break;
41
42 helpmod_line_fix(&ptr);
43
44 /* support comments */
45 if (*ptr == '%')
46 continue;
47 if (!*ptr)
48 continue;
49
50 /* check what kind of a line it was */
51 if (!strcmp(ptr, "channel"))
52 {
53 if (helpmod_config_read_channel(in))
54 Error("helpmod", ERR_WARNING, "Reading of database entry 'channel' failed");
55 }
56 else if (!strcmp(ptr, "account"))
57 {
58 if (helpmod_config_read_account(in))
59 Error("helpmod", ERR_WARNING, "Reading of database entry 'account' failed");
60 }
61 else if (!strcmp(ptr, "ban"))
62 {
63 if (helpmod_config_read_ban(in))
64 Error("helpmod", ERR_WARNING, "Reading of database entry 'ban' failed");
65 }
66 else if (!strcmp(ptr, "lamercontrol profile"))
67 {
68 if (helpmod_config_read_hlc_profile(in))
69 Error("helpmod", ERR_WARNING, "Reading of database entry 'lamercontrol_profile' failed");
70 }
71 else if (!strcmp(ptr, "term"))
72 {
73 if (helpmod_config_read_term(in, NULL))
74 Error("helpmod", ERR_WARNING, "Reading of database entry 'term' failed");
75 }
76 else if (!strcmp(ptr, "globals"))
77 {
78 if (helpmod_config_read_globals(in))
79 Error("helpmod", ERR_WARNING, "Reading of database entry 'globals' failed");
80 }
81 else if (!strcmp(ptr, "report"))
82 {
83 if (helpmod_config_read_report(in))
84 Error("helpmod", ERR_WARNING, "Reading of database entry 'report' failed");
85 }
86 else if (!strcmp(ptr, "ticket"))
87 {
88 if (helpmod_config_read_ticket(in))
89 Error("helpmod", ERR_WARNING, "Reading of database entry 'ticket' failed");
90 }
91 else
92 Error("helpmod", ERR_WARNING, "Unknown database entry '%s'", ptr);
93 }
94
95 hchannels_match_accounts();
96
97 fclose(in);
98 return 0;
99 }
100
101 int helpmod_config_write(const char *fname)
102 {
103 FILE *out;
104 /*char buf[512];*/
105 time_t timer = time(NULL);
106
107 if ((out = fopen(fname,"wt")) == NULL)
108 return -1;
109
110 fprintf(out, "%% H2 version %s database\n", HELPMOD_VERSION);
111 fprintf(out, "%% %s\n\n", asctime(localtime(&timer)));
112
113
114 { /* lamercontrol profiles */
115 hlc_profile *ptr = hlc_profiles;
116
117 fprintf(out,"%% lamercontrol profile structure:\n");
118 fprintf(out,"%% X (string):\n");
119 fprintf(out,"%% Y (string):\n");
120 fprintf(out,"%% Z (int):\n");
121
122 for(;ptr;ptr=ptr->next)
123 {
124 fprintf(out, "lamercontrol profile\n");
125 helpmod_config_write_hlc_profile(out, ptr);
126 }
127 }
128
129 { /* channels */
130 hchannel *ptr = hchannels;
131
132 fprintf(out,"\n%% channel structure:\n");
133 fprintf(out,"%% name (string):\n");
134 fprintf(out,"%% flags (integer):\n");
135 fprintf(out,"%% welcome message (string):\n");
136 fprintf(out,"%% lamercontrol profile (string):\n");
137
138 for(;ptr;ptr=ptr->next)
139 {
140 fprintf(out, "channel\n");
141 helpmod_config_write_channel(out, ptr);
142 }
143 }
144
145 fprintf(out,"\n");
146
147 { /* accounts */
148 haccount *ptr = haccounts;
149
150 fprintf(out,"\n%% account structure:\n");
151 fprintf(out,"%% name (string):\n");
152 fprintf(out,"%% level (integer):\n");
153
154 for(;ptr;ptr=ptr->next)
155 {
156 fprintf(out, "account\n");
157 helpmod_config_write_account(out, ptr);
158 }
159 }
160
161 fprintf(out,"\n");
162
163 { /* bans */
164 hban *ptr = hbans;
165
166 fprintf(out,"\n%% ban structure:\n");
167 fprintf(out,"%% banmask (string):\n");
168 fprintf(out,"%% reason (string):\n");
169 fprintf(out,"%% expiration (int):\n");
170
171 for(;ptr;ptr=ptr->next)
172 {
173 fprintf(out, "ban\n");
174 helpmod_config_write_ban(out, ptr);
175 }
176 }
177
178 { /* global terms */
179 hterm *ptr = hterms;
180
181 fprintf(out,"\n%% term structure:\n");
182 fprintf(out,"%% name (string):\n");
183 fprintf(out,"%% description (string):\n");
184
185 for(;ptr;ptr=ptr->next)
186 {
187 fprintf(out, "term\n");
188 helpmod_config_write_term(out, ptr);
189 }
190 }
191
192 { /* tickets */
193 hchannel *hchan;
194 hticket *htick;
195
196 fprintf(out, "\n%% ticket structure:\n");
197 fprintf(out, "%% channel (string)\n");
198 fprintf(out, "%% authname (string)\n");
199 fprintf(out, "%% expiration time (int)\n");
200
201 for (hchan = hchannels;hchan;hchan = hchan->next)
202 for (htick = hchan->htickets;htick;htick = htick->next)
203 {
204 fprintf(out, "ticket\n");
205 helpmod_config_write_ticket(out, htick, hchan);
206 }
207 }
208
209 { /* reports */
210 hchannel *hchan;
211
212 fprintf(out, "\n%% report structure:\n");
213 fprintf(out, "%% channel reported\n");
214 fprintf(out, "%% channel reported to\n");
215
216 for (hchan = hchannels;hchan;hchan = hchan->next)
217 {
218 if ((hchan->flags & H_REPORT) && hchannel_is_valid(hchan->report_to))
219 {
220 fprintf(out, "report\n");
221 helpmod_config_write_report(out, hchan);
222 }
223 }
224 }
225
226 { /* globals */
227
228 fprintf(out,"\n%% global variables\n");
229 fprintf(out,"%% hstat_cycle\n");
230 fprintf(out,"globals\n");
231 helpmod_config_write_globals(out);
232 }
233
234 fclose(out);
235
236 return 0;
237 }
238
239 int helpmod_config_read_channel(FILE *in)
240 {
241 hchannel *hchan;
242
243 char buf[256],*ptr=(char*)buf;
244 int flags, entries, i;
245 /* name */
246 fgets(buf, 256, in);
247 if (feof(in))
248 return -1;
249 helpmod_line_fix(&ptr);
250
251 hchan = hchannel_add(ptr);
252 /* flags */
253 fgets((ptr = buf), 256, in);
254 if (feof(in))
255 return -1;
256 helpmod_line_fix(&ptr);
257
258 if (sscanf(ptr, "%x", (unsigned int*)&flags) != 1)
259 return -1;
260
261 hchan->flags = flags;
262 /* welcome message */
263 fgets((ptr = buf), 256, in);
264 if (feof(in))
265 return -1;
266 helpmod_line_fix(&ptr);
267
268 strcpy(hchan->welcome, ptr);
269
270 /* lamercontrol profile */
271 fgets((ptr = buf), 256, in);
272 if (feof(in))
273 return -1;
274 helpmod_line_fix(&ptr);
275
276 hchan->lc_profile = hlc_get(ptr);
277
278 /* censor entries for channel, a bit complex */
279 fgets((ptr = buf), 256, in);
280 if (feof(in))
281 return -1;
282
283 helpmod_line_fix(&ptr);
284
285 if (sscanf(ptr, "%d", &entries) != 1)
286 return -1;
287 for (i = 0;i<entries;i++)
288 {
289 char buf2[512], *ptr2;
290
291 fgets((ptr = buf), 256, in);
292 if (feof(in))
293 return -1;
294 helpmod_line_fix(&ptr);
295
296 fgets((ptr2 = buf2), 256, in);
297 if (feof(in))
298 return -1;
299 helpmod_line_fix(&ptr2);
300
301 hcensor_add(&hchan->censor, ptr, ptr2);
302 }
303 /* channel specific hterms */
304 fgets((ptr = buf), 256, in);
305 if (feof(in))
306 return -1;
307
308 helpmod_line_fix(&ptr);
309
310 if (sscanf(ptr, "%d", &entries) != 1)
311 return -1;
312 for (i=0;i<entries;i++)
313 helpmod_config_read_term(in, &hchan->channel_hterms);
314
315 helpmod_config_read_chanstats(in, hchan->stats);
316
317 /* needs to be done here */
318 hchannel_mode_check(hchan);
319
320 return 0;
321 }
322
323 int helpmod_config_write_channel(FILE *out, hchannel *target)
324 {
325 fprintf(out, "\t%s\n", hchannel_get_name(target));
326 fprintf(out, "\t%x\n", target->flags & 0x0FFFFFF);
327 fprintf(out, "\t%s\n", target->welcome);
328 if (target->lc_profile == NULL)
329 fprintf(out, "\t(null)\n");
330 else
331 fprintf(out, "\t%s\n", target->lc_profile->name->content);
332
333 fprintf(out, "\t%d %% censor\n", hcensor_count(target->censor));
334 {
335 hcensor *hcens = target->censor;
336 for (;hcens;hcens = hcens->next)
337 {
338 fprintf(out, "\t\t%s\n", hcens->pattern->content);
339 fprintf(out, "\t\t%s\n", hcens->reason->content);
340 }
341 }
342
343 fprintf(out, "\t%d %% terms\n", hterm_count(target->channel_hterms));
344 {
345 hterm *tmp = target->channel_hterms;
346 for (;tmp;tmp = tmp->next)
347 {
348 helpmod_config_write_term(out, tmp);
349 }
350 }
351
352 helpmod_config_write_chanstats(out, target->stats);
353
354 return 0;
355 }
356
357
358 int helpmod_config_read_account(FILE *in)
359 {
360 haccount *hack;
361 int nstats;
362
363 char buf[256],*ptr=(char*)buf;
364 int flags, level;
365
366 fgets(ptr = buf, 256, in);
367 if (feof(in))
368 return -1;
369 helpmod_line_fix(&ptr);
370
371 hack = haccount_add(ptr, H_PEON);
372
373 fgets(ptr = buf, 256, in);
374 if (feof(in))
375 return -1;
376 helpmod_line_fix(&ptr);
377
378 if (sscanf(ptr, "%x %x", (unsigned int*)&level, (unsigned int*)&flags) != 2)
379 return -1;
380
381 hack->level = level;
382 hack->flags = flags;
383
384 fgets(ptr = buf, 256, in);
385 if (feof(in))
386 return -1;
387 helpmod_line_fix(&ptr);
388
389 if (sscanf(ptr, "%d", &nstats) != 1)
390 return -1;
391
392 while (nstats)
393 {
394 hstat_account *tmp = hack->stats;
395 hack->stats = (hstat_account*)malloc(sizeof(hstat_account));
396 hack->stats->next = tmp;
397 if (helpmod_config_read_stats(in, hack->stats) == -1)
398 return -1;
399 nstats--;
400 }
401
402 return 0;
403 }
404
405 int helpmod_config_write_account(FILE *out, haccount *target)
406 {
407 hstat_account *tmp;
408 fprintf(out, "\t%s\n", target->name->content);
409 fprintf(out, "\t%x\t%x\n", target->level, target->flags);
410
411 fprintf(out, "\t%d %% statistics for this channel\n", hstat_account_count(target->stats));
412 for (tmp = target->stats;tmp;tmp = tmp->next)
413 helpmod_config_write_stats(out, tmp);
414
415 return 0;
416 }
417
418 int helpmod_config_read_ban(FILE *in)
419 {
420 char buf1[512], buf2[512], buf3[512], *ptr1, *ptr2, *ptr3;
421 int tmp;
422
423 fgets(ptr1 = buf1, 256, in);
424 if (feof(in))
425 return -1;
426 helpmod_line_fix(&ptr1);
427
428 fgets(ptr2 = buf2, 256, in);
429 if (feof(in))
430 return -1;
431 helpmod_line_fix(&ptr2);
432
433 fgets(ptr3 = buf3, 256, in);
434 if (feof(in))
435 return -1;
436 helpmod_line_fix(&ptr3);
437
438 if (sscanf(ptr3, "%d", &tmp) != 1)
439 return -1;
440
441 hban_add(ptr1, ptr2, tmp, 0);
442
443 return 0;
444 }
445
446 int helpmod_config_write_ban(FILE *out, hban *hb)
447 {
448 fprintf(out, "\t%s\n", bantostring(hb->real_ban));
449 fprintf(out, "\t%s\n", hb->reason?hb->reason->content:NULL);
450 fprintf(out, "\t%d\n", hb->expiration);
451 return 0;
452 }
453
454 int helpmod_config_read_hlc_profile(FILE *in)
455 {
456 hlc_profile *hlc_prof;
457 int tmp[3];
458 float tmp_float;
459 char buf[256],*ptr=(char*)buf;
460
461 /* name */
462 fgets(ptr = buf, 256, in);
463 if (feof(in))
464 return -1;
465 helpmod_line_fix(&ptr);
466
467 if ((hlc_prof = hlc_add(ptr)) == NULL)
468 return -1;
469
470 /* caps */
471 fgets(ptr = buf, 256, in);
472 if (feof(in))
473 return -1;
474 helpmod_line_fix(&ptr);
475
476 if (sscanf(ptr, "%d %d", &tmp[0], &tmp[1]) != 2)
477 return -1;
478
479 hlc_prof->caps_max_percentage = tmp[0];
480 hlc_prof->caps_min_count = tmp[1];
481
482 /* repeating */
483 fgets(ptr = buf, 256, in);
484 if (feof(in))
485 return -1;
486 helpmod_line_fix(&ptr);
487
488 if (sscanf(ptr, "%d %d", &tmp[0], &tmp[1]) != 2)
489 return -1;
490
491 hlc_prof->repeats_max_count = tmp[0];
492 hlc_prof->repeats_min_length = tmp[1];
493
494 /* character / symbol repeat */
495 fgets(ptr = buf, 256, in);
496 if (feof(in))
497 return -1;
498 helpmod_line_fix(&ptr);
499
500 if (sscanf(ptr, "%d %d %d", &tmp[0], &tmp[1], &tmp[2]) != 3)
501 return -1;
502
503 hlc_prof->symbol_repeat_max_count = tmp[0];
504 hlc_prof->character_repeat_max_count = tmp[1];
505 hlc_prof->symbol_max_count = tmp[1];
506
507 /* flood, spam const, spam */
508 fgets(ptr = buf, 256, in);
509 if (feof(in))
510 return -1;
511 helpmod_line_fix(&ptr);
512
513 if (sscanf(ptr, "%d %f %d", &tmp[0], &tmp_float, &tmp[1]) != 3)
514 return -1;
515
516 hlc_prof->tolerance_flood = tmp[0];
517 hlc_prof->constant_spam = tmp_float;
518 hlc_prof->tolerance_spam = tmp[1];
519
520 /* tolerances */
521 fgets(ptr = buf, 256, in);
522 if (feof(in))
523 return -1;
524 helpmod_line_fix(&ptr);
525
526 if (sscanf(ptr, "%d %d %d", &tmp[0], &tmp[1], &tmp[2]) != 3)
527 return -1;
528
529 hlc_prof->tolerance_warn = tmp[0];
530 hlc_prof->tolerance_kick = tmp[1];
531 hlc_prof->tolerance_remove = tmp[2];
532
533 return 0;
534 }
535
536 /* we use extended buffers here */
537 int helpmod_config_read_term(FILE *in, hterm** target)
538 {
539 char buf1[2048], buf2[2048], *ptr1 = buf1, *ptr2 = buf2;
540
541 /* name */
542 fgets(buf1, 2048, in);
543 if (feof(in))
544 return -1;
545 helpmod_line_fix(&ptr1);
546
547 /* description */
548 fgets(buf2, 2048, in);
549 if (feof(in))
550 return -1;
551 helpmod_line_fix(&ptr2);
552
553 /* add to public domain */
554 hterm_add(target, ptr1, ptr2);
555
556 return 0;
557 }
558
559 int helpmod_config_write_term(FILE *out, hterm *htrm)
560 {
561 fprintf(out, "\t%s\n", htrm->name->content);
562 fprintf(out, "\t%s\n", htrm->description->content);
563 return 0;
564 }
565
566 int helpmod_config_write_hlc_profile(FILE *out, hlc_profile *hlc_prof)
567 {
568 fprintf(out, "\t%s\n", hlc_prof->name->content);
569 fprintf(out, "\t%d %d\n", hlc_prof->caps_max_percentage, hlc_prof->caps_min_count);
570 fprintf(out, "\t%d %d\n", hlc_prof->repeats_max_count, hlc_prof->repeats_min_length);
571 fprintf(out, "\t%d %d %d\n", hlc_prof->symbol_repeat_max_count, hlc_prof->character_repeat_max_count, hlc_prof->symbol_max_count);
572 fprintf(out, "\t%d %.3f %d\n", hlc_prof->tolerance_flood, hlc_prof->constant_spam, hlc_prof->tolerance_spam);
573 fprintf(out, "\t%d %d %d\n", hlc_prof->tolerance_warn, hlc_prof->tolerance_kick, hlc_prof->tolerance_remove);
574 return 0;
575 }
576
577 int helpmod_config_read_chanstats(FILE *in, hstat_channel *hs_chan)
578 {
579 char buf[256],*ptr=(char*)buf;
580 int i;
581 hstat_channel_entry *entry;
582
583 for (i=0;i<17;i++)
584 {
585 fgets(ptr = buf, 256, in);
586 if (feof(in))
587 return -1;
588 helpmod_line_fix(&ptr);
589
590 if (i < 7) /* days */
591 entry = &hs_chan->week[(hstat_day() + i) % 7];
592 else /* weeks */
593 entry = &hs_chan->longterm[(hstat_week() + (i-7)) % 7];
594
595 if (sscanf(buf, "%d %d %d %d %d %d", &entry->time_spent, &entry->prime_time_spent, &entry->joins, &entry->queue_use, &entry->lines, &entry->words) != 6)
596 return -1;
597 }
598 return 0;
599 }
600
601 int helpmod_config_write_chanstats(FILE *out, hstat_channel *hs_chan)
602 {
603 int i;
604 hstat_channel_entry *entry;
605
606 for (i=0;i<17;i++)
607 {
608 if (i < 7) /* days */
609 entry = &hs_chan->week[(hstat_day() + i) % 7];
610 else /* weeks */
611 entry = &hs_chan->longterm[(hstat_week() + (i-7)) % 7];
612
613 fprintf(out, "\t%d %d %d %d %d %d\n", entry->time_spent, entry->prime_time_spent, entry->joins, entry->queue_use, entry->lines, entry->words);
614 }
615 return 0;
616 }
617
618 int helpmod_config_read_stats(FILE *in, hstat_account *hs_acc)
619 {
620 char buf[256],*ptr=(char*)buf;
621 int i;
622 hstat_account_entry *entry;
623
624 fgets(ptr = buf, 256, in);
625 if (feof(in))
626 return -1;
627 helpmod_line_fix(&ptr);
628
629 hs_acc->hchan = hchannel_get_by_name(ptr);
630
631 if (hs_acc->hchan == NULL)
632 return -1;
633
634 for (i=0;i<17;i++)
635 {
636 fgets(ptr = buf, 256, in);
637 if (feof(in))
638 return -1;
639 helpmod_line_fix(&ptr);
640
641 if (i < 7) /* days */
642 entry = &hs_acc->week[(hstat_day() + i) % 7];
643 else /* weeks */
644 entry = &hs_acc->longterm[(hstat_week() + (i-7)) % 7];
645
646 if (sscanf(buf, "%d %d %d %d", &entry->time_spent, &entry->prime_time_spent, &entry->lines, &entry->words) != 4)
647 return -1;
648 }
649 return 0;
650 }
651
652 int helpmod_config_write_stats(FILE *out, hstat_account *hs_acc)
653 {
654 int i;
655 hstat_account_entry *entry;
656
657 fprintf(out, "\t%s\n", hchannel_get_name(hs_acc->hchan));
658
659 for (i=0;i<17;i++)
660 {
661 if (i < 7) /* days */
662 entry = &hs_acc->week[(hstat_day() + i) % 7];
663 else /* weeks */
664 entry = &hs_acc->longterm[(hstat_week() + (i-7)) % 7];
665
666 fprintf(out, "\t%d %d %d %d\n", entry->time_spent, entry->prime_time_spent, entry->lines, entry->words);
667 }
668 return 0;
669 }
670
671 int helpmod_config_read_globals(FILE *in)
672 {
673 char buf[256],*ptr=(char*)buf;
674
675 fgets(ptr = buf, 256, in);
676 if (feof(in))
677 return -1;
678 helpmod_line_fix(&ptr);
679
680 if (sscanf(ptr, "%d", &hstat_cycle) != 1)
681 return -1;
682
683 return 0;
684 }
685
686 int helpmod_config_write_globals(FILE *out)
687 {
688 fprintf(out, "\t%d\n", hstat_cycle);
689 return 0;
690 }
691
692 int helpmod_config_read_report(FILE *in)
693 {
694 char buf[256], *ptr=(char*)buf;
695 hchannel *tmp1, *tmp2;
696
697 fgets(ptr = buf, 256, in);
698 if (feof(in))
699 return -1;
700 helpmod_line_fix(&ptr);
701
702 tmp1 = hchannel_get_by_name(ptr);
703
704 fgets(ptr = buf, 256, in);
705 if (feof(in))
706 return -1;
707 helpmod_line_fix(&ptr);
708
709 tmp2 = hchannel_get_by_name(ptr);
710
711 if (hchannel_is_valid(tmp1) && hchannel_is_valid(tmp2))
712 tmp1->report_to = tmp2;
713
714 return 0;
715 }
716
717 int helpmod_config_write_report(FILE *out, hchannel *hchan)
718 {
719 fprintf(out, "\t%s\n", hchannel_get_name(hchan));
720 fprintf(out, "\t%s\n", hchannel_get_name(hchan->report_to));
721 return 0;
722 }
723
724 int helpmod_config_read_ticket(FILE *in)
725 {
726 char buf[256], buf2[256], buf3[64], *ptr=(char*)buf;
727 unsigned int tmp;
728
729 fgets(ptr = buf, 256, in);
730 if (feof(in))
731 return -1;
732 helpmod_line_fix(&ptr);
733
734 strcpy(buf2, ptr);
735
736 fgets(ptr = buf, 64, in);
737 if (feof(in))
738 return -1;
739 helpmod_line_fix(&ptr);
740
741 strcpy(buf3, ptr);
742
743 fgets(ptr = buf, 256, in);
744 if (feof(in))
745 return -1;
746 helpmod_line_fix(&ptr);
747
748 if (!sscanf(ptr, "%u", &tmp))
749 return -1;
750
751 if (tmp > time(NULL))
752 hticket_add(buf3, tmp, hchannel_get_by_name(buf2));
753
754 return 0;
755 }
756
757 int helpmod_config_write_ticket(FILE *out, hticket *htick, hchannel *hchan)
758 {
759 fprintf(out, "\t%s\n", hchannel_get_name(hchan));
760 fprintf(out, "\t%s\n", htick->authname);
761 fprintf(out, "\t%u\n", htick->time_expiration);
762
763 return 0;
764 }
765
766 void helpmod_config_scheduled_events(void)
767 {
768 rename(HELPMOD_DEFAULT_DB, HELPMOD_DEFAULT_DB".old");
769 helpmod_config_write(HELPMOD_DEFAULT_DB);
770 }