]> git.donarmstrong.com Git - infobot.git/blobdiff - src/DynaConfig.pl
* Replace old Units convertion code with dpkg version
[infobot.git] / src / DynaConfig.pl
index ba4245195c24ae36650201259bf58453f01b264c..de814b4d2435aa0f83b17ada3148725c201eff42 100644 (file)
@@ -6,22 +6,45 @@
 #         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
 #####
 
 sub readUserFile {
-    if (!open IN,"$bot_misc_dir/blootbot.users") {
-       &ERROR("cannot read userfile.");
+    my $f = "$bot_state_dir/infobot.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) {
+           &FIXME("rUF: backup file bigger than current file.");
+       }
+    }
+
+    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/) {
@@ -41,13 +64,18 @@ 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;
            }
 
+           if (!defined $nick) {
+               &WARN("DynaConfig: invalid line: $_");
+               next;
+           }
+
            # nice little hack.
-           if ($what eq "HOSTS") {
+           if ($what eq 'HOSTS') {
                $users{$nick}{$what}{$val} = 1;
            } else {
                $users{$nick}{$what} = $val;
@@ -58,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);
@@ -73,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);
@@ -96,14 +124,19 @@ 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;
     }
 
-    my $time           = scalar(localtime);
+    if (!open OUT,">$bot_state_dir/infobot.users") {
+       &ERROR("Cannot write userfile ($bot_state_dir/infobot.users): $!");
+       return;
+    }
+
+    my $time           = scalar(gmtime);
 
-    print OUT "#v1: blootbot -- $ident -- written $time\n\n";
+    print OUT "#v1: infobot -- $ident -- written $time\n\n";
 
     ### USER LIST.
     my $cusers = 0;
@@ -122,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";
            }
@@ -200,8 +235,18 @@ sub writeUserFile {
 #####
 
 sub readChanFile {
-    if (!open IN,"$bot_misc_dir/blootbot.chan") {
-       &ERROR("cannot erad chanfile.");
+    my $f = "$bot_state_dir/infobot.chan";
+    if ( -f $f and -f "$f~") {
+       my $s1 = -s $f;
+       my $s2 = -s "$f~";
+
+       if ($s2 > $s1*3) {
+           &FIXME("rCF: backup file bigger than current file.");
+       }
+    }
+
+    if (!open IN, $f) {
+       &ERROR("Cannot read chanfile ($f): $!");
        return;
     }
 
@@ -213,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;
@@ -225,9 +271,10 @@ sub readChanFile {
            $chanconf{$chan}{$1} = 1;
 
        } elsif (/^[\s\t]+\-(\S+)$/) {          # bool, false.
+           # although this is supported in run-time configuration.
            $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 {
@@ -240,7 +287,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}{$_};
@@ -251,13 +299,18 @@ 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/infobot.chan") {
+       &ERROR("Cannot write chanfile ($bot_state_dir/infobot.chan): $!");
        return;
     }
 
-    my $time           = scalar(localtime);
-    print OUT "#v1: blootbot -- $ident -- written $time\n\n";
+    my $time           = scalar(gmtime);
+    print OUT "#v1: infobot -- $ident -- written $time\n\n";
 
     if ($flag_quit) {
 
@@ -274,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};
            }
@@ -302,8 +356,8 @@ sub writeChanFile {
            }
        }
 
-       &DEBUG("chans => ".scalar(keys %chanconf)." - 1");
        foreach (keys %opts) {
+           next unless ($opts{$_} > 2);
            &DEBUG("  opts{$_} => $opts{$_}");
        }
 
@@ -349,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);
 
@@ -369,19 +425,19 @@ sub IsFlag {
 
 sub verifyUser {
     my ($nick, $lnuh) = @_;
-    my ($user,$m);
+    my ($user, $m);
 
     if ($userHandle = $dcc{'CHATvrfy'}{$who}) {
-       &DEBUG("vUser: cached auth for $who.");
+       &VERB("vUser: cached auth for $who.",2);
        return $userHandle;
     }
 
-    $userHandle = "";
+    $userHandle = '';
 
     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;
@@ -397,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).");
@@ -416,15 +472,20 @@ 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 = 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));
@@ -443,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;
     }
