]> git.donarmstrong.com Git - infobot.git/blobdiff - src/IRC/Schedulers.pl
- added CR's here and there; minor reformatting
[infobot.git] / src / IRC / Schedulers.pl
index e6ae976991b99f3d32d36aeaf59ef215bf3ad1b6..25544e9f4edb62df9befc5b2857aaf5faffecf4a 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);
@@ -28,14 +29,16 @@ sub setupSchedulers {
     &leakCheck(2);     # mandatory
     &ignoreCheck(1);   # mandatory
     &seenFlushOld(2);
-    &ircCheck(1);      # mandatory
-    &miscCheck(2);     # mandatory
+    &ircCheck(2);      # mandatory
+    &miscCheck(1);     # mandatory
+    &miscCheck2(2);    # mandatory
     &shmFlush(1);      # mandatory
     &slashdotLoop(2);
     &freshmeatLoop(2);
     &kernelLoop(2);
     &wingateWriteFile(2);
-    &factoidCheck(2);
+    &factoidCheck(1);
+    &newsFlush(1);
 
 #    my $count = map { exists $sched{$_}{TIME} } keys %sched;
     my $count  = 0;
@@ -71,7 +74,7 @@ sub ScheduleThis {
     my $retval = $conn->schedule($waittime, \&$codename, @args);
     $sched{$codename}{LABEL}   = $retval;
     $sched{$codename}{TIME}    = time()+$waittime;
-    $sched{$codename}{RUNNING} = 1;
+    $sched{$codename}{LOOP}    = 1;
 }
 
 ####
@@ -83,8 +86,6 @@ sub randomQuote {
     if (@_) {
        &ScheduleThis($interval, "randomQuote");
        return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"randomQuote"}{RUNNING};
     }
 
     my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.randtext");
@@ -111,14 +112,13 @@ sub randomFactoid {
     if (@_) {
        &ScheduleThis($interval, "randomFactoid");
        return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"randomFactoid"}{RUNNING};
     }
 
     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.");
@@ -142,8 +142,6 @@ sub randomFreshmeat {
     if (@_) {
        &ScheduleThis($interval, "randomFreshmeat");
        return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"randomFreshmeat"}{RUNNING};
     }
 
     my @chans = &ChanConfList("randomFreshmeat");
@@ -165,8 +163,6 @@ sub logLoop {
     if (@_) {
        &ScheduleThis(60, "logLoop");
        return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"logLoop"}{RUNNING};
     }
 
     return unless (defined fileno LOG);
@@ -239,8 +235,6 @@ sub seenFlushOld {
     if (@_) {
        &ScheduleThis(1440, "seenFlushOld");
        return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"seenFlushOld"}{RUNNING};
     }
 
     # is this global-only?
@@ -281,41 +275,142 @@ sub seenFlushOld {
 
 }
 
+sub newsFlush {
+    if (@_) {
+       &ScheduleThis(60, "newsFlush");
+       return if ($_[0] eq "2");       # defer.
+    }
+
+    if (&ChanConfList("news")) {
+    } else {
+       &DEBUG("newsFlush: news disabled? (chan => $chan)");
+       return;
+    }
+
+    my $delete = 0;
+    my $oldest = time();
+    my %none;
+    foreach $chan (keys %::news) {
+       my $i           = 0;
+       my $total       = scalar(keys %{ $::news{$chan} });
+
+       &DEBUG("newsFlush: chan => $chan (total => $total)");
+
+       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;
+           }
+
+           my $delta = $t - time();
+
+           next unless (time() > $t);
+
+           # todo: show how old it was.
+           delete $::news{$chan}{$item};
+           &status("NEWS: (newsflush) deleted '$item'");
+           $delete++;
+           $i++;
+       }
+
+       &status("NEWS (newsflush) {$chan}: deleted [$i/$total] news entries.");
+       $none{$chan} = 1 if ($total == $i);
+    }
+
+    # todo: flush users aswell.
+    my $duser  = 0;
+    foreach $chan (keys %::newsuser) {
+       next if (exists $none{$chan});
+
+       foreach (keys %{ $::newsuser{$chan} }) {
+           my $t = $::newsuser{$chan}{$_};
+           if (!defined $t or ($t > 2 and $t < 1000)) {
+               &DEBUG("something wrong with newsuser{$chan}{$_} => $t");
+               next;
+           }
+
+           next unless ($oldest > $t);
+
+           delete $::newsuser{$chan}{$_};
+           $duser++;
+       }
+
+       my $i = scalar(keys %{ $::newsuser{$chan} });
+       delete $::newsuser{$chan} unless ($i);
+    }
+
+    if ($delete or $duser) {
+       &DEBUG("newsF: Writing news.");
+       &News::writeNews();
+       &status("NEWS (newsflush) deleted: $delete news entries; $duser user cache.");
+    }
+}
+
 sub chanlimitCheck {
     my $interval = &getChanConfDefault("chanlimitcheckInterval", 10);
 
     if (@_) {
        &ScheduleThis($interval, "chanlimitCheck");
        return if ($_[0] eq "2");
-    } else {
-       delete $sched{"chanlimitCheck"}{RUNNING};
     }
 
-    foreach ( &ChanConfList("chanlimitcheck") ) {
-       next unless (&validChan($_));
+    foreach $chan ( &ChanConfList("chanlimitcheck") ) {
+       next unless (&validChan($chan));
+
+       my $limitplus   = &getChanConfDefault("chanlimitcheckPlus", 5, $chan);
+       my $newlimit    = scalar(keys %{ $channels{$chan}{''} }) + $limitplus;
+       my $limit       = $channels{$chan}{'l'};
+
+       if (scalar keys %netsplitservers) {
+           if (defined $limit) {
+               &DEBUG("chanlimit: removing it for $chan.");
+               &rawout("MODE $chan -l");
+               $cache{chanlimitChange}{$chan} = time();
+           }
 
-       my $limitplus   = &getChanConfDefault("chanlimitcheckPlus", 5, $_);
-       my $newlimit    = scalar(keys %{$channels{$_}{''}}) + $limitplus;
-       my $limit       = $channels{$_}{'l'};
+           next;
+       }
 
-       if (scalar keys %{$channels{$_}{''}} > $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{$_}{'o'}{$ident}) {
-           &ERROR("chanlimitcheck: dont have ops on $_.");
-           ### TODO: check chanserv?
+       if (!exists $channels{$chan}{'o'}{$ident}) {
+           &status("ChanLimit: dont have ops on $chan.") unless (exists $cache{warn}{chanlimit}{$chan});
+           $cache{warn}{chanlimit}{$chan} = 1;
+           &chanServCheck($chan);
            next;
        }
+       delete $cache{warn}{chanlimit}{$chan};
 
        if (!defined $limit) {
-           &DEBUG("setting limit for first time or from netsplit for $_");
+           &status("ChanLimit: setting for first time or from netsplit, for $chan");
        }
 
-       &rawout("MODE $_ +l $newlimit");
+       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();
     }
 }
 
@@ -325,17 +420,25 @@ sub netsplitCheck {
     if (@_) {
        &ScheduleThis(30, "netsplitCheck");
        return if ($_[0] eq "2");
-    } else {
-       delete $sched{"netsplitCheck"}{RUNNING};
     }
 
+    &DEBUG("running netsplitCheck...");
+
     foreach $s1 (keys %netsplitservers) {
-       foreach $s2 (keys %{$netsplitservers{$s1}}) {
+       &DEBUG("nsC: s1 => $s1");
+
+       foreach $s2 (keys %{ $netsplitservers{$s1} }) {
+           my $delta = time() - $netsplitservers{$s1}{$s2};
+           &DEBUG("nss{$s1}{$s2} = $delta");
+
            if (time() - $netsplitservers{$s1}{$s2} > 3600) {
                &status("netsplit between $s1 and $s2 appears to be stale.");
                delete $netsplitservers{$s1}{$s2};
            }
        }
+
+       my $i = scalar(keys %{ $netsplitservers{$s1} });
+       delete $netsplitservers{$s1} unless ($i);
     }
 
     # %netsplit hash checker.
@@ -344,6 +447,7 @@ sub netsplitCheck {
        if (&IsNickInAnyChan($_)) {
            &DEBUG("netsplitC: $_ is in some chan; removing from netsplit list.");
            delete $netsplit{$_};
+           next;
        }
        next unless (time() - $netsplit{$_} > 60*10);
 
@@ -351,8 +455,11 @@ sub netsplitCheck {
        delete $netsplit{$_};
     }
 
+    &DEBUG("nsC: netsplitservers: ".scalar(keys %netsplitservers) );
+    &DEBUG("nsC: netsplit: ".scalar(keys %netsplit) );
+
     if ($count and !scalar keys %netsplit) {
-       &DEBUG("ok, netsplit is hopefully gone. reinstating chanlimit check.");
+       &DEBUG("nsC: netsplit is hopefully gone. reinstating chanlimit check.");
        &chanlimitCheck();
     }
 }
@@ -364,15 +471,13 @@ sub floodLoop {
     if (@_) {
        &ScheduleThis(60, "floodLoop"); # minutes.
        return if ($_[0] eq "2");
-    } else {
-       delete $sched{"floodLoop"}{RUNNING};
     }
 
     my $time           = time();
     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;
@@ -392,8 +497,6 @@ sub seenFlush {
        my $interval = &getChanConfDefault("seenFlushInterval", 60);
        &ScheduleThis($interval, "seenFlush");
        return if ($_[0] eq "2");
-    } else {
-       delete $sched{"seenFlush"}{RUNNING};
     }
 
     my %stats;
@@ -405,6 +508,22 @@ sub seenFlush {
 
     if ($param{'DBType'} =~ /^mysql|pg|postgres/i) {
        foreach $nick (keys %seencache) {
+           my $retval = &dbReplace("seen", "nick", $nick, (
+###                    "nick" => $seencache{$nick}{'nick'},
+                       "time" => $seencache{$nick}{'time'},
+                       "host" => $seencache{$nick}{'host'},
+                       "channel" => $seencache{$nick}{'chan'},
+                       "message" => $seencache{$nick}{'msg'},
+           ) );
+           delete $seencache{$nick};
+           $flushed++;
+
+           next;
+
+           ###
+           ### old code.
+           ###
+
            my $exists = &dbGet("seen","nick", $nick, "nick");
 
            if (defined $exists and $exists) {
@@ -456,10 +575,10 @@ 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'}, $stats{'count_old'} ), 2)        if ($stats{'new'});
+       $stats{'new'}*100/($stats{'count_old'} || 1),
+       $stats{'new'}, ( $stats{'count_old'} || 1) ), 2) if ($stats{'new'});
     &VERB(sprintf("  now seen: %3.1f%% (%d/%d)",
-       $stats{'old'}*100/&countKeys("seen"),
+       $stats{'old'}*100 / ( &countKeys("seen") || 1),
        $stats{'old'}, &countKeys("seen") ), 2)         if ($stats{'old'});
 
     &WARN("scalar keys seenflush != 0!")       if (scalar keys %seenflush);
@@ -472,29 +591,45 @@ sub leakCheck {
     if (@_) {
        &ScheduleThis(240, "leakCheck");
        return if ($_[0] eq "2");
-    } else {
-       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($_));
@@ -504,7 +639,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);
 }
 
@@ -512,8 +647,6 @@ sub ignoreCheck {
     if (@_) {
        &ScheduleThis(60, "ignoreCheck");
        return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"ignoreCheck"}{RUNNING};
     }
 
     my $time   = time();
@@ -536,37 +669,40 @@ sub ignoreCheck {
 }
 
 sub ircCheck {
-
     if (@_) {
-       &ScheduleThis(120, "ircCheck");
+       &ScheduleThis(60, "ircCheck");
        return if ($_[0] eq "2");       # defer.
-    } else {
-       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 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$/) {
+       # 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} );
@@ -575,22 +711,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();
@@ -607,8 +743,6 @@ sub miscCheck {
     if (@_) {
        &ScheduleThis(240, "miscCheck");
        return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"miscCheck"}{RUNNING};
     }
 
     # SHM check.
@@ -635,14 +769,35 @@ 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");
        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.
+    }
+
     # debian check.
     opendir(DEBIAN, "$bot_base_dir/debian");
     foreach ( grep /gz$/, readdir(DEBIAN) ) {
@@ -654,26 +809,21 @@ sub miscCheck {
     }
     closedir DEBIAN;
 
-    # user/chan file check.
-    foreach ("chan","users") {
-       my $f           = $bot_misc_dir."/blootbot.$_";
-       my $backup      = 0;
+    # 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);
 
-       if ( -e "$f~" ) {
-           $backup++ if ( -s $f > -s "$f~");
-           $backup++ if ( (stat $f)[9] - (stat "$f~")[9] > 60*60*24*7);
-       } else {
-           $backup++;
-       }
-       next unless ($backup);
+    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);
 
-       ### TODO: do internal copying.
-       &status("Backup: $f to $f~");
-       CORE::system("/bin/cp $f $f~");
+       &compress("$bot_base_dir/log/$f");
     }
-
-    ### check modules if they've been modified. might be evil.
-    &reloadAllModules();
+    closedir DIR;
 }
 
 sub shmFlush {
@@ -682,8 +832,6 @@ sub shmFlush {
     if (@_) {
        &ScheduleThis(5, "shmFlush");
        return if ($_[0] eq "2");
-    } else {
-       delete $sched{"shmFlush"}{RUNNING};
     }
 
     my $time;
@@ -723,28 +871,25 @@ sub shmFlush {
 
 ### this is semi-scheduled
 sub getNickInUse {
-    if (@_) {
-       &ScheduleThis(30, "getNickInUse");
-       return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"getNickInUse"}{RUNNING};
-    }
-
     if ($ident eq $param{'ircNick'}) {
        &status("okay, got my nick back.");
        return;
     }
 
-    &status("Trying to get my nick back.");
+    if (@_) {
+       &ScheduleThis(30, "getNickInUse");
+       return if ($_[0] eq "2");       # defer.
+    }
+
     &nick( $param{'ircNick'} );
 }
 
 sub uptimeLoop {
+    return unless &IsChanConf("uptime");
+
     if (@_) {
        &ScheduleThis(60, "uptimeLoop");
        return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"uptimeLoop"}{RUNNING};
     }
 
     &uptimeWriteFile();
@@ -755,8 +900,6 @@ sub slashdotLoop {
     if (@_) {
        &ScheduleThis(60, "slashdotLoop");
        return if ($_[0] eq "2");
-    } else {
-       delete $sched{"slashdotLoop"}{RUNNING};
     }
 
     my @chans = &ChanConfList("slashdotAnnounce");
@@ -779,8 +922,6 @@ sub freshmeatLoop {
     if (@_) {
        &ScheduleThis(60, "freshmeatLoop");
        return if ($_[0] eq "2");
-    } else {
-       delete $sched{"freshmeatLoop"}{RUNNING};
     }
 
     my @chans = &ChanConfList("freshmeatAnnounce");
@@ -802,8 +943,6 @@ sub kernelLoop {
     if (@_) {
        &ScheduleThis(240, "kernelLoop");
        return if ($_[0] eq "2");
-    } else {
-       delete $sched{"kernelLoop"}{RUNNING};
     }
 
     my @chans = &ChanConfList("kernelAnnounce");
@@ -862,8 +1001,6 @@ sub wingateWriteFile {
     if (@_) {
        &ScheduleThis(60, "wingateWriteFile");
        return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"wingateWriteFile"}{RUNNING};
     }
 
     return unless (scalar @wingateCache);
@@ -890,15 +1027,12 @@ sub wingateWriteFile {
        print OUT "$_\n";
     }
     close OUT;
-
 }
 
 sub factoidCheck {
     if (@_) {
-       &ScheduleThis(1440, "factoidCheck");
+       &ScheduleThis(720, "factoidCheck");
        return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"factoidCheck"}{RUNNING};
     }
 
     my @list   = &searchTable("factoids", "factoid_key", "factoid_key", " #DEL#");
@@ -907,16 +1041,29 @@ sub factoidCheck {
 
     foreach (@list) {
        my $age = &getFactInfo($_, "modified_time");    
+       &DEBUG("fC: _ => '$_'; age => $age");
+
        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("del factoid: old cruft (no time): $_");
+           &delFactoid($_);
            next;
        }
 
+       &DEBUG("del factoid: delta => ".($time - $age) );
        next unless ($time - $age > $stale);
 
        my $fix = $_;
        $fix =~ s/ #DEL#$//g;
-       &DEBUG("safedel: Removing $fix ($_) for good.");
+       my $agestr = &Time2String($time - $age);
+       &DEBUG("safedel: Removing '$_' for good. [$agestr old]");
 
        &delFactoid($_);
     }
@@ -929,8 +1076,6 @@ sub dccStatus {
     if (@_) {
        &ScheduleThis(10, "dccStatus");
        return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"dccStatus"}{RUNNING};
     }
 
     my $time = strftime("%H:%M", localtime(time()) );
@@ -985,7 +1130,7 @@ sub getChanConfDefault {
 
     if (exists $param{$what}) {
        if (!exists $cache{config}{$what}) {
-           &status("gCCD: backward-compat: found param{$what} ($param{$what}) instead.");
+           &status("conf: backward-compat: found param{$what} ($param{$what}) instead.");
            $cache{config}{$what} = 1;
        }
 
@@ -998,10 +1143,35 @@ sub getChanConfDefault {
     }
 
     $param{$what}      = $default;
-    &status("gCCD: setting default for param{$what} = $default");
+    &status("conf: auto-setting param{$what} = $default");
     $cache{config}{$what} = 1;
 
     return $default;
 }
 
+sub mkBackup {
+    my($file, $time)   = @_;
+    my $backup         = 0;
+
+    if (! -f $file) {
+       &WARN("mkB: file '$file' does not exist.");
+       return;
+    }
+
+    if ( -e "$file~" ) {
+       $backup++ if ((stat $file)[9] - (stat "$file~")[9] > $time);
+    } else {
+       $backup++;
+    }
+    return unless ($backup);
+
+    # should delta be time(file) - time(file~)?
+    my $delta  = time() - (stat "$file~")[9];
+    my $age    = &Time2String($delta);
+
+    ### TODO: do internal copying.
+    &status("Backup: $file ($age)");
+    CORE::system("/bin/cp $file $file~");
+}
+
 1;