13 static void helpmod_line_fix(char **ptr
)
15 while (isspace(**ptr
))
18 if (strchr(*ptr
, '\n') != NULL
)
19 *strchr(*ptr
, '\n') = '\0';
21 if (strchr(*ptr
, '\r') != NULL
)
22 *strchr(*ptr
, '\r') = '\0';
26 int helpmod_config_read(const char *fname
)
31 if ((in
= fopen(fname
,"rt")) == NULL
)
42 helpmod_line_fix(&ptr
);
44 /* support comments */
50 /* check what kind of a line it was */
51 if (!strcmp(ptr
, "channel"))
53 if (helpmod_config_read_channel(in
))
54 Error("helpmod", ERR_WARNING
, "Reading of database entry 'channel' failed");
56 else if (!strcmp(ptr
, "account"))
58 if (helpmod_config_read_account(in
))
59 Error("helpmod", ERR_WARNING
, "Reading of database entry 'account' failed");
61 else if (!strcmp(ptr
, "ban"))
63 if (helpmod_config_read_ban(in
))
64 Error("helpmod", ERR_WARNING
, "Reading of database entry 'ban' failed");
66 else if (!strcmp(ptr
, "lamercontrol profile"))
68 if (helpmod_config_read_hlc_profile(in
))
69 Error("helpmod", ERR_WARNING
, "Reading of database entry 'lamercontrol_profile' failed");
71 else if (!strcmp(ptr
, "term"))
73 if (helpmod_config_read_term(in
, NULL
))
74 Error("helpmod", ERR_WARNING
, "Reading of database entry 'term' failed");
76 else if (!strcmp(ptr
, "globals"))
78 if (helpmod_config_read_globals(in
))
79 Error("helpmod", ERR_WARNING
, "Reading of database entry 'globals' failed");
81 else if (!strcmp(ptr
, "report"))
83 if (helpmod_config_read_report(in
))
84 Error("helpmod", ERR_WARNING
, "Reading of database entry 'report' failed");
86 else if (!strcmp(ptr
, "ticket"))
88 if (helpmod_config_read_ticket(in
))
89 Error("helpmod", ERR_WARNING
, "Reading of database entry 'ticket' failed");
92 Error("helpmod", ERR_WARNING
, "Unknown database entry '%s'", ptr
);
95 hchannels_match_accounts();
101 int helpmod_config_write(const char *fname
)
105 time_t timer
= time(NULL
);
107 if ((out
= fopen(fname
,"wt")) == NULL
)
110 fprintf(out
, "%% H2 version %s database\n", HELPMOD_VERSION
);
111 fprintf(out
, "%% %s\n\n", asctime(localtime(&timer
)));
114 { /* lamercontrol profiles */
115 hlc_profile
*ptr
= hlc_profiles
;
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");
122 for(;ptr
;ptr
=ptr
->next
)
124 fprintf(out
, "lamercontrol profile\n");
125 helpmod_config_write_hlc_profile(out
, ptr
);
130 hchannel
*ptr
= hchannels
;
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");
138 for(;ptr
;ptr
=ptr
->next
)
140 fprintf(out
, "channel\n");
141 helpmod_config_write_channel(out
, ptr
);
148 haccount
*ptr
= haccounts
;
150 fprintf(out
,"\n%% account structure:\n");
151 fprintf(out
,"%% name (string):\n");
152 fprintf(out
,"%% level (integer):\n");
154 for(;ptr
;ptr
=ptr
->next
)
156 fprintf(out
, "account\n");
157 helpmod_config_write_account(out
, ptr
);
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");
171 for(;ptr
;ptr
=ptr
->next
)
173 fprintf(out
, "ban\n");
174 helpmod_config_write_ban(out
, ptr
);
181 fprintf(out
,"\n%% term structure:\n");
182 fprintf(out
,"%% name (string):\n");
183 fprintf(out
,"%% description (string):\n");
185 for(;ptr
;ptr
=ptr
->next
)
187 fprintf(out
, "term\n");
188 helpmod_config_write_term(out
, ptr
);
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");
201 for (hchan
= hchannels
;hchan
;hchan
= hchan
->next
)
202 for (htick
= hchan
->htickets
;htick
;htick
= htick
->next
)
204 fprintf(out
, "ticket\n");
205 helpmod_config_write_ticket(out
, htick
, hchan
);
212 fprintf(out
, "\n%% report structure:\n");
213 fprintf(out
, "%% channel reported\n");
214 fprintf(out
, "%% channel reported to\n");
216 for (hchan
= hchannels
;hchan
;hchan
= hchan
->next
)
218 if ((hchan
->flags
& H_REPORT
) && hchannel_is_valid(hchan
->report_to
))
220 fprintf(out
, "report\n");
221 helpmod_config_write_report(out
, hchan
);
228 fprintf(out
,"\n%% global variables\n");
229 fprintf(out
,"%% hstat_cycle\n");
230 fprintf(out
,"globals\n");
231 helpmod_config_write_globals(out
);
239 int helpmod_config_read_channel(FILE *in
)
243 char buf
[256],*ptr
=(char*)buf
;
244 int flags
, entries
, i
;
249 helpmod_line_fix(&ptr
);
251 hchan
= hchannel_add(ptr
);
253 fgets((ptr
= buf
), 256, in
);
256 helpmod_line_fix(&ptr
);
258 if (sscanf(ptr
, "%x", (unsigned int*)&flags
) != 1)
261 hchan
->flags
= flags
;
262 /* welcome message */
263 fgets((ptr
= buf
), 256, in
);
266 helpmod_line_fix(&ptr
);
268 strcpy(hchan
->welcome
, ptr
);
270 /* lamercontrol profile */
271 fgets((ptr
= buf
), 256, in
);
274 helpmod_line_fix(&ptr
);
276 hchan
->lc_profile
= hlc_get(ptr
);
278 /* censor entries for channel, a bit complex */
279 fgets((ptr
= buf
), 256, in
);
283 helpmod_line_fix(&ptr
);
285 if (sscanf(ptr
, "%d", &entries
) != 1)
287 for (i
= 0;i
<entries
;i
++)
289 char buf2
[512], *ptr2
;
291 fgets((ptr
= buf
), 256, in
);
294 helpmod_line_fix(&ptr
);
296 fgets((ptr2
= buf2
), 256, in
);
299 helpmod_line_fix(&ptr2
);
301 hcensor_add(&hchan
->censor
, ptr
, ptr2
);
303 /* channel specific hterms */
304 fgets((ptr
= buf
), 256, in
);
308 helpmod_line_fix(&ptr
);
310 if (sscanf(ptr
, "%d", &entries
) != 1)
312 for (i
=0;i
<entries
;i
++)
313 helpmod_config_read_term(in
, &hchan
->channel_hterms
);
315 helpmod_config_read_chanstats(in
, hchan
->stats
);
317 /* needs to be done here */
318 hchannel_mode_check(hchan
);
323 int helpmod_config_write_channel(FILE *out
, hchannel
*target
)
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");
331 fprintf(out
, "\t%s\n", target
->lc_profile
->name
->content
);
333 fprintf(out
, "\t%d %% censor\n", hcensor_count(target
->censor
));
335 hcensor
*hcens
= target
->censor
;
336 for (;hcens
;hcens
= hcens
->next
)
338 fprintf(out
, "\t\t%s\n", hcens
->pattern
->content
);
339 fprintf(out
, "\t\t%s\n", hcens
->reason
->content
);
343 fprintf(out
, "\t%d %% terms\n", hterm_count(target
->channel_hterms
));
345 hterm
*tmp
= target
->channel_hterms
;
346 for (;tmp
;tmp
= tmp
->next
)
348 helpmod_config_write_term(out
, tmp
);
352 helpmod_config_write_chanstats(out
, target
->stats
);
358 int helpmod_config_read_account(FILE *in
)
363 char buf
[256],*ptr
=(char*)buf
;
366 fgets(ptr
= buf
, 256, in
);
369 helpmod_line_fix(&ptr
);
371 hack
= haccount_add(ptr
, H_PEON
);
373 fgets(ptr
= buf
, 256, in
);
376 helpmod_line_fix(&ptr
);
378 if (sscanf(ptr
, "%x %x", (unsigned int*)&level
, (unsigned int*)&flags
) != 2)
384 fgets(ptr
= buf
, 256, in
);
387 helpmod_line_fix(&ptr
);
389 if (sscanf(ptr
, "%d", &nstats
) != 1)
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)
405 int helpmod_config_write_account(FILE *out
, haccount
*target
)
408 fprintf(out
, "\t%s\n", target
->name
->content
);
409 fprintf(out
, "\t%x\t%x\n", target
->level
, target
->flags
);
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
);
418 int helpmod_config_read_ban(FILE *in
)
420 char buf1
[512], buf2
[512], buf3
[512], *ptr1
, *ptr2
, *ptr3
;
423 fgets(ptr1
= buf1
, 256, in
);
426 helpmod_line_fix(&ptr1
);
428 fgets(ptr2
= buf2
, 256, in
);
431 helpmod_line_fix(&ptr2
);
433 fgets(ptr3
= buf3
, 256, in
);
436 helpmod_line_fix(&ptr3
);
438 if (sscanf(ptr3
, "%d", &tmp
) != 1)
441 hban_add(ptr1
, ptr2
, tmp
, 0);
446 int helpmod_config_write_ban(FILE *out
, hban
*hb
)
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%u\n", hb
->expiration
);
454 int helpmod_config_read_hlc_profile(FILE *in
)
456 hlc_profile
*hlc_prof
;
459 char buf
[256],*ptr
=(char*)buf
;
462 fgets(ptr
= buf
, 256, in
);
465 helpmod_line_fix(&ptr
);
467 if ((hlc_prof
= hlc_add(ptr
)) == NULL
)
471 fgets(ptr
= buf
, 256, in
);
474 helpmod_line_fix(&ptr
);
476 if (sscanf(ptr
, "%d %d", &tmp
[0], &tmp
[1]) != 2)
479 hlc_prof
->caps_max_percentage
= tmp
[0];
480 hlc_prof
->caps_min_count
= tmp
[1];
483 fgets(ptr
= buf
, 256, in
);
486 helpmod_line_fix(&ptr
);
488 if (sscanf(ptr
, "%d %d", &tmp
[0], &tmp
[1]) != 2)
491 hlc_prof
->repeats_max_count
= tmp
[0];
492 hlc_prof
->repeats_min_length
= tmp
[1];
494 /* character / symbol repeat */
495 fgets(ptr
= buf
, 256, in
);
498 helpmod_line_fix(&ptr
);
500 if (sscanf(ptr
, "%d %d %d", &tmp
[0], &tmp
[1], &tmp
[2]) != 3)
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];
507 /* flood, spam const, spam */
508 fgets(ptr
= buf
, 256, in
);
511 helpmod_line_fix(&ptr
);
513 if (sscanf(ptr
, "%d %f %d", &tmp
[0], &tmp_float
, &tmp
[1]) != 3)
516 hlc_prof
->tolerance_flood
= tmp
[0];
517 hlc_prof
->constant_spam
= tmp_float
;
518 hlc_prof
->tolerance_spam
= tmp
[1];
521 fgets(ptr
= buf
, 256, in
);
524 helpmod_line_fix(&ptr
);
526 if (sscanf(ptr
, "%d %d %d", &tmp
[0], &tmp
[1], &tmp
[2]) != 3)
529 hlc_prof
->tolerance_warn
= tmp
[0];
530 hlc_prof
->tolerance_kick
= tmp
[1];
531 hlc_prof
->tolerance_remove
= tmp
[2];
536 /* we use extended buffers here */
537 int helpmod_config_read_term(FILE *in
, hterm
** target
)
539 char buf1
[2048], buf2
[2048], *ptr1
= buf1
, *ptr2
= buf2
;
542 fgets(buf1
, 2048, in
);
545 helpmod_line_fix(&ptr1
);
548 fgets(buf2
, 2048, in
);
551 helpmod_line_fix(&ptr2
);
553 /* add to public domain */
554 hterm_add(target
, ptr1
, ptr2
);
559 int helpmod_config_write_term(FILE *out
, hterm
*htrm
)
561 fprintf(out
, "\t%s\n", htrm
->name
->content
);
562 fprintf(out
, "\t%s\n", htrm
->description
->content
);
566 int helpmod_config_write_hlc_profile(FILE *out
, hlc_profile
*hlc_prof
)
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
);
577 int helpmod_config_read_chanstats(FILE *in
, hstat_channel
*hs_chan
)
579 char buf
[256],*ptr
=(char*)buf
;
581 hstat_channel_entry
*entry
;
585 fgets(ptr
= buf
, 256, in
);
588 helpmod_line_fix(&ptr
);
590 if (i
< 7) /* days */
591 entry
= &hs_chan
->week
[(hstat_day() + i
) % 7];
593 entry
= &hs_chan
->longterm
[(hstat_week() + (i
-7)) % 10];
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)
601 int helpmod_config_write_chanstats(FILE *out
, hstat_channel
*hs_chan
)
604 hstat_channel_entry
*entry
;
608 if (i
< 7) /* days */
609 entry
= &hs_chan
->week
[(hstat_day() + i
) % 7];
611 entry
= &hs_chan
->longterm
[(hstat_week() + (i
-7)) % 10];
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
);
618 int helpmod_config_read_stats(FILE *in
, hstat_account
*hs_acc
)
620 char buf
[256],*ptr
=(char*)buf
;
622 hstat_account_entry
*entry
;
624 fgets(ptr
= buf
, 256, in
);
627 helpmod_line_fix(&ptr
);
629 hs_acc
->hchan
= hchannel_get_by_name(ptr
);
631 if (hs_acc
->hchan
== NULL
)
636 fgets(ptr
= buf
, 256, in
);
639 helpmod_line_fix(&ptr
);
641 if (i
< 7) /* days */
642 entry
= &hs_acc
->week
[(hstat_day() + i
) % 7];
644 entry
= &hs_acc
->longterm
[(hstat_week() + (i
-7)) % 10];
646 if (sscanf(buf
, "%d %d %d %d", &entry
->time_spent
, &entry
->prime_time_spent
, &entry
->lines
, &entry
->words
) != 4)
652 int helpmod_config_write_stats(FILE *out
, hstat_account
*hs_acc
)
655 hstat_account_entry
*entry
;
657 fprintf(out
, "\t%s\n", hchannel_get_name(hs_acc
->hchan
));
661 if (i
< 7) /* days */
662 entry
= &hs_acc
->week
[(hstat_day() + i
) % 7];
664 entry
= &hs_acc
->longterm
[(hstat_week() + (i
-7)) % 10];
666 fprintf(out
, "\t%d %d %d %d\n", entry
->time_spent
, entry
->prime_time_spent
, entry
->lines
, entry
->words
);
671 int helpmod_config_read_globals(FILE *in
)
673 char buf
[256],*ptr
=(char*)buf
;
675 fgets(ptr
= buf
, 256, in
);
678 helpmod_line_fix(&ptr
);
680 if (sscanf(ptr
, "%d", &hstat_cycle
) != 1)
686 int helpmod_config_write_globals(FILE *out
)
688 fprintf(out
, "\t%d\n", hstat_cycle
);
692 int helpmod_config_read_report(FILE *in
)
694 char buf
[256], *ptr
=(char*)buf
;
695 hchannel
*tmp1
, *tmp2
;
697 fgets(ptr
= buf
, 256, in
);
700 helpmod_line_fix(&ptr
);
702 tmp1
= hchannel_get_by_name(ptr
);
704 fgets(ptr
= buf
, 256, in
);
707 helpmod_line_fix(&ptr
);
709 tmp2
= hchannel_get_by_name(ptr
);
711 if (hchannel_is_valid(tmp1
) && hchannel_is_valid(tmp2
))
712 tmp1
->report_to
= tmp2
;
717 int helpmod_config_write_report(FILE *out
, hchannel
*hchan
)
719 fprintf(out
, "\t%s\n", hchannel_get_name(hchan
));
720 fprintf(out
, "\t%s\n", hchannel_get_name(hchan
->report_to
));
724 int helpmod_config_read_ticket(FILE *in
)
726 char buf
[256], buf2
[256], buf3
[64], *ptr
=(char*)buf
;
729 fgets(ptr
= buf
, 256, in
);
732 helpmod_line_fix(&ptr
);
736 fgets(ptr
= buf
, 64, in
);
739 helpmod_line_fix(&ptr
);
743 fgets(ptr
= buf
, 256, in
);
746 helpmod_line_fix(&ptr
);
748 if (!sscanf(ptr
, "%u", &tmp
))
751 if (tmp
> time(NULL
))
752 hticket_add(buf3
, tmp
, hchannel_get_by_name(buf2
));
757 int helpmod_config_write_ticket(FILE *out
, hticket
*htick
, hchannel
*hchan
)
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
);
766 void helpmod_config_scheduled_events(void)
768 rename(HELPMOD_DEFAULT_DB
, HELPMOD_DEFAULT_DB
".old");
769 helpmod_config_write(HELPMOD_DEFAULT_DB
);