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);
20 # possible chars to include in FLAG
21 'A', # bot administration over /msg
22 # default is only via DCC CHAT
23 'O', # dynamic ops (as on channel). (automatic +o)
25 'a', # ask/request factoid.
26 'm', # modify all factoids. (includes renaming)
27 'M', # modify own factoids. (includes renaming)
28 'n', # bot owner, can 'reload'
29 'o', # master of bot (automatic +amrt)
30 # can search on factoid strings shorter than 2 chars
31 # can tell bot to join new channels
32 # can [un]lock factoids
33 'r', # remove factoid.
34 't', # teach/add factoid.
35 's', # Bypass +silent on channels
39 ##### USERFILE CONFIGURATION READER/WRITER
43 my $f = "$bot_state_dir/infobot.users";
46 &DEBUG('userfile not found; new fresh run detected.');
50 if ( -f $f and -f "$f~" ) {
54 if ( $s2 > $s1 * 3 ) {
55 &FIXME('rUF: backup file bigger than current file.');
60 &ERROR("Cannot read userfile ($f): $!");
65 undef %users; # clear on reload.
67 undef %ignore; # reset.
70 if ( $ver !~ /^#v1/ ) {
71 &ERROR('old or invalid user file found.');
84 if (/^--(\S+)[\s\t]+(.*)$/) { # user: middle entry.
85 my ( $what, $val ) = ( $1, $2 );
87 if ( !defined $val or $val eq '' ) {
88 &WARN("$what: val == NULL.");
92 if ( !defined $nick ) {
93 &WARN("DynaConfig: invalid line: $_");
98 if ( $what eq 'HOSTS' ) {
99 $users{$nick}{$what}{$val} = 1;
102 $users{$nick}{$what} = $val;
106 elsif (/^(\S+)$/) { # user: start entry.
110 elsif (/^::(\S+) ignore$/) { # ignore: start entry.
115 elsif ( /^- (\S+):\+(\d+):\+(\d+):(\S+):(.*)$/ and $type eq 'ignore' ) {
116 ### ignore: middle entry.
118 my (@array) = ( $2, $3, $4, $5 );
119 ### DEBUG purposes only!
120 if ( $mask !~ /^$mask{nuh}$/ ) {
121 &WARN("ignore: mask $mask is invalid.");
124 $ignore{$chan}{$mask} = \@array;
127 elsif (/^::(\S+) bans$/) { # bans: start entry.
132 elsif ( /^- (\S+):\+(\d+):\+(\d+):(\d+):(\S+):(.*)$/
133 and $type eq 'bans' )
135 ### bans: middle entry.
136 # $btime, $atime, $count, $whoby, $reason.
137 my (@array) = ( $2, $3, $4, $5, $6 );
138 $bans{$chan}{$1} = \@array;
142 &WARN("unknown line: $_");
149 'USERFILE: Loaded: %d users, %d bans, %d ignore',
150 scalar( keys %users ) - 1,
151 scalar( keys %bans ), # ??
152 scalar( keys %ignore ), # ??
158 if ( !scalar keys %users ) {
159 &DEBUG('wUF: nothing to write.');
163 if ( !open OUT, ">$bot_state_dir/infobot.users" ) {
164 &ERROR("Cannot write userfile ($bot_state_dir/infobot.users): $!");
168 my $time = scalar(gmtime);
170 print OUT "#v1: infobot -- $ident -- written $time\n\n";
174 foreach ( sort keys %users ) {
177 my $count = scalar keys %{ $users{$user} };
179 &WARN("user $user has no other attributes; skipping.");
185 foreach ( sort keys %{ $users{$user} } ) {
187 my $val = $users{$user}{$_};
189 if ( ref($val) eq 'HASH' ) {
190 foreach ( sort keys %{ $users{$user}{$_} } ) {
191 print OUT "--$what\t\t$_\n";
195 elsif ( $_ eq 'FLAGS' ) {
197 . join( '', sort split( '', $val ) ) . "\n";
200 print OUT "--$_\t\t$val\n";
208 foreach ( keys %bans ) {
212 my $count = scalar keys %{ $bans{$chan} };
214 &WARN("bans: chan $chan has no other attributes; skipping.");
218 print OUT "::$chan bans\n";
219 foreach ( keys %{ $bans{$chan} } ) {
221 # format: bans: mask expire time-added count who-added reason
222 my @array = @{ $bans{$chan}{$_} };
223 if ( scalar @array != 5 ) {
224 &WARN("bans: $chan/$_ is corrupted.");
228 printf OUT "- %s:+%d:+%d:%d:%s:%s\n", $_, @array;
231 print OUT "\n" if ($cbans);
235 foreach ( keys %ignore ) {
239 my $count = scalar keys %{ $ignore{$chan} };
241 &WARN("ignore: chan $chan has no other attributes; skipping.");
245 ### TODO: use hash instead of array for flexibility?
246 print OUT "::$chan ignore\n";
247 foreach ( keys %{ $ignore{$chan} } ) {
249 # format: ignore: mask expire time-added who-added reason
250 my @array = @{ $ignore{$chan}{$_} };
251 if ( scalar @array != 4 ) {
252 &WARN("ignore: $chan/$_ is corrupted.");
256 printf OUT "- %s:+%d:+%d:%s:%s\n", $_, @array;
262 $wtime_userfile = time();
264 "--- Saved USERFILE ($cusers users; $cbans bans; $cignore ignore) at $time"
266 if ( defined $msgType and $msgType =~ /^chat$/ ) {
267 &performStrictReply('--- Writing user file...');
272 ##### CHANNEL CONFIGURATION READER/WRITER
276 my $f = "$bot_state_dir/infobot.chan";
277 if ( -f $f and -f "$f~" ) {
281 if ( $s2 > $s1 * 3 ) {
282 &FIXME('rCF: backup file bigger than current file.');
286 if ( !open IN, $f ) {
287 &ERROR("Cannot read chanfile ($f): $!");
291 undef %chanconf; # reset.
293 $_ = <IN>; # version string.
300 next if /^\// or /^\;/; # / or ; are comment lines.
306 next unless ( defined $chan );
308 if (/^[\s\t]+\+(\S+)$/) { # bool, true.
309 $chanconf{$chan}{$1} = 1;
312 elsif (/^[\s\t]+\-(\S+)$/) { # bool, false.
313 # although this is supported in run-time configuration.
314 $chanconf{$chan}{$1} = 0;
317 elsif (/^[\s\t]+(\S+)[\s\t]+(.*)$/) { # what = val.
318 $chanconf{$chan}{$1} = $2;
322 &WARN("unknown line: $_") unless (/^#/);
327 # verify configuration
328 ### TODO: check against valid params.
329 foreach $chan ( keys %chanconf ) {
330 foreach ( keys %{ $chanconf{$chan} } ) {
333 &WARN("invalid param: chanconf{$chan}{$_}; removing.");
334 delete $chanconf{$chan}{$_};
335 undef $chanconf{$chan}{$_};
340 'CHANFILE: Loaded: ' . ( scalar( keys %chanconf ) - 1 ) . ' chans' );
344 if ( !scalar keys %chanconf ) {
345 &DEBUG('wCF: nothing to write.');
349 if ( !open OUT, ">$bot_state_dir/infobot.chan" ) {
350 &ERROR("Cannot write chanfile ($bot_state_dir/infobot.chan): $!");
354 my $time = scalar(gmtime);
355 print OUT "#v1: infobot -- $ident -- written $time\n\n";
359 ### Process 1: if defined in _default, remove same definition
360 ### from non-default channels.
361 foreach ( keys %{ $chanconf{_default} } ) {
363 my $val = $chanconf{_default}{$opt};
366 foreach ( keys %chanconf ) {
369 next if ( $chan eq '_default' );
370 next unless ( exists $chanconf{$chan}{$opt} );
371 next unless ( $val eq $chanconf{$chan}{$opt} );
373 push( @chans, $chan );
374 delete $chanconf{$chan}{$opt};
377 if ( scalar @chans ) {
379 "Removed config $opt to @chans since it's defiend in '_default'"
384 ### Process 2: if defined in all chans but _default, set in
385 ### _default and remove all others.
386 my ( %optsval, %opts );
387 foreach ( keys %chanconf ) {
389 next if ( $chan eq '_default' );
392 foreach ( keys %{ $chanconf{$chan} } ) {
394 if ( exists $optsval{$opt}
395 and $optsval{$opt} eq $chanconf{$chan}{$opt} )
400 $optsval{$opt} = $chanconf{$chan}{$opt};
405 foreach ( keys %opts ) {
406 next unless ( $opts{$_} > 2 );
407 &DEBUG(" opts{$_} => $opts{$_}");
410 ### other optimizations are in UserDCC.pl
414 foreach ( sort keys %chanconf ) {
419 foreach ( sort keys %{ $chanconf{$chan} } ) {
420 my $val = $chanconf{$chan}{$_};
422 if ( $val =~ /^0$/ ) { # bool, false.
426 elsif ( $val =~ /^1$/ ) { # bool, true.
431 print OUT " $_ $val\n";
441 $wtime_chanfile = time();
442 &status('--- Saved CHANFILE ('
443 . scalar( keys %chanconf )
444 . " chans) at $time" );
446 if ( defined $msgType and $msgType =~ /^chat$/ ) {
447 &performStrictReply('--- Writing chan file...');
455 # TODO: support multiple flags.
456 # TODO: return all flags for opers
459 my ( $ret, $f, $o ) = '';
461 &verifyUser( $who, $nuh );
463 foreach $f ( split //, $users{$userHandle}{FLAGS} ) {
464 foreach $o ( split //, $flags ) {
465 next unless ( $f eq $o );
476 my ( $nick, $lnuh ) = @_;
479 if ( $userHandle = $dcc{'CHATvrfy'}{$who} ) {
480 &VERB( "vUser: cached auth for $who.", 2 );
486 foreach $user ( keys %users ) {
487 next if ( $user eq '_default' );
489 foreach $m ( keys %{ $users{$user}{HOSTS} } ) {
492 $m =~ s/([\@\(\)\[\]])/\\$1/g;
494 next unless ( $lnuh =~ /^$m$/i );
496 if ( $user !~ /^\Q$nick\E$/i and !exists $cache{VUSERWARN}{$user} )
498 &status("vU: host matched but diff nick ($nick != $user).");
499 $cache{VUSERWARN}{$user} = 1;
506 last if ( $userHandle ne '' );
508 if ( $user =~ /^\Q$nick\E$/i and !exists $cache{VUSERWARN}{$user} ) {
509 &status("vU: nick matched but host is not in list ($lnuh).");
510 $cache{VUSERWARN}{$user} = 1;
514 $userHandle ||= '_default';
516 # what's talkchannel for?
517 $talkWho{$talkchannel} = $who if ( defined $talkchannel );
525 # returns true if arg1 encrypts to arg2
526 my ( $plain, $encrypted ) = @_;
527 if ( $encrypted eq '' ) {
528 ( $plain, $encrypted ) = split( /\s+/, $plain, 2 );
530 return 0 unless ( $plain ne '' and $encrypted ne '' );
532 # MD5 // DES. Bobby Billingsley++.
534 if ( $encrypted =~ /^(\S{2})/ and length $encrypted == 13 ) {
537 elsif ( $encrypted =~ /^\$\d\$(\w\w)\$/ ) {
541 &DEBUG("unknown salt from $encrypted.");
545 return ( $encrypted eq crypt( $plain, $salt ) );
548 # mainly for dcc chat... hrm.
552 if ( &IsFlag($flag) eq $flag ) {
556 &status("DCC CHAT: <$who> $message -- not enough flags.");
558 "error: you do not have enough flags for that. ($flag required)");
563 # expire is time in minutes
565 my ( $mask, $chan, $expire, $comment ) = @_;
567 $chan ||= '*'; # global if undefined.
568 $comment ||= ''; # optional.
569 $expire ||= 0; # permament.
573 $expire = ( $expire * 60 ) + time();
580 $exist++ if ( exists $ignore{$chan}{$mask} );
582 $ignore{$chan}{$mask} = [ $expire, time(), $who, $comment ];
584 # TODO: improve this.
585 if ( $expire == 0 ) {
587 "ignore: Added $mask for $chan to NEVER expire, by $who, for $comment"
592 "ignore: Added $mask for $chan to expire $expire mins, by $who, for $comment"
597 $utime_userfile = time();
611 ### TODO: support wildcards.
612 foreach ( keys %ignore ) {
615 foreach ( grep /^\Q$mask\E$/i, keys %{ $ignore{$chan} } ) {
616 delete $ignore{$chan}{$mask};
617 push( @match, $chan );
620 &DEBUG( 'iD: scalar => ' . scalar( keys %{ $ignore{$chan} } ) );
623 if ( scalar @match ) {
624 $utime_userfile = time();
632 my ( $nick, $mask ) = @_;
634 if ( exists $users{$nick} ) {
638 $utime_userfile = time();
641 if ( defined $mask and $mask !~ /^\s*$/ ) {
642 &DEBUG("userAdd: mask => $mask");
643 $users{$nick}{HOSTS}{$mask} = 1;
646 $users{$nick}{FLAGS} ||= $users{_default}{FLAGS};
654 if ( !exists $users{$nick} ) {
658 $utime_userfile = time();
661 delete $users{$nick};
667 my ( $mask, $chan, $expire, $reason ) = @_;
673 $expire = $expire * 60 + time();
677 $exist++ if ( exists $bans{$chan}{$mask}
678 or exists $bans{'*'}{$mask} );
679 $bans{$chan}{$mask} = [ $expire, time(), 0, $who, $reason ];
681 my @chans = ( $chan eq '*' ) ? keys %channels : $chan;
687 foreach ( keys %{ $channels{$chan}{''} } ) {
688 next unless ( exists $nuh{ lc $_ } );
689 next unless ( $nuh{ lc $_ } =~ /^$m$/i );
690 &FIXME("nuh{$_} =~ /$m/");
695 $utime_userfile = time();
706 foreach ( keys %bans ) {
709 foreach ( grep /^\Q$mask\E$/i, keys %{ $bans{$chan} } ) {
710 delete $bans{$chan}{$_};
711 push( @match, $chan );
714 &DEBUG( 'bans: scalar => ' . scalar( keys %{ $bans{$chan} } ) );
717 if ( scalar @match ) {
718 $utime_userfile = time();
728 if ( &getUser($user) ) {
739 if ( !defined $user ) {
740 &WARN('getUser: user == NULL.');
744 if ( my @retval = grep /^\Q$user\E$/i, keys %users ) {
745 if ( $retval[0] ne $user ) {
746 &WARN("getUser: retval[0] ne user ($retval[0] ne $user)");
748 my $count = scalar keys %{ $users{ $retval[0] } };
749 &DEBUG("count => $count.");
759 my ( $cmd, $chan, $what, $val ) = @_;
761 if ( $cmd eq 'chanadd' ) {
762 if ( exists $chanconf{$chan} ) {
763 &performStrictReply("chan $chan already exists.");
766 $chanconf{$chan}{_time_added} = time();
767 $chanconf{$chan}{autojoin} = $conn->nick();
769 &performStrictReply("Joining $chan...");
775 if ( !exists $chanconf{$chan} ) {
776 &performStrictReply("no such channel $chan");
782 if ( defined $what and $what =~ s/^([+-])(\S+)/$2/ ) {
784 ### '.chanset +blah 10' -- error.
786 my $set = ( $1 eq '+' ) ? 1 : 0;
787 my $was = $chanconf{$chan}{$what};
789 if ($set) { # add/set.
790 if ( defined $was and $was eq '1' ) {
791 &performStrictReply("setting $what for $chan already 1.");
798 else { # delete/unset.
799 if ( !defined $was ) {
800 &performStrictReply("setting $what for $chan is not set.");
807 # alter for cosmetic (print out) reasons only.
808 $was = ( defined $was ) ? "; was '$was'" : '';
811 &performStrictReply("Unsetting $what for $chan$was.");
812 delete $chanconf{$chan}{$what};
813 delete $cache{ircTextCounters} if $what eq 'ircTextCounters';
816 &performStrictReply("Setting $what for $chan to '$val'$was.");
817 $chanconf{$chan}{$what} = $val;
818 delete $cache{ircTextCounters} if $what eq 'ircTextCounters';
824 elsif ( defined $val ) {
825 ### '.chanset blah testing'
827 my $was = $chanconf{$chan}{$what};
828 if ( defined $was and $was eq $val ) {
829 &performStrictReply("setting $what for $chan already '$val'.");
832 $was = ($was) ? "; was '$was'" : '';
833 &performStrictReply("Setting $what for $chan to '$val'$was.");
835 $chanconf{$chan}{$what} = $val;
836 delete $cache{ircTextCounters} if $what eq 'ircTextCounters';
845 if ( !defined $what ) {
846 &WARN('chanset/DC: what == undefine.');
850 if ( exists $chanconf{$chan}{$what} ) {
851 &performStrictReply("$what for $chan is '$chanconf{$chan}{$what}'");
854 &performStrictReply("$what for $chan is not set.");
859 $utime_chanfile = time();
868 # this is an attempt to fix where an option is enabled but the module
869 # has been not loaded. it also can be used for other things.
871 foreach ( keys %{ $cache{confvars} } ) {
872 my $i = $cache{confvars}{$_};
873 &DEBUG("rehashConfVars: _ => $_");
875 if ( /^news$/ and $i ) {
876 &loadMyModule('News');
877 delete $cache{confvars}{$_};
880 if ( /^uptime$/ and $i ) {
881 &loadMyModule('Uptime');
882 delete $cache{confvars}{$_};
885 if ( /^rootwarn$/i and $i ) {
886 &loadMyModule('RootWarn');
887 delete $cache{confvars}{$_};
890 if ( /^onjoin$/i and $i ) {
891 &loadMyModule('OnJoin');
892 delete $cache{confvars}{$_};
896 &DEBUG('end of rehashConfVars');
898 delete $cache{confvars};
903 # vim:ts=4:sw=4:expandtab:tw=80