]> git.donarmstrong.com Git - infobot.git/blobdiff - src/DynaConfig.pl
- added top3 irctextcounter stats
[infobot.git] / src / DynaConfig.pl
index bfc5dd151200680dda55f8e6572186b345b81ff5..9fa01ddd57fe6ca88eb9ad7a21c1a081c28b4600 100644 (file)
@@ -13,12 +13,32 @@ if (&IsParam("useStrict")) { use strict; }
 #####
 
 sub readUserFile {
-    if (!open IN,"$bot_misc_dir/blootbot.users") {
+    my $f = "$bot_misc_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) {
+           &DEBUG("rUF: backup file bigger than current file. FIXME");
+       }
+    }
+
+    if (!open IN, $f) {
        &ERROR("cannot read userfile.");
        &closeLog();
        exit 1;
     }
 
+    undef %users;      # clear on reload.
+    undef %bans;       # reset.
+    undef %ingore;     # reset.
+
     my $ver = <IN>;
     if ($ver !~ /^#v1/) {
        &ERROR("old or invalid user file found.");
@@ -27,6 +47,7 @@ sub readUserFile {
     }
 
     my $nick;
+    my $type;
     while (<IN>) {
        chop;
 
@@ -36,11 +57,16 @@ sub readUserFile {
        if (/^--(\S+)[\s\t]+(.*)$/) {           # user: middle entry.
            my ($what,$val) = ($1,$2);
 
+           if (!defined $val or $val eq "") {
+               &WARN("$what: val == NULL.");
+               next;
+           }
+
            # nice little hack.
            if ($what eq "HOSTS") {
-               $users{$nick}{$1}{$2} = 1;
+               $users{$nick}{$what}{$val} = 1;
            } else {
-               $users{$nick}{$1} = $2;
+               $users{$nick}{$what} = $val;
            }
 
        } elsif (/^(\S+)$/) {                   # user: start entry.
@@ -48,17 +74,24 @@ sub readUserFile {
 
        } elsif (/^::(\S+) ignore$/) {          # ignore: start entry.
            $chan       = $1;
+           $type       = "ignore";
 
-       } elsif (/^- (\S+):+(\d+):(\S+):(\d+):(.*)$/) {
+       } elsif (/^- (\S+):\+(\d+):\+(\d+):(\S+):(.*)$/ and $type eq "ignore") {
            ### ignore: middle entry.
-           # $mask, $count?, $whoby, $atime, $comment.
+           my $mask = $1;
            my(@array) = ($2,$3,$4,$5);
-           $ignore{$chan}{$1} = \@array;
+           ### DEBUG purposes only!
+           if ($mask !~ /^$mask{nuh}$/) {
+               &WARN("ignore: mask $mask is invalid.");
+               next;
+           }
+           $ignore{$chan}{$mask} = \@array;
 
        } elsif (/^::(\S+) bans$/) {            # bans: start entry.
            $chan       = $1;
+           $type       = "bans";
 
-       } elsif (/^- (\S+):+(\d+):+(\d+):(\d+):(\S+):(.*)$/) {
+       } elsif (/^- (\S+):\+(\d+):\+(\d+):(\d+):(\S+):(.*)$/ and $type eq "bans") {
            ### bans: middle entry.
            # $btime, $atime, $count, $whoby, $reason.
            my(@array) = ($2,$3,$4,$5,$6);
@@ -72,13 +105,18 @@ sub readUserFile {
 
     &status( sprintf("USERFILE: Loaded: %d users, %d bans, %d ignore",
                scalar(keys %users)-1,
-               scalar(keys %bans),
-               scalar(keys %ignore),
+               scalar(keys %bans),             # ??
+               scalar(keys %ignore),           # ??
        )
     );
 }
 
 sub writeUserFile {
+    if (!scalar keys %users) {
+       &DEBUG("wUF: nothing to write.");
+       return;
+    }
+
     if (!open OUT,">$bot_misc_dir/blootbot.users") {
        &ERROR("cannot write to userfile.");
        return;
@@ -130,12 +168,15 @@ sub writeUserFile {
        }
 
        print OUT "::$chan bans\n";
-       &DEBUG("::$chan bans");
        foreach (keys %{ $bans{$chan} }) {
-           printf OUT "- %s:+%d:+%d:%d:%s:%s\n",
-           $_, @{ $bans{$chan}{$_} };
-           &DEBUG( sprintf("- %s:+%d:+%d:%d:%s:%s\n",
-           $_, @{ $bans{$chan}{$_} } ));
+# format: bans: mask expire time-added count who-added reason
+           my @array = @{ $bans{$chan}{$_} };
+           if (scalar @array != 5) {
+               &WARN("bans: $chan/$_ is corrupted.");
+               next;
+           }
+
+           printf OUT "- %s:+%d:+%d:%d:%s:%s\n", $_, @array;
        }
     }
     print OUT "\n" if ($cbans);
@@ -152,13 +193,17 @@ sub writeUserFile {
            next;
        }
 
+       ### TODO: use hash instead of array for flexibility?
        print OUT "::$chan ignore\n";
-       &DEBUG("::$chan ignore");
        foreach (keys %{ $ignore{$chan} }) {
-           printf OUT "- %s:+%d:%s:%d:%s\n",
-           $_, @{ $bans{$chan}{$_} };
-           &DEBUG( sprintf("- %s:+%d:%s:%d:%s\n",
-                   $_, @{ $bans{$chan}{$_} } ));
+# format: ignore: mask expire time-added who-added reason
+           my @array = @{ $ignore{$chan}{$_} };
+           if (scalar @array != 4) {
+               &WARN("ignore: $chan/$_ is corrupted.");
+               next;
+           }
+
+           printf OUT "- %s:+%d:+%d:%s:%s\n", $_, @array;
        }
     }
 
@@ -176,12 +221,24 @@ sub writeUserFile {
 #####
 
 sub readChanFile {
-    if (!open IN,"$bot_misc_dir/blootbot.chan") {
+    my $f = "$bot_misc_dir/blootbot.chan";
+    if ( -f $f and -f "$f~") {
+       my $s1 = -s $f;
+       my $s2 = -s "$f~";
+
+       if ($s2 > $s1*3) {
+           &DEBUG("rCF: backup file bigger than current file. FIXME");
+       }
+    }
+
+    if (!open IN, $f) {
        &ERROR("cannot erad chanfile.");
        return;
     }
 
-    $_ = <IN>; # version string.
+    undef %chanconf;   # reset.
+
+    $_ = <IN>;         # version string.
 
     my $chan;
     while (<IN>) {
@@ -199,7 +256,10 @@ sub readChanFile {
            $chanconf{$chan}{$1} = 1;
 
        } elsif (/^[\s\t]+\-(\S+)$/) {          # bool, false.
-           $chanconf{$chan}{$1} = 0;
+           &DEBUG("deprecated support of negative options.") unless ($cache{negative});
+           # although this is supported in run-time configuration.
+           $cache{negative} = 1;
+#          $chanconf{$chan}{$1} = 0;
 
        } elsif (/^[\s\t]+(\S+)[\ss\t]+(.*)$/) {# what = val.
            $chanconf{$chan}{$1} = $2;
@@ -210,10 +270,28 @@ sub readChanFile {
     }
     close IN;
 
+    # verify configuration
+    ### TODO: check against valid params.
+    foreach $chan (keys %chanconf) {
+       foreach (keys %{ $chanconf{$chan} }) {
+           next unless (/^[+-]/);
+           &WARN("invalid param: chanconf{$chan}{$_}; removing.");
+           delete $chanconf{$chan}{$_};
+           undef $chanconf{$chan}{$_};
+       }
+    }
+
+    delete $cache{negative};
+
     &status("CHANFILE: Loaded: ".(scalar(keys %chanconf)-1)." chans");
 }
 
 sub writeChanFile {
+    if (!scalar keys %chanconf) {
+       &DEBUG("wCF: nothing to write.");
+       return;
+    }
+
     if (!open OUT,">$bot_misc_dir/blootbot.chan") {
        &ERROR("cannot write chanfile.");
        return;
@@ -265,8 +343,8 @@ sub writeChanFile {
            }
        }
 
-       &DEBUG("chans => ".scalar(keys %chanconf)." - 1");
        foreach (keys %opts) {
+           next unless ($opts{$_} > 1);
            &DEBUG("  opts{$_} => $opts{$_}");
        }
 
@@ -316,6 +394,8 @@ sub IsFlag {
     my $flags = shift;
     my ($ret, $f, $o) = "";
 
+    &verifyUser($who, $nuh);
+
     foreach $f (split //, $users{$userHandle}{FLAGS}) {
        foreach $o ( split //, $flags ) {
            next unless ($f eq $o);
@@ -332,20 +412,26 @@ sub verifyUser {
     my ($nick, $lnuh) = @_;
     my ($user,$m);
 
+    if ($userHandle = $dcc{'CHATvrfy'}{$who}) {
+       &VERB("vUser: cached auth for $who.",2);
+       return $userHandle;
+    }
+
     $userHandle = "";
 
     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;
 
            next unless ($lnuh =~ /^$m$/i);
 
-           if ($user !~ /^\Q$nick\E$/i) {
+           if ($user !~ /^\Q$nick\E$/i and !exists $cache{VUSERWARN}{$user}) {
                &status("vU: host matched but diff nick ($nick != $user).");
+               $cache{VUSERWARN}{$user} = 1;
            }
 
            $userHandle = $user;
@@ -354,12 +440,14 @@ sub verifyUser {
 
        last if ($userHandle ne "");
 
-       if ($user =~ /^\Q$nick\E$/i) {
+       if ($user =~ /^\Q$nick\E$/i and !exists $cache{VUSERWARN}{$user}) {
            &status("vU: nick matched but host is not in list ($lnuh).");
+           $cache{VUSERWARN}{$user} = 1;
        }
     }
 
     $userHandle ||= "_default";
+    # what's talkchannel for?
     $talkWho{$talkchannel} = $who if (defined $talkchannel);
     $talkWho = $who;
 
@@ -375,9 +463,14 @@ sub ckpasswd {
     return 0 unless ($plain ne "" and $encrypted ne "");
 
     # MD5 // DES. Bobby Billingsley++.
-    my $salt = substr($encrypted, 0, 2);
-    if ($encrypted =~ /^\$\d\$(\w\w)\$/) {
+    my $salt;
+    if ($encrypted =~ /^(\S{2})/ and length $encrypted == 13) {
+       $salt = $1;
+    } elsif ($encrypted =~ /^\$\d\$(\w\w)\$/) {
        $salt = $1;
+    } else {
+       &DEBUG("unknown salt from $encrypted.");
+       return 0;
     }
 
     return ($encrypted eq crypt($plain, $salt));
@@ -410,14 +503,17 @@ sub ignoreAdd {
        $expire         = 0;
     }
 
-    $ignore{$chan}{$mask} = [$expire, $count, $who, time(), $comment];
+    my $exist  = 0;
+    $exist++ if (exists $ignore{$chan}{$mask});
 
-    if (exists $ignore{$chan}{$mask}) {
-       return 0;
-    } else {
+    $ignore{$chan}{$mask} = [$expire, time(), $who, $comment];
+
+    if ($exist) {
        $utime_userfile = time();
        $ucount_userfile++;
 
+       return 2;
+    } else {
        return 1;
     }
 }
@@ -457,7 +553,7 @@ sub userAdd {
     $ucount_userfile++;
 
     $users{$nick}{HOSTS}{$mask} = 1;
-    $users{$nick}{FLAGS}       = $users{_default}{FLAGS};
+    $users{$nick}{FLAGS}       ||= $users{_default}{FLAGS};
 
     return 1;
 }
@@ -489,8 +585,21 @@ sub banAdd {
 
     my $exist  = 1;
     $exist++ if (exists $bans{$chan}{$mask} or
-               exists $bans{_default}{$mask});
-    $bans{$chan}{$mask} = [$expire, 0, $who, time(), $reason];
+               exists $bans{'*'}{$mask});
+    $bans{$chan}{$mask} = [$expire, time(), 0, $who, $reason];
+
+    my @chans  = ($chan eq "*") ? keys %channels : $chan;
+    my $m      = $mask;
+    $m         =~ s/\?/\\./g;
+    $m         =~ s/\*/\\S*/g;
+    foreach (@chans) {
+       my $chan = $_;
+       foreach (keys %{ $channels{$chan}{''} }) {
+           next unless (exists $nuh{lc $_});
+           next unless ($nuh{lc $_} =~ /^$m$/i);
+           &FIXME("nuh{$_} =~ /$m/");
+       }
+    }
 
     if ($exist == 1) {
        $utime_userfile = time();
@@ -554,6 +663,107 @@ sub getUser {
     }
 }
 
+sub chanSet {
+    my($cmd, $chan, $what, $val) = @_;
+
+    if ($cmd eq "+chan") {
+       if (exists $chanconf{$chan}) {
+           &pSReply("chan $chan already exists.");
+           return;
+       }
+       $chanconf{$chan}{_time_added}   = time();
+       $chanconf{$what}{autojoin}      = 1;
+
+       &pSReply("Joining $chan...");
+       &joinchan($chan);
+
+       return;
+    }
+
+    if (!exists $chanconf{$chan}) {
+       &pSReply("no such channel $chan");
+       return;
+    }
+
+    my $update = 0;
+
+    ### ".chanset +blah"
+    ### ".chanset +blah 10"            -- error.
+    if (defined $what and $what =~ s/^([+-])(\S+)/$2/) {
+       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.");
+               return;
+           }
+
+           $was        = ($was) ? "; was '$was'" : "";
+           $val        = 1;
+
+       } else {                        # delete/unset.
+           if (!defined $was) {
+               &pSReply("setting $what for $chan is not set.");
+               return;
+           }
+
+           if ($was eq "0") {
+               &pSReply("setting $what for $chan already 0.");
+               return;
+           }
+
+           $was        = ($was) ? "; was '$was'" : "";
+           $val        = 0;
+       }
+
+       if ($val eq "0") {
+           &pSReply("Unsetting $what for $chan$was.");
+           delete $chanconf{$chan}{$what};
+       } else {
+           &pSReply("Setting $what for $chan to '$val'$was.");
+           $chanconf{$chan}{$what}     = $val;
+       }
+
+       $update++;
+
+    ### ".chanset blah testing"
+    } elsif (defined $val) {
+       my $was = $chanconf{$chan}{$what};
+       if (defined $was and $was eq $val) {
+           &pSReply("setting $what for $chan already '$val'.");
+           return;
+       }
+       $was    = ($was) ? "; was '$was'" : "";
+       &pSReply("Setting $what for $chan to '$val'$was.");
+
+       $chanconf{$chan}{$what} = $val;
+
+       $update++;
+
+    ### ".chanset"
+    ### ".chanset blah"
+    } else {                           # read only.
+       if (!defined $what) {
+           &WARN("chanset/DC: what == undefine.");
+           return;
+       }
+
+       if (exists $chanconf{$chan}{$what}) {
+           &pSReply("$what for $chan is '$chanconf{$chan}{$what}'");
+       } else {
+           &pSReply("$what for $chan is not set.");
+       }
+    }
+
+    if ($update) {
+       $utime_chanfile = time();
+       $ucount_chanfile++;
+    }
+
+    return;
+}
+
 my @regFlagsChan = (
        "autojoin",
        "freshmeat",
@@ -564,6 +774,19 @@ my @regFlagsChan = (
 ### TODO: finish off this list.
 );
 
-my @regFlagsUser = ("mno");    # todo...
+my @regFlagsUser = (
+       "m",            # master
+       "n",            # owner
+       "o",            # op
+);     # todo...
 
 1;
+
+#####
+# Userflags
+#      +r      - ability to remove factoids
+#      +t      - ability to teach factoids
+#      +m      - ability to modify factoids
+#      +n      - bot owner
+#      +o      - authorised user of bot (like +m on eggdrop)
+#####