]> git.donarmstrong.com Git - infobot.git/blobdiff - src/IRC/Schedulers.pl
hookMode: change chan to nick. if deopped by chanserv, check it
[infobot.git] / src / IRC / Schedulers.pl
index 2df8512bfbecdd9a4c8b565c077b3d10adb54587..d82c9eb63efdca5bbc7d3c6eae4235f1a4d8e9a7 100644 (file)
@@ -29,8 +29,9 @@ sub setupSchedulers {
     &leakCheck(2);     # mandatory
     &ignoreCheck(1);   # mandatory
     &seenFlushOld(2);
-    &ircCheck(1);      # mandatory
+    &ircCheck(2);      # mandatory
     &miscCheck(1);     # mandatory
+    &miscCheck2(2);    # mandatory
     &shmFlush(1);      # mandatory
     &slashdotLoop(2);
     &freshmeatLoop(2);
@@ -323,7 +324,7 @@ sub newsFlush {
     foreach $chan (keys %::newsuser) {
        foreach (keys %{ $::newsuser{$chan} }) {
            my $t = $::newsuser{$chan}{$_};
-           if (!defined $t or $t < 1000) {
+           if (!defined $t or ($t > 2 and $t < 1000)) {
                &DEBUG("something wrong with newsuser{$chan}{$_} => $t");
                next;
            }
@@ -351,24 +352,33 @@ sub chanlimitCheck {
        delete $sched{"chanlimitCheck"}{RUNNING};
     }
 
+    if (scalar keys %netsplitservers) {
+       &WARN("clC: netsplit active (2); skipping. (netsplit => $netsplit)");
+       return;
+    }
+
     foreach $chan ( &ChanConfList("chanlimitcheck") ) {
        next unless (&validChan($chan));
 
        my $limitplus   = &getChanConfDefault("chanlimitcheckPlus", 5, $chan);
-       my $newlimit    = scalar(keys %{$channels{$chan}{''}}) + $limitplus;
+       my $newlimit    = scalar(keys %{ $channels{$chan}{''} }) + $limitplus;
        my $limit       = $channels{$chan}{'l'};
 
-       if (defined $limit and scalar keys %{$channels{$chan}{''}} > $limit) {
+       if (defined $limit and scalar keys %{ $channels{$chan}{''} } > $limit) {
            &FIXME("LIMIT: set too low!!! FIXME");
            ### run NAMES again and flush it.
        }
 
-       next unless (!defined $limit or $limit != $newlimit);
+       next unless (!defined $limit);
+       if (defined $limit and $limit == $newlimit) {
+           $cache{chanlimitChange}{$chan} = time();
+           next;
+       }
 
        if (!exists $channels{$chan}{'o'}{$ident}) {
            &status("ChanLimit: dont have ops on $chan.") unless (exists $cache{warn}{chanlimit}{$chan});
            $cache{warn}{chanlimit}{$chan} = 1;
-           ### TODO: check chanserv?
+           &chanServCheck($chan);
            next;
        }
        delete $cache{warn}{chanlimit}{$chan};
@@ -377,7 +387,16 @@ sub chanlimitCheck {
            &status("ChanLimit: setting for first time or from netsplit, for $chan");
        }
 
+       if (exists $cache{chanlimitChange}{$chan}) {
+           my $delta = time() - $cache{chanlimitChange}{$chan};
+           if ($delta < $interval*60) {
+               &DEBUG("not going to change chanlimit! ($delta<$interval*60)");
+               return;
+           }
+       }
+
        &rawout("MODE $chan +l $newlimit");
+       $cache{chanlimitChange}{$chan} = time();
     }
 }
 
@@ -392,7 +411,7 @@ sub netsplitCheck {
     }
 
     foreach $s1 (keys %netsplitservers) {
-       foreach $s2 (keys %{$netsplitservers{$s1}}) {
+       foreach $s2 (keys %{ $netsplitservers{$s1} }) {
            if (time() - $netsplitservers{$s1}{$s2} > 3600) {
                &status("netsplit between $s1 and $s2 appears to be stale.");
                delete $netsplitservers{$s1}{$s2};
@@ -406,6 +425,7 @@ sub netsplitCheck {
        if (&IsNickInAnyChan($_)) {
            &DEBUG("netsplitC: $_ is in some chan; removing from netsplit list.");
            delete $netsplit{$_};
+           next;
        }
        next unless (time() - $netsplit{$_} > 60*10);
 
@@ -434,7 +454,7 @@ sub floodLoop {
     my $interval       = &getChanConfDefault("floodCycle",60);
 
     foreach $who (keys %flood) {
-       foreach (keys %{$flood{$who}}) {
+       foreach (keys %{ $flood{$who} }) {
            if (!exists $flood{$who}{$_}) {
                &WARN("flood{$who}{$_} undefined?");
                next;
@@ -468,7 +488,8 @@ sub seenFlush {
     if ($param{'DBType'} =~ /^mysql|pg|postgres/i) {
        foreach $nick (keys %seencache) {
            if (0) {
-           my $retval = &dbReplace("seen", $nick, (
+           #BROKEN#
+           my $retval = &dbReplace("seen", "nick", $nick, (
                        "nick" => $seencache{$nick}{'nick'},
                        "time" => $seencache{$nick}{'time'},
                        "host" => $seencache{$nick}{'host'},
@@ -554,25 +575,43 @@ sub leakCheck {
        delete $sched{"leakCheck"}{RUNNING};
     }
 
-    # flood.
+    # flood. this is dealt with in floodLoop()
     foreach $blah1 (keys %flood) {
-       foreach $blah2 (keys %{$flood{$blah1}}) {
-           $count += scalar(keys %{$flood{$blah1}{$blah2}});
+       foreach $blah2 (keys %{ $flood{$blah1} }) {
+           $count += scalar(keys %{ $flood{$blah1}{$blah2} });
        }
     }
-    &VERB("\%flood has $count total keys.",2);
+    &DEBUG("leak: hash flood has $count total keys.",2);
+
+    # floodjoin.
+    $count = 0;
+    foreach $blah1 (keys %floodjoin) {
+       foreach $blah2 (keys %{ $floodjoin{$blah1} }) {
+           $count += scalar(keys %{ $floodjoin{$blah1}{$blah2} });
+       }
+    }
+    &DEBUG("leak: hash floodjoin has $count total keys.",2);
+
+    # floodwarn.
+    $count = scalar(keys %floodwarn);
+    &DEBUG("leak: hash floodwarn has $count total keys.",2);
 
     my $chan;
     foreach $chan (grep /[A-Z]/, keys %channels) {
        &DEBUG("leak: chan => '$chan'.");
        my ($i,$j);
-       foreach $i (keys %{$channels{$chan}}) {
-           foreach (keys %{$channels{$chan}{$i}}) {
+       foreach $i (keys %{ $channels{$chan} }) {
+           foreach (keys %{ $channels{$chan}{$i} }) {
                &DEBUG("leak:   \$channels{$chan}{$i}{$_} ...");
            }
        }
     }
 
+    # chanstats
+    $count = scalar(keys %chanstats);
+    &DEBUG("leak: hash chanstats has $count total keys.",2);
+
+    # nuh.
     my $delete = 0;
     foreach (keys %nuh) {
        next if (&IsNickInAnyChan($_));
@@ -582,7 +621,7 @@ sub leakCheck {
        $delete++;
     }
 
-    &status("leakC: $delete nuh{} items deleted; now have ".
+    &status("leak: $delete nuh{} items deleted; now have ".
                                scalar(keys %nuh) ) if ($delete);
 }
 
@@ -614,7 +653,6 @@ sub ignoreCheck {
 }
 
 sub ircCheck {
-
     if (@_) {
        &ScheduleThis(60, "ircCheck");
        return if ($_[0] eq "2");       # defer.
@@ -622,22 +660,16 @@ sub ircCheck {
        delete $sched{"ircCheck"}{RUNNING};
     }
 
-    my @array = grep !/^_default$/, keys %chanconf;
-    my $iconf = scalar(@array);
-    my $inow  = scalar(keys %channels);
+    my @x      = &getJoinChans();
+    my $iconf  = scalar( @x );
+    my $inow   = scalar( keys %channels );
     if ($iconf > 2 and $inow * 2 <= $iconf) {
        &FIXME("ircCheck: current channels * 2 <= config channels. FIXME.");
+       @joinchan       = @x;
+       &joinNextChan();
     }
 
-    # chanserv ops.
-    foreach ( &ChanConfList("chanServ_ops") ) {
-       next if (exists $channels{$chan}{'o'}{$ident});
-
-       &status("ChanServ ==> Requesting ops for $chan.");
-       &rawout("PRIVMSG ChanServ :OP $chan $ident");
-    }
-
-    if (!$conn->connected or time - $msgtime > 3600) {
+    if (!$conn->connected or time() - $msgtime > 3600) {
        # todo: shouldn't we use cache{connect} somewhere?
        if (exists $cache{connect}) {
            &WARN("ircCheck: no msg for 3600 and disco'd! reconnecting!");
@@ -653,7 +685,10 @@ sub ircCheck {
     }
 
     if ($ident !~ /^\Q$param{ircNick}\E$/) {
+       # this does not work unfortunately.
        &WARN("ircCheck: ident($ident) != param{ircNick}($param{IrcNick}).");
+
+       # this check is misleading... perhaps we should do a notify.
        if (! &IsNickInAnyChan( $param{ircNick} ) ) {
            &DEBUG("$param{ircNick} not in use... changing!");
            &nick( $param{ircNick} );
@@ -662,22 +697,22 @@ sub ircCheck {
        }
     }
 
-    &joinNextChan();
-       # if scalar @joinnext => join more channels
-       # else check for chanserv.
-
     if (grep /^\s*$/, keys %channels) {
-       &WARN("we have a NULL chan in hash channels? removing!");
-       delete $channels{''};
-
-       &DEBUG("channels now:");
-       foreach (keys %channels) {
-           &status("  $_");
+       &WARN("ircCheck: we have a NULL chan in hash channels? removing!");
+       if (exists $channels{''}) {
+           &DEBUG("ircCheck: ok it existed!");
+       } else {
+           &DEBUG("ircCheck: this hsould never happen!");
        }
 
-       &DEBUG("channels END");
+       delete $channels{''};
     }
 
+    &DEBUG("ircstats...");
+    &DEBUG("  pubsleep: $pubsleep");
+    &DEBUG("  msgsleep: $msgsleep");
+    &DEBUG("  notsleep: $notsleep");
+
     ### USER FILE.
     if ($utime_userfile > $wtime_userfile and time() - $wtime_userfile > 3600) {
        &writeUserFile();
@@ -730,6 +765,31 @@ sub miscCheck {
        CORE::system("/usr/bin/ipcrm shm $shmid >/dev/null");
     }
 
+    # make backup of important files.
+    &mkBackup( $bot_misc_dir."/blootbot.chan", 60*60*24*3);
+    &mkBackup( $bot_misc_dir."/blootbot.users", 60*60*24*3);
+    &mkBackup( $bot_base_dir."/blootbot-news.txt", 60*60*24*1);
+
+    # flush cache{lobotomy}
+    foreach (keys %{ $cache{lobotomy} }) {
+       next unless (time() - $cache{lobotomy}{$_} > 60*60);
+       delete $cache{lobotomy}{$_};
+    }
+
+    ### check modules if they've been modified. might be evil.
+    &reloadAllModules();
+}
+
+sub miscCheck2 {
+    if (@_) {
+       &ScheduleThis(240, "miscCheck2");
+       return if ($_[0] eq "2");       # defer.
+    } else {
+       delete $sched{"miscCheck2"}{RUNNING};
+    }
+
+    &DEBUG("miscCheck2: Doing debian checking...");
+
     # debian check.
     opendir(DEBIAN, "$bot_base_dir/debian");
     foreach ( grep /gz$/, readdir(DEBIAN) ) {
@@ -741,19 +801,21 @@ sub miscCheck {
     }
     closedir DEBIAN;
 
-    # make backup of important files.
-    &mkBackup( $bot_misc_dir."/blootbot.chan", 60*60*24*1);
-    &mkBackup( $bot_misc_dir."/blootbot.users", 60*60*24*1);
-    &mkBackup( $bot_base_dir."/blootbot-news.txt", 60*60*24*1);
+    # compress logs that should have been compressed.
+    # todo: use strftime?
+    my ($day,$month,$year) = (localtime(time()))[3,4,5];
+    my $date = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
 
-    # flush cache{lobotomy}
-    foreach (keys %{ $cache{lobotomy} }) {
-       next unless (time() - $cache{lobotomy}{$_} > 60*60);
-       delete $cache{lobotomy}{$_};
-    }
+    opendir(DIR,"$bot_base_dir/log");
+    while (my $f = readdir(DIR)) {
+       next unless ( -f "$bot_base_dir/log/$f");
+       next if ($f =~ /gz|bz2/);
+       next unless ($f =~ /(\d{8})/);
+       next if ($date eq $1);
 
-    ### check modules if they've been modified. might be evil.
-    &reloadAllModules();
+       &compress("$bot_base_dir/log/$f");
+    }
+    closedir DIR;
 }
 
 sub shmFlush {
@@ -803,6 +865,12 @@ sub shmFlush {
 
 ### this is semi-scheduled
 sub getNickInUse {
+    &DEBUG("gNIU: ident => $ident, ircNick => $param{'ircNick'}");
+    if ($ident eq $param{'ircNick'}) {
+       &status("okay, got my nick back.");
+       return;
+    }
+
     if (@_) {
        &ScheduleThis(30, "getNickInUse");
        return if ($_[0] eq "2");       # defer.
@@ -810,11 +878,6 @@ sub getNickInUse {
        delete $sched{"getNickInUse"}{RUNNING};
     }
 
-    if ($ident eq $param{'ircNick'}) {
-       &status("okay, got my nick back.");
-       return;
-    }
-
     &status("Trying to get my nick back.");
     &nick( $param{'ircNick'} );
 }
@@ -989,8 +1052,18 @@ sub factoidCheck {
 
     foreach (@list) {
        my $age = &getFactInfo($_, "modified_time");    
+
        if (!defined $age or $age !~ /^\d+$/) {
-           &WARN("age == NULL or not numeric.");
+           if (scalar @list > 50) {
+               if (!$cache{warnDel}) {
+                   &WARN("list is over 50 (".scalar(@list)."... giving it a miss.");
+                   $cache{warnDel} = 1;
+                   last;
+               }
+           }
+
+           &WARN("old cruft (no time): $_");
+           &delFactoid($_);
            next;
        }
 
@@ -1090,18 +1163,23 @@ sub mkBackup {
     my($file, $time) = @_;
     my $backup = 0;
 
+    if (! -f $file) {
+       &WARN("mkB: file $file don't exist.");
+       return;
+    }
+
     if ( -e "$file~" ) {
-       $backup++ if ( -s $file > -s "$file~");
        $backup++ if ((stat $file)[9] - (stat "$file~")[9] > $time);
     } else {
        $backup++;
     }
     return unless ($backup);
 
+    my $age = &Time2String(time() - (stat $file)[9]);
+
     ### TODO: do internal copying.
-    &status("Backup: $file to $file~");
+    &status("Backup: $file ($age)");
     CORE::system("/bin/cp $file $file~");
-    CORE::system("/bin/touch $file~"); # needed!
 }
 
 1;