@@ -462,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++;
@@ -506,7 +575,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;
@@ -530,7 +603,7 @@ sub userDel {
 sub banAdd {
     my($mask,$chan,$expire,$reason) = @_;
 
-    $chan      ||= "*";
+    $chan      ||= '*';
     $expire    ||= 0;
 
     if ($expire > 0) {
@@ -542,6 +615,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,16 +690,155 @@ sub getUser {
     }
 }
 
-my @regFlagsChan = (
-       "autojoin",
-       "freshmeat",
-       "limitcheckInterval",
-       "limitcheckPlus",
-       "allowConv",
-       "allowDNS",
-### TODO: finish off this list.
-);
+sub chanSet {
+    my($cmd, $chan, $what, $val) = @_;
+
+    if ($cmd eq "+chan") {
+       if (exists $chanconf{$chan}) {
+           &performStrictReply("chan $chan already exists.");
+           return;
+       }
+       $chanconf{$chan}{_time_added}   = time();
+       $chanconf{$chan}{autojoin}      = $conn->nick();
+
+       &performStrictReply("Joining $chan...");
+       &joinchan($chan);
+
+       return;
+    }
+
+    if (!exists $chanconf{$chan}) {
+       &performStrictReply("no such channel $chan");
+       return;
+    }
+
+    my $update = 0;
+
+    if (defined $what and $what =~ s/^([+-])(\S+)/$2/) {
+       ### ".chanset +blah"
+       ### ".chanset +blah 10"         -- error.
+
+       my $set = ($1 eq "+") ? 1 : 0;
+       my $was         = $chanconf{$chan}{$what};
+
+       if ($set) {                     # add/set.
+           if (defined $was and $was eq '1') {
+               &performStrictReply("setting $what for $chan already 1.");
+               return;
+           }
+
+           $val        = 1;
+
+       } else {                        # delete/unset.
+           if (!defined $was) {
+               &performStrictReply("setting $what for $chan is not set.");
+               return;
+           }
+
+           $val        = 0;
+       }
+
+       # 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 {
+           &performStrictReply("Setting $what for $chan to '$val'$was.");
+           $chanconf{$chan}{$what}     = $val;
+       }
+
+       $update++;
+
+    } elsif (defined $val) {
+       ### ".chanset blah testing"
+
+       my $was = $chanconf{$chan}{$what};
+       if (defined $was and $was eq $val) {
+           &performStrictReply("setting $what for $chan already '$val'.");
+           return;
+       }
+       $was    = ($was) ? "; was '$was'" : '';
+       &performStrictReply("Setting $what for $chan to '$val'$was.");
+
+       $chanconf{$chan}{$what} = $val;
+
+       $update++;
+
+    } else {                           # read only.
+       ### ".chanset"
+       ### ".chanset blah"
+
+       if (!defined $what) {
+           &WARN("chanset/DC: what == undefine.");
+           return;
+       }
 
-my @regFlagsUser = ("mno");    # todo...
+       if (exists $chanconf{$chan}{$what}) {
+           &performStrictReply("$what for $chan is '$chanconf{$chan}{$what}'");
+       } else {
+           &performStrictReply("$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 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');
+           delete $cache{confvars}{$_};
+       }
+
+       if (/^uptime$/ and $i) {
+           &loadMyModule('Uptime');
+           delete $cache{confvars}{$_};
+       }
+
+       if (/^rootwarn$/i and $i) {
+           &loadMyModule('RootWarn');
+           delete $cache{confvars}{$_};
+       }
+
+       if (/^onjoin$/i and $i) {
+           &loadMyModule('OnJoin');
+           delete $cache{confvars}{$_};
+       }
+    }
+
+    &DEBUG("end of rehashConfVars");
+
+    delete $cache{confvars};
+}
+
+my @regFlagsUser = (
+       # 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;