2 # DynaConfig.pl: Read/Write configuration files dynamically.
4 # Version: v0.1 (20010120)
6 # NOTE: Merged from User.pl
9 if (&IsParam("useStrict")) { use strict; }
12 ##### USERFILE CONFIGURATION READER/WRITER
16 my $f = "$bot_misc_dir/blootbot.users";
19 &DEBUG("userfile not found; new fresh run detected.");
23 if ( -f $f and -f "$f~") {
28 &DEBUG("rUF: backup file bigger than current file. FIXME");
33 &ERROR("cannot read userfile.");
38 undef %users; # clear on reload.
40 undef %ingore; # reset.
44 &ERROR("old or invalid user file found.");
57 if (/^--(\S+)[\s\t]+(.*)$/) { # user: middle entry.
58 my ($what,$val) = ($1,$2);
60 if (!defined $val or $val eq "") {
61 &WARN("$what: val == NULL.");
66 if ($what eq "HOSTS") {
67 $users{$nick}{$what}{$val} = 1;
69 $users{$nick}{$what} = $val;
72 } elsif (/^(\S+)$/) { # user: start entry.
75 } elsif (/^::(\S+) ignore$/) { # ignore: start entry.
79 } elsif (/^- (\S+):\+(\d+):\+(\d+):(\S+):(.*)$/ and $type eq "ignore") {
80 ### ignore: middle entry.
82 my(@array) = ($2,$3,$4,$5);
83 ### DEBUG purposes only!
84 if ($mask !~ /^$mask{nuh}$/) {
85 &WARN("ignore: mask $mask is invalid.");
88 $ignore{$chan}{$mask} = \@array;
90 } elsif (/^::(\S+) bans$/) { # bans: start entry.
94 } elsif (/^- (\S+):\+(\d+):\+(\d+):(\d+):(\S+):(.*)$/ and $type eq "bans") {
95 ### bans: middle entry.
96 # $btime, $atime, $count, $whoby, $reason.
97 my(@array) = ($2,$3,$4,$5,$6);
98 $bans{$chan}{$1} = \@array;
101 &WARN("unknown line: $_");
106 &status( sprintf("USERFILE: Loaded: %d users, %d bans, %d ignore",
107 scalar(keys %users)-1,
108 scalar(keys %bans), # ??
109 scalar(keys %ignore), # ??
115 if (!scalar keys %users) {
116 &DEBUG("wUF: nothing to write.");
120 if (!open OUT,">$bot_misc_dir/blootbot.users") {
121 &ERROR("cannot write to userfile.");
125 my $time = scalar(localtime);
127 print OUT "#v1: blootbot -- $ident -- written $time\n\n";
131 foreach (sort keys %users) {
134 my $count = scalar keys %{ $users{$user} };
136 &WARN("user $user has no other attributes; skipping.");
142 foreach (sort keys %{ $users{$user} }) {
144 my $val = $users{$user}{$_};
146 if (ref($val) eq "HASH") {
147 foreach (sort keys %{ $users{$user}{$_} }) {
148 print OUT "--$what\t\t$_\n";
152 print OUT "--$_\t\t$val\n";
160 foreach (keys %bans) {
164 my $count = scalar keys %{ $bans{$chan} };
166 &WARN("bans: chan $chan has no other attributes; skipping.");
170 print OUT "::$chan bans\n";
171 foreach (keys %{ $bans{$chan} }) {
172 # format: bans: mask expire time-added count who-added reason
173 my @array = @{ $bans{$chan}{$_} };
174 if (scalar @array != 5) {
175 &WARN("bans: $chan/$_ is corrupted.");
179 printf OUT "- %s:+%d:+%d:%d:%s:%s\n", $_, @array;
182 print OUT "\n" if ($cbans);
186 foreach (keys %ignore) {
190 my $count = scalar keys %{ $ignore{$chan} };
192 &WARN("ignore: chan $chan has no other attributes; skipping.");
196 ### TODO: use hash instead of array for flexibility?
197 print OUT "::$chan ignore\n";
198 foreach (keys %{ $ignore{$chan} }) {
199 # format: ignore: mask expire time-added who-added reason
200 my @array = @{ $ignore{$chan}{$_} };
201 if (scalar @array != 4) {
202 &WARN("ignore: $chan/$_ is corrupted.");
206 printf OUT "- %s:+%d:+%d:%s:%s\n", $_, @array;
212 $wtime_userfile = time();
213 &status("--- Saved USERFILE ($cusers users; $cbans bans; $cignore ignore) at $time");
214 if (defined $msgType and $msgType =~ /^chat$/) {
215 &performStrictReply("--- Writing user file...");
220 ##### CHANNEL CONFIGURATION READER/WRITER
224 my $f = "$bot_misc_dir/blootbot.chan";
225 if ( -f $f and -f "$f~") {
230 &DEBUG("rCF: backup file bigger than current file. FIXME");
235 &ERROR("cannot erad chanfile.");
239 undef %chanconf; # reset.
241 $_ = <IN>; # version string.
253 next unless (defined $chan);
255 if (/^[\s\t]+\+(\S+)$/) { # bool, true.
256 $chanconf{$chan}{$1} = 1;
258 } elsif (/^[\s\t]+\-(\S+)$/) { # bool, false.
259 &DEBUG("deprecated support of negative options.") unless ($cache{negative});
260 # although this is supported in run-time configuration.
261 $cache{negative} = 1;
262 # $chanconf{$chan}{$1} = 0;
264 } elsif (/^[\s\t]+(\S+)[\ss\t]+(.*)$/) {# what = val.
265 $chanconf{$chan}{$1} = $2;
268 &WARN("unknown line: $_") unless (/^#/);
273 # verify configuration
274 ### TODO: check against valid params.
275 foreach $chan (keys %chanconf) {
276 foreach (keys %{ $chanconf{$chan} }) {
277 next unless (/^[+-]/);
278 &WARN("invalid param: chanconf{$chan}{$_}; removing.");
279 delete $chanconf{$chan}{$_};
280 undef $chanconf{$chan}{$_};
284 delete $cache{negative};
286 &status("CHANFILE: Loaded: ".(scalar(keys %chanconf)-1)." chans");
290 if (!scalar keys %chanconf) {
291 &DEBUG("wCF: nothing to write.");
295 if (!open OUT,">$bot_misc_dir/blootbot.chan") {
296 &ERROR("cannot write chanfile.");
300 my $time = scalar(localtime);
301 print OUT "#v1: blootbot -- $ident -- written $time\n\n";
305 ### Process 1: if defined in _default, remove same definition
306 ### from non-default channels.
307 foreach (keys %{ $chanconf{_default} }) {
309 my $val = $chanconf{_default}{$opt};
312 foreach (keys %chanconf) {
315 next if ($chan eq "_default");
316 next unless (exists $chanconf{$chan}{$opt});
317 next unless ($val eq $chanconf{$chan}{$opt});
319 delete $chanconf{$chan}{$opt};
323 &DEBUG("Removed config $opt to @chans since it's defiend in '_default'");
327 ### Process 2: if defined in all chans but _default, set in
328 ### _default and remove all others.
329 my (%optsval, %opts);
330 foreach (keys %chanconf) {
332 next if ($chan eq "_default");
335 foreach (keys %{ $chanconf{$chan} }) {
337 if (exists $optsval{$opt} and $optsval{$opt} eq $chanconf{$chan}{$opt}) {
341 $optsval{$opt} = $chanconf{$chan}{$opt};
346 foreach (keys %opts) {
347 next unless ($opts{$_} > 1);
348 &DEBUG(" opts{$_} => $opts{$_}");
351 ### other optimizations are in UserDCC.pl
355 foreach (sort keys %chanconf) {
360 foreach (sort keys %{ $chanconf{$chan} }) {
361 my $val = $chanconf{$chan}{$_};
363 if ($val =~ /^0$/) { # bool, false.
366 } elsif ($val =~ /^1$/) { # bool, true.
369 } else { # what = val.
370 print OUT " $_ $val\n";
380 $wtime_chanfile = time();
381 &status("--- Saved CHANFILE (".scalar(keys %chanconf).
384 if (defined $msgType and $msgType =~ /^chat$/) {
385 &performStrictReply("--- Writing chan file...");
395 my ($ret, $f, $o) = "";
397 &verifyUser($who, $nuh);
399 foreach $f (split //, $users{$userHandle}{FLAGS}) {
400 foreach $o ( split //, $flags ) {
401 next unless ($f eq $o);
412 my ($nick, $lnuh) = @_;
415 if ($userHandle = $dcc{'CHATvrfy'}{$who}) {
416 &VERB("vUser: cached auth for $who.",2);
422 foreach $user (keys %users) {
423 next if ($user eq "_default");
425 foreach $m (keys %{ $users{$user}{HOSTS} }) {
428 $m =~ s/([\@\(\)\[\]])/\\$1/g;
430 next unless ($lnuh =~ /^$m$/i);
432 if ($user !~ /^\Q$nick\E$/i and !exists $cache{VUSERWARN}{$user}) {
433 &status("vU: host matched but diff nick ($nick != $user).");
434 $cache{VUSERWARN}{$user} = 1;
441 last if ($userHandle ne "");
443 if ($user =~ /^\Q$nick\E$/i and !exists $cache{VUSERWARN}{$user}) {
444 &status("vU: nick matched but host is not in list ($lnuh).");
445 $cache{VUSERWARN}{$user} = 1;
449 $userHandle ||= "_default";
450 # what's talkchannel for?
451 $talkWho{$talkchannel} = $who if (defined $talkchannel);
458 # returns true if arg1 encrypts to arg2
459 my ($plain, $encrypted) = @_;
460 if ($encrypted eq "") {
461 ($plain, $encrypted) = split(/\s+/, $plain, 2);
463 return 0 unless ($plain ne "" and $encrypted ne "");
465 # MD5 // DES. Bobby Billingsley++.
467 if ($encrypted =~ /^(\S{2})/ and length $encrypted == 13) {
469 } elsif ($encrypted =~ /^\$\d\$(\w\w)\$/) {
472 &DEBUG("unknown salt from $encrypted.");
476 return ($encrypted eq crypt($plain, $salt));
479 # mainly for dcc chat... hrm.
483 if (&IsFlag($flag) eq $flag) {
486 &status("DCC CHAT: <$who> $message -- not enough flags.");
487 &performStrictReply("error: you do not have enough flags for that. ($flag required)");
493 my($mask,$chan,$expire,$comment) = @_;
495 $chan ||= "*"; # global if undefined.
496 $comment ||= ""; # optional.
497 $expire ||= 0; # permament.
501 $expire = $expire*60 + time();
507 $exist++ if (exists $ignore{$chan}{$mask});
509 $ignore{$chan}{$mask} = [$expire, time(), $who, $comment];
512 $utime_userfile = time();
525 ### TODO: support wildcards.
526 foreach (keys %ignore) {
529 foreach (grep /^\Q$mask\E$/i, keys %{ $ignore{$chan} }) {
530 delete $ignore{$chan}{$mask};
534 &DEBUG("iD: scalar => ".scalar(keys %{ $ignore{$chan} }) );
538 $utime_userfile = time();
546 my($nick,$mask) = @_;
548 if (exists $users{$nick}) {
552 $utime_userfile = time();
555 $users{$nick}{HOSTS}{$mask} = 1;
556 $users{$nick}{FLAGS} ||= $users{_default}{FLAGS};
564 if (!exists $users{$nick}) {
568 $utime_userfile = time();
571 delete $users{$nick};
577 my($mask,$chan,$expire,$reason) = @_;
583 $expire = $expire*60 + time();
587 $exist++ if (exists $bans{$chan}{$mask} or
588 exists $bans{'*'}{$mask});
589 $bans{$chan}{$mask} = [$expire, time(), 0, $who, $reason];
591 my @chans = ($chan eq "*") ? keys %channels : $chan;
597 foreach (keys %{ $channels{$chan}{''} }) {
598 next unless (exists $nuh{lc $_});
599 next unless ($nuh{lc $_} =~ /^$m$/i);
600 &FIXME("nuh{$_} =~ /$m/");
605 $utime_userfile = time();
616 foreach (keys %bans) {
619 foreach (grep /^\Q$mask\E$/i, keys %{ $bans{$chan} }) {
620 delete $bans{$chan}{$_};
624 &DEBUG("bans: scalar => ".scalar(keys %{ $bans{$chan} }) );
628 $utime_userfile = time();
638 if ( &getUser($user) ) {
648 if (!defined $user) {
649 &WARN("getUser: user == NULL.");
653 if (my @retval = grep /^\Q$user\E$/i, keys %users) {
654 if ($retval[0] ne $user) {
655 &WARN("getUser: retval[0] ne user ($retval[0] ne $user)");
657 my $count = scalar keys %{ $users{$retval[0]} };
658 &DEBUG("count => $count.");
667 my($cmd, $chan, $what, $val) = @_;
669 if ($cmd eq "+chan") {
670 if (exists $chanconf{$chan}) {
671 &pSReply("chan $chan already exists.");
674 $chanconf{$chan}{_time_added} = time();
675 $chanconf{$what}{autojoin} = 1;
677 &pSReply("Joining $chan...");
683 if (!exists $chanconf{$chan}) {
684 &pSReply("no such channel $chan");
691 ### ".chanset +blah 10" -- error.
692 if (defined $what and $what =~ s/^([+-])(\S+)/$2/) {
693 my $state = ($1 eq "+") ? 1 : 0;
694 my $was = $chanconf{$chan}{$what};
696 if ($state) { # add/set.
697 if (defined $was and $was eq "1") {
698 &pSReply("setting $what for $chan already 1.");
702 $was = ($was) ? "; was '$was'" : "";
705 } else { # delete/unset.
707 &pSReply("setting $what for $chan is not set.");
712 &pSReply("setting $what for $chan already 0.");
716 $was = ($was) ? "; was '$was'" : "";
721 &pSReply("Unsetting $what for $chan$was.");
722 delete $chanconf{$chan}{$what};
724 &pSReply("Setting $what for $chan to '$val'$was.");
725 $chanconf{$chan}{$what} = $val;
730 ### ".chanset blah testing"
731 } elsif (defined $val) {
732 my $was = $chanconf{$chan}{$what};
733 if (defined $was and $was eq $val) {
734 &pSReply("setting $what for $chan already '$val'.");
737 $was = ($was) ? "; was '$was'" : "";
738 &pSReply("Setting $what for $chan to '$val'$was.");
740 $chanconf{$chan}{$what} = $val;
746 } else { # read only.
747 if (!defined $what) {
748 &WARN("chanset/DC: what == undefine.");
752 if (exists $chanconf{$chan}{$what}) {
753 &pSReply("$what for $chan is '$chanconf{$chan}{$what}'");
755 &pSReply("$what for $chan is not set.");
760 $utime_chanfile = time();
770 "limitcheckInterval",
774 ### TODO: finish off this list.
787 # +r - ability to remove factoids
788 # +t - ability to teach factoids
789 # +m - ability to modify factoids
791 # +o - authorised user of bot (like +m on eggdrop)