]> git.donarmstrong.com Git - infobot.git/blobdiff - src/IRC/Schedulers.pl
A round of fixes:
[infobot.git] / src / IRC / Schedulers.pl
index 0e1fa4b9d7124d74ebacc7ed57fd87ba46aa0d8e..2df8512bfbecdd9a4c8b565c077b3d10adb54587 100644 (file)
@@ -16,6 +16,7 @@ sub setupSchedulers {
     # ONCE OFF.
 
     # REPETITIVE.
+    # 1 for run straight away, 2 for on next-run.
     &uptimeLoop(1);
     &randomQuote(2);
     &randomFactoid(2);
@@ -24,18 +25,19 @@ sub setupSchedulers {
     &chanlimitCheck(1);
     &netsplitCheck(1); # mandatory
     &floodLoop(1);     # mandatory
-    &seenFlush(1);
-    &leakCheck(1);     # mandatory
+    &seenFlush(2);
+    &leakCheck(2);     # mandatory
     &ignoreCheck(1);   # mandatory
-    &seenFlushOld(1);
+    &seenFlushOld(2);
     &ircCheck(1);      # mandatory
-    &miscCheck(2);     # mandatory
+    &miscCheck(1);     # mandatory
     &shmFlush(1);      # mandatory
     &slashdotLoop(2);
     &freshmeatLoop(2);
     &kernelLoop(2);
-    &wingateWriteFile(1);
-    &factoidCheck(1);
+    &wingateWriteFile(2);
+    &factoidCheck(2);
+    &newsFlush(1);
 
 #    my $count = map { exists $sched{$_}{TIME} } keys %sched;
     my $count  = 0;
@@ -94,7 +96,7 @@ sub randomQuote {
     }
 
     foreach ( &ChanConfList("randomQuote") ) {
-       next unless (&validChan($_));   # ???
+       next unless (&validChan($_));
 
        &status("sending random Quote to $_.");
        &action($_, "Ponders: ".$line);
@@ -118,7 +120,8 @@ sub randomFactoid {
     while (1) {
        ($key,$val) = &randKey("factoids","factoid_key,factoid_value");
 ###    $val =~ tr/^[A-Z]/[a-z]/;       # blah is Good => blah is good.
-       last if ($val !~ /^</);
+       last if (defined $val and $val !~ /^</);
+
        $error++;
        if ($error == 5) {
            &ERROR("rF: tried 5 times but failed.");
@@ -127,7 +130,7 @@ sub randomFactoid {
     }
 
     foreach ( &ChanConfList("randomFactoid") ) {
-       next unless (&validChan($_));   # ???
+       next unless (&validChan($_));
 
        &status("sending random Factoid to $_.");
        &action($_, "Thinks: \037$key\037 is $val");
@@ -153,7 +156,7 @@ sub randomFreshmeat {
        my $retval = &Freshmeat::randPackage();
 
        foreach (@chans) {
-           next unless (&validChan($_));       # ???
+           next unless (&validChan($_));
 
            &status("sending random Freshmeat to $_.");
            &say($_, $line);
@@ -191,7 +194,7 @@ sub logLoop {
        }
 
        &closeLog();
-       system("/bin/mv '$param{'logfile'}' '$file{log}'");
+       CORE::system("/bin/mv '$param{'logfile'}' '$file{log}'");
        &compress($file{log});
        &openLog();
        &status("cycling log file.");
@@ -281,6 +284,63 @@ sub seenFlushOld {
 
 }
 
+sub newsFlush {
+    if (@_) {
+       &ScheduleThis(1440, "newsFlush");
+       return if ($_[0] eq "2");       # defer.
+    } else {
+       delete $sched{"newsFlush"}{RUNNING};
+    }
+
+    return unless (&IsChanConf("news") > 0);
+
+    my $delete = 0;
+    my $oldest = time();
+    foreach $chan (keys %::news) {
+       foreach $item (keys %{ $::news{$chan} }) {
+           my $t = $::news{$chan}{$item}{Expire};
+
+           my $tadd    = $::news{$chan}{$item}{Time};
+           $oldest     = $tadd if ($oldest > $tadd);
+
+           next if ($t == 0 or $t == -1);
+           if ($t < 1000) {
+               &status("newsFlush: Fixed Expire time for $chan/$item, should not happen anyway.");
+               $::news{$chan}{$item}{Expire} = time() + $t*60*60*24;
+               next;
+           }
+
+           next unless (time() > $t);
+           # todo: show how old it was.
+           delete $::news{$chan}{$item};
+           &VERB("NEWS: deleted '$item'", 2);
+           $delete++;
+       }
+    }
+
+    # todo: flush users aswell.
+    my $duser  = 0;
+    foreach $chan (keys %::newsuser) {
+       foreach (keys %{ $::newsuser{$chan} }) {
+           my $t = $::newsuser{$chan}{$_};
+           if (!defined $t or $t < 1000) {
+               &DEBUG("something wrong with newsuser{$chan}{$_} => $t");
+               next;
+           }
+
+           next unless ($oldest > $t);
+
+           delete $::newsuser{$chan}{$_};
+           $duser++;
+       }
+    }
+
+#    &VERB("NEWS deleted $delete seen entries.",2);
+    &status("NEWS deleted: $delete news entries; $duser user cache.");
+
+    &News::writeNews();
+}
+
 sub chanlimitCheck {
     my $interval = &getChanConfDefault("chanlimitcheckInterval", 10);
 
@@ -291,27 +351,34 @@ sub chanlimitCheck {
        delete $sched{"chanlimitCheck"}{RUNNING};
     }
 
-    foreach ( &ChanConfList("chanlimitcheck") ) {
-       next unless (&validChan($_));   # ???
+    foreach $chan ( &ChanConfList("chanlimitcheck") ) {
+       next unless (&validChan($chan));
 
-       my $limitplus   = &getChanConfDefault("chanlimitcheckPlus", 5, $_);
-       my $newlimit    = scalar(keys %{$channels{$_}{''}}) + $limitplus;
-       my $limit       = $channels{$_}{'l'};
+       my $limitplus   = &getChanConfDefault("chanlimitcheckPlus", 5, $chan);
+       my $newlimit    = scalar(keys %{$channels{$chan}{''}}) + $limitplus;
+       my $limit       = $channels{$chan}{'l'};
 
-       if (scalar keys %{$channels{$_}{''}} > $limit) {
-           &status("LIMIT: set too low!!! FIXME");
+       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);
 
-       if (!exists $channels{$_}{'o'}{$ident}) {
-           &ERROR("chanlimitcheck: dont have ops on $_.");
+       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?
            next;
        }
-       &rawout("MODE $_ +l $newlimit");
-    }
+       delete $cache{warn}{chanlimit}{$chan};
+
+       if (!defined $limit) {
+           &status("ChanLimit: setting for first time or from netsplit, for $chan");
+       }
 
+       &rawout("MODE $chan +l $newlimit");
+    }
 }
 
 sub netsplitCheck {
@@ -334,17 +401,22 @@ sub netsplitCheck {
     }
 
     # %netsplit hash checker.
+    my $count  = scalar keys %netsplit;
     foreach (keys %netsplit) {
        if (&IsNickInAnyChan($_)) {
            &DEBUG("netsplitC: $_ is in some chan; removing from netsplit list.");
            delete $netsplit{$_};
        }
-       next unless (time() - $netsplit{$_} > 60*60*2); # 2 hours.
-       next if (&IsNickInAnyChan($_));
+       next unless (time() - $netsplit{$_} > 60*10);
 
-       &DEBUG("netsplitC: $_ didn't come back from netsplit in 2 hours; removing from netsplit list.");
+       &DEBUG("netsplitC: $_ didn't come back from netsplit; removing from netsplit list.");
        delete $netsplit{$_};
     }
+
+    if ($count and !scalar keys %netsplit) {
+       &DEBUG("ok, netsplit is hopefully gone. reinstating chanlimit check.");
+       &chanlimitCheck();
+    }
 }
 
 sub floodLoop {
@@ -378,13 +450,6 @@ sub floodLoop {
 }
 
 sub seenFlush {
-    my %stats;
-    my $nick;
-    my $flushed        = 0;
-    $stats{'count_old'} = &countKeys("seen") || 0;
-    $stats{'new'}      = 0;
-    $stats{'old'}      = 0;
-
     if (@_) {
        my $interval = &getChanConfDefault("seenFlushInterval", 60);
        &ScheduleThis($interval, "seenFlush");
@@ -393,8 +458,31 @@ sub seenFlush {
        delete $sched{"seenFlush"}{RUNNING};
     }
 
+    my %stats;
+    my $nick;
+    my $flushed        = 0;
+    $stats{'count_old'} = &countKeys("seen") || 0;
+    $stats{'new'}      = 0;
+    $stats{'old'}      = 0;
+
     if ($param{'DBType'} =~ /^mysql|pg|postgres/i) {
        foreach $nick (keys %seencache) {
+           if (0) {
+           my $retval = &dbReplace("seen", $nick, (
+                       "nick" => $seencache{$nick}{'nick'},
+                       "time" => $seencache{$nick}{'time'},
+                       "host" => $seencache{$nick}{'host'},
+                       "channel" => $seencache{$nick}{'chan'},
+                       "message" => $seencache{$nick}{'msg'},
+           ) );
+           &DEBUG("retval => $retval.");
+           delete $seencache{$nick};
+           $flushed++;
+
+           next;
+           }
+           ### OLD CODE...
+
            my $exists = &dbGet("seen","nick", $nick, "nick");
 
            if (defined $exists and $exists) {
@@ -446,14 +534,13 @@ sub seenFlush {
 
     &status("Flushed $flushed seen entries.")          if ($flushed);
     &VERB(sprintf("  new seen: %03.01f%% (%d/%d)",
-       $stats{'new'}*100/$stats{'count_old'},
+       $stats{'new'}*100/($stats{'count_old'} || 1),
        $stats{'new'}, $stats{'count_old'} ), 2)        if ($stats{'new'});
     &VERB(sprintf("  now seen: %3.1f%% (%d/%d)",
        $stats{'old'}*100/&countKeys("seen"),
        $stats{'old'}, &countKeys("seen") ), 2)         if ($stats{'old'});
 
     &WARN("scalar keys seenflush != 0!")       if (scalar keys %seenflush);
-
 }
 
 sub leakCheck {
@@ -461,7 +548,7 @@ sub leakCheck {
     my $count = 0;
 
     if (@_) {
-       &ScheduleThis(60, "leakCheck");
+       &ScheduleThis(240, "leakCheck");
        return if ($_[0] eq "2");
     } else {
        delete $sched{"leakCheck"}{RUNNING};
@@ -489,11 +576,13 @@ sub leakCheck {
     my $delete = 0;
     foreach (keys %nuh) {
        next if (&IsNickInAnyChan($_));
+       next if (exists $dcc{CHAT}{$_});
+
        delete $nuh{$_};
        $delete++;
     }
 
-    &DEBUG("$delete nuh{} items deleted; now have ".
+    &status("leakC: $delete nuh{} items deleted; now have ".
                                scalar(keys %nuh) ) if ($delete);
 }
 
@@ -527,7 +616,7 @@ sub ignoreCheck {
 sub ircCheck {
 
     if (@_) {
-       &ScheduleThis(240, "ircCheck");
+       &ScheduleThis(60, "ircCheck");
        return if ($_[0] eq "2");       # defer.
     } else {
        delete $sched{"ircCheck"}{RUNNING};
@@ -548,15 +637,29 @@ sub ircCheck {
        &rawout("PRIVMSG ChanServ :OP $chan $ident");
     }
 
-    if (!$conn->connected and time - $msgtime > 3600) {
-       &WARN("ircCheck: no msg for 3600 and disco'd! reconnecting!");
-       $msgtime = time();      # just in case.
-       &ircloop();
+    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!");
+           $msgtime = time();  # just in case.
+           &ircloop();
+           delete $cache{connect};
+       } else {
+           &status("IRCTEST: possible lost in space; checking. ".
+               scalar(localtime) );
+           &msg($ident, "TEST");
+           $cache{connect} = time();
+       }
     }
 
     if ($ident !~ /^\Q$param{ircNick}\E$/) {
        &WARN("ircCheck: ident($ident) != param{ircNick}($param{IrcNick}).");
-       ### TODO: schedule check for own nick if taken?
+       if (! &IsNickInAnyChan( $param{ircNick} ) ) {
+           &DEBUG("$param{ircNick} not in use... changing!");
+           &nick( $param{ircNick} );
+       } else {
+           &WARN("$param{ircNick} is still in use...");
+       }
     }
 
     &joinNextChan();
@@ -565,11 +668,13 @@ sub ircCheck {
 
     if (grep /^\s*$/, keys %channels) {
        &WARN("we have a NULL chan in hash channels? removing!");
-       delete $channels{''};   # ???
+       delete $channels{''};
+
        &DEBUG("channels now:");
        foreach (keys %channels) {
            &status("  $_");
        }
+
        &DEBUG("channels END");
     }
 
@@ -617,18 +722,18 @@ sub miscCheck {
            next if (time() - $time < 60*60);
 
        } else {
-           &DEBUG("shm: $shmid is not ours or old blootbot => ($z)");
-           next;
+#          &DEBUG("shm: $shmid is not ours or old blootbot => ($z)");
+#          next;
        }
 
        &status("SHM: nuking shmid $shmid");
-       system("/usr/bin/ipcrm shm $shmid >/dev/null");
+       CORE::system("/usr/bin/ipcrm shm $shmid >/dev/null");
     }
 
     # debian check.
     opendir(DEBIAN, "$bot_base_dir/debian");
     foreach ( grep /gz$/, readdir(DEBIAN) ) {
-       my $exit = system("gzip -t $bot_base_dir/debian/$_");
+       my $exit = CORE::system("gzip -t $bot_base_dir/debian/$_");
        next unless ($exit);
 
        &status("debian: unlinking file => $_");
@@ -636,6 +741,17 @@ 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);
+
+    # 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();
 }
@@ -658,6 +774,7 @@ sub shmFlush {
     }
 
     foreach (split '\|\|', $shmmsg) {
+       next if (/^$/);
        &VERB("shm: Processing '$_'.",2);
 
        if (/^DCC SEND (\S+) (\S+)$/) {
@@ -703,6 +820,8 @@ sub getNickInUse {
 }
 
 sub uptimeLoop {
+    return unless &IsChanConf("uptime");
+
     if (@_) {
        &ScheduleThis(60, "uptimeLoop");
        return if ($_[0] eq "2");       # defer.
@@ -864,22 +983,31 @@ sub factoidCheck {
        delete $sched{"factoidCheck"}{RUNNING};
     }
 
-    my @list = &searchTable("factoids", "factoid_key", "factoid_key", " #DEL#");
-    my $stale = &getChanConfDefault("factoidDeleteDelay", 7)*60*60*24;
+    my @list   = &searchTable("factoids", "factoid_key", "factoid_key", " #DEL#");
+    my $stale  = &getChanConfDefault("factoidDeleteDelay", 30) *60*60*24;
+    my $time   = time();
 
     foreach (@list) {
        my $age = &getFactInfo($_, "modified_time");    
-       next unless (time() - $age > $stale);
+       if (!defined $age or $age !~ /^\d+$/) {
+           &WARN("age == NULL or not numeric.");
+           next;
+       }
+
+       next unless ($time - $age > $stale);
 
        my $fix = $_;
        $fix =~ s/ #DEL#$//g;
-       &VERB("safedel: Removing $fix for good.",2);
+       &DEBUG("safedel: Removing $fix ($_) for good.");
+
        &delFactoid($_);
     }
 
 }
 
 sub dccStatus {
+    return unless (scalar keys %{ $dcc{CHAT} });
+
     if (@_) {
        &ScheduleThis(10, "dccStatus");
        return if ($_[0] eq "2");       # defer.
@@ -889,10 +1017,18 @@ sub dccStatus {
 
     my $time = strftime("%H:%M", localtime(time()) );
 
-    return unless (scalar keys %{ $DCC{CHAT} });
-
+    my $c;
     foreach (keys %channels) {
-       &DCCBroadcast("[$time] $_: $users members ($chops chops), $bans bans","+o");
+       my $c           = $_;
+       my $users       = keys %{ $channels{$c}{''} };
+       my $chops       = keys %{ $channels{$c}{o}  };
+       my $bans        = keys %{ $channels{$c}{b}  };
+
+       my $txt = "[$time] $c: $users members ($chops chops), $bans bans";
+       foreach (keys %{ $dcc{'CHAT'} }) {
+           next unless (exists $channels{$c}{''}{lc $_});
+           $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
+       }
     }
 }
 
@@ -931,7 +1067,7 @@ sub getChanConfDefault {
 
     if (exists $param{$what}) {
        if (!exists $cache{config}{$what}) {
-           &DEBUG("backward-compat: found param{$what} instead.");
+           &status("conf: backward-compat: found param{$what} ($param{$what}) instead.");
            $cache{config}{$what} = 1;
        }
 
@@ -942,10 +1078,30 @@ sub getChanConfDefault {
     if (defined $val) {
        return $val;
     }
+
     $param{$what}      = $default;
-    &status("gCCD: not configured; setting param{$what} = $default");
-    ### TODO: set some vars?
+    &status("conf: auto-setting param{$what} = $default");
+    $cache{config}{$what} = 1;
+
     return $default;
 }
 
+sub mkBackup {
+    my($file, $time) = @_;
+    my $backup = 0;
+
+    if ( -e "$file~" ) {
+       $backup++ if ( -s $file > -s "$file~");
+       $backup++ if ((stat $file)[9] - (stat "$file~")[9] > $time);
+    } else {
+       $backup++;
+    }
+    return unless ($backup);
+
+    ### TODO: do internal copying.
+    &status("Backup: $file to $file~");
+    CORE::system("/bin/cp $file $file~");
+    CORE::system("/bin/touch $file~"); # needed!
+}
+
 1;