]> git.donarmstrong.com Git - infobot.git/blobdiff - src/DynaConfig.pl
take a few more things literally
[infobot.git] / src / DynaConfig.pl
index b76e7579763d94f8bbf519822a8c75ccf0ea4e97..85168e0483c6e964d685fab54b2aef6104e33409 100644 (file)
@@ -6,7 +6,14 @@
 #         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);
+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
@@ -25,19 +32,19 @@ 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/) {
@@ -57,7 +64,7 @@ sub readUserFile {
        if (/^--(\S+)[\s\t]+(.*)$/) {           # user: middle entry.
            my ($what,$val) = ($1,$2);
 
-           if (!defined $val or $val eq "") {
+           if (!defined $val or $val eq '') {
                &WARN("$what: val == NULL.");
                next;
            }
@@ -68,7 +75,7 @@ sub readUserFile {
            }
 
            # nice little hack.
-           if ($what eq "HOSTS") {
+           if ($what eq 'HOSTS') {
                $users{$nick}{$what}{$val} = 1;
            } else {
                $users{$nick}{$what} = $val;
@@ -79,9 +86,9 @@ sub readUserFile {
 
        } elsif (/^::(\S+) ignore$/) {          # ignore: start entry.
            $chan       = $1;
-           $type       = "ignore";
+           $type       = 'ignore';
 
-       } elsif (/^- (\S+):\+(\d+):\+(\d+):(\S+):(.*)$/ and $type eq "ignore") {
+       } elsif (/^- (\S+):\+(\d+):\+(\d+):(\S+):(.*)$/ and $type eq 'ignore') {
            ### ignore: middle entry.
            my $mask = $1;
            my(@array) = ($2,$3,$4,$5);
@@ -94,9 +101,9 @@ sub readUserFile {
 
        } elsif (/^::(\S+) bans$/) {            # bans: start entry.
            $chan       = $1;
-           $type       = "bans";
+           $type       = 'bans';
 
-       } elsif (/^- (\S+):\+(\d+):\+(\d+):(\d+):(\S+):(.*)$/ and $type eq "bans") {
+       } 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);
@@ -123,11 +130,11 @@ sub writeUserFile {
     }
 
     if (!open OUT,">$bot_state_dir/blootbot.users") {
-       &ERROR("cannot write to userfile.");
+       &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";
 
@@ -148,11 +155,13 @@ sub writeUserFile {
            my $what    = $_;
            my $val     = $users{$user}{$_};
 
-           if (ref($val) eq "HASH") {
+           if (ref($val) eq 'HASH') {
                foreach (sort keys %{ $users{$user}{$_} }) {
                    print OUT "--$what\t\t$_\n";
                }
 
+           } elsif ($_ eq 'FLAGS') {
+               print OUT "--$_\t\t" . join('', sort split('', $val)) . "\n";
            } else {
                print OUT "--$_\t\t$val\n";
            }
@@ -232,12 +241,12 @@ sub readChanFile {
        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;
     }
 
@@ -249,7 +258,8 @@ sub readChanFile {
     while (<IN>) {
        chop;
 
-       next if /^$/;
+       next if /^\s*$/;
+       next if /^\// or /^\;/; # / or ; are comment lines.
 
        if (/^(\S+)\s*$/) {
            $chan       = $1;
@@ -261,12 +271,10 @@ sub readChanFile {
            $chanconf{$chan}{$1} = 1;
 
        } elsif (/^[\s\t]+\-(\S+)$/) {          # bool, false.
-           &DEBUG("deprecated support of negative options.") unless ($cache{negative});
            # although this is supported in run-time configuration.
-           $cache{negative} = 1;
-#          $chanconf{$chan}{$1} = 0;
+           $chanconf{$chan}{$1} = 0;
 
-       } elsif (/^[\s\t]+(\S+)[\ss\t]+(.*)$/) {# what = val.
+       } elsif (/^[\s\t]+(\S+)[\s\t]+(.*)$/) {# what = val.
            $chanconf{$chan}{$1} = $2;
 
        } else {
@@ -279,15 +287,14 @@ 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");
 }
 
@@ -298,11 +305,11 @@ sub writeChanFile {
     }
 
     if (!open OUT,">$bot_state_dir/blootbot.chan") {
-       &ERROR("cannot write chanfile.");
+       &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) {
@@ -320,6 +327,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};
            }
@@ -395,9 +403,11 @@ sub writeChanFile {
 ##### USER COMMANDS.
 #####
 
+# TODO: support multiple flags.
+# TODO: return all flags for opers
 sub IsFlag {
     my $flags = shift;
-    my ($ret, $f, $o) = "";
+    my ($ret, $f, $o) = '';
 
     &verifyUser($who, $nuh);
 
@@ -415,14 +425,14 @@ 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);
        return $userHandle;
     }
 
-    $userHandle = "";
+    $userHandle = '';
 
     foreach $user (keys %users) {
        next if ($user eq "_default");
@@ -443,7 +453,7 @@ sub verifyUser {
            last;
        }
 
-       last if ($userHandle ne "");
+       last if ($userHandle ne '');
 
        if ($user =~ /^\Q$nick\E$/i and !exists $cache{VUSERWARN}{$user}) {
            &status("vU: nick matched but host is not in list ($lnuh).");
@@ -462,10 +472,10 @@ sub verifyUser {
 sub ckpasswd {
     # returns true if arg1 encrypts to arg2
     my ($plain, $encrypted) = @_;
-    if ($encrypted eq "") {
+    if ($encrypted eq '') {
        ($plain, $encrypted) = split(/\s+/, $plain, 2);
     }
-    return 0 unless ($plain ne "" and $encrypted ne "");
+    return 0 unless ($plain ne '' and $encrypted ne '');
 
     # MD5 // DES. Bobby Billingsley++.
     my $salt;
@@ -494,16 +504,17 @@ sub hasFlag {
     }
 }
 
+# expire is time in minutes
 sub ignoreAdd {
     my($mask,$chan,$expire,$comment) = @_;
 
-    $chan      ||= "*";        # global if undefined.
-    $comment   ||= "";         # optional.
+    $chan      ||= '*';        # global if undefined.
+    $comment   ||= '';         # optional.
     $expire    ||= 0;          # permament.
     my $count  ||= 0;
 
     if ($expire > 0) {
-       $expire         = $expire*60 + time();
+       $expire         = ($expire*60) + time();
     } else {
        $expire         = 0;
     }
@@ -513,6 +524,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++;
@@ -585,7 +603,7 @@ sub userDel {
 sub banAdd {
     my($mask,$chan,$expire,$reason) = @_;
 
-    $chan      ||= "*";
+    $chan      ||= '*';
     $expire    ||= 0;
 
     if ($expire > 0) {
@@ -597,7 +615,7 @@ sub banAdd {
                exists $bans{'*'}{$mask});
     $bans{$chan}{$mask} = [$expire, time(), 0, $who, $reason];
 
-    my @chans  = ($chan eq "*") ? keys %channels : $chan;
+    my @chans  = ($chan eq '*') ? keys %channels : $chan;
     my $m      = $mask;
     $m         =~ s/\?/\\./g;
     $m         =~ s/\*/\\S*/g;
@@ -677,91 +695,90 @@ sub chanSet {
 
     if ($cmd eq "+chan") {
        if (exists $chanconf{$chan}) {
-           &pSReply("chan $chan already exists.");
+           &performStrictReply("chan $chan already exists.");
            return;
        }
        $chanconf{$chan}{_time_added}   = time();
-       $chanconf{$what}{autojoin}      = 1;
+       $chanconf{$chan}{autojoin}      = $conn->nick();
 
-       &pSReply("Joining $chan...");
+       &performStrictReply("Joining $chan...");
        &joinchan($chan);
 
        return;
     }
 
     if (!exists $chanconf{$chan}) {
-       &pSReply("no such channel $chan");
+       &performStrictReply("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;
+       ### ".chanset +blah"
+       ### ".chanset +blah 10"         -- error.
+
+       my $set = ($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.");
+       if ($set) {                     # add/set.
+           if (defined $was and $was eq '1') {
+               &performStrictReply("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.");
+               &performStrictReply("setting $what for $chan is not set.");
                return;
            }
 
-           $was        = ($was) ? "; was '$was'" : "";
            $val        = 0;
        }
 
-       if ($val eq "0") {
-           &pSReply("Unsetting $what for $chan$was.");
+       # alter for cosmetic (print out) reasons only.
+       $was    = (defined $was) ? "; was '$was'" : '';
+
+       if ($val eq '0') {
+           &performStrictReply("Unsetting $what for $chan$was.");
            delete $chanconf{$chan}{$what};
        } else {
-           &pSReply("Setting $what for $chan to '$val'$was.");
+           &performStrictReply("Setting $what for $chan to '$val'$was.");
            $chanconf{$chan}{$what}     = $val;
        }
 
        $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'.");
+           &performStrictReply("setting $what for $chan already '$val'.");
            return;
        }
-       $was    = ($was) ? "; was '$was'" : "";
-       &pSReply("Setting $what for $chan to '$val'$was.");
+       $was    = ($was) ? "; was '$was'" : '';
+       &performStrictReply("Setting $what for $chan to '$val'$was.");
 
        $chanconf{$chan}{$what} = $val;
 
        $update++;
 
-    ### ".chanset"
-    ### ".chanset blah"
     } else {                           # read only.
+       ### ".chanset"
+       ### ".chanset blah"
+
        if (!defined $what) {
            &WARN("chanset/DC: what == undefine.");
            return;
        }
 
        if (exists $chanconf{$chan}{$what}) {
-           &pSReply("$what for $chan is '$chanconf{$chan}{$what}'");
+           &performStrictReply("$what for $chan is '$chanconf{$chan}{$what}'");
        } else {
-           &pSReply("$what for $chan is not set.");
+           &performStrictReply("$what for $chan is not set.");
        }
     }
 
@@ -774,25 +791,30 @@ sub chanSet {
 }
 
 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.
+    # 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.
 
     foreach (keys %{ $cache{confvars} }) {
        my $i = $cache{confvars}{$_};
        &DEBUG("rehashConfVars: _ => $_");
 
        if (/^news$/ and $i) {
-           &loadMyModule("news");
+           &loadMyModule('News');
            delete $cache{confvars}{$_};
        }
 
        if (/^uptime$/ and $i) {
-           &loadMyModule("uptime");
+           &loadMyModule('Uptime');
            delete $cache{confvars}{$_};
        }
 
        if (/^rootwarn$/i and $i) {
-           &loadMyModule($_);
+           &loadMyModule('RootWarn');
+           delete $cache{confvars}{$_};
+       }
+
+       if (/^onjoin$/i and $i) {
+           &loadMyModule('OnJoin');
            delete $cache{confvars}{$_};
        }
     }
@@ -802,29 +824,21 @@ sub rehashConfVars {
     delete $cache{confvars};
 }
 
-my @regFlagsChan = (
-       "autojoin",
-       "freshmeat",
-       "limitcheckInterval",
-       "limitcheckPlus",
-       "allowConv",
-       "allowDNS",
-### TODO: finish off this list.
-);
-
 my @regFlagsUser = (
-       "m",            # master
-       "n",            # owner
-       "o",            # op
-);     # todo...
+       # possible chars to include in FLAG
+       'A',    # bot administration over /msg
+                       # default is only via DCC CHAT
+       'O',    # dynamic ops (as on channel). (automatic +o)
+       'T',    # add topics.
+       'a',    # ask/request factoid.
+       'm',    # modify factoid. (includes renaming)
+       'n',    # bot owner, can 'reload'
+       'o',    # master of bot (automatic +amrt)
+                       # can search on factoid strings shorter than 2 chars
+                       # can tell bot to join new channels
+                       # can [un]lock factoids
+       'r',    # remove factoid.
+       't',    # teach/add factoid.
+);
 
 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)
-#####