]> git.donarmstrong.com Git - infobot.git/blobdiff - src/DynaConfig.pl
OnJoin - tensai, fixes by TimRiker
[infobot.git] / src / DynaConfig.pl
index 2a78ebf249848e976c6579a2dacfd15f3d0e9185..e1cf464a1a68960ac691cf6b98e0002aa5639923 100644 (file)
@@ -6,22 +6,45 @@
 #         NOTE: Merged from User.pl
 #
 
-if (&IsParam("useStrict")) { use strict; }
+use strict;
+
+use vars qw(%chanconf %cache %bans %channels %nuh %users %ignore
+       %talkWho %dcc %mask);
+use vars qw($utime_userfile $ucount_userfile $utime_chanfile $who
+       $ucount_chanfile $userHandle $chan $msgType $talkchannel
+       $ident $bot_state_dir $talkWho $flag_quit $wtime_userfile
+       $wcount_userfile $wtime_chanfile $nuh $message);
 
 #####
 ##### USERFILE CONFIGURATION READER/WRITER
 #####
 
 sub readUserFile {
-    if (!open IN,"$bot_misc_dir/blootbot.users") {
-       &ERROR("cannot read userfile.");
+    my $f = "$bot_state_dir/blootbot.users";
+
+    if (! -f $f) {
+       &DEBUG("userfile not found; new fresh run detected.");
+       return;
+    }
+
+    if ( -f $f and -f "$f~") {
+       my $s1 = -s $f;
+       my $s2 = -s "$f~";
+
+       if ($s2 > $s1*3) {
+           &FIXME("rUF: backup file bigger than current file.");
+       }
+    }
+
+    if (!open IN, $f) {
+       &ERROR("Cannot read userfile ($f): $!");
        &closeLog();
        exit 1;
     }
 
     undef %users;      # clear on reload.
     undef %bans;       # reset.
-    undef %ingore;     # reset.
+    undef %ignore;     # reset.
 
     my $ver = <IN>;
     if ($ver !~ /^#v1/) {
@@ -46,6 +69,11 @@ sub readUserFile {
                next;
            }
 
+           if (!defined $nick) {
+               &WARN("DynaConfig: invalid line: $_");
+               next;
+           }
+
            # nice little hack.
            if ($what eq "HOSTS") {
                $users{$nick}{$what}{$val} = 1;
@@ -96,12 +124,17 @@ sub readUserFile {
 }
 
 sub writeUserFile {
-    if (!open OUT,">$bot_misc_dir/blootbot.users") {
-       &ERROR("cannot write to userfile.");
+    if (!scalar keys %users) {
+       &DEBUG("wUF: nothing to write.");
        return;
     }
 
-    my $time           = scalar(localtime);
+    if (!open OUT,">$bot_state_dir/blootbot.users") {
+       &ERROR("Cannot write userfile ($bot_state_dir/blootbot.users): $!");
+       return;
+    }
+
+    my $time           = scalar(gmtime);
 
     print OUT "#v1: blootbot -- $ident -- written $time\n\n";
 
@@ -127,6 +160,8 @@ sub writeUserFile {
                    print OUT "--$what\t\t$_\n";
                }
 
+           } elsif ($_ eq 'FLAGS') {
+               print OUT "--$_\t\t" . join('', sort split('', $val)) . "\n";
            } else {
                print OUT "--$_\t\t$val\n";
            }
@@ -200,8 +235,18 @@ sub writeUserFile {
 #####
 
 sub readChanFile {
-    if (!open IN,"$bot_misc_dir/blootbot.chan") {
-       &ERROR("cannot erad chanfile.");
+    my $f = "$bot_state_dir/blootbot.chan";
+    if ( -f $f and -f "$f~") {
+       my $s1 = -s $f;
+       my $s2 = -s "$f~";
+
+       if ($s2 > $s1*3) {
+           &FIXME("rCF: backup file bigger than current file.");
+       }
+    }
+
+    if (!open IN, $f) {
+       &ERROR("Cannot read chanfile ($f): $!");
        return;
     }
 
@@ -213,7 +258,8 @@ sub readChanFile {
     while (<IN>) {
        chop;
 
-       next if /^$/;
+       next if /^\s*$/;
+       next if /^\// or /^\;/; # / or ; are comment lines.
 
        if (/^(\S+)\s*$/) {
            $chan       = $1;
@@ -230,7 +276,7 @@ sub readChanFile {
            $cache{negative} = 1;
 #          $chanconf{$chan}{$1} = 0;
 
-       } elsif (/^[\s\t]+(\S+)[\ss\t]+(.*)$/) {# what = val.
+       } elsif (/^[\s\t]+(\S+)[\s\t]+(.*)$/) {# what = val.
            $chanconf{$chan}{$1} = $2;
 
        } else {
@@ -243,7 +289,8 @@ sub readChanFile {
     ### TODO: check against valid params.
     foreach $chan (keys %chanconf) {
        foreach (keys %{ $chanconf{$chan} }) {
-           next unless (/^[+-]/);
+           next unless /^[+-]/;
+
            &WARN("invalid param: chanconf{$chan}{$_}; removing.");
            delete $chanconf{$chan}{$_};
            undef $chanconf{$chan}{$_};
@@ -256,12 +303,17 @@ sub readChanFile {
 }
 
 sub writeChanFile {
-    if (!open OUT,">$bot_misc_dir/blootbot.chan") {
-       &ERROR("cannot write chanfile.");
+    if (!scalar keys %chanconf) {
+       &DEBUG("wCF: nothing to write.");
        return;
     }
 
-    my $time           = scalar(localtime);
+    if (!open OUT,">$bot_state_dir/blootbot.chan") {
+       &ERROR("Cannot write chanfile ($bot_state_dir/blootbot.chan): $!");
+       return;
+    }
+
+    my $time           = scalar(gmtime);
     print OUT "#v1: blootbot -- $ident -- written $time\n\n";
 
     if ($flag_quit) {
@@ -279,6 +331,7 @@ sub writeChanFile {
                next if ($chan eq "_default");
                next unless (exists $chanconf{$chan}{$opt});
                next unless ($val eq $chanconf{$chan}{$opt});
+
                push(@chans,$chan);
                delete $chanconf{$chan}{$opt};
            }
@@ -308,7 +361,7 @@ sub writeChanFile {
        }
 
        foreach (keys %opts) {
-           next unless ($opts{$_} > 1);
+           next unless ($opts{$_} > 2);
            &DEBUG("  opts{$_} => $opts{$_}");
        }
 
@@ -354,6 +407,8 @@ sub writeChanFile {
 ##### USER COMMANDS.
 #####
 
+# TODO: support multiple flags.
+# TODO: return all flags for opers
 sub IsFlag {
     my $flags = shift;
     my ($ret, $f, $o) = "";
@@ -374,7 +429,7 @@ sub IsFlag {
 
 sub verifyUser {
     my ($nick, $lnuh) = @_;
-    my ($user,$m);
+    my ($user, $m);
 
     if ($userHandle = $dcc{'CHATvrfy'}{$who}) {
        &VERB("vUser: cached auth for $who.",2);
@@ -386,7 +441,7 @@ sub verifyUser {
     foreach $user (keys %users) {
        next if ($user eq "_default");
 
-       foreach $m (keys %{$users{$user}{HOSTS}}) {
+       foreach $m (keys %{ $users{$user}{HOSTS} }) {
            $m =~ s/\?/./g;
            $m =~ s/\*/.*?/g;
            $m =~ s/([\@\(\)\[\]])/\\$1/g;
@@ -453,6 +508,7 @@ sub hasFlag {
     }
 }
 
+# expire is time in minutes
 sub ignoreAdd {
     my($mask,$chan,$expire,$comment) = @_;
 
@@ -462,7 +518,7 @@ sub ignoreAdd {
     my $count  ||= 0;
 
     if ($expire > 0) {
-       $expire         = $expire*60 + time();
+       $expire         = ($expire*60) + time();
     } else {
        $expire         = 0;
     }
@@ -472,6 +528,13 @@ sub ignoreAdd {
 
     $ignore{$chan}{$mask} = [$expire, time(), $who, $comment];
 
+    # TODO: improve this.
+    if ($expire == 0) {
+       &status("ignore: Added $mask for $chan to NEVER expire, by $who, for $comment");
+    } else {
+       &status("ignore: Added $mask for $chan to expire $expire mins, by $who, for $comment");
+    }
+
     if ($exist) {
        $utime_userfile = time();
        $ucount_userfile++;
@@ -516,7 +579,11 @@ sub userAdd {
     $utime_userfile = time();
     $ucount_userfile++;
 
-    $users{$nick}{HOSTS}{$mask} = 1;
+    if (defined $mask and $mask !~ /^\s*$/) {
+       &DEBUG("userAdd: mask => $mask");
+       $users{$nick}{HOSTS}{$mask} = 1;
+    }
+
     $users{$nick}{FLAGS}       ||= $users{_default}{FLAGS};
 
     return 1;
@@ -632,85 +699,95 @@ sub chanSet {
 
     if ($cmd eq "+chan") {
        if (exists $chanconf{$chan}) {
-           &pSReply("chan $chan already exists.");
+           &performStrictReply("chan $chan already exists.");
            return;
        }
        $chanconf{$chan}{_time_added}   = time();
-       $chanconf{$what}{autojoin}      = 1;
+       $chanconf{$chan}{autojoin}      = $conn->nick();
 
-       &pSReply("Joining $chan...");
+       &performStrictReply("Joining $chan...");
        &joinchan($chan);
 
        return;
     }
 
     if (!exists $chanconf{$chan}) {
-       &pSReply("no such channel $chan");
+       &performStrictReply("no such channel $chan");
        return;
     }
 
     my $update = 0;
 
-    ### ".chanset +blah"
-    ### ".chanset +blah 10"            -- error.
     if (defined $what and $what =~ s/^([+-])(\S+)/$2/) {
+       ### ".chanset +blah"
+       ### ".chanset +blah 10"         -- error.
+
        my $state       = ($1 eq "+") ? 1 : 0;
        my $was         = $chanconf{$chan}{$what};
 
        if ($state) {                   # add/set.
            if (defined $was and $was eq "1") {
-               &pSReply("setting $what for $chan already 1.");
+               &performStrictReply("setting $what for $chan already 1.");
                return;
            }
 
-           $was        = ($was) ? "; was '$was'" : "";
            $val        = 1;
 
        } else {                        # delete/unset.
            if (!defined $was) {
-               &pSReply("setting $what for $chan is not set.");
+               &performStrictReply("setting $what for $chan is not set.");
                return;
            }
 
            if ($was eq "0") {
-               &pSReply("setting $what for $chan already 0.");
+               &performStrictReply("setting $what for $chan already 0.");
                return;
            }
 
-           $was        = ($was) ? "; was '$was'" : "";
            $val        = 0;
        }
 
-       $chanconf{$chan}{$what} = $val;
-       &pSReply("Setting $what for $chan to '$val'$was.");
+       # alter for cosmetic (print out) reasons only.
+       $was    = ($was) ? "; was '$was'" : "";
+
+       if ($val eq "0") {
+           &performStrictReply("Unsetting $what for $chan$was.");
+           delete $chanconf{$chan}{$what};
+       } else {
+           &performStrictReply("Setting $what for $chan to '$val'$was.");
+           $chanconf{$chan}{$what}     = $val;
+       }
+
        $update++;
 
-    ### ".chanset blah testing"
     } elsif (defined $val) {
+       ### ".chanset blah testing"
+
        my $was = $chanconf{$chan}{$what};
        if (defined $was and $was eq $val) {
-           &pSReply("setting $what for $chan already '$val'.");
+           &performStrictReply("setting $what for $chan already '$val'.");
            return;
        }
        $was    = ($was) ? "; was '$was'" : "";
-       &pSReply("Setting $what for $chan to '$val'$was.");
+       &performStrictReply("Setting $what for $chan to '$val'$was.");
 
        $chanconf{$chan}{$what} = $val;
 
        $update++;
 
-    ### ".chanset"
-    ### ".chanset blah"
     } else {                           # read only.
+       ### ".chanset"
+       ### ".chanset blah"
+
        if (!defined $what) {
            &WARN("chanset/DC: what == undefine.");
            return;
        }
 
        if (exists $chanconf{$chan}{$what}) {
-           &pSReply("$what for $chan is '$chanconf{$chan}{$what}'");
+           &performStrictReply("$what for $chan is '$chanconf{$chan}{$what}'");
        } else {
-           &pSReply("$what for $chan is not set.");
+           &performStrictReply("$what for $chan is not set.");
        }
     }
 
@@ -722,20 +799,55 @@ sub chanSet {
     return;
 }
 
-my @regFlagsChan = (
-       "autojoin",
-       "freshmeat",
-       "limitcheckInterval",
-       "limitcheckPlus",
-       "allowConv",
-       "allowDNS",
-### TODO: finish off this list.
-);
+sub rehashConfVars {
+    # this is an attempt to fix where an option is enabled but the module
+    # has been not loaded. it also can be used for other things.
+
+    foreach (keys %{ $cache{confvars} }) {
+       my $i = $cache{confvars}{$_};
+       &DEBUG("rehashConfVars: _ => $_");
+
+       if (/^news$/ and $i) {
+           &loadMyModule('News');
+           delete $cache{confvars}{$_};
+       }
+
+       if (/^uptime$/ and $i) {
+           &loadMyModule('Uptime');
+           delete $cache{confvars}{$_};
+       }
+
+       if (/^rootwarn$/i and $i) {
+           &loadMyModule('RootWarn');
+           delete $cache{confvars}{$_};
+       }
+
+       if (/^onjoin$/i and $i) {
+           &loadMyModule('OnJoin');
+           delete $cache{confvars}{$_};
+       }
+    }
+
+    &DEBUG("end of rehashConfVars");
+
+    delete $cache{confvars};
+}
 
 my @regFlagsUser = (
-       "m",            # master
-       "n",            # owner
-       "o",            # op
-);     # todo...
+       # possible chars to include in FLAG
+       "A",    # bot administration over /msg
+                       # default is only via DCC CHAT
+       "O",    # dynamic ops (as on channel). (automatic +o)
+       "T",    # add topics.
+       "a",    # ask/request factoid.
+       "m",    # modify factoid. (includes renaming)
+       "n",    # bot owner, can "reload"
+       "o",    # master of bot (automatic +amrt)
+                       # can search on factoid strings shorter than 2 chars
+                       # can tell bot to join new channels
+                       # can [un]lock factoids
+       "r",    # remove factoid.
+       "t",    # teach/add factoid.
+);
 
 1;