]> git.donarmstrong.com Git - infobot.git/blobdiff - src/DynaConfig.pl
OnJoin - tensai, fixes by TimRiker
[infobot.git] / src / DynaConfig.pl
index f6f066668079f9d578ec046471de5dc29faf9a0f..e1cf464a1a68960ac691cf6b98e0002aa5639923 100644 (file)
@@ -9,7 +9,7 @@
 use strict;
 
 use vars qw(%chanconf %cache %bans %channels %nuh %users %ignore
-       %talkWho %dcc %mask %flag2attr);
+       %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
@@ -47,7 +47,7 @@ sub readUserFile {
     undef %ignore;     # reset.
 
     my $ver = <IN>;
-    if ($ver !~ /^#v[12]/) {
+    if ($ver !~ /^#v1/) {
        &ERROR("old or invalid user file found.");
        &closeLog();
        exit 1; # correct?
@@ -61,7 +61,7 @@ sub readUserFile {
        next if /^$/;
        next if /^#/;
 
-       if (/^--(\S+)[\s\t]+(.*)$/) {           # user: body
+       if (/^--(\S+)[\s\t]+(.*)$/) {           # user: middle entry.
            my ($what,$val) = ($1,$2);
 
            if (!defined $val or $val eq "") {
@@ -74,29 +74,22 @@ sub readUserFile {
                next;
            }
 
-           # hack for attribute support.
-           if ($what =~ /^attr$/) {
-               foreach (split / /, $val) {
-                   $users{$nick}{$what}{$_} = 1;
-               }
-               next;
-           }
-
-           if ($what =~ /^HOSTS$/) {
+           # nice little hack.
+           if ($what eq "HOSTS") {
                $users{$nick}{$what}{$val} = 1;
            } else {
                $users{$nick}{$what} = $val;
            }
 
-       } elsif (/^(\S+)$/) {                   # user: start.
+       } elsif (/^(\S+)$/) {                   # user: start entry.
            $nick       = $1;
 
-       } elsif (/^::(\S+) ignore$/) {          # ignore: start.
+       } elsif (/^::(\S+) ignore$/) {          # ignore: start entry.
            $chan       = $1;
            $type       = "ignore";
 
        } elsif (/^- (\S+):\+(\d+):\+(\d+):(\S+):(.*)$/ and $type eq "ignore") {
-           ### ignore: body.
+           ### ignore: middle entry.
            my $mask = $1;
            my(@array) = ($2,$3,$4,$5);
            ### DEBUG purposes only!
@@ -106,12 +99,12 @@ sub readUserFile {
            }
            $ignore{$chan}{$mask} = \@array;
 
-       } elsif (/^::(\S+) bans$/) {            # bans: start.
+       } elsif (/^::(\S+) bans$/) {            # bans: start entry.
            $chan       = $1;
            $type       = "bans";
 
        } elsif (/^- (\S+):\+(\d+):\+(\d+):(\d+):(\S+):(.*)$/ and $type eq "bans") {
-           ### bans: body.
+           ### bans: middle entry.
            # $btime, $atime, $count, $whoby, $reason.
            my(@array) = ($2,$3,$4,$5,$6);
            $bans{$chan}{$1} = \@array;
@@ -159,47 +152,18 @@ sub writeUserFile {
        print OUT "$user\n";
 
        foreach (sort keys %{ $users{$user} }) {
-           # todo: rename what to key?
            my $what    = $_;
-           my $val     = $users{$user}{$what};
+           my $val     = $users{$user}{$_};
 
-           if (ref($val) eq "SCALAR") {
-               print OUT "--$what\t\t$val\n";
-               next;
-           }
-
-           next unless (ref($val) eq "HASH");
-
-           if ($what ne "attr") {
-               foreach (sort keys %{ $users{$user}{$what} }) {
+           if (ref($val) eq "HASH") {
+               foreach (sort keys %{ $users{$user}{$_} }) {
                    print OUT "--$what\t\t$_\n";
                }
-               next;
-           }
 
-           # 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";
+           } elsif ($_ eq 'FLAGS') {
+               print OUT "--$_\t\t" . join('', sort split('', $val)) . "\n";
+           } else {
+               print OUT "--$_\t\t$val\n";
            }
        }
        print OUT "\n";
@@ -262,7 +226,7 @@ sub writeUserFile {
     $wtime_userfile = time();
     &status("--- Saved USERFILE ($cusers users; $cbans bans; $cignore ignore) at $time");
     if (defined $msgType and $msgType =~ /^chat$/) {
-       &pSReply("--- Writing user file...");
+       &performStrictReply("--- Writing user file...");
     }
 }
 
@@ -312,7 +276,7 @@ sub readChanFile {
            $cache{negative} = 1;
 #          $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 {
@@ -435,7 +399,7 @@ sub writeChanFile {
                " chans) at $time");
 
     if (defined $msgType and $msgType =~ /^chat$/) {
-       &pSReply("--- Writing chan file...");
+       &performStrictReply("--- Writing chan file...");
     }
 }
 
@@ -443,27 +407,16 @@ sub writeChanFile {
 ##### USER COMMANDS.
 #####
 
-# todo: support multiple flags, eg: "+o-m"
+# TODO: support multiple flags.
+# TODO: return all flags for opers
 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;
@@ -546,13 +499,13 @@ sub ckpasswd {
 sub hasFlag {
     my ($flag) = @_;
 
-    if (&IsFlag($flag) ne $flag) {
+    if (&IsFlag($flag) eq $flag) {
+       return 1;
+    } else {
        &status("DCC CHAT: <$who> $message -- not enough flags.");
-       &pSReply("error: you do not have enough flags for that. ($flag required)");
+       &performStrictReply("error: you do not have enough flags for that. ($flag required)");
        return 0;
     }
-
-    return 1;
 }
 
 # expire is time in minutes
@@ -575,7 +528,7 @@ sub ignoreAdd {
 
     $ignore{$chan}{$mask} = [$expire, time(), $who, $comment];
 
-    # todo: improve this.
+    # TODO: improve this.
     if ($expire == 0) {
        &status("ignore: Added $mask for $chan to NEVER expire, by $who, for $comment");
     } else {
@@ -617,9 +570,11 @@ sub ignoreDel {
 }
 
 sub userAdd {
-    my($nick,$mask) = @_;
+    my($nick,$mask)    = @_;
 
-    return 0 if (exists $users{$nick});
+    if (exists $users{$nick}) {
+       return 0;
+    }
 
     $utime_userfile = time();
     $ucount_userfile++;
@@ -629,7 +584,7 @@ sub userAdd {
        $users{$nick}{HOSTS}{$mask} = 1;
     }
 
-    $users{$nick}{FLAGS} ||= $users{_default}{FLAGS};
+    $users{$nick}{FLAGS}       ||= $users{_default}{FLAGS};
 
     return 1;
 }
@@ -637,7 +592,9 @@ sub userAdd {
 sub userDel {
     my($nick)  = @_;
 
-    return 0 if (!exists $users{$nick});
+    if (!exists $users{$nick}) {
+       return 0;
+    }
 
     $utime_userfile = time();
     $ucount_userfile++;
@@ -649,12 +606,17 @@ sub userDel {
 
 sub banAdd {
     my($mask,$chan,$expire,$reason) = @_;
+
     $chan      ||= "*";
     $expire    ||= 0;
-    $expire    = $expire*60 + time() if ($expire > 0);
+
+    if ($expire > 0) {
+       $expire         = $expire*60 + time();
+    }
 
     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;
@@ -737,20 +699,20 @@ 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{$chan}{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;
     }
 
@@ -765,7 +727,7 @@ sub chanSet {
 
        if ($state) {                   # add/set.
            if (defined $was and $was eq "1") {
-               &pSReply("setting $what for $chan already 1.");
+               &performStrictReply("setting $what for $chan already 1.");
                return;
            }
 
@@ -773,12 +735,12 @@ sub chanSet {
 
        } else {                        # delete/unset.
            if (!defined $was) {
-               &pSReply("setting $what for $chan is not set.");
+               &performStrictReply("setting $what for $chan is not set.");
                return;
            }
 
            if ($was eq "0") {
-               &pSReply("setting $what for $chan already 0.");
+               &performStrictReply("setting $what for $chan already 0.");
                return;
            }
 
@@ -789,10 +751,10 @@ sub chanSet {
        $was    = ($was) ? "; was '$was'" : "";
 
        if ($val eq "0") {
-           &pSReply("Unsetting $what for $chan$was.");
+           &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;
        }
 
@@ -803,11 +765,11 @@ sub chanSet {
 
        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.");
+       &performStrictReply("Setting $what for $chan to '$val'$was.");
 
        $chanconf{$chan}{$what} = $val;
 
@@ -823,9 +785,9 @@ sub chanSet {
        }
 
        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.");
        }
     }
 
@@ -846,17 +808,22 @@ sub rehashConfVars {
        &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}{$_};
        }
     }
@@ -866,43 +833,21 @@ sub 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"
+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;