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_state_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 &WARN("DynaConfig: invalid line: $_");
71 if ($what eq "HOSTS") {
72 $users{$nick}{$what}{$val} = 1;
74 $users{$nick}{$what} = $val;
77 } elsif (/^(\S+)$/) { # user: start entry.
80 } elsif (/^::(\S+) ignore$/) { # ignore: start entry.
84 } elsif (/^- (\S+):\+(\d+):\+(\d+):(\S+):(.*)$/ and $type eq "ignore") {
85 ### ignore: middle entry.
87 my(@array) = ($2,$3,$4,$5);
88 ### DEBUG purposes only!
89 if ($mask !~ /^$mask{nuh}$/) {
90 &WARN("ignore: mask $mask is invalid.");
93 $ignore{$chan}{$mask} = \@array;
95 } elsif (/^::(\S+) bans$/) { # bans: start entry.
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;
106 &WARN("unknown line: $_");
111 &status( sprintf("USERFILE: Loaded: %d users, %d bans, %d ignore",
112 scalar(keys %users)-1,
113 scalar(keys %bans), # ??
114 scalar(keys %ignore), # ??
120 if (!scalar keys %users) {
121 &DEBUG("wUF: nothing to write.");
125 if (!open OUT,">$bot_state_dir/blootbot.users") {
126 &ERROR("cannot write to userfile.");
130 my $time = scalar(localtime);
132 print OUT "#v1: blootbot -- $ident -- written $time\n\n";
136 foreach (sort keys %users) {
139 my $count = scalar keys %{ $users{$user} };
141 &WARN("user $user has no other attributes; skipping.");
147 foreach (sort keys %{ $users{$user} }) {
149 my $val = $users{$user}{$_};
151 if (ref($val) eq "HASH") {
152 foreach (sort keys %{ $users{$user}{$_} }) {
153 print OUT "--$what\t\t$_\n";
157 print OUT "--$_\t\t$val\n";
165 foreach (keys %bans) {
169 my $count = scalar keys %{ $bans{$chan} };
171 &WARN("bans: chan $chan has no other attributes; skipping.");
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.");
184 printf OUT "- %s:+%d:+%d:%d:%s:%s\n", $_, @array;
187 print OUT "\n" if ($cbans);
191 foreach (keys %ignore) {
195 my $count = scalar keys %{ $ignore{$chan} };
197 &WARN("ignore: chan $chan has no other attributes; skipping.");
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.");
211 printf OUT "- %s:+%d:+%d:%s:%s\n", $_, @array;
217 $wtime_userfile = time();
218 &status("--- Saved USERFILE ($cusers users; $cbans bans; $cignore ignore) at $time");
219 if (defined $msgType and $msgType =~ /^chat$/) {
220 &performStrictReply("--- Writing user file...");
225 ##### CHANNEL CONFIGURATION READER/WRITER
229 my $f = "$bot_state_dir/blootbot.chan";
230 if ( -f $f and -f "$f~") {
235 &DEBUG("rCF: backup file bigger than current file. FIXME");
240 &ERROR("cannot erad chanfile.");
244 undef %chanconf; # reset.
246 $_ = <IN>; # version string.
258 next unless (defined $chan);
260 if (/^[\s\t]+\+(\S+)$/) { # bool, true.
261 $chanconf{$chan}{$1} = 1;
263 } elsif (/^[\s\t]+\-(\S+)$/) { # bool, false.
264 &DEBUG("deprecated support of negative options.") unless ($cache{negative});
265 # although this is supported in run-time configuration.
266 $cache{negative} = 1;
267 # $chanconf{$chan}{$1} = 0;
269 } elsif (/^[\s\t]+(\S+)[\ss\t]+(.*)$/) {# what = val.
270 $chanconf{$chan}{$1} = $2;
273 &WARN("unknown line: $_") unless (/^#/);
278 # verify configuration
279 ### TODO: check against valid params.
280 foreach $chan (keys %chanconf) {
281 foreach (keys %{ $chanconf{$chan} }) {
282 next unless (/^[+-]/);
283 &WARN("invalid param: chanconf{$chan}{$_}; removing.");
284 delete $chanconf{$chan}{$_};
285 undef $chanconf{$chan}{$_};
289 delete $cache{negative};
291 &status("CHANFILE: Loaded: ".(scalar(keys %chanconf)-1)." chans");
295 if (!scalar keys %chanconf) {
296 &DEBUG("wCF: nothing to write.");
300 if (!open OUT,">$bot_state_dir/blootbot.chan") {
301 &ERROR("cannot write chanfile.");
305 my $time = scalar(localtime);
306 print OUT "#v1: blootbot -- $ident -- written $time\n\n";
310 ### Process 1: if defined in _default, remove same definition
311 ### from non-default channels.
312 foreach (keys %{ $chanconf{_default} }) {
314 my $val = $chanconf{_default}{$opt};
317 foreach (keys %chanconf) {
320 next if ($chan eq "_default");
321 next unless (exists $chanconf{$chan}{$opt});
322 next unless ($val eq $chanconf{$chan}{$opt});
324 delete $chanconf{$chan}{$opt};
328 &DEBUG("Removed config $opt to @chans since it's defiend in '_default'");
332 ### Process 2: if defined in all chans but _default, set in
333 ### _default and remove all others.
334 my (%optsval, %opts);
335 foreach (keys %chanconf) {
337 next if ($chan eq "_default");
340 foreach (keys %{ $chanconf{$chan} }) {
342 if (exists $optsval{$opt} and $optsval{$opt} eq $chanconf{$chan}{$opt}) {
346 $optsval{$opt} = $chanconf{$chan}{$opt};
351 foreach (keys %opts) {
352 next unless ($opts{$_} > 2);
353 &DEBUG(" opts{$_} => $opts{$_}");
356 ### other optimizations are in UserDCC.pl
360 foreach (sort keys %chanconf) {
365 foreach (sort keys %{ $chanconf{$chan} }) {
366 my $val = $chanconf{$chan}{$_};
368 if ($val =~ /^0$/) { # bool, false.
371 } elsif ($val =~ /^1$/) { # bool, true.
374 } else { # what = val.
375 print OUT " $_ $val\n";
385 $wtime_chanfile = time();
386 &status("--- Saved CHANFILE (".scalar(keys %chanconf).
389 if (defined $msgType and $msgType =~ /^chat$/) {
390 &performStrictReply("--- Writing chan file...");
400 my ($ret, $f, $o) = "";
402 &verifyUser($who, $nuh);
404 foreach $f (split //, $users{$userHandle}{FLAGS}) {
405 foreach $o ( split //, $flags ) {
406 next unless ($f eq $o);
417 my ($nick, $lnuh) = @_;
420 if ($userHandle = $dcc{'CHATvrfy'}{$who}) {
421 &VERB("vUser: cached auth for $who.",2);
427 foreach $user (keys %users) {
428 next if ($user eq "_default");
430 foreach $m (keys %{ $users{$user}{HOSTS} }) {
433 $m =~ s/([\@\(\)\[\]])/\\$1/g;
435 next unless ($lnuh =~ /^$m$/i);
437 if ($user !~ /^\Q$nick\E$/i and !exists $cache{VUSERWARN}{$user}) {
438 &status("vU: host matched but diff nick ($nick != $user).");
439 $cache{VUSERWARN}{$user} = 1;
446 last if ($userHandle ne "");
448 if ($user =~ /^\Q$nick\E$/i and !exists $cache{VUSERWARN}{$user}) {
449 &status("vU: nick matched but host is not in list ($lnuh).");
450 $cache{VUSERWARN}{$user} = 1;
454 $userHandle ||= "_default";
455 # what's talkchannel for?
456 $talkWho{$talkchannel} = $who if (defined $talkchannel);
463 # returns true if arg1 encrypts to arg2
464 my ($plain, $encrypted) = @_;
465 if ($encrypted eq "") {
466 ($plain, $encrypted) = split(/\s+/, $plain, 2);
468 return 0 unless ($plain ne "" and $encrypted ne "");
470 # MD5 // DES. Bobby Billingsley++.
472 if ($encrypted =~ /^(\S{2})/ and length $encrypted == 13) {
474 } elsif ($encrypted =~ /^\$\d\$(\w\w)\$/) {
477 &DEBUG("unknown salt from $encrypted.");
481 return ($encrypted eq crypt($plain, $salt));
484 # mainly for dcc chat... hrm.
488 if (&IsFlag($flag) eq $flag) {
491 &status("DCC CHAT: <$who> $message -- not enough flags.");
492 &performStrictReply("error: you do not have enough flags for that. ($flag required)");
498 my($mask,$chan,$expire,$comment) = @_;
500 $chan ||= "*"; # global if undefined.
501 $comment ||= ""; # optional.
502 $expire ||= 0; # permament.
506 $expire = $expire*60 + time();
512 $exist++ if (exists $ignore{$chan}{$mask});
514 $ignore{$chan}{$mask} = [$expire, time(), $who, $comment];
516 # todo: improve this.
517 &status("ignore: Added $mask for $chan to expire $expire, by $who, for $comment");
520 $utime_userfile = time();
533 ### TODO: support wildcards.
534 foreach (keys %ignore) {
537 foreach (grep /^\Q$mask\E$/i, keys %{ $ignore{$chan} }) {
538 delete $ignore{$chan}{$mask};
542 &DEBUG("iD: scalar => ".scalar(keys %{ $ignore{$chan} }) );
546 $utime_userfile = time();
554 my($nick,$mask) = @_;
556 if (exists $users{$nick}) {
560 $utime_userfile = time();
563 if (defined $mask and $mask !~ /^\s*$/) {
564 &DEBUG("userAdd: mask => $mask");
565 $users{$nick}{HOSTS}{$mask} = 1;
568 $users{$nick}{FLAGS} ||= $users{_default}{FLAGS};
576 if (!exists $users{$nick}) {
580 $utime_userfile = time();
583 delete $users{$nick};
589 my($mask,$chan,$expire,$reason) = @_;
595 $expire = $expire*60 + time();
599 $exist++ if (exists $bans{$chan}{$mask} or
600 exists $bans{'*'}{$mask});
601 $bans{$chan}{$mask} = [$expire, time(), 0, $who, $reason];
603 my @chans = ($chan eq "*") ? keys %channels : $chan;
609 foreach (keys %{ $channels{$chan}{''} }) {
610 next unless (exists $nuh{lc $_});
611 next unless ($nuh{lc $_} =~ /^$m$/i);
612 &FIXME("nuh{$_} =~ /$m/");
617 $utime_userfile = time();
628 foreach (keys %bans) {
631 foreach (grep /^\Q$mask\E$/i, keys %{ $bans{$chan} }) {
632 delete $bans{$chan}{$_};
636 &DEBUG("bans: scalar => ".scalar(keys %{ $bans{$chan} }) );
640 $utime_userfile = time();
650 if ( &getUser($user) ) {
660 if (!defined $user) {
661 &WARN("getUser: user == NULL.");
665 if (my @retval = grep /^\Q$user\E$/i, keys %users) {
666 if ($retval[0] ne $user) {
667 &WARN("getUser: retval[0] ne user ($retval[0] ne $user)");
669 my $count = scalar keys %{ $users{$retval[0]} };
670 &DEBUG("count => $count.");
679 my($cmd, $chan, $what, $val) = @_;
681 if ($cmd eq "+chan") {
682 if (exists $chanconf{$chan}) {
683 &pSReply("chan $chan already exists.");
686 $chanconf{$chan}{_time_added} = time();
687 $chanconf{$what}{autojoin} = 1;
689 &pSReply("Joining $chan...");
695 if (!exists $chanconf{$chan}) {
696 &pSReply("no such channel $chan");
703 ### ".chanset +blah 10" -- error.
704 if (defined $what and $what =~ s/^([+-])(\S+)/$2/) {
705 my $state = ($1 eq "+") ? 1 : 0;
706 my $was = $chanconf{$chan}{$what};
708 if ($state) { # add/set.
709 if (defined $was and $was eq "1") {
710 &pSReply("setting $what for $chan already 1.");
714 $was = ($was) ? "; was '$was'" : "";
717 } else { # delete/unset.
719 &pSReply("setting $what for $chan is not set.");
724 &pSReply("setting $what for $chan already 0.");
728 $was = ($was) ? "; was '$was'" : "";
733 &pSReply("Unsetting $what for $chan$was.");
734 delete $chanconf{$chan}{$what};
736 &pSReply("Setting $what for $chan to '$val'$was.");
737 $chanconf{$chan}{$what} = $val;
742 ### ".chanset blah testing"
743 } elsif (defined $val) {
744 my $was = $chanconf{$chan}{$what};
745 if (defined $was and $was eq $val) {
746 &pSReply("setting $what for $chan already '$val'.");
749 $was = ($was) ? "; was '$was'" : "";
750 &pSReply("Setting $what for $chan to '$val'$was.");
752 $chanconf{$chan}{$what} = $val;
758 } else { # read only.
759 if (!defined $what) {
760 &WARN("chanset/DC: what == undefine.");
764 if (exists $chanconf{$chan}{$what}) {
765 &pSReply("$what for $chan is '$chanconf{$chan}{$what}'");
767 &pSReply("$what for $chan is not set.");
772 $utime_chanfile = time();
780 # this is an attempt to fix where an option is loaded but the module
781 # has not loaded. it also can be used for other things.
783 foreach (keys %{ $cache{confvars} }) {
784 my $i = $cache{confvars}{$_};
785 &DEBUG("rehashConfVars: _ => $_");
787 if (/^news$/ and $i) {
788 &loadMyModule("news");
789 delete $cache{confvars}{$_};
792 if (/^uptime$/ and $i) {
793 &loadMyModule("uptime");
794 delete $cache{confvars}{$_};
797 if (/^rootwarn$/i and $i) {
799 delete $cache{confvars}{$_};
803 &DEBUG("end of rehashConfVars");
805 delete $cache{confvars};
811 "limitcheckInterval",
815 ### TODO: finish off this list.
828 # +r - ability to remove factoids
829 # +t - ability to teach factoids
830 # +m - ability to modify factoids
832 # +o - authorised user of bot (like +m on eggdrop)