11 static int hconf_version
;
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 /* Assume G 2.0 configuration */
32 hconf_version
= HELPMOD_VERSION_2_0
;
34 if ((in
= fopen(fname
,"rt")) == NULL
)
45 helpmod_line_fix(&ptr
);
47 /* support comments */
53 /* check what kind of a line it was */
54 if (!strcmp(ptr
, "version"))
55 { /* If no version is present, then the assumed G 2.0 version is used */
56 if (helpmod_config_read_version(in
))
57 Error("helpmod", ERR_WARNING
, "Reading of database entry 'version' failed");
59 if (hconf_version
> HELPMOD_VERSION_INTERNAL
)
60 Error("helpmod", ERR_WARNING
, "Database version is higher than the current version");
62 else if (!strcmp(ptr
, "channel"))
64 if (helpmod_config_read_channel(in
))
65 Error("helpmod", ERR_WARNING
, "Reading of database entry 'channel' failed");
67 else if (!strcmp(ptr
, "account"))
69 if (helpmod_config_read_account(in
))
70 Error("helpmod", ERR_WARNING
, "Reading of database entry 'account' failed");
72 else if (!strcmp(ptr
, "ban"))
74 if (helpmod_config_read_ban(in
))
75 Error("helpmod", ERR_WARNING
, "Reading of database entry 'ban' failed");
77 else if (!strcmp(ptr
, "lamercontrol profile"))
79 if (helpmod_config_read_hlc_profile(in
))
80 Error("helpmod", ERR_WARNING
, "Reading of database entry 'lamercontrol_profile' failed");
82 else if (!strcmp(ptr
, "term"))
84 if (helpmod_config_read_term(in
, NULL
))
85 Error("helpmod", ERR_WARNING
, "Reading of database entry 'term' failed");
87 else if (!strcmp(ptr
, "globals"))
89 if (helpmod_config_read_globals(in
))
90 Error("helpmod", ERR_WARNING
, "Reading of database entry 'globals' failed");
92 else if (!strcmp(ptr
, "report"))
94 if (helpmod_config_read_report(in
))
95 Error("helpmod", ERR_WARNING
, "Reading of database entry 'report' failed");
97 else if (!strcmp(ptr
, "ticket"))
99 if (helpmod_config_read_ticket(in
))
100 Error("helpmod", ERR_WARNING
, "Reading of database entry 'ticket' failed");
103 Error("helpmod", ERR_WARNING
, "Unknown database entry '%s'", ptr
);
106 hchannels_match_accounts();
112 int helpmod_config_write(const char *fname
)
115 time_t timer
= time(NULL
);
117 if ((out
= fopen(fname
,"wt")) == NULL
)
120 fprintf(out
, "%% G2 version %s database\n", HELPMOD_VERSION
);
121 fprintf(out
, "%% %s\n\n", asctime(localtime(&timer
)));
125 fprintf(out
, "%% G internal version\n");
126 fprintf(out
, "version\n");
127 helpmod_config_write_version(out
);
132 fprintf(out
,"\n%% global variables\n");
133 fprintf(out
,"%% hstat_cycle\n");
134 fprintf(out
,"globals\n");
135 helpmod_config_write_globals(out
);
138 { /* lamercontrol profiles */
139 hlc_profile
*ptr
= hlc_profiles
;
141 fprintf(out
,"%% lamercontrol profile structure:\n");
142 fprintf(out
,"%% X (string):\n");
143 fprintf(out
,"%% Y (string):\n");
144 fprintf(out
,"%% Z (int):\n");
146 for(;ptr
;ptr
=ptr
->next
)
148 fprintf(out
, "lamercontrol profile\n");
149 helpmod_config_write_hlc_profile(out
, ptr
);
154 hchannel
*ptr
= hchannels
;
156 fprintf(out
,"\n%% channel structure:\n");
157 fprintf(out
,"%% name (string):\n");
158 fprintf(out
,"%% flags (integer):\n");
159 fprintf(out
,"%% welcome message (string):\n");
160 fprintf(out
,"%% lamercontrol profile (string):\n");
162 for(;ptr
;ptr
=ptr
->next
)
164 fprintf(out
, "channel\n");
165 helpmod_config_write_channel(out
, ptr
);
172 haccount
*ptr
= haccounts
;
174 fprintf(out
,"\n%% account structure:\n");
175 fprintf(out
,"%% name (string):\n");
176 fprintf(out
,"%% level (integer) flags (integer) last_activity (integer):\n");
178 for(;ptr
;ptr
=ptr
->next
)
180 fprintf(out
, "account\n");
181 helpmod_config_write_account(out
, ptr
);
190 fprintf(out
,"\n%% ban structure:\n");
191 fprintf(out
,"%% banmask (string):\n");
192 fprintf(out
,"%% reason (string):\n");
193 fprintf(out
,"%% expiration (int):\n");
195 for(;ptr
;ptr
=ptr
->next
)
197 fprintf(out
, "ban\n");
198 helpmod_config_write_ban(out
, ptr
);
205 fprintf(out
,"\n%% term structure:\n");
206 fprintf(out
,"%% name (string):\n");
207 fprintf(out
,"%% description (string):\n");
209 for(;ptr
;ptr
=ptr
->next
)
211 fprintf(out
, "term\n");
212 helpmod_config_write_term(out
, ptr
);
220 fprintf(out
, "\n%% ticket structure:\n");
221 fprintf(out
, "%% channel (string)\n");
222 fprintf(out
, "%% authname (string)\n");
223 fprintf(out
, "%% expiration time (int)\n");
225 for (hchan
= hchannels
;hchan
;hchan
= hchan
->next
)
226 for (htick
= hchan
->htickets
;htick
;htick
= htick
->next
)
228 fprintf(out
, "ticket\n");
229 helpmod_config_write_ticket(out
, htick
, hchan
);
236 fprintf(out
, "\n%% report structure:\n");
237 fprintf(out
, "%% channel reported\n");
238 fprintf(out
, "%% channel reported to\n");
240 for (hchan
= hchannels
;hchan
;hchan
= hchan
->next
)
242 if ((hchan
->flags
& H_REPORT
) && hchannel_is_valid(hchan
->report_to
))
244 fprintf(out
, "report\n");
245 helpmod_config_write_report(out
, hchan
);
255 int helpmod_config_read_channel(FILE *in
)
259 char buf
[256],*ptr
=(char*)buf
;
260 int flags
, entries
, idlekick
, i
;
265 helpmod_line_fix(&ptr
);
267 hchan
= hchannel_add(ptr
);
269 fgets((ptr
= buf
), 256, in
);
272 helpmod_line_fix(&ptr
);
274 if (sscanf(ptr
, "%x", (unsigned int*)&flags
) != 1)
277 hchan
->flags
= flags
;
278 /* welcome message */
279 fgets((ptr
= buf
), 256, in
);
282 helpmod_line_fix(&ptr
);
284 strcpy(hchan
->welcome
, ptr
);
286 /* lamercontrol profile */
287 fgets((ptr
= buf
), 256, in
);
290 helpmod_line_fix(&ptr
);
292 hchan
->lc_profile
= hlc_get(ptr
);
294 if (hconf_version
>= HELPMOD_VERSION_2_11
)
296 fgets((ptr
= buf
), 256, in
);
299 helpmod_line_fix(&ptr
);
301 if (sscanf(ptr
, "%d", &idlekick
) != 1)
304 hchan
->max_idle
= idlekick
;
306 fgets((ptr
= buf
), 256, in
);
309 helpmod_line_fix(&ptr
);
311 hchan
->ticket_message
= getsstring(ptr
,strlen(ptr
));
314 /* censor entries for channel, a bit complex */
315 fgets((ptr
= buf
), 256, in
);
319 helpmod_line_fix(&ptr
);
321 if (sscanf(ptr
, "%d", &entries
) != 1)
323 for (i
= 0;i
<entries
;i
++)
325 char buf2
[512], *ptr2
;
328 fgets((ptr
= buf
), 256, in
);
331 helpmod_line_fix(&ptr
);
333 if (hconf_version
>= HELPMOD_VERSION_2_10
)
335 if (!sscanf(ptr
, "%d", &type
))
338 fgets((ptr
= buf
), 256, in
);
341 helpmod_line_fix(&ptr
);
346 fgets((ptr2
= buf2
), 256, in
);
349 helpmod_line_fix(&ptr2
);
354 hcensor_add(&hchan
->censor
, ptr
, ptr2
, type
);
356 /* channel specific hterms */
357 fgets((ptr
= buf
), 256, in
);
361 helpmod_line_fix(&ptr
);
363 if (sscanf(ptr
, "%d", &entries
) != 1)
365 for (i
=0;i
<entries
;i
++)
366 helpmod_config_read_term(in
, &hchan
->channel_hterms
);
368 helpmod_config_read_chanstats(in
, hchan
->stats
);
370 /* needs to be done here */
371 hchannel_mode_check(hchan
);
376 int helpmod_config_write_channel(FILE *out
, hchannel
*target
)
378 fprintf(out
, "\t%s\n", hchannel_get_name(target
));
379 fprintf(out
, "\t%x\n", target
->flags
& 0x0FFFFFF);
380 fprintf(out
, "\t%s\n", target
->welcome
);
381 if (target
->lc_profile
== NULL
)
382 fprintf(out
, "\t(null)\n");
384 fprintf(out
, "\t%s\n", target
->lc_profile
->name
->content
);
386 fprintf(out
, "\t%d\n", target
->max_idle
);
388 if (target
->ticket_message
== NULL
)
389 fprintf(out
, "\t(null)\n");
391 fprintf(out
, "\t%s\n", target
->ticket_message
->content
);
393 fprintf(out
, "\t%d %% censor\n", hcensor_count(target
->censor
));
395 hcensor
*hcens
= target
->censor
;
396 for (;hcens
;hcens
= hcens
->next
)
398 fprintf(out
, "\t\t%d\n", hcens
->type
);
399 fprintf(out
, "\t\t%s\n", hcens
->pattern
->content
);
400 fprintf(out
, "\t\t%s\n", hcens
->reason
?hcens
->reason
->content
:"");
404 fprintf(out
, "\t%d %% terms\n", hterm_count(target
->channel_hterms
));
406 hterm
*tmp
= target
->channel_hterms
;
407 for (;tmp
;tmp
= tmp
->next
)
409 helpmod_config_write_term(out
, tmp
);
413 helpmod_config_write_chanstats(out
, target
->stats
);
419 int helpmod_config_read_account(FILE *in
)
424 char buf
[256],*ptr
=(char*)buf
;
425 int flags
, level
, last_activity
= time(NULL
);
427 fgets(ptr
= buf
, 256, in
);
430 helpmod_line_fix(&ptr
);
432 hack
= haccount_add(ptr
, H_PEON
);
434 fgets(ptr
= buf
, 256, in
);
437 helpmod_line_fix(&ptr
);
438 if (hconf_version
< HELPMOD_VERSION_2_11
)
440 if (sscanf(ptr
, "%x %x", (unsigned int*)&level
, (unsigned int*)&flags
) != 2)
444 if (sscanf(ptr
, "%x %x %x", (unsigned int*)&level
, (unsigned int*)&flags
, (unsigned int *)&last_activity
) != 3)
447 if (hconf_version
< HELPMOD_VERSION_2_16
)
448 { /* For the new friend userlevel */
449 if (level
>= H_FRIEND
)
455 hack
->last_activity
= last_activity
;
457 fgets(ptr
= buf
, 256, in
);
460 helpmod_line_fix(&ptr
);
462 if (sscanf(ptr
, "%d", &nstats
) != 1)
467 hstat_account
*tmp
= hack
->stats
;
468 hack
->stats
= (hstat_account
*)malloc(sizeof(hstat_account
));
469 hack
->stats
->next
= tmp
;
470 if (helpmod_config_read_stats(in
, hack
->stats
) == -1)
478 int helpmod_config_write_account(FILE *out
, haccount
*target
)
481 fprintf(out
, "\t%s\n", target
->name
->content
);
482 fprintf(out
, "\t%x\t%x\t%x\n", target
->level
, target
->flags
, (unsigned int)target
->last_activity
);
484 fprintf(out
, "\t%d %% statistics for this channel\n", hstat_account_count(target
->stats
));
485 for (tmp
= target
->stats
;tmp
;tmp
= tmp
->next
)
486 helpmod_config_write_stats(out
, tmp
);
491 int helpmod_config_read_ban(FILE *in
)
493 char buf1
[512], buf2
[512], buf3
[512], *ptr1
, *ptr2
, *ptr3
;
496 fgets(ptr1
= buf1
, 256, in
);
499 helpmod_line_fix(&ptr1
);
501 fgets(ptr2
= buf2
, 256, in
);
504 helpmod_line_fix(&ptr2
);
506 fgets(ptr3
= buf3
, 256, in
);
509 helpmod_line_fix(&ptr3
);
511 if (sscanf(ptr3
, "%d", &tmp
) != 1)
514 hban_add(ptr1
, ptr2
, tmp
, 0);
519 int helpmod_config_write_ban(FILE *out
, hban
*hb
)
521 fprintf(out
, "\t%s\n", bantostring(hb
->real_ban
));
522 fprintf(out
, "\t%s\n", hb
->reason
?hb
->reason
->content
:NULL
);
523 fprintf(out
, "\t%u\n", (unsigned int)hb
->expiration
);
527 int helpmod_config_read_hlc_profile(FILE *in
)
529 hlc_profile
*hlc_prof
;
532 char buf
[256],*ptr
=(char*)buf
;
535 fgets(ptr
= buf
, 256, in
);
538 helpmod_line_fix(&ptr
);
540 if ((hlc_prof
= hlc_add(ptr
)) == NULL
)
544 fgets(ptr
= buf
, 256, in
);
547 helpmod_line_fix(&ptr
);
549 if (sscanf(ptr
, "%d %d", &tmp
[0], &tmp
[1]) != 2)
552 hlc_prof
->caps_max_percentage
= tmp
[0];
553 hlc_prof
->caps_min_count
= tmp
[1];
556 fgets(ptr
= buf
, 256, in
);
559 helpmod_line_fix(&ptr
);
561 if (sscanf(ptr
, "%d %d", &tmp
[0], &tmp
[1]) != 2)
564 hlc_prof
->repeats_max_count
= tmp
[0];
565 hlc_prof
->repeats_min_length
= tmp
[1];
567 /* character / symbol repeat */
568 fgets(ptr
= buf
, 256, in
);
571 helpmod_line_fix(&ptr
);
573 if (sscanf(ptr
, "%d %d %d", &tmp
[0], &tmp
[1], &tmp
[2]) != 3)
576 hlc_prof
->symbol_repeat_max_count
= tmp
[0];
577 hlc_prof
->character_repeat_max_count
= tmp
[1];
578 hlc_prof
->symbol_max_count
= tmp
[1];
580 /* flood, spam const, spam */
581 fgets(ptr
= buf
, 256, in
);
584 helpmod_line_fix(&ptr
);
586 if (sscanf(ptr
, "%d %f %d", &tmp
[0], &tmp_float
, &tmp
[1]) != 3)
589 hlc_prof
->tolerance_flood
= tmp
[0];
590 hlc_prof
->constant_spam
= tmp_float
;
591 hlc_prof
->tolerance_spam
= tmp
[1];
594 fgets(ptr
= buf
, 256, in
);
597 helpmod_line_fix(&ptr
);
599 if (sscanf(ptr
, "%d %d %d", &tmp
[0], &tmp
[1], &tmp
[2]) != 3)
602 hlc_prof
->tolerance_warn
= tmp
[0];
603 hlc_prof
->tolerance_kick
= tmp
[1];
604 hlc_prof
->tolerance_remove
= tmp
[2];
609 /* we use extended buffers here */
610 int helpmod_config_read_term(FILE *in
, hterm
** target
)
612 char buf1
[2048], buf2
[2048], *ptr1
= buf1
, *ptr2
= buf2
;
615 fgets(buf1
, 2048, in
);
618 helpmod_line_fix(&ptr1
);
621 fgets(buf2
, 2048, in
);
624 helpmod_line_fix(&ptr2
);
626 /* add to public domain */
627 hterm_add(target
, ptr1
, ptr2
);
632 int helpmod_config_write_term(FILE *out
, hterm
*htrm
)
634 fprintf(out
, "\t%s\n", htrm
->name
->content
);
635 fprintf(out
, "\t%s\n", htrm
->description
->content
);
639 int helpmod_config_write_hlc_profile(FILE *out
, hlc_profile
*hlc_prof
)
641 fprintf(out
, "\t%s\n", hlc_prof
->name
->content
);
642 fprintf(out
, "\t%d %d\n", hlc_prof
->caps_max_percentage
, hlc_prof
->caps_min_count
);
643 fprintf(out
, "\t%d %d\n", hlc_prof
->repeats_max_count
, hlc_prof
->repeats_min_length
);
644 fprintf(out
, "\t%d %d %d\n", hlc_prof
->symbol_repeat_max_count
, hlc_prof
->character_repeat_max_count
, hlc_prof
->symbol_max_count
);
645 fprintf(out
, "\t%d %.3f %d\n", hlc_prof
->tolerance_flood
, hlc_prof
->constant_spam
, hlc_prof
->tolerance_spam
);
646 fprintf(out
, "\t%d %d %d\n", hlc_prof
->tolerance_warn
, hlc_prof
->tolerance_kick
, hlc_prof
->tolerance_remove
);
650 int helpmod_config_read_chanstats(FILE *in
, hstat_channel
*hs_chan
)
652 char buf
[256],*ptr
=(char*)buf
;
654 hstat_channel_entry
*entry
;
658 fgets(ptr
= buf
, 256, in
);
661 helpmod_line_fix(&ptr
);
663 if (i
< 7) /* days */
664 entry
= &hs_chan
->week
[(hstat_day() + i
) % 7];
666 entry
= &hs_chan
->longterm
[(hstat_week() + (i
-7)) % 10];
668 if (hconf_version
< HELPMOD_VERSION_2_10
)
670 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)
675 if (sscanf(buf
, "%d %d %d %d %d %d %d %d", &entry
->active_time
, &entry
->staff_active_time
, &entry
->time_spent
, &entry
->prime_time_spent
, &entry
->joins
, &entry
->queue_use
, &entry
->lines
, &entry
->words
) != 8)
682 int helpmod_config_write_chanstats(FILE *out
, hstat_channel
*hs_chan
)
685 hstat_channel_entry
*entry
;
689 if (i
< 7) /* days */
690 entry
= &hs_chan
->week
[(hstat_day() + i
) % 7];
692 entry
= &hs_chan
->longterm
[(hstat_week() + (i
-7)) % 10];
694 fprintf(out
, "\t%d %d %d %d %d %d %d %d\n", entry
->active_time
, entry
->staff_active_time
, entry
->time_spent
, entry
->prime_time_spent
, entry
->joins
, entry
->queue_use
, entry
->lines
, entry
->words
);
699 int helpmod_config_read_stats(FILE *in
, hstat_account
*hs_acc
)
701 char buf
[256],*ptr
=(char*)buf
;
703 hstat_account_entry
*entry
;
705 fgets(ptr
= buf
, 256, in
);
708 helpmod_line_fix(&ptr
);
710 hs_acc
->hchan
= hchannel_get_by_name(ptr
);
712 if (hs_acc
->hchan
== NULL
)
717 fgets(ptr
= buf
, 256, in
);
720 helpmod_line_fix(&ptr
);
722 if (i
< 7) /* days */
723 entry
= &hs_acc
->week
[(hstat_day() + i
) % 7];
725 entry
= &hs_acc
->longterm
[(hstat_week() + (i
-7)) % 10];
727 if (sscanf(buf
, "%d %d %d %d", &entry
->time_spent
, &entry
->prime_time_spent
, &entry
->lines
, &entry
->words
) != 4)
733 int helpmod_config_write_stats(FILE *out
, hstat_account
*hs_acc
)
736 hstat_account_entry
*entry
;
738 fprintf(out
, "\t%s\n", hchannel_get_name(hs_acc
->hchan
));
742 if (i
< 7) /* days */
743 entry
= &hs_acc
->week
[(hstat_day() + i
) % 7];
745 entry
= &hs_acc
->longterm
[(hstat_week() + (i
-7)) % 10];
747 fprintf(out
, "\t%d %d %d %d\n", entry
->time_spent
, entry
->prime_time_spent
, entry
->lines
, entry
->words
);
752 int helpmod_config_read_globals(FILE *in
)
754 char buf
[256],*ptr
=(char*)buf
;
756 fgets(ptr
= buf
, 256, in
);
759 helpmod_line_fix(&ptr
);
761 if (sscanf(ptr
, "%d", &hstat_cycle
) != 1)
767 int helpmod_config_write_globals(FILE *out
)
769 fprintf(out
, "\t%d\n", hstat_cycle
);
773 int helpmod_config_read_report(FILE *in
)
775 char buf
[256], *ptr
=(char*)buf
;
776 hchannel
*tmp1
, *tmp2
;
778 fgets(ptr
= buf
, 256, in
);
781 helpmod_line_fix(&ptr
);
783 tmp1
= hchannel_get_by_name(ptr
);
785 fgets(ptr
= buf
, 256, in
);
788 helpmod_line_fix(&ptr
);
790 tmp2
= hchannel_get_by_name(ptr
);
792 if (hchannel_is_valid(tmp1
) && hchannel_is_valid(tmp2
))
793 tmp1
->report_to
= tmp2
;
798 int helpmod_config_write_report(FILE *out
, hchannel
*hchan
)
800 fprintf(out
, "\t%s\n", hchannel_get_name(hchan
));
801 fprintf(out
, "\t%s\n", hchannel_get_name(hchan
->report_to
));
805 int helpmod_config_read_ticket(FILE *in
)
807 char buf
[256], buf2
[256], buf3
[64], *ptr
=(char*)buf
;
810 fgets(ptr
= buf
, 256, in
);
813 helpmod_line_fix(&ptr
);
817 fgets(ptr
= buf
, 64, in
);
820 helpmod_line_fix(&ptr
);
824 fgets(ptr
= buf
, 256, in
);
827 helpmod_line_fix(&ptr
);
829 if (!sscanf(ptr
, "%u", &tmp
))
832 if (tmp
> time(NULL
))
834 if (hconf_version
< HELPMOD_VERSION_2_17
)
835 hticket_add(buf3
, tmp
, hchannel_get_by_name(buf2
), NULL
);
838 fgets(ptr
= buf
, 256, in
);
841 helpmod_line_fix(&ptr
);
844 hticket_add(buf3
, tmp
, hchannel_get_by_name(buf2
), NULL
);
846 hticket_add(buf3
, tmp
, hchannel_get_by_name(buf2
), ptr
);
853 int helpmod_config_write_ticket(FILE *out
, hticket
*htick
, hchannel
*hchan
)
855 fprintf(out
, "\t%s\n", hchannel_get_name(hchan
));
856 fprintf(out
, "\t%s\n", htick
->authname
);
857 fprintf(out
, "\t%u\n", (unsigned int)htick
->time_expiration
);
859 fprintf(out
, "\t%s\n", htick
->message
->content
);
866 int helpmod_config_read_version(FILE *in
)
868 char buf
[256], *ptr
=(char*)buf
;
870 fgets(ptr
= buf
, 256, in
);
873 helpmod_line_fix(&ptr
);
875 if (sscanf(ptr
, "%d", &hconf_version
) != 1)
881 int helpmod_config_write_version(FILE *out
)
883 fprintf(out
, "\t%d\n", HELPMOD_VERSION_INTERNAL
);
888 void helpmod_config_scheduled_events(void)
890 rename(HELPMOD_DEFAULT_DB
, HELPMOD_DEFAULT_DB
".old");
891 helpmod_config_write(HELPMOD_DEFAULT_DB
);