]> git.donarmstrong.com Git - infobot.git/blob - src/DynaConfig.pl
- added weather from Nathan Moschkin <logeist@guinerd.myip.org>. Thanks!
[infobot.git] / src / DynaConfig.pl
1 #
2 # DynaConfig.pl: Read/Write configuration files dynamically.
3 #        Author: dms
4 #       Version: v0.1 (20010120)
5 #       Created: 20010119
6 #          NOTE: Merged from User.pl
7 #
8
9 #use strict;
10
11 #####
12 ##### USERFILE CONFIGURATION READER/WRITER
13 #####
14
15 sub readUserFile {
16     my $f = "$bot_state_dir/blootbot.users";
17
18     if (! -f $f) {
19         &DEBUG("userfile not found; new fresh run detected.");
20         return;
21     }
22
23     if ( -f $f and -f "$f~") {
24         my $s1 = -s $f;
25         my $s2 = -s "$f~";
26
27         if ($s2 > $s1*3) {
28             &DEBUG("rUF: backup file bigger than current file. FIXME");
29         }
30     }
31
32     if (!open IN, $f) {
33         &ERROR("Cannot read userfile ($f): $!");
34         &closeLog();
35         exit 1;
36     }
37
38     undef %users;       # clear on reload.
39     undef %bans;        # reset.
40     undef %ignore;      # reset.
41
42     my $ver = <IN>;
43     if ($ver !~ /^#v1/) {
44         &ERROR("old or invalid user file found.");
45         &closeLog();
46         exit 1; # correct?
47     }
48
49     my $nick;
50     my $type;
51     while (<IN>) {
52         chop;
53
54         next if /^$/;
55         next if /^#/;
56
57         if (/^--(\S+)[\s\t]+(.*)$/) {           # user: middle entry.
58             my ($what,$val) = ($1,$2);
59
60             if (!defined $val or $val eq "") {
61                 &WARN("$what: val == NULL.");
62                 next;
63             }
64
65             if (!defined $nick) {
66                 &WARN("DynaConfig: invalid line: $_");
67                 next;
68             }
69
70             # nice little hack.
71             if ($what eq "HOSTS") {
72                 $users{$nick}{$what}{$val} = 1;
73             } else {
74                 $users{$nick}{$what} = $val;
75             }
76
77         } elsif (/^(\S+)$/) {                   # user: start entry.
78             $nick       = $1;
79
80         } elsif (/^::(\S+) ignore$/) {          # ignore: start entry.
81             $chan       = $1;
82             $type       = "ignore";
83
84         } elsif (/^- (\S+):\+(\d+):\+(\d+):(\S+):(.*)$/ and $type eq "ignore") {
85             ### ignore: middle entry.
86             my $mask = $1;
87             my(@array) = ($2,$3,$4,$5);
88             ### DEBUG purposes only!
89             if ($mask !~ /^$mask{nuh}$/) {
90                 &WARN("ignore: mask $mask is invalid.");
91                 next;
92             }
93             $ignore{$chan}{$mask} = \@array;
94
95         } elsif (/^::(\S+) bans$/) {            # bans: start entry.
96             $chan       = $1;
97             $type       = "bans";
98
99         } elsif (/^- (\S+):\+(\d+):\+(\d+):(\d+):(\S+):(.*)$/ and $type eq "bans") {
100             ### bans: middle entry.
101             # $btime, $atime, $count, $whoby, $reason.
102             my(@array) = ($2,$3,$4,$5,$6);
103             $bans{$chan}{$1} = \@array;
104
105         } else {                                # unknown.
106             &WARN("unknown line: $_");
107         }
108     }
109     close IN;
110
111     &status( sprintf("USERFILE: Loaded: %d users, %d bans, %d ignore",
112                 scalar(keys %users)-1,
113                 scalar(keys %bans),             # ??
114                 scalar(keys %ignore),           # ??
115         )
116     );
117 }
118
119 sub writeUserFile {
120     if (!scalar keys %users) {
121         &DEBUG("wUF: nothing to write.");
122         return;
123     }
124
125     if (!open OUT,">$bot_state_dir/blootbot.users") {
126         &ERROR("Cannot write userfile ($bot_state_dir/blootbot.users): $!");
127         return;
128     }
129
130     my $time            = scalar(localtime);
131
132     print OUT "#v1: blootbot -- $ident -- written $time\n\n";
133
134     ### USER LIST.
135     my $cusers  = 0;
136     foreach (sort keys %users) {
137         my $user = $_;
138         $cusers++;
139         my $count = scalar keys %{ $users{$user} };
140         if (!$count) {
141             &WARN("user $user has no other attributes; skipping.");
142             next;
143         }
144
145         print OUT "$user\n";
146
147         foreach (sort keys %{ $users{$user} }) {
148             my $what    = $_;
149             my $val     = $users{$user}{$_};
150
151             if (ref($val) eq "HASH") {
152                 foreach (sort keys %{ $users{$user}{$_} }) {
153                     print OUT "--$what\t\t$_\n";
154                 }
155
156             } else {
157                 print OUT "--$_\t\t$val\n";
158             }
159         }
160         print OUT "\n";
161     }
162
163     ### BAN LIST.
164     my $cbans   = 0;
165     foreach (keys %bans) {
166         my $chan = $_;
167         $cbans++;
168
169         my $count = scalar keys %{ $bans{$chan} };
170         if (!$count) {
171             &WARN("bans: chan $chan has no other attributes; skipping.");
172             next;
173         }
174
175         print OUT "::$chan bans\n";
176         foreach (keys %{ $bans{$chan} }) {
177 # format: bans: mask expire time-added count who-added reason
178             my @array = @{ $bans{$chan}{$_} };
179             if (scalar @array != 5) {
180                 &WARN("bans: $chan/$_ is corrupted.");
181                 next;
182             }
183
184             printf OUT "- %s:+%d:+%d:%d:%s:%s\n", $_, @array;
185         }
186     }
187     print OUT "\n" if ($cbans);
188
189     ### IGNORE LIST.
190     my $cignore = 0;
191     foreach (keys %ignore) {
192         my $chan = $_;
193         $cignore++;
194
195         my $count = scalar keys %{ $ignore{$chan} };
196         if (!$count) {
197             &WARN("ignore: chan $chan has no other attributes; skipping.");
198             next;
199         }
200
201         ### TODO: use hash instead of array for flexibility?
202         print OUT "::$chan ignore\n";
203         foreach (keys %{ $ignore{$chan} }) {
204 # format: ignore: mask expire time-added who-added reason
205             my @array = @{ $ignore{$chan}{$_} };
206             if (scalar @array != 4) {
207                 &WARN("ignore: $chan/$_ is corrupted.");
208                 next;
209             }
210
211             printf OUT "- %s:+%d:+%d:%s:%s\n", $_, @array;
212         }
213     }
214
215     close OUT;
216
217     $wtime_userfile = time();
218     &status("--- Saved USERFILE ($cusers users; $cbans bans; $cignore ignore) at $time");
219     if (defined $msgType and $msgType =~ /^chat$/) {
220         &pSReply("--- Writing user file...");
221     }
222 }
223
224 #####
225 ##### CHANNEL CONFIGURATION READER/WRITER
226 #####
227
228 sub readChanFile {
229     my $f = "$bot_state_dir/blootbot.chan";
230     if ( -f $f and -f "$f~") {
231         my $s1 = -s $f;
232         my $s2 = -s "$f~";
233
234         if ($s2 > $s1*3) {
235             &DEBUG("rCF: backup file bigger than current file. FIXME");
236         }
237     }
238
239     if (!open IN, $f) {
240         &ERROR("Cannot read chanfile ($f): $!");
241         return;
242     }
243
244     undef %chanconf;    # reset.
245
246     $_ = <IN>;          # version string.
247
248     my $chan;
249     while (<IN>) {
250         chop;
251
252         next if /^\s*$/;
253         next if /^\// or /^\;/; # / or ; are comment lines.
254
255         if (/^(\S+)\s*$/) {
256             $chan       = $1;
257             next;
258         }
259         next unless (defined $chan);
260
261         if (/^[\s\t]+\+(\S+)$/) {               # bool, true.
262             $chanconf{$chan}{$1} = 1;
263
264         } elsif (/^[\s\t]+\-(\S+)$/) {          # bool, false.
265             &DEBUG("deprecated support of negative options.") unless ($cache{negative});
266             # although this is supported in run-time configuration.
267             $cache{negative} = 1;
268 #           $chanconf{$chan}{$1} = 0;
269
270         } elsif (/^[\s\t]+(\S+)[\ss\t]+(.*)$/) {# what = val.
271             $chanconf{$chan}{$1} = $2;
272
273         } else {
274             &WARN("unknown line: $_") unless (/^#/);
275         }
276     }
277     close IN;
278
279     # verify configuration
280     ### TODO: check against valid params.
281     foreach $chan (keys %chanconf) {
282         foreach (keys %{ $chanconf{$chan} }) {
283             next unless /^[+-]/;
284
285             &WARN("invalid param: chanconf{$chan}{$_}; removing.");
286             delete $chanconf{$chan}{$_};
287             undef $chanconf{$chan}{$_};
288         }
289     }
290
291     delete $cache{negative};
292
293     &status("CHANFILE: Loaded: ".(scalar(keys %chanconf)-1)." chans");
294 }
295
296 sub writeChanFile {
297     if (!scalar keys %chanconf) {
298         &DEBUG("wCF: nothing to write.");
299         return;
300     }
301
302     if (!open OUT,">$bot_state_dir/blootbot.chan") {
303         &ERROR("Cannot write chanfile ($bot_state_dir/blootbot.chan): $!");
304         return;
305     }
306
307     my $time            = scalar(localtime);
308     print OUT "#v1: blootbot -- $ident -- written $time\n\n";
309
310     if ($flag_quit) {
311
312         ### Process 1: if defined in _default, remove same definition
313         ###             from non-default channels.
314         foreach (keys %{ $chanconf{_default} }) {
315             my $opt     = $_;
316             my $val     = $chanconf{_default}{$opt};
317             my @chans;
318
319             foreach (keys %chanconf) {
320                 $chan = $_;
321
322                 next if ($chan eq "_default");
323                 next unless (exists $chanconf{$chan}{$opt});
324                 next unless ($val eq $chanconf{$chan}{$opt});
325
326                 push(@chans,$chan);
327                 delete $chanconf{$chan}{$opt};
328             }
329
330             if (scalar @chans) {
331                 &DEBUG("Removed config $opt to @chans since it's defiend in '_default'");
332             }
333         }
334
335         ### Process 2: if defined in all chans but _default, set in
336         ###             _default and remove all others.
337         my (%optsval, %opts);
338         foreach (keys %chanconf) {
339             $chan = $_;
340             next if ($chan eq "_default");
341             my $opt;
342
343             foreach (keys %{ $chanconf{$chan} }) {
344                 $opt = $_;
345                 if (exists $optsval{$opt} and $optsval{$opt} eq $chanconf{$chan}{$opt}) {
346                     $opts{$opt}++;
347                     next;
348                 }
349                 $optsval{$opt}  = $chanconf{$chan}{$opt};
350                 $opts{$opt}     = 1;
351             }
352         }
353
354         foreach (keys %opts) {
355             next unless ($opts{$_} > 2);
356             &DEBUG("  opts{$_} => $opts{$_}");
357         }
358
359         ### other optimizations are in UserDCC.pl
360     }
361
362     ### lets do it...
363     foreach (sort keys %chanconf) {
364         $chan   = $_;
365
366         print OUT "$chan\n";
367
368         foreach (sort keys %{ $chanconf{$chan} }) {
369             my $val = $chanconf{$chan}{$_};
370
371             if ($val =~ /^0$/) {                # bool, false.
372                 print OUT "    -$_\n";
373
374             } elsif ($val =~ /^1$/) {           # bool, true.
375                 print OUT "    +$_\n";
376
377             } else {                            # what = val.
378                 print OUT "    $_ $val\n";
379
380             }
381
382         }
383         print OUT "\n";
384     }
385
386     close OUT;
387
388     $wtime_chanfile = time();
389     &status("--- Saved CHANFILE (".scalar(keys %chanconf).
390                 " chans) at $time");
391
392     if (defined $msgType and $msgType =~ /^chat$/) {
393         &pSReply("--- Writing chan file...");
394     }
395 }
396
397 #####
398 ##### USER COMMANDS.
399 #####
400
401 sub IsFlag {
402     my $flags = shift;
403     my ($ret, $f, $o) = "";
404
405     &verifyUser($who, $nuh);
406
407     foreach $f (split //, $users{$userHandle}{FLAGS}) {
408         foreach $o ( split //, $flags ) {
409             next unless ($f eq $o);
410
411             $ret = $f;
412             last;
413         }
414     }
415
416     $ret;
417 }
418
419 sub verifyUser {
420     my ($nick, $lnuh) = @_;
421     my ($user,$m);
422
423     if ($userHandle = $dcc{'CHATvrfy'}{$who}) {
424         &VERB("vUser: cached auth for $who.",2);
425         return $userHandle;
426     }
427
428     $userHandle = "";
429
430     foreach $user (keys %users) {
431         next if ($user eq "_default");
432
433         foreach $m (keys %{ $users{$user}{HOSTS} }) {
434             $m =~ s/\?/./g;
435             $m =~ s/\*/.*?/g;
436             $m =~ s/([\@\(\)\[\]])/\\$1/g;
437
438             next unless ($lnuh =~ /^$m$/i);
439
440             if ($user !~ /^\Q$nick\E$/i and !exists $cache{VUSERWARN}{$user}) {
441                 &status("vU: host matched but diff nick ($nick != $user).");
442                 $cache{VUSERWARN}{$user} = 1;
443             }
444
445             $userHandle = $user;
446             last;
447         }
448
449         last if ($userHandle ne "");
450
451         if ($user =~ /^\Q$nick\E$/i and !exists $cache{VUSERWARN}{$user}) {
452             &status("vU: nick matched but host is not in list ($lnuh).");
453             $cache{VUSERWARN}{$user} = 1;
454         }
455     }
456
457     $userHandle ||= "_default";
458     # what's talkchannel for?
459     $talkWho{$talkchannel} = $who if (defined $talkchannel);
460     $talkWho = $who;
461
462     return $userHandle;
463 }
464
465 sub ckpasswd {
466     # returns true if arg1 encrypts to arg2
467     my ($plain, $encrypted) = @_;
468     if ($encrypted eq "") {
469         ($plain, $encrypted) = split(/\s+/, $plain, 2);
470     }
471     return 0 unless ($plain ne "" and $encrypted ne "");
472
473     # MD5 // DES. Bobby Billingsley++.
474     my $salt;
475     if ($encrypted =~ /^(\S{2})/ and length $encrypted == 13) {
476         $salt = $1;
477     } elsif ($encrypted =~ /^\$\d\$(\w\w)\$/) {
478         $salt = $1;
479     } else {
480         &DEBUG("unknown salt from $encrypted.");
481         return 0;
482     }
483
484     return ($encrypted eq crypt($plain, $salt));
485 }
486
487 # mainly for dcc chat... hrm.
488 sub hasFlag {
489     my ($flag) = @_;
490
491     if (&IsFlag($flag) eq $flag) {
492         return 1;
493     } else {
494         &status("DCC CHAT: <$who> $message -- not enough flags.");
495         &pSReply("error: you do not have enough flags for that. ($flag required)");
496         return 0;
497     }
498 }
499
500 sub ignoreAdd {
501     my($mask,$chan,$expire,$comment) = @_;
502
503     $chan       ||= "*";        # global if undefined.
504     $comment    ||= "";         # optional.
505     $expire     ||= 0;          # permament.
506     my $count   ||= 0;
507
508     if ($expire > 0) {
509         $expire         = $expire*60 + time();
510     } else {
511         $expire         = 0;
512     }
513
514     my $exist   = 0;
515     $exist++ if (exists $ignore{$chan}{$mask});
516
517     $ignore{$chan}{$mask} = [$expire, time(), $who, $comment];
518
519     # todo: improve this.
520     &status("ignore: Added $mask for $chan to expire $expire, by $who, for $comment");
521
522     if ($exist) {
523         $utime_userfile = time();
524         $ucount_userfile++;
525
526         return 2;
527     } else {
528         return 1;
529     }
530 }
531
532 sub ignoreDel {
533     my($mask)   = @_;
534     my @match;
535
536     ### TODO: support wildcards.
537     foreach (keys %ignore) {
538         my $chan = $_;
539
540         foreach (grep /^\Q$mask\E$/i, keys %{ $ignore{$chan} }) {
541             delete $ignore{$chan}{$mask};
542             push(@match,$chan);
543         }
544
545         &DEBUG("iD: scalar => ".scalar(keys %{ $ignore{$chan} }) );
546     }
547
548     if (scalar @match) {
549         $utime_userfile = time();
550         $ucount_userfile++;
551     }
552
553     return @match;
554 }
555
556 sub userAdd {
557     my($nick,$mask)     = @_;
558
559     if (exists $users{$nick}) {
560         return 0;
561     }
562
563     $utime_userfile = time();
564     $ucount_userfile++;
565
566     if (defined $mask and $mask !~ /^\s*$/) {
567         &DEBUG("userAdd: mask => $mask");
568         $users{$nick}{HOSTS}{$mask} = 1;
569     }
570
571     $users{$nick}{FLAGS}        ||= $users{_default}{FLAGS};
572
573     return 1;
574 }
575
576 sub userDel {
577     my($nick)   = @_;
578
579     if (!exists $users{$nick}) {
580         return 0;
581     }
582
583     $utime_userfile = time();
584     $ucount_userfile++;
585
586     delete $users{$nick};
587
588     return 1;
589 }
590
591 sub banAdd {
592     my($mask,$chan,$expire,$reason) = @_;
593
594     $chan       ||= "*";
595     $expire     ||= 0;
596
597     if ($expire > 0) {
598         $expire         = $expire*60 + time();
599     }
600
601     my $exist   = 1;
602     $exist++ if (exists $bans{$chan}{$mask} or
603                 exists $bans{'*'}{$mask});
604     $bans{$chan}{$mask} = [$expire, time(), 0, $who, $reason];
605
606     my @chans   = ($chan eq "*") ? keys %channels : $chan;
607     my $m       = $mask;
608     $m          =~ s/\?/\\./g;
609     $m          =~ s/\*/\\S*/g;
610     foreach (@chans) {
611         my $chan = $_;
612         foreach (keys %{ $channels{$chan}{''} }) {
613             next unless (exists $nuh{lc $_});
614             next unless ($nuh{lc $_} =~ /^$m$/i);
615             &FIXME("nuh{$_} =~ /$m/");
616         }
617     }
618
619     if ($exist == 1) {
620         $utime_userfile = time();
621         $ucount_userfile++;
622     }
623
624     return $exist;
625 }
626
627 sub banDel {
628     my($mask)   = @_;
629     my @match;
630
631     foreach (keys %bans) {
632         my $chan        = $_;
633
634         foreach (grep /^\Q$mask\E$/i, keys %{ $bans{$chan} }) {
635             delete $bans{$chan}{$_};
636             push(@match, $chan);
637         }
638
639         &DEBUG("bans: scalar => ".scalar(keys %{ $bans{$chan} }) );
640     }
641
642     if (scalar @match) {
643         $utime_userfile = time();
644         $ucount_userfile++;
645     }
646
647     return @match;
648 }
649
650 sub IsUser {
651     my($user) = @_;
652
653     if ( &getUser($user) ) {
654         return 1;
655     } else {
656         return 0;
657     }
658 }
659
660 sub getUser {
661     my($user) = @_;
662
663     if (!defined $user) {
664         &WARN("getUser: user == NULL.");
665         return;
666     }
667
668     if (my @retval = grep /^\Q$user\E$/i, keys %users) {
669         if ($retval[0] ne $user) {
670             &WARN("getUser: retval[0] ne user ($retval[0] ne $user)");
671         }
672         my $count = scalar keys %{ $users{$retval[0]} };
673         &DEBUG("count => $count.");
674
675         return $retval[0];
676     } else {
677         return;
678     }
679 }
680
681 sub chanSet {
682     my($cmd, $chan, $what, $val) = @_;
683
684     if ($cmd eq "+chan") {
685         if (exists $chanconf{$chan}) {
686             &pSReply("chan $chan already exists.");
687             return;
688         }
689         $chanconf{$chan}{_time_added}   = time();
690         $chanconf{$what}{autojoin}      = 1;
691
692         &pSReply("Joining $chan...");
693         &joinchan($chan);
694
695         return;
696     }
697
698     if (!exists $chanconf{$chan}) {
699         &pSReply("no such channel $chan");
700         return;
701     }
702
703     my $update  = 0;
704
705     ### ".chanset +blah"
706     ### ".chanset +blah 10"             -- error.
707     if (defined $what and $what =~ s/^([+-])(\S+)/$2/) {
708         my $state       = ($1 eq "+") ? 1 : 0;
709         my $was         = $chanconf{$chan}{$what};
710
711         if ($state) {                   # add/set.
712             if (defined $was and $was eq "1") {
713                 &pSReply("setting $what for $chan already 1.");
714                 return;
715             }
716
717             $was        = ($was) ? "; was '$was'" : "";
718             $val        = 1;
719
720         } else {                        # delete/unset.
721             if (!defined $was) {
722                 &pSReply("setting $what for $chan is not set.");
723                 return;
724             }
725
726             if ($was eq "0") {
727                 &pSReply("setting $what for $chan already 0.");
728                 return;
729             }
730
731             $was        = ($was) ? "; was '$was'" : "";
732             $val        = 0;
733         }
734
735         if ($val eq "0") {
736             &pSReply("Unsetting $what for $chan$was.");
737             delete $chanconf{$chan}{$what};
738         } else {
739             &pSReply("Setting $what for $chan to '$val'$was.");
740             $chanconf{$chan}{$what}     = $val;
741         }
742
743         $update++;
744
745     ### ".chanset blah testing"
746     } elsif (defined $val) {
747         my $was = $chanconf{$chan}{$what};
748         if (defined $was and $was eq $val) {
749             &pSReply("setting $what for $chan already '$val'.");
750             return;
751         }
752         $was    = ($was) ? "; was '$was'" : "";
753         &pSReply("Setting $what for $chan to '$val'$was.");
754
755         $chanconf{$chan}{$what} = $val;
756
757         $update++;
758
759     ### ".chanset"
760     ### ".chanset blah"
761     } else {                            # read only.
762         if (!defined $what) {
763             &WARN("chanset/DC: what == undefine.");
764             return;
765         }
766
767         if (exists $chanconf{$chan}{$what}) {
768             &pSReply("$what for $chan is '$chanconf{$chan}{$what}'");
769         } else {
770             &pSReply("$what for $chan is not set.");
771         }
772     }
773
774     if ($update) {
775         $utime_chanfile = time();
776         $ucount_chanfile++;
777     }
778
779     return;
780 }
781
782 sub rehashConfVars {
783     # this is an attempt to fix where an option is loaded but the module
784     # has not loaded. it also can be used for other things.
785
786     foreach (keys %{ $cache{confvars} }) {
787         my $i = $cache{confvars}{$_};
788         &DEBUG("rehashConfVars: _ => $_");
789
790         if (/^news$/ and $i) {
791             &loadMyModule("news");
792             delete $cache{confvars}{$_};
793         }
794
795         if (/^uptime$/ and $i) {
796             &loadMyModule("uptime");
797             delete $cache{confvars}{$_};
798         }
799
800         if (/^rootwarn$/i and $i) {
801             &loadMyModule($_);
802             delete $cache{confvars}{$_};
803         }
804     }
805
806     &DEBUG("end of rehashConfVars");
807
808     delete $cache{confvars};
809 }
810
811 my @regFlagsChan = (
812         "autojoin",
813         "freshmeat",
814         "limitcheckInterval",
815         "limitcheckPlus",
816         "allowConv",
817         "allowDNS",
818 ### TODO: finish off this list.
819 );
820
821 my @regFlagsUser = (
822         "m",            # master
823         "n",            # owner
824         "o",            # op
825 );      # todo...
826
827 1;
828
829 #####
830 # Userflags
831 #       +r      - ability to remove factoids
832 #       +t      - ability to teach factoids
833 #       +m      - ability to modify factoids
834 #       +n      - bot owner
835 #       +o      - authorised user of bot (like +m on eggdrop)
836 #####