]> git.donarmstrong.com Git - infobot.git/blobdiff - src/DynaConfig.pl
- another round of patches from lear. "we love you, lear!" j/k :o
[infobot.git] / src / DynaConfig.pl
index 2450882f037688a01bf2334731be6914583592b7..d90f6fafd8f7be28cda8f7ea80a4164bf62ea491 100644 (file)
@@ -13,13 +13,32 @@ if (&IsParam("useStrict")) { use strict; }
 #####
 
 sub readUserFile {
-    if (!open IN,"$bot_misc_dir/blootbot.users") {
+    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) {
+           &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 %users;      # clear on reload.
+    undef %bans;       # reset.
+    undef %ingore;     # reset.
+
     my $ver = <IN>;
     if ($ver !~ /^#v1/) {
        &ERROR("old or invalid user file found.");
@@ -43,6 +62,11 @@ sub readUserFile {
                next;
            }
 
+           if (!defined $nick) {
+               &WARN("invalid line: $_");
+               next;
+           }
+
            # nice little hack.
            if ($what eq "HOSTS") {
                $users{$nick}{$what}{$val} = 1;
@@ -60,12 +84,12 @@ sub readUserFile {
        } elsif (/^- (\S+):\+(\d+):\+(\d+):(\S+):(.*)$/ and $type eq "ignore") {
            ### ignore: middle entry.
            my $mask = $1;
+           my(@array) = ($2,$3,$4,$5);
            ### DEBUG purposes only!
            if ($mask !~ /^$mask{nuh}$/) {
                &WARN("ignore: mask $mask is invalid.");
                next;
            }
-           my(@array) = ($2,$3,$4,$5);
            $ignore{$chan}{$mask} = \@array;
 
        } elsif (/^::(\S+) bans$/) {            # bans: start entry.
@@ -93,7 +117,12 @@ sub readUserFile {
 }
 
 sub writeUserFile {
-    if (!open OUT,">$bot_misc_dir/blootbot.users") {
+    if (!scalar keys %users) {
+       &DEBUG("wUF: nothing to write.");
+       return;
+    }
+
+    if (!open OUT,">$bot_state_dir/blootbot.users") {
        &ERROR("cannot write to userfile.");
        return;
     }
@@ -146,8 +175,13 @@ sub writeUserFile {
        print OUT "::$chan bans\n";
        foreach (keys %{ $bans{$chan} }) {
 # format: bans: mask expire time-added count who-added reason
-           printf OUT "- %s:+%d:+%d:%d:%s:%s\n",
-           $_, @{ $bans{$chan}{$_} };
+           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);
@@ -166,15 +200,15 @@ sub writeUserFile {
 
        ### TODO: use hash instead of array for flexibility?
        print OUT "::$chan ignore\n";
-       &DEBUG("ignore: chan => $chan");
        foreach (keys %{ $ignore{$chan} }) {
-           &DEBUG("   => $_");
 # format: ignore: mask expire time-added who-added reason
-           printf OUT "- %s:+%d:+%d:%s:%s\n", $_,
-                                       @{ $ignore{$chan}{$_} };
-           foreach ( @{ $ignore{$chan}{$_} } ) {
-               &DEBUG("      => $_");
+           my @array = @{ $ignore{$chan}{$_} };
+           if (scalar @array != 4) {
+               &WARN("ignore: $chan/$_ is corrupted.");
+               next;
            }
+
+           printf OUT "- %s:+%d:+%d:%s:%s\n", $_, @array;
        }
     }
 
@@ -192,14 +226,23 @@ sub writeUserFile {
 #####
 
 sub readChanFile {
-    if (!open IN,"$bot_misc_dir/blootbot.chan") {
+    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) {
+           &DEBUG("rCF: backup file bigger than current file. FIXME");
+       }
+    }
+
+    if (!open IN, $f) {
        &ERROR("cannot erad chanfile.");
        return;
     }
 
     undef %chanconf;   # reset.
-    undef %bans;       # reset.
-    undef %ingore;     # reset.
+
     $_ = <IN>;         # version string.
 
     my $chan;
@@ -218,7 +261,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;
@@ -240,11 +286,18 @@ sub readChanFile {
        }
     }
 
+    delete $cache{negative};
+
     &status("CHANFILE: Loaded: ".(scalar(keys %chanconf)-1)." chans");
 }
 
 sub writeChanFile {
-    if (!open OUT,">$bot_misc_dir/blootbot.chan") {
+    if (!scalar keys %chanconf) {
+       &DEBUG("wCF: nothing to write.");
+       return;
+    }
+
+    if (!open OUT,">$bot_state_dir/blootbot.chan") {
        &ERROR("cannot write chanfile.");
        return;
     }
@@ -295,8 +348,8 @@ sub writeChanFile {
            }
        }
 
-       &DEBUG("chans => ".scalar(keys %chanconf)." - 1");
        foreach (keys %opts) {
+           next unless ($opts{$_} > 2);
            &DEBUG("  opts{$_} => $opts{$_}");
        }
 
@@ -374,7 +427,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;
@@ -415,9 +468,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));
@@ -499,7 +557,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;
@@ -535,6 +597,19 @@ sub banAdd {
                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();
        $ucount_userfile++;
@@ -597,6 +672,136 @@ 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;
+}
+
+sub rehashConfVars {
+    # this is an attempt to fix where an option is loaded but the module
+    # has 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($_);
+           delete $cache{confvars}{$_};
+       }
+    }
+
+    &DEBUG("end of rehashConfVars");
+
+    delete $cache{confvars};
+}
+
 my @regFlagsChan = (
        "autojoin",
        "freshmeat",
@@ -607,6 +812,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)
+#####