]> git.donarmstrong.com Git - infobot.git/blobdiff - src/DynaConfig.pl
- added weather from Nathan Moschkin <logeist@guinerd.myip.org>. Thanks!
[infobot.git] / src / DynaConfig.pl
index ba4245195c24ae36650201259bf58453f01b264c..da1739a0070b3d19b2292cf0b5b091bb028001b1 100644 (file)
@@ -6,22 +6,38 @@
 #         NOTE: Merged from User.pl
 #
 
-if (&IsParam("useStrict")) { use strict; }
+#use strict;
 
 #####
 ##### 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) {
+           &DEBUG("rUF: backup file bigger than current file. FIXME");
+       }
+    }
+
+    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 +62,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,8 +117,13 @@ 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;
+    }
+
+    if (!open OUT,">$bot_state_dir/blootbot.users") {
+       &ERROR("Cannot write userfile ($bot_state_dir/blootbot.users): $!");
        return;
     }
 
@@ -191,7 +217,7 @@ sub writeUserFile {
     $wtime_userfile = time();
     &status("--- Saved USERFILE ($cusers users; $cbans bans; $cignore ignore) at $time");
     if (defined $msgType and $msgType =~ /^chat$/) {
-       &performStrictReply("--- Writing user file...");
+       &pSReply("--- Writing user file...");
     }
 }
 
@@ -200,8 +226,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) {
+           &DEBUG("rCF: backup file bigger than current file. FIXME");
+       }
+    }
+
+    if (!open IN, $f) {
+       &ERROR("Cannot read chanfile ($f): $!");
        return;
     }
 
@@ -213,7 +249,8 @@ sub readChanFile {
     while (<IN>) {
        chop;
 
-       next if /^$/;
+       next if /^\s*$/;
+       next if /^\// or /^\;/; # / or ; are comment lines.
 
        if (/^(\S+)\s*$/) {
            $chan       = $1;
@@ -225,7 +262,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,19 +280,27 @@ 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}{$_};
        }
     }
 
+    delete $cache{negative};
+
     &status("CHANFILE: Loaded: ".(scalar(keys %chanconf)-1)." chans");
 }
 
 sub writeChanFile {
-    if (!open OUT,">$bot_misc_dir/blootbot.chan") {
-       &ERROR("cannot write chanfile.");
+    if (!scalar keys %chanconf) {
+       &DEBUG("wCF: nothing to write.");
+       return;
+    }
+
+    if (!open OUT,">$bot_state_dir/blootbot.chan") {
+       &ERROR("Cannot write chanfile ($bot_state_dir/blootbot.chan): $!");
        return;
     }
 
@@ -274,6 +322,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};
            }
@@ -302,8 +351,8 @@ sub writeChanFile {
            }
        }
 
-       &DEBUG("chans => ".scalar(keys %chanconf)." - 1");
        foreach (keys %opts) {
+           next unless ($opts{$_} > 2);
            &DEBUG("  opts{$_} => $opts{$_}");
        }
 
@@ -341,7 +390,7 @@ sub writeChanFile {
                " chans) at $time");
 
     if (defined $msgType and $msgType =~ /^chat$/) {
-       &performStrictReply("--- Writing chan file...");
+       &pSReply("--- Writing chan file...");
     }
 }
 
@@ -372,7 +421,7 @@ sub verifyUser {
     my ($user,$m);
 
     if ($userHandle = $dcc{'CHATvrfy'}{$who}) {
-       &DEBUG("vUser: cached auth for $who.");
+       &VERB("vUser: cached auth for $who.",2);
        return $userHandle;
     }
 
@@ -381,7 +430,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;
@@ -422,9 +471,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));
@@ -438,7 +492,7 @@ sub hasFlag {
        return 1;
     } else {
        &status("DCC CHAT: <$who> $message -- not enough flags.");
-       &performStrictReply("error: you do not have enough flags for that. ($flag required)");
+       &pSReply("error: you do not have enough flags for that. ($flag required)");
        return 0;
     }
 }
@@ -462,6 +516,9 @@ sub ignoreAdd {
 
     $ignore{$chan}{$mask} = [$expire, time(), $who, $comment];
 
+    # todo: improve this.
+    &status("ignore: Added $mask for $chan to expire $expire, by $who, for $comment");
+
     if ($exist) {
        $utime_userfile = time();
        $ucount_userfile++;
@@ -506,7 +563,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;
@@ -542,6 +603,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++;
@@ -604,6 +678,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",
@@ -614,6 +818,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)
+#####