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 &DEBUG("rUF: backup file bigger than current file. FIXME");
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";
164 print OUT "--$_\t\t$val\n";
172 foreach (keys %bans) {
176 my $count = scalar keys %{ $bans{$chan} };
178 &WARN("bans: chan $chan has no other attributes; skipping.");
182 print OUT "::$chan bans\n";
183 foreach (keys %{ $bans{$chan} }) {
184 # format: bans: mask expire time-added count who-added reason
185 my @array = @{ $bans{$chan}{$_} };
186 if (scalar @array != 5) {
187 &WARN("bans: $chan/$_ is corrupted.");
191 printf OUT "- %s:+%d:+%d:%d:%s:%s\n", $_, @array;
194 print OUT "\n" if ($cbans);
198 foreach (keys %ignore) {
202 my $count = scalar keys %{ $ignore{$chan} };
204 &WARN("ignore: chan $chan has no other attributes; skipping.");
208 ### TODO: use hash instead of array for flexibility?
209 print OUT "::$chan ignore\n";
210 foreach (keys %{ $ignore{$chan} }) {
211 # format: ignore: mask expire time-added who-added reason
212 my @array = @{ $ignore{$chan}{$_} };
213 if (scalar @array != 4) {
214 &WARN("ignore: $chan/$_ is corrupted.");
218 printf OUT "- %s:+%d:+%d:%s:%s\n", $_, @array;
224 $wtime_userfile = time();
225 &status("--- Saved USERFILE ($cusers users; $cbans bans; $cignore ignore) at $time");
226 if (defined $msgType and $msgType =~ /^chat$/) {
227 &pSReply("--- Writing user file...");
232 ##### CHANNEL CONFIGURATION READER/WRITER
236 my $f = "$bot_state_dir/blootbot.chan";
237 if ( -f $f and -f "$f~") {
242 &DEBUG("rCF: backup file bigger than current file. FIXME");
247 &ERROR("Cannot read chanfile ($f): $!");
251 undef %chanconf; # reset.
253 $_ = <IN>; # version string.
260 next if /^\// or /^\;/; # / or ; are comment lines.
266 next unless (defined $chan);
268 if (/^[\s\t]+\+(\S+)$/) { # bool, true.
269 $chanconf{$chan}{$1} = 1;
271 } elsif (/^[\s\t]+\-(\S+)$/) { # bool, false.
272 &DEBUG("deprecated support of negative options.") unless ($cache{negative});
273 # although this is supported in run-time configuration.
274 $cache{negative} = 1;
275 # $chanconf{$chan}{$1} = 0;
277 } elsif (/^[\s\t]+(\S+)[\ss\t]+(.*)$/) {# what = val.
278 $chanconf{$chan}{$1} = $2;
281 &WARN("unknown line: $_") unless (/^#/);
286 # verify configuration
287 ### TODO: check against valid params.
288 foreach $chan (keys %chanconf) {
289 foreach (keys %{ $chanconf{$chan} }) {
292 &WARN("invalid param: chanconf{$chan}{$_}; removing.");
293 delete $chanconf{$chan}{$_};
294 undef $chanconf{$chan}{$_};
298 delete $cache{negative};
300 &status("CHANFILE: Loaded: ".(scalar(keys %chanconf)-1)." chans");
304 if (!scalar keys %chanconf) {
305 &DEBUG("wCF: nothing to write.");
309 if (!open OUT,">$bot_state_dir/blootbot.chan") {
310 &ERROR("Cannot write chanfile ($bot_state_dir/blootbot.chan): $!");
314 my $time = scalar(gmtime);
315 print OUT "#v1: blootbot -- $ident -- written $time\n\n";
319 ### Process 1: if defined in _default, remove same definition
320 ### from non-default channels.
321 foreach (keys %{ $chanconf{_default} }) {
323 my $val = $chanconf{_default}{$opt};
326 foreach (keys %chanconf) {
329 next if ($chan eq "_default");
330 next unless (exists $chanconf{$chan}{$opt});
331 next unless ($val eq $chanconf{$chan}{$opt});
334 delete $chanconf{$chan}{$opt};
338 &DEBUG("Removed config $opt to @chans since it's defiend in '_default'");
342 ### Process 2: if defined in all chans but _default, set in
343 ### _default and remove all others.
344 my (%optsval, %opts);
345 foreach (keys %chanconf) {
347 next if ($chan eq "_default");
350 foreach (keys %{ $chanconf{$chan} }) {
352 if (exists $optsval{$opt} and $optsval{$opt} eq $chanconf{$chan}{$opt}) {
356 $optsval{$opt} = $chanconf{$chan}{$opt};
361 foreach (keys %opts) {
362 next unless ($opts{$_} > 2);
363 &DEBUG(" opts{$_} => $opts{$_}");
366 ### other optimizations are in UserDCC.pl
370 foreach (sort keys %chanconf) {
375 foreach (sort keys %{ $chanconf{$chan} }) {
376 my $val = $chanconf{$chan}{$_};
378 if ($val =~ /^0$/) { # bool, false.
381 } elsif ($val =~ /^1$/) { # bool, true.
384 } else { # what = val.
385 print OUT " $_ $val\n";
395 $wtime_chanfile = time();
396 &status("--- Saved CHANFILE (".scalar(keys %chanconf).
399 if (defined $msgType and $msgType =~ /^chat$/) {
400 &pSReply("--- Writing chan file...");
408 # todo: support multiple flags.
411 my ($ret, $f, $o) = "";
413 &verifyUser($who, $nuh);
415 foreach $f (split //, $users{$userHandle}{FLAGS}) {
416 foreach $o ( split //, $flags ) {
417 next unless ($f eq $o);
428 my ($nick, $lnuh) = @_;
431 if ($userHandle = $dcc{'CHATvrfy'}{$who}) {
432 &VERB("vUser: cached auth for $who.",2);
438 foreach $user (keys %users) {
439 next if ($user eq "_default");
441 foreach $m (keys %{ $users{$user}{HOSTS} }) {
444 $m =~ s/([\@\(\)\[\]])/\\$1/g;
446 next unless ($lnuh =~ /^$m$/i);
448 if ($user !~ /^\Q$nick\E$/i and !exists $cache{VUSERWARN}{$user}) {
449 &status("vU: host matched but diff nick ($nick != $user).");
450 $cache{VUSERWARN}{$user} = 1;
457 last if ($userHandle ne "");
459 if ($user =~ /^\Q$nick\E$/i and !exists $cache{VUSERWARN}{$user}) {
460 &status("vU: nick matched but host is not in list ($lnuh).");
461 $cache{VUSERWARN}{$user} = 1;
465 $userHandle ||= "_default";
466 # what's talkchannel for?
467 $talkWho{$talkchannel} = $who if (defined $talkchannel);
474 # returns true if arg1 encrypts to arg2
475 my ($plain, $encrypted) = @_;
476 if ($encrypted eq "") {
477 ($plain, $encrypted) = split(/\s+/, $plain, 2);
479 return 0 unless ($plain ne "" and $encrypted ne "");
481 # MD5 // DES. Bobby Billingsley++.
483 if ($encrypted =~ /^(\S{2})/ and length $encrypted == 13) {
485 } elsif ($encrypted =~ /^\$\d\$(\w\w)\$/) {
488 &DEBUG("unknown salt from $encrypted.");
492 return ($encrypted eq crypt($plain, $salt));
495 # mainly for dcc chat... hrm.
499 if (&IsFlag($flag) eq $flag) {
502 &status("DCC CHAT: <$who> $message -- not enough flags.");
503 &pSReply("error: you do not have enough flags for that. ($flag required)");
509 my($mask,$chan,$expire,$comment) = @_;
511 $chan ||= "*"; # global if undefined.
512 $comment ||= ""; # optional.
513 $expire ||= 0; # permament.
517 $expire = ($expire*60) + time();
523 $exist++ if (exists $ignore{$chan}{$mask});
525 $ignore{$chan}{$mask} = [$expire, time(), $who, $comment];
527 # todo: improve this.
529 &status("ignore: Added $mask for $chan to NEVER expire, by $who, for $comment");
531 &status("ignore: Added $mask for $chan to expire $expire mins, by $who, for $comment");
535 $utime_userfile = time();
548 ### TODO: support wildcards.
549 foreach (keys %ignore) {
552 foreach (grep /^\Q$mask\E$/i, keys %{ $ignore{$chan} }) {
553 delete $ignore{$chan}{$mask};
557 &DEBUG("iD: scalar => ".scalar(keys %{ $ignore{$chan} }) );
561 $utime_userfile = time();
569 my($nick,$mask) = @_;
571 if (exists $users{$nick}) {
575 $utime_userfile = time();
578 if (defined $mask and $mask !~ /^\s*$/) {
579 &DEBUG("userAdd: mask => $mask");
580 $users{$nick}{HOSTS}{$mask} = 1;
583 $users{$nick}{FLAGS} ||= $users{_default}{FLAGS};
591 if (!exists $users{$nick}) {
595 $utime_userfile = time();
598 delete $users{$nick};
604 my($mask,$chan,$expire,$reason) = @_;
610 $expire = $expire*60 + time();
614 $exist++ if (exists $bans{$chan}{$mask} or
615 exists $bans{'*'}{$mask});
616 $bans{$chan}{$mask} = [$expire, time(), 0, $who, $reason];
618 my @chans = ($chan eq "*") ? keys %channels : $chan;
624 foreach (keys %{ $channels{$chan}{''} }) {
625 next unless (exists $nuh{lc $_});
626 next unless ($nuh{lc $_} =~ /^$m$/i);
627 &FIXME("nuh{$_} =~ /$m/");
632 $utime_userfile = time();
643 foreach (keys %bans) {
646 foreach (grep /^\Q$mask\E$/i, keys %{ $bans{$chan} }) {
647 delete $bans{$chan}{$_};
651 &DEBUG("bans: scalar => ".scalar(keys %{ $bans{$chan} }) );
655 $utime_userfile = time();
665 if ( &getUser($user) ) {
675 if (!defined $user) {
676 &WARN("getUser: user == NULL.");
680 if (my @retval = grep /^\Q$user\E$/i, keys %users) {
681 if ($retval[0] ne $user) {
682 &WARN("getUser: retval[0] ne user ($retval[0] ne $user)");
684 my $count = scalar keys %{ $users{$retval[0]} };
685 &DEBUG("count => $count.");
694 my($cmd, $chan, $what, $val) = @_;
696 if ($cmd eq "+chan") {
697 if (exists $chanconf{$chan}) {
698 &pSReply("chan $chan already exists.");
701 $chanconf{$chan}{_time_added} = time();
702 $chanconf{$chan}{autojoin} = 1;
704 &pSReply("Joining $chan...");
710 if (!exists $chanconf{$chan}) {
711 &pSReply("no such channel $chan");
717 if (defined $what and $what =~ s/^([+-])(\S+)/$2/) {
719 ### ".chanset +blah 10" -- error.
721 my $state = ($1 eq "+") ? 1 : 0;
722 my $was = $chanconf{$chan}{$what};
724 if ($state) { # add/set.
725 if (defined $was and $was eq "1") {
726 &pSReply("setting $what for $chan already 1.");
732 } else { # delete/unset.
734 &pSReply("setting $what for $chan is not set.");
739 &pSReply("setting $what for $chan already 0.");
746 # alter for cosmetic (print out) reasons only.
747 $was = ($was) ? "; was '$was'" : "";
750 &pSReply("Unsetting $what for $chan$was.");
751 delete $chanconf{$chan}{$what};
753 &pSReply("Setting $what for $chan to '$val'$was.");
754 $chanconf{$chan}{$what} = $val;
759 } elsif (defined $val) {
760 ### ".chanset blah testing"
762 my $was = $chanconf{$chan}{$what};
763 if (defined $was and $was eq $val) {
764 &pSReply("setting $what for $chan already '$val'.");
767 $was = ($was) ? "; was '$was'" : "";
768 &pSReply("Setting $what for $chan to '$val'$was.");
770 $chanconf{$chan}{$what} = $val;
774 } else { # read only.
778 if (!defined $what) {
779 &WARN("chanset/DC: what == undefine.");
783 if (exists $chanconf{$chan}{$what}) {
784 &pSReply("$what for $chan is '$chanconf{$chan}{$what}'");
786 &pSReply("$what for $chan is not set.");
791 $utime_chanfile = time();
799 # this is an attempt to fix where an option is enabled but the module
800 # has been not loaded. it also can be used for other things.
802 foreach (keys %{ $cache{confvars} }) {
803 my $i = $cache{confvars}{$_};
804 &DEBUG("rehashConfVars: _ => $_");
806 if (/^news$/ and $i) {
807 &loadMyModule("news");
808 delete $cache{confvars}{$_};
811 if (/^uptime$/ and $i) {
812 &loadMyModule("uptime");
813 delete $cache{confvars}{$_};
816 if (/^rootwarn$/i and $i) {
818 delete $cache{confvars}{$_};
822 &DEBUG("end of rehashConfVars");
824 delete $cache{confvars};
827 # following arrays are not used... forgot what I intended to use them for.
831 "limitcheckInterval",
835 ### TODO: finish off this list.
839 "m", # modify factoid.
840 "r", # remove factoid.
841 "s", # search (deprecated)
842 "t", # teach/add factoid.
843 "a", # ask/request factoid.
845 "o", # master of bot (automatic +amrt)
846 "O", # dynamic ops (as on channel). (automatic +o)
853 # +r - ability to remove factoids
854 # +t - ability to teach factoids
855 # +m - ability to modify factoids
857 # +o - authorised user of bot (like +m on eggdrop)