]> git.donarmstrong.com Git - infobot.git/blobdiff - src/DynaConfig.pl
default to _default
[infobot.git] / src / DynaConfig.pl
index 29a44ec1846fe620b5a565c6b5b520af068ba2ff..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;
@@ -243,7 +280,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,8 +294,13 @@ 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;
+    }
+
+    if (!open OUT,">$bot_state_dir/blootbot.chan") {
+       &ERROR("Cannot write chanfile ($bot_state_dir/blootbot.chan): $!");
        return;
     }
 
@@ -279,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};
            }
@@ -307,8 +351,8 @@ sub writeChanFile {
            }
        }
 
-       &DEBUG("chans => ".scalar(keys %chanconf)." - 1");
        foreach (keys %opts) {
+           next unless ($opts{$_} > 2);
            &DEBUG("  opts{$_} => $opts{$_}");
        }
 
@@ -346,7 +390,7 @@ sub writeChanFile {
                " chans) at $time");
 
     if (defined $msgType and $msgType =~ /^chat$/) {
-       &performStrictReply("--- Writing chan file...");
+       &pSReply("--- Writing chan file...");
     }
 }
 
@@ -386,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;
@@ -448,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;
     }
 }
@@ -472,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++;
@@ -516,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;
@@ -681,8 +732,14 @@ sub chanSet {
            $val        = 0;
        }
 
-       $chanconf{$chan}{$what} = $val;
-       &pSReply("Setting $what for $chan to '$val'$was.");
+       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"
@@ -722,6 +779,35 @@ sub chanSet {
     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",
@@ -739,3 +825,12 @@ my @regFlagsUser = (
 );     # 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)
+#####