]> git.donarmstrong.com Git - infobot.git/blobdiff - src/DynaConfig.pl
standard TODO: and FIXME:
[infobot.git] / src / DynaConfig.pl
index fad4ab27d8303da880bd5ead71ce05ce7c730552..6b4ee5aa6f16bd0bd9eaf3cecc631d545648c95c 100644 (file)
@@ -6,14 +6,21 @@
 #         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 %flag2attr);
+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 {
-    my $f = "$bot_misc_dir/blootbot.users";
+    my $f = "$bot_state_dir/blootbot.users";
 
     if (! -f $f) {
        &DEBUG("userfile not found; new fresh run detected.");
@@ -25,22 +32,22 @@ sub readUserFile {
        my $s2 = -s "$f~";
 
        if ($s2 > $s1*3) {
-           &DEBUG("rUF: backup file bigger than current file. FIXME");
+           &FIXME("rUF: backup file bigger than current file.");
        }
     }
 
     if (!open IN, $f) {
-       &ERROR("cannot read userfile.");
+       &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/) {
+    if ($ver !~ /^#v[12]/) {
        &ERROR("old or invalid user file found.");
        &closeLog();
        exit 1; # correct?
@@ -54,7 +61,7 @@ sub readUserFile {
        next if /^$/;
        next if /^#/;
 
-       if (/^--(\S+)[\s\t]+(.*)$/) {           # user: middle entry.
+       if (/^--(\S+)[\s\t]+(.*)$/) {           # user: body
            my ($what,$val) = ($1,$2);
 
            if (!defined $val or $val eq "") {
@@ -62,22 +69,34 @@ sub readUserFile {
                next;
            }
 
-           # nice little hack.
-           if ($what eq "HOSTS") {
+           if (!defined $nick) {
+               &WARN("DynaConfig: invalid line: $_");
+               next;
+           }
+
+           # hack for attribute support.
+           if ($what =~ /^attr$/) {
+               foreach (split / /, $val) {
+                   $users{$nick}{$what}{$_} = 1;
+               }
+               next;
+           }
+
+           if ($what =~ /^HOSTS$/) {
                $users{$nick}{$what}{$val} = 1;
            } else {
                $users{$nick}{$what} = $val;
            }
 
-       } elsif (/^(\S+)$/) {                   # user: start entry.
+       } elsif (/^(\S+)$/) {                   # user: start.
            $nick       = $1;
 
-       } elsif (/^::(\S+) ignore$/) {          # ignore: start entry.
+       } elsif (/^::(\S+) ignore$/) {          # ignore: start.
            $chan       = $1;
            $type       = "ignore";
 
        } elsif (/^- (\S+):\+(\d+):\+(\d+):(\S+):(.*)$/ and $type eq "ignore") {
-           ### ignore: middle entry.
+           ### ignore: body.
            my $mask = $1;
            my(@array) = ($2,$3,$4,$5);
            ### DEBUG purposes only!
@@ -87,12 +106,12 @@ sub readUserFile {
            }
            $ignore{$chan}{$mask} = \@array;
 
-       } elsif (/^::(\S+) bans$/) {            # bans: start entry.
+       } elsif (/^::(\S+) bans$/) {            # bans: start.
            $chan       = $1;
            $type       = "bans";
 
        } elsif (/^- (\S+):\+(\d+):\+(\d+):(\d+):(\S+):(.*)$/ and $type eq "bans") {
-           ### bans: middle entry.
+           ### bans: body.
            # $btime, $atime, $count, $whoby, $reason.
            my(@array) = ($2,$3,$4,$5,$6);
            $bans{$chan}{$1} = \@array;
@@ -117,12 +136,12 @@ sub writeUserFile {
        return;
     }
 
-    if (!open OUT,">$bot_misc_dir/blootbot.users") {
-       &ERROR("cannot write to userfile.");
+    if (!open OUT,">$bot_state_dir/blootbot.users") {
+       &ERROR("Cannot write userfile ($bot_state_dir/blootbot.users): $!");
        return;
     }
 
-    my $time           = scalar(localtime);
+    my $time           = scalar(gmtime);
 
     print OUT "#v1: blootbot -- $ident -- written $time\n\n";
 
@@ -140,16 +159,47 @@ sub writeUserFile {
        print OUT "$user\n";
 
        foreach (sort keys %{ $users{$user} }) {
+           # todo: rename what to key?
            my $what    = $_;
-           my $val     = $users{$user}{$_};
+           my $val     = $users{$user}{$what};
+
+           if (ref($val) eq "SCALAR") {
+               print OUT "--$what\t\t$val\n";
+               next;
+           }
+
+           next unless (ref($val) eq "HASH");
 
-           if (ref($val) eq "HASH") {
-               foreach (sort keys %{ $users{$user}{$_} }) {
+           if ($what ne "attr") {
+               foreach (sort keys %{ $users{$user}{$what} }) {
                    print OUT "--$what\t\t$_\n";
                }
+               next;
+           }
 
-           } else {
-               print OUT "--$_\t\t$val\n";
+           # disabled until confirmed to work flawlessly.
+           next if (1);
+
+           # attr support.
+           my $str = "--$what\t\t";
+           my @attr = sort keys %{ $users{$user}{$what} };
+           # some fucking weird code ;) does it work?
+           # any room for improvement?
+           while (@attr) {
+               my $attr = shift(@attr);
+               my $_str = $str." ".$attr;
+               my $print = 0;
+
+               if (length $str < 60 && length $_str > 60) {
+                   $print++;
+               } else {
+                   $str = $_str;
+                   $print++ if (!@attr);
+               }
+
+               next unless ($print);
+               print OUT "$str\n";
+               $str = "--$what\t\t";
            }
        }
        print OUT "\n";
@@ -212,7 +262,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...");
     }
 }
 
@@ -221,18 +271,18 @@ sub writeUserFile {
 #####
 
 sub readChanFile {
-    my $f = "$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");
+           &FIXME("rCF: backup file bigger than current file.");
        }
     }
 
     if (!open IN, $f) {
-       &ERROR("cannot erad chanfile.");
+       &ERROR("Cannot read chanfile ($f): $!");
        return;
     }
 
@@ -244,7 +294,8 @@ sub readChanFile {
     while (<IN>) {
        chop;
 
-       next if /^$/;
+       next if /^\s*$/;
+       next if /^\// or /^\;/; # / or ; are comment lines.
 
        if (/^(\S+)\s*$/) {
            $chan       = $1;
@@ -274,7 +325,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}{$_};
@@ -292,12 +344,12 @@ sub writeChanFile {
        return;
     }
 
-    if (!open OUT,">$bot_misc_dir/blootbot.chan") {
-       &ERROR("cannot write chanfile.");
+    if (!open OUT,">$bot_state_dir/blootbot.chan") {
+       &ERROR("Cannot write chanfile ($bot_state_dir/blootbot.chan): $!");
        return;
     }
 
-    my $time           = scalar(localtime);
+    my $time           = scalar(gmtime);
     print OUT "#v1: blootbot -- $ident -- written $time\n\n";
 
     if ($flag_quit) {
@@ -315,6 +367,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};
            }
@@ -344,7 +397,7 @@ sub writeChanFile {
        }
 
        foreach (keys %opts) {
-           next unless ($opts{$_} > 1);
+           next unless ($opts{$_} > 2);
            &DEBUG("  opts{$_} => $opts{$_}");
        }
 
@@ -382,7 +435,7 @@ sub writeChanFile {
                " chans) at $time");
 
     if (defined $msgType and $msgType =~ /^chat$/) {
-       &performStrictReply("--- Writing chan file...");
+       &pSReply("--- Writing chan file...");
     }
 }
 
@@ -390,14 +443,27 @@ sub writeChanFile {
 ##### USER COMMANDS.
 #####
 
+# TODO: support multiple flags, eg: "+o-m"
 sub IsFlag {
     my $flags = shift;
     my ($ret, $f, $o) = "";
 
     &verifyUser($who, $nuh);
 
+    # userfile v2 support:
+    if ($users{$userHandle}{attr}) {
+       # todo: fix for multiple flags/attr
+       my $attr = $flag2attr{$flags};
+       if (!$attr) {
+           &WARN("IsFlag: !flag2attr{$flags}");
+           return 0;
+       }
+       return 1 if ($users{$userHandle}{attr}{$attr});
+       return 0;
+    }
+
     foreach $f (split //, $users{$userHandle}{FLAGS}) {
-       foreach $o ( split //, $flags ) {
+       foreach $o (split //, $flags) {
            next unless ($f eq $o);
 
            $ret = $f;
@@ -410,7 +476,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);
@@ -480,15 +546,16 @@ sub ckpasswd {
 sub hasFlag {
     my ($flag) = @_;
 
-    if (&IsFlag($flag) eq $flag) {
-       return 1;
-    } else {
+    if (&IsFlag($flag) ne $flag) {
        &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;
     }
+
+    return 1;
 }
 
+# expire is time in minutes
 sub ignoreAdd {
     my($mask,$chan,$expire,$comment) = @_;
 
@@ -498,7 +565,7 @@ sub ignoreAdd {
     my $count  ||= 0;
 
     if ($expire > 0) {
-       $expire         = $expire*60 + time();
+       $expire         = ($expire*60) + time();
     } else {
        $expire         = 0;
     }
@@ -508,6 +575,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++;
@@ -543,11 +617,9 @@ sub ignoreDel {
 }
 
 sub userAdd {
-    my($nick,$mask)    = @_;
+    my($nick,$mask) = @_;
 
-    if (exists $users{$nick}) {
-       return 0;
-    }
+    return 0 if (exists $users{$nick});
 
     $utime_userfile = time();
     $ucount_userfile++;
@@ -557,7 +629,7 @@ sub userAdd {
        $users{$nick}{HOSTS}{$mask} = 1;
     }
 
-    $users{$nick}{FLAGS}       ||= $users{_default}{FLAGS};
+    $users{$nick}{FLAGS} ||= $users{_default}{FLAGS};
 
     return 1;
 }
@@ -565,9 +637,7 @@ sub userAdd {
 sub userDel {
     my($nick)  = @_;
 
-    if (!exists $users{$nick}) {
-       return 0;
-    }
+    return 0 if (!exists $users{$nick});
 
     $utime_userfile = time();
     $ucount_userfile++;
@@ -579,17 +649,12 @@ sub userDel {
 
 sub banAdd {
     my($mask,$chan,$expire,$reason) = @_;
-
     $chan      ||= "*";
     $expire    ||= 0;
-
-    if ($expire > 0) {
-       $expire         = $expire*60 + time();
-    }
+    $expire    = $expire*60 + time() if ($expire > 0);
 
     my $exist  = 1;
-    $exist++ if (exists $bans{$chan}{$mask} or
-               exists $bans{'*'}{$mask});
+    $exist++ if (exists $bans{$chan}{$mask} or exists $bans{'*'}{$mask});
     $bans{$chan}{$mask} = [$expire, time(), 0, $who, $reason];
 
     my @chans  = ($chan eq "*") ? keys %channels : $chan;
@@ -676,7 +741,7 @@ sub chanSet {
            return;
        }
        $chanconf{$chan}{_time_added}   = time();
-       $chanconf{$what}{autojoin}      = 1;
+       $chanconf{$chan}{autojoin}      = 1;
 
        &pSReply("Joining $chan...");
        &joinchan($chan);
@@ -691,9 +756,10 @@ sub chanSet {
 
     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};
 
@@ -703,7 +769,6 @@ sub chanSet {
                return;
            }
 
-           $was        = ($was) ? "; was '$was'" : "";
            $val        = 1;
 
        } else {                        # delete/unset.
@@ -717,10 +782,12 @@ sub chanSet {
                return;
            }
 
-           $was        = ($was) ? "; was '$was'" : "";
            $val        = 0;
        }
 
+       # alter for cosmetic (print out) reasons only.
+       $was    = ($was) ? "; was '$was'" : "";
+
        if ($val eq "0") {
            &pSReply("Unsetting $what for $chan$was.");
            delete $chanconf{$chan}{$what};
@@ -731,8 +798,9 @@ sub chanSet {
 
        $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'.");
@@ -745,9 +813,10 @@ sub chanSet {
 
        $update++;
 
-    ### ".chanset"
-    ### ".chanset blah"
     } else {                           # read only.
+       ### ".chanset"
+       ### ".chanset blah"
+
        if (!defined $what) {
            &WARN("chanset/DC: what == undefine.");
            return;
@@ -768,29 +837,72 @@ 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.
 
-my @regFlagsUser = (
-       "m",            # master
-       "n",            # owner
-       "o",            # op
-);     # todo...
+    foreach (keys %{ $cache{confvars} }) {
+       my $i = $cache{confvars}{$_};
+       &DEBUG("rehashConfVars: _ => $_");
 
-1;
+       if (/^news$/ and $i) {
+           &loadMyModule("news");
+           delete $cache{confvars}{$_};
+       }
 
-#####
-# 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)
-#####
+       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};
+}
+
+sub convertUserFileVer2 {
+    foreach (keys %users) {
+       my $handle = $_;
+       my $flags = $users{$handle}{FLAGS};
+       if (!$flags) {
+           &WARN("cUFV2: handle $handle has no flags!");
+           next;
+       }
+
+       my $fail = 0;
+       foreach (split //, $flags) {
+           my $flag = $_;
+           my $attr = $flag2attr{$flag};
+           if (!$attr) {
+               &DEBUG("cUFV2: handle=$handle: flag=$flag does not exist.");
+               $fail++;
+               next;
+           }
+           $users{$handle}{attr}{$attr} = 1;
+       }
+
+       next if ($fail);
+# uncomment to enable attribute conversion support.
+#      delete $users{$handle}{FLAGS};
+    }
+}
+
+# support more than one attribute?
+%flag2attr = (
+       m => "modify_factoid",
+       r => "delete_factoid",
+       t => "add_factoid",
+       a => "ask_factoid",
+       n => "bot_owner",
+       o => "bot_master",
+       A => "admin_over_msg",
+       T => "topic"
+);
+
+1;