2 # DynaConfig.pl: Read/Write configuration files dynamically.
4 # Version: v0.1 (20010120)
6 # NOTE: Merged from User.pl
11 use vars qw(%chanconf %cache %bans %channels %nuh %users %ignore
13 use vars qw($utime_userfile $ucount_userfile $utime_chanfile $who
14 $ucount_chanfile $userHandle $chan $msgType $talkchannel
15 $ident $bot_state_dir $talkWho $flag_quit $wtime_userfile
16 $wcount_userfile $wtime_chanfile $nuh $message);
19 ##### USERFILE CONFIGURATION READER/WRITER
23 my $f = "$bot_state_dir/blootbot.users";
26 &DEBUG("userfile not found; new fresh run detected.");
30 if ( -f $f and -f "$f~") {
35 &FIXME("rUF: backup file bigger than current file.");
40 &ERROR("Cannot read userfile ($f): $!");
45 undef %users; # clear on reload.
47 undef %ignore; # reset.
51 &ERROR("old or invalid user file found.");
64 if (/^--(\S+)[\s\t]+(.*)$/) { # user: middle entry.
65 my ($what,$val) = ($1,$2);
67 if (!defined $val or $val eq "") {
68 &WARN("$what: val == NULL.");
73 &WARN("DynaConfig: invalid line: $_");
78 if ($what eq "HOSTS") {
79 $users{$nick}{$what}{$val} = 1;
81 $users{$nick}{$what} = $val;
84 } elsif (/^(\S+)$/) { # user: start entry.
87 } elsif (/^::(\S+) ignore$/) { # ignore: start entry.
91 } elsif (/^- (\S+):\+(\d+):\+(\d+):(\S+):(.*)$/ and $type eq "ignore") {
92 ### ignore: middle entry.
94 my(@array) = ($2,$3,$4,$5);
95 ### DEBUG purposes only!
96 if ($mask !~ /^$mask{nuh}$/) {
97 &WARN("ignore: mask $mask is invalid.");
100 $ignore{$chan}{$mask} = \@array;
102 } elsif (/^::(\S+) bans$/) { # bans: start entry.
106 } elsif (/^- (\S+):\+(\d+):\+(\d+):(\d+):(\S+):(.*)$/ and $type eq "bans") {
107 ### bans: middle entry.
108 # $btime, $atime, $count, $whoby, $reason.
109 my(@array) = ($2,$3,$4,$5,$6);
110 $bans{$chan}{$1} = \@array;
113 &WARN("unknown line: $_");
118 &status( sprintf("USERFILE: Loaded: %d users, %d bans, %d ignore",
119 scalar(keys %users)-1,
120 scalar(keys %bans), # ??
121 scalar(keys %ignore), # ??
127 if (!scalar keys %users) {
128 &DEBUG("wUF: nothing to write.");
132 if (!open OUT,">$bot_state_dir/blootbot.users") {
133 &ERROR("Cannot write userfile ($bot_state_dir/blootbot.users): $!");
137 my $time = scalar(gmtime);
139 print OUT "#v1: blootbot -- $ident -- written $time\n\n";
143 foreach (sort keys %users) {
146 my $count = scalar keys %{ $users{$user} };
148 &WARN("user $user has no other attributes; skipping.");
154 foreach (sort keys %{ $users{$user} }) {
156 my $val = $users{$user}{$_};
158 if (ref($val) eq "HASH") {
159 foreach (sort keys %{ $users{$user}{$_} }) {
160 print OUT "--$what\t\t$_\n";
163 } elsif ($_ eq 'FLAGS') {
164 print OUT "--$_\t\t" . join('', sort split('', $val)) . "\n";
166 print OUT "--$_\t\t$val\n";
174 foreach (keys %bans) {
178 my $count = scalar keys %{ $bans{$chan} };
180 &WARN("bans: chan $chan has no other attributes; skipping.");
184 print OUT "::$chan bans\n";
185 foreach (keys %{ $bans{$chan} }) {
186 # format: bans: mask expire time-added count who-added reason
187 my @array = @{ $bans{$chan}{$_} };
188 if (scalar @array != 5) {
189 &WARN("bans: $chan/$_ is corrupted.");
193 printf OUT "- %s:+%d:+%d:%d:%s:%s\n", $_, @array;
196 print OUT "\n" if ($cbans);
200 foreach (keys %ignore) {
204 my $count = scalar keys %{ $ignore{$chan} };
206 &WARN("ignore: chan $chan has no other attributes; skipping.");
210 ### TODO: use hash instead of array for flexibility?
211 print OUT "::$chan ignore\n";
212 foreach (keys %{ $ignore{$chan} }) {
213 # format: ignore: mask expire time-added who-added reason
214 my @array = @{ $ignore{$chan}{$_} };
215 if (scalar @array != 4) {
216 &WARN("ignore: $chan/$_ is corrupted.");
220 printf OUT "- %s:+%d:+%d:%s:%s\n", $_, @array;
226 $wtime_userfile = time();
227 &status("--- Saved USERFILE ($cusers users; $cbans bans; $cignore ignore) at $time");
228 if (defined $msgType and $msgType =~ /^chat$/) {
229 &pSReply("--- Writing user file...");
234 ##### CHANNEL CONFIGURATION READER/WRITER
238 my $f = "$bot_state_dir/blootbot.chan";
239 if ( -f $f and -f "$f~") {
244 &FIXME("rCF: backup file bigger than current file.");
249 &ERROR("Cannot read chanfile ($f): $!");
253 undef %chanconf; # reset.
255 $_ = <IN>; # version string.
262 next if /^\// or /^\;/; # / or ; are comment lines.
268 next unless (defined $chan);
270 if (/^[\s\t]+\+(\S+)$/) { # bool, true.
271 $chanconf{$chan}{$1} = 1;
273 } elsif (/^[\s\t]+\-(\S+)$/) { # bool, false.
274 &DEBUG("deprecated support of negative options.") unless ($cache{negative});
275 # although this is supported in run-time configuration.
276 $cache{negative} = 1;
277 # $chanconf{$chan}{$1} = 0;
279 } elsif (/^[\s\t]+(\S+)[\ss\t]+(.*)$/) {# what = val.
280 $chanconf{$chan}{$1} = $2;
283 &WARN("unknown line: $_") unless (/^#/);
288 # verify configuration
289 ### TODO: check against valid params.
290 foreach $chan (keys %chanconf) {
291 foreach (keys %{ $chanconf{$chan} }) {
294 &WARN("invalid param: chanconf{$chan}{$_}; removing.");
295 delete $chanconf{$chan}{$_};
296 undef $chanconf{$chan}{$_};
300 delete $cache{negative};
302 &status("CHANFILE: Loaded: ".(scalar(keys %chanconf)-1)." chans");
306 if (!scalar keys %chanconf) {
307 &DEBUG("wCF: nothing to write.");
311 if (!open OUT,">$bot_state_dir/blootbot.chan") {
312 &ERROR("Cannot write chanfile ($bot_state_dir/blootbot.chan): $!");
316 my $time = scalar(gmtime);
317 print OUT "#v1: blootbot -- $ident -- written $time\n\n";
321 ### Process 1: if defined in _default, remove same definition
322 ### from non-default channels.
323 foreach (keys %{ $chanconf{_default} }) {
325 my $val = $chanconf{_default}{$opt};
328 foreach (keys %chanconf) {
331 next if ($chan eq "_default");
332 next unless (exists $chanconf{$chan}{$opt});
333 next unless ($val eq $chanconf{$chan}{$opt});
336 delete $chanconf{$chan}{$opt};
340 &DEBUG("Removed config $opt to @chans since it's defiend in '_default'");
344 ### Process 2: if defined in all chans but _default, set in
345 ### _default and remove all others.
346 my (%optsval, %opts);
347 foreach (keys %chanconf) {
349 next if ($chan eq "_default");
352 foreach (keys %{ $chanconf{$chan} }) {
354 if (exists $optsval{$opt} and $optsval{$opt} eq $chanconf{$chan}{$opt}) {
358 $optsval{$opt} = $chanconf{$chan}{$opt};
363 foreach (keys %opts) {
364 next unless ($opts{$_} > 2);
365 &DEBUG(" opts{$_} => $opts{$_}");
368 ### other optimizations are in UserDCC.pl
372 foreach (sort keys %chanconf) {
377 foreach (sort keys %{ $chanconf{$chan} }) {
378 my $val = $chanconf{$chan}{$_};
380 if ($val =~ /^0$/) { # bool, false.
383 } elsif ($val =~ /^1$/) { # bool, true.
386 } else { # what = val.
387 print OUT " $_ $val\n";
397 $wtime_chanfile = time();
398 &status("--- Saved CHANFILE (".scalar(keys %chanconf).
401 if (defined $msgType and $msgType =~ /^chat$/) {
402 &pSReply("--- Writing chan file...");
410 # TODO: support multiple flags.
411 # TODO: return all flags for opers
414 my ($ret, $f, $o) = "";
416 &verifyUser($who, $nuh);
418 foreach $f (split //, $users{$userHandle}{FLAGS}) {
419 foreach $o ( split //, $flags ) {
420 next unless ($f eq $o);
431 my ($nick, $lnuh) = @_;
434 if ($userHandle = $dcc{'CHATvrfy'}{$who}) {
435 &VERB("vUser: cached auth for $who.",2);
441 foreach $user (keys %users) {
442 next if ($user eq "_default");
444 foreach $m (keys %{ $users{$user}{HOSTS} }) {
447 $m =~ s/([\@\(\)\[\]])/\\$1/g;
449 next unless ($lnuh =~ /^$m$/i);
451 if ($user !~ /^\Q$nick\E$/i and !exists $cache{VUSERWARN}{$user}) {
452 &status("vU: host matched but diff nick ($nick != $user).");
453 $cache{VUSERWARN}{$user} = 1;
460 last if ($userHandle ne "");
462 if ($user =~ /^\Q$nick\E$/i and !exists $cache{VUSERWARN}{$user}) {
463 &status("vU: nick matched but host is not in list ($lnuh).");
464 $cache{VUSERWARN}{$user} = 1;
468 $userHandle ||= "_default";
469 # what's talkchannel for?
470 $talkWho{$talkchannel} = $who if (defined $talkchannel);
477 # returns true if arg1 encrypts to arg2
478 my ($plain, $encrypted) = @_;
479 if ($encrypted eq "") {
480 ($plain, $encrypted) = split(/\s+/, $plain, 2);
482 return 0 unless ($plain ne "" and $encrypted ne "");
484 # MD5 // DES. Bobby Billingsley++.
486 if ($encrypted =~ /^(\S{2})/ and length $encrypted == 13) {
488 } elsif ($encrypted =~ /^\$\d\$(\w\w)\$/) {
491 &DEBUG("unknown salt from $encrypted.");
495 return ($encrypted eq crypt($plain, $salt));
498 # mainly for dcc chat... hrm.
502 if (&IsFlag($flag) eq $flag) {
505 &status("DCC CHAT: <$who> $message -- not enough flags.");
506 &pSReply("error: you do not have enough flags for that. ($flag required)");
511 # expire is time in minutes
513 my($mask,$chan,$expire,$comment) = @_;
515 $chan ||= "*"; # global if undefined.
516 $comment ||= ""; # optional.
517 $expire ||= 0; # permament.
521 $expire = ($expire*60) + time();
527 $exist++ if (exists $ignore{$chan}{$mask});
529 $ignore{$chan}{$mask} = [$expire, time(), $who, $comment];
531 # TODO: improve this.
533 &status("ignore: Added $mask for $chan to NEVER expire, by $who, for $comment");
535 &status("ignore: Added $mask for $chan to expire $expire mins, by $who, for $comment");
539 $utime_userfile = time();
552 ### TODO: support wildcards.
553 foreach (keys %ignore) {
556 foreach (grep /^\Q$mask\E$/i, keys %{ $ignore{$chan} }) {
557 delete $ignore{$chan}{$mask};
561 &DEBUG("iD: scalar => ".scalar(keys %{ $ignore{$chan} }) );
565 $utime_userfile = time();
573 my($nick,$mask) = @_;
575 if (exists $users{$nick}) {
579 $utime_userfile = time();
582 if (defined $mask and $mask !~ /^\s*$/) {
583 &DEBUG("userAdd: mask => $mask");
584 $users{$nick}{HOSTS}{$mask} = 1;
587 $users{$nick}{FLAGS} ||= $users{_default}{FLAGS};
595 if (!exists $users{$nick}) {
599 $utime_userfile = time();
602 delete $users{$nick};
608 my($mask,$chan,$expire,$reason) = @_;
614 $expire = $expire*60 + time();
618 $exist++ if (exists $bans{$chan}{$mask} or
619 exists $bans{'*'}{$mask});
620 $bans{$chan}{$mask} = [$expire, time(), 0, $who, $reason];
622 my @chans = ($chan eq "*") ? keys %channels : $chan;
628 foreach (keys %{ $channels{$chan}{''} }) {
629 next unless (exists $nuh{lc $_});
630 next unless ($nuh{lc $_} =~ /^$m$/i);
631 &FIXME("nuh{$_} =~ /$m/");
636 $utime_userfile = time();
647 foreach (keys %bans) {
650 foreach (grep /^\Q$mask\E$/i, keys %{ $bans{$chan} }) {
651 delete $bans{$chan}{$_};
655 &DEBUG("bans: scalar => ".scalar(keys %{ $bans{$chan} }) );
659 $utime_userfile = time();
669 if ( &getUser($user) ) {
679 if (!defined $user) {
680 &WARN("getUser: user == NULL.");
684 if (my @retval = grep /^\Q$user\E$/i, keys %users) {
685 if ($retval[0] ne $user) {
686 &WARN("getUser: retval[0] ne user ($retval[0] ne $user)");
688 my $count = scalar keys %{ $users{$retval[0]} };
689 &DEBUG("count => $count.");
698 my($cmd, $chan, $what, $val) = @_;
700 if ($cmd eq "+chan") {
701 if (exists $chanconf{$chan}) {
702 &pSReply("chan $chan already exists.");
705 $chanconf{$chan}{_time_added} = time();
706 $chanconf{$chan}{autojoin} = $conn->nick();
708 &pSReply("Joining $chan...");
714 if (!exists $chanconf{$chan}) {
715 &pSReply("no such channel $chan");
721 if (defined $what and $what =~ s/^([+-])(\S+)/$2/) {
723 ### ".chanset +blah 10" -- error.
725 my $state = ($1 eq "+") ? 1 : 0;
726 my $was = $chanconf{$chan}{$what};
728 if ($state) { # add/set.
729 if (defined $was and $was eq "1") {
730 &pSReply("setting $what for $chan already 1.");
736 } else { # delete/unset.
738 &pSReply("setting $what for $chan is not set.");
743 &pSReply("setting $what for $chan already 0.");
750 # alter for cosmetic (print out) reasons only.
751 $was = ($was) ? "; was '$was'" : "";
754 &pSReply("Unsetting $what for $chan$was.");
755 delete $chanconf{$chan}{$what};
757 &pSReply("Setting $what for $chan to '$val'$was.");
758 $chanconf{$chan}{$what} = $val;
763 } elsif (defined $val) {
764 ### ".chanset blah testing"
766 my $was = $chanconf{$chan}{$what};
767 if (defined $was and $was eq $val) {
768 &pSReply("setting $what for $chan already '$val'.");
771 $was = ($was) ? "; was '$was'" : "";
772 &pSReply("Setting $what for $chan to '$val'$was.");
774 $chanconf{$chan}{$what} = $val;
778 } else { # read only.
782 if (!defined $what) {
783 &WARN("chanset/DC: what == undefine.");
787 if (exists $chanconf{$chan}{$what}) {
788 &pSReply("$what for $chan is '$chanconf{$chan}{$what}'");
790 &pSReply("$what for $chan is not set.");
795 $utime_chanfile = time();
803 # this is an attempt to fix where an option is enabled but the module
804 # has been not loaded. it also can be used for other things.
806 foreach (keys %{ $cache{confvars} }) {
807 my $i = $cache{confvars}{$_};
808 &DEBUG("rehashConfVars: _ => $_");
810 if (/^news$/ and $i) {
811 &loadMyModule('News');
812 delete $cache{confvars}{$_};
815 if (/^uptime$/ and $i) {
816 &loadMyModule('Uptime');
817 delete $cache{confvars}{$_};
820 if (/^rootwarn$/i and $i) {
821 &loadMyModule('RootWarn');
822 delete $cache{confvars}{$_};
826 &DEBUG("end of rehashConfVars");
828 delete $cache{confvars};
831 # registered flags... not used yet.
834 "limitcheckInterval",
838 ### TODO: finish off this list.
842 # possible chars to include in FLAG
843 "A", # bot administration over /msg
844 # default is only via DCC CHAT
845 "O", # dynamic ops (as on channel). (automatic +o)
847 "a", # ask/request factoid.
848 "m", # modify factoid. (includes renaming)
849 "n", # bot owner, can "reload"
850 "o", # master of bot (automatic +amrt)
851 # can search on factoid strings shorter than 2 chars
852 # can tell bot to join new channels
853 # can [un]lock factoids
854 "r", # remove factoid.
855 "t", # teach/add factoid.