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 factoid. (includes renaming)
27 'n', # bot owner, can 'reload'
28 'o', # master of bot (automatic +amrt)
29 # can search on factoid strings shorter than 2 chars
30 # can tell bot to join new channels
31 # can [un]lock factoids
32 'r', # remove factoid.
33 't', # teach/add factoid.
34 's', # Bypass +silent on channels
38 ##### USERFILE CONFIGURATION READER/WRITER
42 my $f = "$bot_state_dir/infobot.users";
45 &DEBUG("userfile not found; new fresh run detected.");
49 if ( -f $f and -f "$f~" ) {
53 if ( $s2 > $s1 * 3 ) {
54 &FIXME("rUF: backup file bigger than current file.");
59 &ERROR("Cannot read userfile ($f): $!");
64 undef %users; # clear on reload.
66 undef %ignore; # reset.
69 if ( $ver !~ /^#v1/ ) {
70 &ERROR("old or invalid user file found.");
83 if (/^--(\S+)[\s\t]+(.*)$/) { # user: middle entry.
84 my ( $what, $val ) = ( $1, $2 );
86 if ( !defined $val or $val eq '' ) {
87 &WARN("$what: val == NULL.");
91 if ( !defined $nick ) {
92 &WARN("DynaConfig: invalid line: $_");
97 if ( $what eq 'HOSTS' ) {
98 $users{$nick}{$what}{$val} = 1;
101 $users{$nick}{$what} = $val;
105 elsif (/^(\S+)$/) { # user: start entry.
109 elsif (/^::(\S+) ignore$/) { # ignore: start entry.
114 elsif ( /^- (\S+):\+(\d+):\+(\d+):(\S+):(.*)$/ and $type eq 'ignore' ) {
115 ### ignore: middle entry.
117 my (@array) = ( $2, $3, $4, $5 );
118 ### DEBUG purposes only!
119 if ( $mask !~ /^$mask{nuh}$/ ) {
120 &WARN("ignore: mask $mask is invalid.");
123 $ignore{$chan}{$mask} = \@array;
126 elsif (/^::(\S+) bans$/) { # bans: start entry.
131 elsif ( /^- (\S+):\+(\d+):\+(\d+):(\d+):(\S+):(.*)$/
132 and $type eq 'bans' )
134 ### bans: middle entry.
135 # $btime, $atime, $count, $whoby, $reason.
136 my (@array) = ( $2, $3, $4, $5, $6 );
137 $bans{$chan}{$1} = \@array;
141 &WARN("unknown line: $_");
148 "USERFILE: Loaded: %d users, %d bans, %d ignore",
149 scalar( keys %users ) - 1,
150 scalar( keys %bans ), # ??
151 scalar( keys %ignore ), # ??
157 if ( !scalar keys %users ) {
158 &DEBUG("wUF: nothing to write.");
162 if ( !open OUT, ">$bot_state_dir/infobot.users" ) {
163 &ERROR("Cannot write userfile ($bot_state_dir/infobot.users): $!");
167 my $time = scalar(gmtime);
169 print OUT "#v1: infobot -- $ident -- written $time\n\n";
173 foreach ( sort keys %users ) {
176 my $count = scalar keys %{ $users{$user} };
178 &WARN("user $user has no other attributes; skipping.");
184 foreach ( sort keys %{ $users{$user} } ) {
186 my $val = $users{$user}{$_};
188 if ( ref($val) eq 'HASH' ) {
189 foreach ( sort keys %{ $users{$user}{$_} } ) {
190 print OUT "--$what\t\t$_\n";
194 elsif ( $_ eq 'FLAGS' ) {
196 . join( '', sort split( '', $val ) ) . "\n";
199 print OUT "--$_\t\t$val\n";
207 foreach ( keys %bans ) {
211 my $count = scalar keys %{ $bans{$chan} };
213 &WARN("bans: chan $chan has no other attributes; skipping.");
217 print OUT "::$chan bans\n";
218 foreach ( keys %{ $bans{$chan} } ) {
220 # format: bans: mask expire time-added count who-added reason
221 my @array = @{ $bans{$chan}{$_} };
222 if ( scalar @array != 5 ) {
223 &WARN("bans: $chan/$_ is corrupted.");
227 printf OUT "- %s:+%d:+%d:%d:%s:%s\n", $_, @array;
230 print OUT "\n" if ($cbans);
234 foreach ( keys %ignore ) {
238 my $count = scalar keys %{ $ignore{$chan} };
240 &WARN("ignore: chan $chan has no other attributes; skipping.");
244 ### TODO: use hash instead of array for flexibility?
245 print OUT "::$chan ignore\n";
246 foreach ( keys %{ $ignore{$chan} } ) {
248 # format: ignore: mask expire time-added who-added reason
249 my @array = @{ $ignore{$chan}{$_} };
250 if ( scalar @array != 4 ) {
251 &WARN("ignore: $chan/$_ is corrupted.");
255 printf OUT "- %s:+%d:+%d:%s:%s\n", $_, @array;
261 $wtime_userfile = time();
263 "--- Saved USERFILE ($cusers users; $cbans bans; $cignore ignore) at $time"
265 if ( defined $msgType and $msgType =~ /^chat$/ ) {
266 &performStrictReply("--- Writing user file...");
271 ##### CHANNEL CONFIGURATION READER/WRITER
275 my $f = "$bot_state_dir/infobot.chan";
276 if ( -f $f and -f "$f~" ) {
280 if ( $s2 > $s1 * 3 ) {
281 &FIXME("rCF: backup file bigger than current file.");
285 if ( !open IN, $f ) {
286 &ERROR("Cannot read chanfile ($f): $!");
290 undef %chanconf; # reset.
292 $_ = <IN>; # version string.
299 next if /^\// or /^\;/; # / or ; are comment lines.
305 next unless ( defined $chan );
307 if (/^[\s\t]+\+(\S+)$/) { # bool, true.
308 $chanconf{$chan}{$1} = 1;
311 elsif (/^[\s\t]+\-(\S+)$/) { # bool, false.
312 # although this is supported in run-time configuration.
313 $chanconf{$chan}{$1} = 0;
316 elsif (/^[\s\t]+(\S+)[\s\t]+(.*)$/) { # what = val.
317 $chanconf{$chan}{$1} = $2;
321 &WARN("unknown line: $_") unless (/^#/);
326 # verify configuration
327 ### TODO: check against valid params.
328 foreach $chan ( keys %chanconf ) {
329 foreach ( keys %{ $chanconf{$chan} } ) {
332 &WARN("invalid param: chanconf{$chan}{$_}; removing.");
333 delete $chanconf{$chan}{$_};
334 undef $chanconf{$chan}{$_};
339 "CHANFILE: Loaded: " . ( scalar( keys %chanconf ) - 1 ) . " chans" );
343 if ( !scalar keys %chanconf ) {
344 &DEBUG("wCF: nothing to write.");
348 if ( !open OUT, ">$bot_state_dir/infobot.chan" ) {
349 &ERROR("Cannot write chanfile ($bot_state_dir/infobot.chan): $!");
353 my $time = scalar(gmtime);
354 print OUT "#v1: infobot -- $ident -- written $time\n\n";
358 ### Process 1: if defined in _default, remove same definition
359 ### from non-default channels.
360 foreach ( keys %{ $chanconf{_default} } ) {
362 my $val = $chanconf{_default}{$opt};
365 foreach ( keys %chanconf ) {
368 next if ( $chan eq "_default" );
369 next unless ( exists $chanconf{$chan}{$opt} );
370 next unless ( $val eq $chanconf{$chan}{$opt} );
372 push( @chans, $chan );
373 delete $chanconf{$chan}{$opt};
376 if ( scalar @chans ) {
378 "Removed config $opt to @chans since it's defiend in '_default'"
383 ### Process 2: if defined in all chans but _default, set in
384 ### _default and remove all others.
385 my ( %optsval, %opts );
386 foreach ( keys %chanconf ) {
388 next if ( $chan eq "_default" );
391 foreach ( keys %{ $chanconf{$chan} } ) {
393 if ( exists $optsval{$opt}
394 and $optsval{$opt} eq $chanconf{$chan}{$opt} )
399 $optsval{$opt} = $chanconf{$chan}{$opt};
404 foreach ( keys %opts ) {
405 next unless ( $opts{$_} > 2 );
406 &DEBUG(" opts{$_} => $opts{$_}");
409 ### other optimizations are in UserDCC.pl
413 foreach ( sort keys %chanconf ) {
418 foreach ( sort keys %{ $chanconf{$chan} } ) {
419 my $val = $chanconf{$chan}{$_};
421 if ( $val =~ /^0$/ ) { # bool, false.
425 elsif ( $val =~ /^1$/ ) { # bool, true.
430 print OUT " $_ $val\n";
440 $wtime_chanfile = time();
441 &status("--- Saved CHANFILE ("
442 . scalar( keys %chanconf )
443 . " chans) at $time" );
445 if ( defined $msgType and $msgType =~ /^chat$/ ) {
446 &performStrictReply("--- Writing chan file...");
454 # TODO: support multiple flags.
455 # TODO: return all flags for opers
458 my ( $ret, $f, $o ) = '';
460 &verifyUser( $who, $nuh );
462 foreach $f ( split //, $users{$userHandle}{FLAGS} ) {
463 foreach $o ( split //, $flags ) {
464 next unless ( $f eq $o );
475 my ( $nick, $lnuh ) = @_;
478 if ( $userHandle = $dcc{'CHATvrfy'}{$who} ) {
479 &VERB( "vUser: cached auth for $who.", 2 );
485 foreach $user ( keys %users ) {
486 next if ( $user eq "_default" );
488 foreach $m ( keys %{ $users{$user}{HOSTS} } ) {
491 $m =~ s/([\@\(\)\[\]])/\\$1/g;
493 next unless ( $lnuh =~ /^$m$/i );
495 if ( $user !~ /^\Q$nick\E$/i and !exists $cache{VUSERWARN}{$user} )
497 &status("vU: host matched but diff nick ($nick != $user).");
498 $cache{VUSERWARN}{$user} = 1;
505 last if ( $userHandle ne '' );
507 if ( $user =~ /^\Q$nick\E$/i and !exists $cache{VUSERWARN}{$user} ) {
508 &status("vU: nick matched but host is not in list ($lnuh).");
509 $cache{VUSERWARN}{$user} = 1;
513 $userHandle ||= "_default";
515 # what's talkchannel for?
516 $talkWho{$talkchannel} = $who if ( defined $talkchannel );
524 # returns true if arg1 encrypts to arg2
525 my ( $plain, $encrypted ) = @_;
526 if ( $encrypted eq '' ) {
527 ( $plain, $encrypted ) = split( /\s+/, $plain, 2 );
529 return 0 unless ( $plain ne '' and $encrypted ne '' );
531 # MD5 // DES. Bobby Billingsley++.
533 if ( $encrypted =~ /^(\S{2})/ and length $encrypted == 13 ) {
536 elsif ( $encrypted =~ /^\$\d\$(\w\w)\$/ ) {
540 &DEBUG("unknown salt from $encrypted.");
544 return ( $encrypted eq crypt( $plain, $salt ) );
547 # mainly for dcc chat... hrm.
551 if ( &IsFlag($flag) eq $flag ) {
555 &status("DCC CHAT: <$who> $message -- not enough flags.");
557 "error: you do not have enough flags for that. ($flag required)");
562 # expire is time in minutes
564 my ( $mask, $chan, $expire, $comment ) = @_;
566 $chan ||= '*'; # global if undefined.
567 $comment ||= ''; # optional.
568 $expire ||= 0; # permament.
572 $expire = ( $expire * 60 ) + time();
579 $exist++ if ( exists $ignore{$chan}{$mask} );
581 $ignore{$chan}{$mask} = [ $expire, time(), $who, $comment ];
583 # TODO: improve this.
584 if ( $expire == 0 ) {
586 "ignore: Added $mask for $chan to NEVER expire, by $who, for $comment"
591 "ignore: Added $mask for $chan to expire $expire mins, by $who, for $comment"
596 $utime_userfile = time();
610 ### TODO: support wildcards.
611 foreach ( keys %ignore ) {
614 foreach ( grep /^\Q$mask\E$/i, keys %{ $ignore{$chan} } ) {
615 delete $ignore{$chan}{$mask};
616 push( @match, $chan );
619 &DEBUG( "iD: scalar => " . scalar( keys %{ $ignore{$chan} } ) );
622 if ( scalar @match ) {
623 $utime_userfile = time();
631 my ( $nick, $mask ) = @_;
633 if ( exists $users{$nick} ) {
637 $utime_userfile = time();
640 if ( defined $mask and $mask !~ /^\s*$/ ) {
641 &DEBUG("userAdd: mask => $mask");
642 $users{$nick}{HOSTS}{$mask} = 1;
645 $users{$nick}{FLAGS} ||= $users{_default}{FLAGS};
653 if ( !exists $users{$nick} ) {
657 $utime_userfile = time();
660 delete $users{$nick};
666 my ( $mask, $chan, $expire, $reason ) = @_;
672 $expire = $expire * 60 + time();
676 $exist++ if ( exists $bans{$chan}{$mask}
677 or exists $bans{'*'}{$mask} );
678 $bans{$chan}{$mask} = [ $expire, time(), 0, $who, $reason ];
680 my @chans = ( $chan eq '*' ) ? keys %channels : $chan;
686 foreach ( keys %{ $channels{$chan}{''} } ) {
687 next unless ( exists $nuh{ lc $_ } );
688 next unless ( $nuh{ lc $_ } =~ /^$m$/i );
689 &FIXME("nuh{$_} =~ /$m/");
694 $utime_userfile = time();
705 foreach ( keys %bans ) {
708 foreach ( grep /^\Q$mask\E$/i, keys %{ $bans{$chan} } ) {
709 delete $bans{$chan}{$_};
710 push( @match, $chan );
713 &DEBUG( "bans: scalar => " . scalar( keys %{ $bans{$chan} } ) );
716 if ( scalar @match ) {
717 $utime_userfile = time();
727 if ( &getUser($user) ) {
738 if ( !defined $user ) {
739 &WARN("getUser: user == NULL.");
743 if ( my @retval = grep /^\Q$user\E$/i, keys %users ) {
744 if ( $retval[0] ne $user ) {
745 &WARN("getUser: retval[0] ne user ($retval[0] ne $user)");
747 my $count = scalar keys %{ $users{ $retval[0] } };
748 &DEBUG("count => $count.");
758 my ( $cmd, $chan, $what, $val ) = @_;
760 if ( $cmd eq "+chan" ) {
761 if ( exists $chanconf{$chan} ) {
762 &performStrictReply("chan $chan already exists.");
765 $chanconf{$chan}{_time_added} = time();
766 $chanconf{$chan}{autojoin} = $conn->nick();
768 &performStrictReply("Joining $chan...");
774 if ( !exists $chanconf{$chan} ) {
775 &performStrictReply("no such channel $chan");
781 if ( defined $what and $what =~ s/^([+-])(\S+)/$2/ ) {
783 ### ".chanset +blah 10" -- error.
785 my $set = ( $1 eq "+" ) ? 1 : 0;
786 my $was = $chanconf{$chan}{$what};
788 if ($set) { # add/set.
789 if ( defined $was and $was eq '1' ) {
790 &performStrictReply("setting $what for $chan already 1.");
797 else { # delete/unset.
798 if ( !defined $was ) {
799 &performStrictReply("setting $what for $chan is not set.");
806 # alter for cosmetic (print out) reasons only.
807 $was = ( defined $was ) ? "; was '$was'" : '';
810 &performStrictReply("Unsetting $what for $chan$was.");
811 delete $chanconf{$chan}{$what};
812 delete $cache{ircTextCounters} if $what eq 'ircTextCounters';
815 &performStrictReply("Setting $what for $chan to '$val'$was.");
816 $chanconf{$chan}{$what} = $val;
817 delete $cache{ircTextCounters} if $what eq 'ircTextCounters';
823 elsif ( defined $val ) {
824 ### ".chanset blah testing"
826 my $was = $chanconf{$chan}{$what};
827 if ( defined $was and $was eq $val ) {
828 &performStrictReply("setting $what for $chan already '$val'.");
831 $was = ($was) ? "; was '$was'" : '';
832 &performStrictReply("Setting $what for $chan to '$val'$was.");
834 $chanconf{$chan}{$what} = $val;
835 delete $cache{ircTextCounters} if $what eq 'ircTextCounters';
844 if ( !defined $what ) {
845 &WARN("chanset/DC: what == undefine.");
849 if ( exists $chanconf{$chan}{$what} ) {
850 &performStrictReply("$what for $chan is '$chanconf{$chan}{$what}'");
853 &performStrictReply("$what for $chan is not set.");
858 $utime_chanfile = time();
867 # this is an attempt to fix where an option is enabled but the module
868 # has been not loaded. it also can be used for other things.
870 foreach ( keys %{ $cache{confvars} } ) {
871 my $i = $cache{confvars}{$_};
872 &DEBUG("rehashConfVars: _ => $_");
874 if ( /^news$/ and $i ) {
875 &loadMyModule('News');
876 delete $cache{confvars}{$_};
879 if ( /^uptime$/ and $i ) {
880 &loadMyModule('Uptime');
881 delete $cache{confvars}{$_};
884 if ( /^rootwarn$/i and $i ) {
885 &loadMyModule('RootWarn');
886 delete $cache{confvars}{$_};
889 if ( /^onjoin$/i and $i ) {
890 &loadMyModule('OnJoin');
891 delete $cache{confvars}{$_};
895 &DEBUG("end of rehashConfVars");
897 delete $cache{confvars};
902 # vim:ts=4:sw=4:expandtab:tw=80