]> git.donarmstrong.com Git - infobot.git/blobdiff - src/IRC/Schedulers.pl
* Fix NULL chan being created in %channels
[infobot.git] / src / IRC / Schedulers.pl
index 843c2b2eef36816b5a379f42f8b423503b077123..2e6bc8f877589260222b2b2595218b3e598c114e 100644 (file)
@@ -5,10 +5,50 @@
 #         Created: 20000117
 #
 
-if (&IsParam("useStrict")) { use strict; }
+# use strict;  # TODO
 
 use POSIX qw(strftime);
-use vars qw(%sched);
+use vars qw(%sched %schedule);
+
+# format: function name = (
+#      str     chanconfdefault,
+#      int     internaldefault,
+#      bool    deferred,
+#      int     next run,               (optional)
+# )
+
+#%schedule = {
+#      uptimeLoop => ('', 60, 1),
+#};
+
+sub setupSchedulersII {
+    foreach (keys %schedule) {
+       &queueTask($_, @{ $schedule{$_} });
+    }
+}
+
+sub queueTask {
+    my($codename, $chanconfdef, $intervaldef, $defer) = @_;
+    my $t = &getChanConfDefault($chanconfdef, $intervaldef, $chan);
+    my $waittime = &getRandomInt($t);
+
+    if (!defined $waittime) {
+       &WARN("interval == waittime == UNDEF for $codename.");
+       return;
+    }
+
+    my $time = $schedule{$codename}[3];
+    if (defined $time and $time > time()) {
+       &WARN("Sched for $codename already exists in " . &Time2String(time() - $time) . ".");
+       return;
+    }
+
+    #&VERB("Scheduling \&$codename() for " . &Time2String($waittime),3);
+
+    my $retval = $conn->schedule($waittime, sub {
+               \&$codename;
+    }, @args );
+}
 
 sub setupSchedulers {
     &VERB("Starting schedulers...",2);
@@ -16,34 +56,37 @@ sub setupSchedulers {
     # ONCE OFF.
 
     # REPETITIVE.
-    # 1 for run straight away, 2 for on next-run.
-    &uptimeLoop(1);
+    # 2 for on next-run.
     &randomQuote(2);
     &randomFactoid(2);
-    &randomFreshmeat(2);
-    &logLoop(1);
-    &chanlimitCheck(1);
-    &netsplitCheck(1); # mandatory
-    &floodLoop(1);     # mandatory
     &seenFlush(2);
     &leakCheck(2);     # mandatory
-    &ignoreCheck(1);   # mandatory
     &seenFlushOld(2);
-    &ircCheck(1);      # mandatory
-    &miscCheck(1);     # mandatory
     &miscCheck2(2);    # mandatory
-    &shmFlush(1);      # mandatory
     &slashdotLoop(2);
-    &freshmeatLoop(2);
+    &plugLoop(2);
     &kernelLoop(2);
     &wingateWriteFile(2);
-    &factoidCheck(2);
-    &newsFlush(1);
+    &factoidCheck(2);  # takes a couple of seconds on a 486. defer it
+# TODO: convert to new format... or nuke altogether.
+    &newsFlush(2);
+
+    # 1 for run straight away
+    &uptimeLoop(1);
+    &logLoop(1);
+    &chanlimitCheck(1);
+    &netsplitCheck(1); # mandatory
+    &floodLoop(1);     # mandatory
+    &ignoreCheck(1);   # mandatory
+    &miscCheck(1);     # mandatory
+    &shmFlush(1);      # mandatory
+    sleep 1;
+    &ircCheck(1);      # mandatory
 
+    # TODO: squeeze this into a one-liner.
 #    my $count = map { exists $sched{$_}{TIME} } keys %sched;
     my $count  = 0;
     foreach (keys %sched) {
-#      next unless (exists $sched{$_}{TIME});
        my $time = $sched{$_}{TIME};
        next unless (defined $time and $time > time());
 
@@ -51,7 +94,7 @@ sub setupSchedulers {
     }
 
     &status("Schedulers: $count will be running.");
-###    &scheduleList();
+    &scheduleList();
 }
 
 sub ScheduleThis {
@@ -65,16 +108,16 @@ sub ScheduleThis {
 
     my $time = $sched{$codename}{TIME};
     if (defined $time and $time > time()) {
-       &WARN("Sched for $codename already exists.");
+       &WARN("Sched for $codename already exists in " . &Time2String(time() - $time) . ".");
        return;
     }
 
-#    &VERB("Scheduling \&$codename() for ".&Time2String($waittime),3);
+    &DEBUG("Scheduling \&$codename() " . \&$codename . " for " . &Time2String($waittime),3);
 
     my $retval = $conn->schedule($waittime, \&$codename, @args);
     $sched{$codename}{LABEL}   = $retval;
     $sched{$codename}{TIME}    = time()+$waittime;
-    $sched{$codename}{RUNNING} = 1;
+    $sched{$codename}{LOOP}    = 1;
 }
 
 ####
@@ -82,23 +125,21 @@ sub ScheduleThis {
 ####
 
 sub randomQuote {
-    my $interval = &getChanConfDefault("randomQuoteInterval", 60);
+    my $interval = &getChanConfDefault('randomQuoteInterval', 60, $chan);
     if (@_) {
-       &ScheduleThis($interval, "randomQuote");
-       return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"randomQuote"}{RUNNING};
-    }
-
-    my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.randtext");
-    if (!defined $line) {
-       &ERROR("random Quote: weird error?");
-       return;
+       &ScheduleThis($interval, 'randomQuote');
+       return if ($_[0] eq '2');       # defer.
     }
 
-    foreach ( &ChanConfList("randomQuote") ) {
+    foreach ( &ChanConfList('randomQuote') ) {
        next unless (&validChan($_));
 
+       my $line = &getRandomLineFromFile($bot_data_dir. "/infobot.randtext");
+       if (!defined $line) {
+           &ERROR("random Quote: weird error?");
+           return;
+       }
+
        &status("sending random Quote to $_.");
        &action($_, "Ponders: ".$line);
     }
@@ -110,76 +151,47 @@ sub randomFactoid {
     my ($key,$val);
     my $error = 0;
 
-    my $interval = &getChanConfDefault("randomFactoidInterval", 60);
+    my $interval = &getChanConfDefault('randomFactoidInterval', 60, $chan);
     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 (defined $val and $val !~ /^</);
-
-       $error++;
-       if ($error == 5) {
-           &ERROR("rF: tried 5 times but failed.");
-           return;
-       }
+       &ScheduleThis($interval, 'randomFactoid');
+       return if ($_[0] eq '2');       # defer.
     }
 
-    foreach ( &ChanConfList("randomFactoid") ) {
+    foreach ( &ChanConfList('randomFactoid') ) {
        next unless (&validChan($_));
 
        &status("sending random Factoid to $_.");
+       while (1) {
+           ($key,$val) = &randKey('factoids',"factoid_key,factoid_value");
+           &DEBUG("rF: $key, $val");
+###        $val =~ tr/^[A-Z]/[a-z]/;   # blah is Good => blah is good.
+           last if ((defined $val) and ($val !~ /^</) and ($key !~ /\#DEL\#/) and ($key !~ /^cmd:/));
+
+           $error++;
+           if ($error == 5) {
+               &ERROR("rF: tried 5 times but failed.");
+               return;
+           }
+       }
        &action($_, "Thinks: \037$key\037 is $val");
        ### FIXME: Use &getReply() on above to format factoid properly?
        $good++;
     }
 }
 
-sub randomFreshmeat {
-    my $interval = &getChanConfDefault("randomFresheatInterval", 60);
-
-    if (@_) {
-       &ScheduleThis($interval, "randomFreshmeat");
-       return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"randomFreshmeat"}{RUNNING};
-    }
-
-    my @chans = &ChanConfList("randomFreshmeat");
-    return unless (scalar @chans);
-
-    &Forker("freshmeat", sub {
-       my $retval = &Freshmeat::randPackage();
-
-       foreach (@chans) {
-           next unless (&validChan($_));
-
-           &status("sending random Freshmeat to $_.");
-           &say($_, $line);
-       }
-    } );
-}
-
 sub logLoop {
     if (@_) {
-       &ScheduleThis(60, "logLoop");
-       return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"logLoop"}{RUNNING};
+       &ScheduleThis(60, 'logLoop');
+       return if ($_[0] eq '2');       # defer.
     }
 
     return unless (defined fileno LOG);
-    return unless (&IsParam("logfile"));
-    return unless (&IsParam("maxLogSize"));
+    return unless (&IsParam('logfile'));
+    return unless (&IsParam('maxLogSize'));
 
     ### check if current size is too large.
     if ( -s $file{log} > $param{'maxLogSize'}) {
-       my $date = sprintf("%04d%02d%02d", (localtime)[5,4,3]);
+       my $date = sprintf("%04d%02d%02d", (gmtime)[5,4,3]);
        $file{log} = $param{'logfile'} ."-". $date;
        &status("cycling log file.");
 
@@ -202,84 +214,77 @@ sub logLoop {
     }
 
     ### check if all the logs exceed size.
-    my $logdir = "$bot_base_dir/log/";
-    if (opendir(LOGS, $logdir)) {
-       my $tsize = 0;
-       my (%age, %size);
-
-       while (defined($_ = readdir LOGS)) {
-           my $logfile         = "$logdir/$_";
+    if (!opendir(LOGS, $bot_log_dir)) {
+       &WARN("logLoop: could not open dir '$bot_log_dir'");
+       return;
+    }
 
-           next unless ( -f $logfile);
-           my $size            = -s $logfile;
-           my $age             = (stat $logfile)[9];
+    my $tsize          = 0;
+    my (%age, %size);
+    while (defined($_ = readdir LOGS)) {
+       my $logfile     = "$bot_log_dir/$_";
 
-           $age{$age}          = $logfile;
-           $size{$logfile}     = $size;
+       next unless ( -f $logfile);
 
-           $tsize              += $size;
-       }
-       closedir LOGS;
-
-       my $delete      = 0;
-       while ($tsize > $param{'maxLogSize'}) {
-           &status("LOG: current size > max ($tsize > $param{'maxLogSize'})");
-           my $oldest  = (sort {$a <=> $b} keys %age)[0];
-           &status("LOG: unlinking $age{$oldest}.");
-           unlink $age{$oldest};
-           $tsize      -= $oldest;
-           $delete++;
-       }
+       my $size        = -s $logfile;
+       my $age         = (stat $logfile)[9];
+       $age{$age}      = $logfile;
+       $size{$logfile} = $size;
+       $tsize          += $size;
+    }
+    closedir LOGS;
 
-       ### TODO: add how many b,kb,mb removed?
-       &status("LOG: removed $delete logs.") if ($delete);
-    } else {
-       &WARN("could not open dir $logdir");
+    my $delete = 0;
+    while ($tsize > $param{'maxLogSize'}) {
+       &status("LOG: current size > max ($tsize > $param{'maxLogSize'})");
+       my $oldest      = (sort {$a <=> $b} keys %age)[0];
+       &status("LOG: unlinking $age{$oldest}.");
+       unlink $age{$oldest};
+       $tsize          -= $oldest;
+       $delete++;
     }
 
+    ### TODO: add how many b,kb,mb removed?
+    &status("LOG: removed $delete logs.") if ($delete);
 }
 
 sub seenFlushOld {
     if (@_) {
-       &ScheduleThis(1440, "seenFlushOld");
-       return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"seenFlushOld"}{RUNNING};
+       &ScheduleThis(1440, 'seenFlushOld');
+       return if ($_[0] eq '2');       # defer.
     }
 
     # is this global-only?
-    return unless (&IsChanConf("seen") > 0);
-    return unless (&IsChanConf("seenFlushInterval") > 0);
+    return unless (&IsChanConf('seen') > 0);
+    return unless (&IsChanConf('seenFlushInterval') > 0);
 
     # global setting. does not make sense for per-channel.
-    my $max_time = &getChanConfDefault("seenMaxDays", 30) *60*60*24;
+    my $max_time = &getChanConfDefault('seenMaxDays', 30, $chan) *60*60*24;
     my $delete   = 0;
 
-    if ($param{'DBType'} =~ /^pg|postgres|mysql/i) {
-       my $query = "SELECT nick,time FROM seen GROUP BY nick HAVING UNIX_TIMESTAMP() - time > $max_time";
-       my $sth = $dbh->prepare($query);
-       $sth->execute;
-
-       while (my @row = $sth->fetchrow_array) {
-           my ($nick,$time) = @row;
+    if ($param{'DBType'} =~ /^(pgsql|mysql|sqlite(2)?)$/i) {
+       my $query;
 
-           &dbDel("seen","nick",$nick);
-           $delete++;
+       if ($param{'DBType'} =~ /^(mysql|sqlite(2)?)$/i) {
+           $query = "SELECT nick,time FROM seen GROUP BY nick HAVING ".
+                       "UNIX_TIMESTAMP() - time > $max_time";
+       } else {        # pgsql.
+           $query = "SELECT nick,time FROM seen WHERE ".
+               "extract(epoch from timestamp 'now') - time > $max_time";
        }
-       $sth->finish;
-    } elsif ($param{'DBType'} =~ /^dbm/i) {
-       my $time = time();
 
-       foreach (keys %seen) {
-           my $delta_time = $time - &dbGet("seen", "NULL", $_, "time");
-           next unless ($delta_time > $max_time);
+       my $sth = $dbh->prepare($query);
+       if ($sth->execute) {
+           while (my @row = $sth->fetchrow_array) {
+               my ($nick,$time) = @row;
 
-           &DEBUG("seenFlushOld: ".&Time2String($delta_time) );
-           delete $seen{$_};
-           $delete++;
+               &sqlDelete('seen', { nick => $nick } );
+               $delete++;
+           }
+           $sth->finish;
        }
     } else {
-       &FIXME("seenFlushOld: for PG/NO-DB.");
+       &FIXME("seenFlushOld: for bad DBType:" . $param{'DBType'} . ".");
     }
     &VERB("SEEN deleted $delete seen entries.",2);
 
@@ -287,17 +292,27 @@ sub seenFlushOld {
 
 sub newsFlush {
     if (@_) {
-       &ScheduleThis(1440, "newsFlush");
-       return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"newsFlush"}{RUNNING};
+       &ScheduleThis(60, 'newsFlush');
+       return if ($_[0] eq '2');       # defer.
     }
 
-    return unless (&IsChanConf("news") > 0);
+    if (!&ChanConfList('News')) {
+       &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} });
+
+       if (!$total) {
+           delete $::news{$chan};
+           next;
+       }
+
        foreach $item (keys %{ $::news{$chan} }) {
            my $t = $::news{$chan}{$item}{Expire};
 
@@ -311,17 +326,26 @@ sub newsFlush {
                next;
            }
 
+           my $delta = $t - time();
+
            next unless (time() > $t);
-           # todo: show how old it was.
+
+           # TODO: show how old it was.
            delete $::news{$chan}{$item};
-           &VERB("NEWS: deleted '$item'", 2);
+           &status("NEWS: (newsflush) deleted '$item'");
            $delete++;
+           $i++;
        }
+
+       &status("NEWS (newsflush) {$chan}: deleted [$i/$total] news entries.") if ($i);
+       $none{$chan} = 1 if ($total == $i);
     }
 
-    # todo: flush users aswell.
+    # 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)) {
@@ -334,60 +358,82 @@ sub newsFlush {
            delete $::newsuser{$chan}{$_};
            $duser++;
        }
-    }
 
-#    &VERB("NEWS deleted $delete seen entries.",2);
-    &status("NEWS deleted: $delete news entries; $duser user cache.");
+       my $i = scalar(keys %{ $::newsuser{$chan} });
+       delete $::newsuser{$chan} unless ($i);
+    }
 
-    &News::writeNews();
+    if ($delete or $duser) {
+       &status("NewsFlush: deleted: $delete news entries; $duser user cache.");
+    }
 }
 
 sub chanlimitCheck {
-    my $interval = &getChanConfDefault("chanlimitcheckInterval", 10);
+    my $interval = &getChanConfDefault('chanlimitcheckInterval', 10, $chan);
+    my $mynick=$conn->nick();
 
     if (@_) {
-       &ScheduleThis($interval, "chanlimitCheck");
-       return if ($_[0] eq "2");
-    } else {
-       delete $sched{"chanlimitCheck"}{RUNNING};
+       &ScheduleThis($interval, 'chanlimitCheck');
+       return if ($_[0] eq '2');
     }
 
-    foreach $chan ( &ChanConfList("chanlimitcheck") ) {
+    my $str = join(' ', &ChanConfList('chanlimitcheck') );
+
+    foreach $chan ( &ChanConfList('chanlimitcheck') ) {
        next unless (&validChan($chan));
 
-       my $limitplus   = &getChanConfDefault("chanlimitcheckPlus", 5, $chan);
-       my $newlimit    = scalar(keys %{$channels{$chan}{''}}) + $limitplus;
+       if ($chan eq '_default') {
+           &WARN("chanlimit: we're doing $chan!! HELP ME!");
+           next;
+       }
+
+       my $limitplus   = &getChanConfDefault('chanlimitcheckPlus', 5, $chan);
+       my $newlimit    = scalar(keys %{ $channels{$chan}{''} }) + $limitplus;
        my $limit       = $channels{$chan}{'l'};
 
-       if (defined $limit and scalar keys %{$channels{$chan}{''}} > $limit) {
-           &FIXME("LIMIT: set too low!!! FIXME");
+       if (scalar keys %netsplitservers) {
+           if (defined $limit) {
+               &status("chanlimit: netsplit; removing it for $chan.");
+               $conn->mode($chan, "-l");
+               $cache{chanlimitChange}{$chan} = time();
+               &status("chanlimit: netsplit; removed.");
+           }
+
+           next;
+       }
+
+       if (defined $limit and scalar keys %{ $channels{$chan}{''} } > $limit) {
+           &FIXME("LIMIT: set too low!!!");
            ### run NAMES again and flush it.
        }
 
-       next unless (!defined $limit or $limit != $newlimit);
+       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});
+       if (!exists $channels{$chan}{'o'}{$mynick}) {
+           &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};
 
        if (!defined $limit) {
-           &status("ChanLimit: setting for first time or from netsplit, for $chan");
+           &status("chanlimit: $chan: setting for first time or from netsplit.");
        }
 
-       if (exists $cache{chanlimitChange_$chan}) {
-           if (time() - $cache{chanlimitChange_$chan} < 60) {
-               &DEBUG("not going to change chanlimit!");
+       if (exists $cache{chanlimitChange}{$chan}) {
+           my $delta = time() - $cache{chanlimitChange}{$chan};
+           if ($delta < $interval*60) {
+               &DEBUG("chanlimit: not going to change chanlimit! ($delta<$interval*60)");
                return;
            }
-           delete $cache{chanlimitChange_$chan};
        }
 
-       &rawout("MODE $chan +l $newlimit");
-       $cache{chanlimitChange_$chan} = time();
+       $conn->mode($chan, "+l", $newlimit);
+       $cache{chanlimitChange}{$chan} = time();
     }
 }
 
@@ -395,37 +441,75 @@ sub netsplitCheck {
     my ($s1,$s2);
 
     if (@_) {
-       &ScheduleThis(30, "netsplitCheck");
-       return if ($_[0] eq "2");
-    } else {
-       delete $sched{"netsplitCheck"}{RUNNING};
+       &ScheduleThis(15, 'netsplitCheck');
+       return if ($_[0] eq '2');
+    }
+
+    $cache{'netsplitCache'}++;
+#    &DEBUG("running netsplitCheck... $cache{netsplitCache}");
+
+    if (!scalar %netsplit and scalar %netsplitservers) {
+       &DEBUG("nsC: !hash netsplit but hash netsplitservers <- removing!");
+       undef %netsplitservers;
+       return;
     }
 
+    # well... this shouldn't happen since %netsplit code does it anyway.
     foreach $s1 (keys %netsplitservers) {
-       foreach $s2 (keys %{$netsplitservers{$s1}}) {
-           if (time() - $netsplitservers{$s1}{$s2} > 3600) {
+
+       foreach $s2 (keys %{ $netsplitservers{$s1} }) {
+           my $delta = time() - $netsplitservers{$s1}{$s2};
+
+           if ($delta > 60*30) {
                &status("netsplit between $s1 and $s2 appears to be stale.");
                delete $netsplitservers{$s1}{$s2};
+               &chanlimitCheck();
            }
        }
+
+       my $i = scalar(keys %{ $netsplitservers{$s1} });
+       delete $netsplitservers{$s1} unless ($i);
     }
 
     # %netsplit hash checker.
     my $count  = scalar keys %netsplit;
+    my $delete = 0;
     foreach (keys %netsplit) {
-       if (&IsNickInAnyChan($_)) {
-           &DEBUG("netsplitC: $_ is in some chan; removing from netsplit list.");
+       if (&IsNickInAnyChan($_)) {     # why would this happen?
+#          &DEBUG("nsC: $_ is in some chan; removing from netsplit list.");
            delete $netsplit{$_};
+           $delete++;
            next;
        }
-       next unless (time() - $netsplit{$_} > 60*10);
 
-       &DEBUG("netsplitC: $_ didn't come back from netsplit; removing from netsplit list.");
+       next unless (time() - $netsplit{$_} > 60*15);
+
+       $delete++;
        delete $netsplit{$_};
     }
 
+    # yet another hack.
+    # FIXED: $ch should be used rather than $chan since it creates NULL channels in the hash
+    foreach my $ch (keys %channels) {
+       my $i = $cache{maxpeeps}{$ch} || 0;
+       my $j = scalar(keys %{ $channels{$ch} });
+       next unless ($i > 10 and 0.25*$i > $j);
+
+       &DEBUG("netsplit: 0.25*max($i) > current($j); possible netsplit?");
+    }
+
+    if ($delete) {
+       my $j = scalar(keys %netsplit);
+       &status("nsC: removed from netsplit list: (before: $count; after: $j)");
+    }
+
+    if (!scalar %netsplit and scalar %netsplitservers) {
+       &DEBUG("nsC: ok hash netsplit is NULL; purging hash netsplitservers");
+       undef %netsplitservers;
+    }
+
     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();
     }
 }
@@ -435,17 +519,15 @@ sub floodLoop {
     my $who;
 
     if (@_) {
-       &ScheduleThis(60, "floodLoop"); # minutes.
-       return if ($_[0] eq "2");
-    } else {
-       delete $sched{"floodLoop"}{RUNNING};
+       &ScheduleThis(60, 'floodLoop'); # minutes.
+       return if ($_[0] eq '2');
     }
 
     my $time           = time();
-    my $interval       = &getChanConfDefault("floodCycle",60);
+    my $interval       = &getChanConfDefault('floodCycle',60, $chan);
 
     foreach $who (keys %flood) {
-       foreach (keys %{$flood{$who}}) {
+       foreach (keys %{ $flood{$who} }) {
            if (!exists $flood{$who}{$_}) {
                &WARN("flood{$who}{$_} undefined?");
                next;
@@ -462,80 +544,26 @@ sub floodLoop {
 
 sub seenFlush {
     if (@_) {
-       my $interval = &getChanConfDefault("seenFlushInterval", 60);
-       &ScheduleThis($interval, "seenFlush");
-       return if ($_[0] eq "2");
-    } else {
-       delete $sched{"seenFlush"}{RUNNING};
+       my $interval = &getChanConfDefault('seenFlushInterval', 60, $chan);
+       &ScheduleThis($interval, 'seenFlush');
+       return if ($_[0] eq '2');
     }
 
     my %stats;
     my $nick;
-    my $flushed        = 0;
-    $stats{'count_old'} = &countKeys("seen") || 0;
+    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) {
-           #BROKEN#
-           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'},
-           ) );
-           &DEBUG("retval => $retval.");
-           delete $seencache{$nick};
-           $flushed++;
-
-           next;
-           }
-           ### OLD CODE...
-
-           my $exists = &dbGet("seen","nick", $nick, "nick");
-
-           if (defined $exists and $exists) {
-               &dbUpdate("seen", "nick", $nick, (
-                       "time" => $seencache{$nick}{'time'},
-                       "host" => $seencache{$nick}{'host'},
-                       "channel" => $seencache{$nick}{'chan'},
-                       "message" => $seencache{$nick}{'msg'},
-               ) );
-               $stats{'old'}++;
-           } else {
-               my $retval = &dbInsert("seen", $nick, (
-                       "nick" => $seencache{$nick}{'nick'},
-                       "time" => $seencache{$nick}{'time'},
-                       "host" => $seencache{$nick}{'host'},
-                       "channel" => $seencache{$nick}{'chan'},
-                       "message" => $seencache{$nick}{'msg'},
-               ) );
-               $stats{'new'}++;
-
-               ### TODO: put bad nick into a list and don't do it again!
-               &FIXME("Should never happen! (nick => $nick)") if !$retval;
-           }
-
-           delete $seencache{$nick};
-           $flushed++;
-       }
-
-    } elsif ($param{'DBType'} =~ /^dbm/i) {
-
+    if ($param{'DBType'} =~ /^(mysql|pgsql|sqlite(2)?)$/i) {
        foreach $nick (keys %seencache) {
-           my $retval = &dbInsert("seen", $nick, (
-               "nick" => $seencache{$nick}{'nick'},
-               "time" => $seencache{$nick}{'time'},
-               "host" => $seencache{$nick}{'host'},
-               "channel" => $seencache{$nick}{'chan'},
-               "message" => $seencache{$nick}{'msg'},
-           ) );
-
-           ### TODO: put bad nick into a list and don't do it again!
-           &FIXME("Should never happen! (nick => $nick)") if !$retval;
+           my $retval = &sqlSet('seen', {'nick' => lc $seencache{$nick}{'nick'}}, {
+                       time    => $seencache{$nick}{'time'},
+                       host    => $seencache{$nick}{'host'},
+                       channel => $seencache{$nick}{'chan'},
+                       message => $seencache{$nick}{'msg'},
+           } );
 
            delete $seencache{$nick};
            $flushed++;
@@ -544,13 +572,13 @@ sub seenFlush {
        &DEBUG("seenFlush: NO VALID FACTOID SUPPORT?");
     }
 
-    &status("Flushed $flushed seen entries.")          if ($flushed);
+    &status("Seen: Flushed $flushed entries.") if ($flushed);
     &VERB(sprintf("  new seen: %03.01f%% (%d/%d)",
        $stats{'new'}*100/($stats{'count_old'} || 1),
-       $stats{'new'}, $stats{'count_old'} ), 2)        if ($stats{'new'});
+       $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'}, &countKeys("seen") ), 2)         if ($stats{'old'});
+       $stats{'old'}*100 / ( &countKeys('seen') || 1),
+       $stats{'old'}, &countKeys('seen') ), 2)         if ($stats{'old'});
 
     &WARN("scalar keys seenflush != 0!")       if (scalar keys %seenflush);
 }
@@ -560,19 +588,17 @@ sub leakCheck {
     my $count = 0;
 
     if (@_) {
-       &ScheduleThis(240, "leakCheck");
-       return if ($_[0] eq "2");
-    } else {
-       delete $sched{"leakCheck"}{RUNNING};
+       &ScheduleThis(240, 'leakCheck');
+       return if ($_[0] eq '2');
     }
 
-    # flood.
+    # flood. this is dealt with in floodLoop()
     foreach $blah1 (keys %flood) {
        foreach $blah2 (keys %{ $flood{$blah1} }) {
            $count += scalar(keys %{ $flood{$blah1}{$blah2} });
        }
     }
-    &DEBUG("leak: hash flood has $count total keys.",2);
+    &VERB("leak: hash flood has $count total keys.",2);
 
     # floodjoin.
     $count = 0;
@@ -581,11 +607,11 @@ sub leakCheck {
            $count += scalar(keys %{ $floodjoin{$blah1}{$blah2} });
        }
     }
-    &DEBUG("leak: hash flood has $count total keys.",2);
+    &VERB("leak: hash floodjoin has $count total keys.",2);
 
     # floodwarn.
     $count = scalar(keys %floodwarn);
-    &DEBUG("leak: hash floodwarn has $count total keys.",2);
+    &VERB("leak: hash floodwarn has $count total keys.",2);
 
     my $chan;
     foreach $chan (grep /[A-Z]/, keys %channels) {
@@ -598,6 +624,11 @@ sub leakCheck {
        }
     }
 
+    # chanstats
+    $count = scalar(keys %chanstats);
+    &VERB("leak: hash chanstats has $count total keys.",2);
+
+    # nuh.
     my $delete = 0;
     foreach (keys %nuh) {
        next if (&IsNickInAnyChan($_));
@@ -613,10 +644,8 @@ sub leakCheck {
 
 sub ignoreCheck {
     if (@_) {
-       &ScheduleThis(60, "ignoreCheck");
-       return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"ignoreCheck"}{RUNNING};
+       &ScheduleThis(60, 'ignoreCheck');
+       return if ($_[0] eq '2');       # defer.
     }
 
     my $time   = time();
@@ -635,74 +664,67 @@ sub ignoreCheck {
            $count++;
        }
     }
+
+    $cache{ignoreCheckTime} = time();
+
     &VERB("ignore: $count items deleted.",2);
 }
 
 sub ircCheck {
-
     if (@_) {
-       &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);
-    if ($iconf > 2 and $inow * 2 <= $iconf) {
-       &FIXME("ircCheck: current channels * 2 <= config channels. FIXME.");
-    }
-
-    # chanserv ops.
-    foreach ( &ChanConfList("chanServ_ops") ) {
-       next if (exists $channels{$chan}{'o'}{$ident});
+       &ScheduleThis(15, 'ircCheck');
+       return if ($_[0] eq '2');       # defer.
+    }
+
+    $cache{statusSafe} = 1;
+    foreach (sort keys %conns) {
+       $conn=$conns{$_};
+       my $mynick=$conn->nick();
+       &DEBUG("ircCheck for $_");
+       my @join = &getJoinChans(1);
+       if (scalar @join) {
+           &FIXME('ircCheck: found channels to join! ' . join(',',@join));
+           &joinNextChan();
+       }
 
-       &status("ChanServ ==> Requesting ops for $chan.");
-       &rawout("PRIVMSG ChanServ :OP $chan $ident");
-    }
+       # TODO: fix on_disconnect()
 
-    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 (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('ircCheck: possible lost in space; checking.'.
+                   scalar(gmtime) );
+               &msg($mynick, 'TEST');
+               $cache{connect} = time();
+           }
        }
     }
 
-    if ($ident !~ /^\Q$param{ircNick}\E$/) {
-       # this does not work unfortunately.
-       &WARN("ircCheck: ident($ident) != param{ircNick}($param{IrcNick}).");
-       if (! &IsNickInAnyChan( $param{ircNick} ) ) {
-           &DEBUG("$param{ircNick} not in use... changing!");
-           &nick( $param{ircNick} );
-       } else {
-           &WARN("$param{ircNick} is still in use...");
+        if (grep /^\s*$/, keys %channels) {
+            &WARN('ircCheck: we have a NULL chan in hash channels? removing!');
+            if (!exists $channels{''}) {
+                &DEBUG('ircCheck: this should never happen!');
+            }
+       }
+     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} );
+       } else {
+           &WARN("$param{ircNick} is still in use...");
        }
     }
 
-    &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("  $_");
-       }
-
-       &DEBUG("channels END");
-    }
+    $cache{statusSafe} = 0;
 
     ### USER FILE.
     if ($utime_userfile > $wtime_userfile and time() - $wtime_userfile > 3600) {
@@ -718,10 +740,8 @@ sub ircCheck {
 
 sub miscCheck {
     if (@_) {
-       &ScheduleThis(240, "miscCheck");
-       return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"miscCheck"}{RUNNING};
+       &ScheduleThis(120, 'miscCheck');
+       return if ($_[0] eq '2');       # defer.
     }
 
     # SHM check.
@@ -733,6 +753,20 @@ sub miscCheck {
        return;
     }
 
+    # make backup of important files.
+    &mkBackup( $bot_state_dir."/infobot.chan", 60*60*24*3);
+    &mkBackup( $bot_state_dir."/infobot.users", 60*60*24*3);
+    &mkBackup( $bot_base_dir."/infobot-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();
+
     # shmid stale remove.
     foreach (@ipcs) {
        chop;
@@ -743,143 +777,87 @@ sub miscCheck {
        my ($shmid, $size) = ($2,$5);
        next unless ($shmid != $shm and $size == 2000);
        my $z   = &shmRead($shmid);
-       if ($z =~ /^(\d+): /) {
-           my $time    = $1;
+       if ($z =~ /^(\S+):(\d+):(\d+): /) {
+           my $n       = $1;
+           my $pid     = $2;
+           my $time    = $3;
            next if (time() - $time < 60*60);
-
+           # FIXME remove not-pid shm if parent process dead
+           next if ($pid == $bot_pid);
+           # don't touch other bots, if they're running.
+           next unless ($param{ircUser} =~ /^\Q$n\E$/);
        } else {
-#          &DEBUG("shm: $shmid is not ours or old blootbot => ($z)");
-#          next;
+           &DEBUG("shm: $shmid is not ours or old infobot => ($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*7);
-    &mkBackup( $bot_misc_dir."/blootbot.users", 60*60*24*7);
-    &mkBackup( $bot_base_dir."/blootbot-news.txt", 60*60*24*7);
-
-    # 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};
+       &ScheduleThis(240, 'miscCheck2');
+       return if ($_[0] eq '2');       # defer.
     }
 
-    &DEBUG("miscCheck2: Doing debian checking...");
-
     # debian check.
-    opendir(DEBIAN, "$bot_base_dir/debian");
+    opendir(DEBIAN, "$bot_state_dir/debian");
     foreach ( grep /gz$/, readdir(DEBIAN) ) {
-       my $exit = CORE::system("gzip -t $bot_base_dir/debian/$_");
+       my $exit = CORE::system("gzip -t $bot_state_dir/debian/$_");
        next unless ($exit);
 
        &status("debian: unlinking file => $_");
-       unlink "$bot_base_dir/debian/$_";
+       unlink "$bot_state_dir/debian/$_";
     }
     closedir DEBIAN;
 
     # compress logs that should have been compressed.
-    # todo: use strftime?
-    my ($day,$month,$year) = (localtime(time()))[3,4,5];
+    # TODO: use strftime?
+    my ($day,$month,$year) = (gmtime(time()))[3,4,5];
     my $date = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
 
-    opendir(DIR,"$bot_base_dir/log");
+    if (!opendir(DIR,"$bot_log_dir")) {
+       &ERROR("misccheck2: log dir $bot_log_dir does not exist.");
+       closedir DIR;
+       return -1;
+    }
+
     while (my $f = readdir(DIR)) {
-       next unless ( -f "$bot_base_dir/log/$f");
+       next unless ( -f "$bot_log_dir/$f");
        next if ($f =~ /gz|bz2/);
        next unless ($f =~ /(\d{8})/);
        next if ($date eq $1);
 
-       &compress("$bot_base_dir/log/$f");
+       &compress("$bot_log_dir/$f");
     }
     closedir DIR;
 }
 
-sub shmFlush {
-    return if ($$ != $::bot_pid); # fork protection.
-
-    if (@_) {
-       &ScheduleThis(5, "shmFlush");
-       return if ($_[0] eq "2");
-    } else {
-       delete $sched{"shmFlush"}{RUNNING};
-    }
-
-    my $time;
-    my $shmmsg = &shmRead($shm);
-    $shmmsg =~ s/\0//g;         # remove padded \0's.
-    if ($shmmsg =~ s/^(\d+): //) {
-       $time   = $1;
-    }
-
-    foreach (split '\|\|', $shmmsg) {
-       next if (/^$/);
-       &VERB("shm: Processing '$_'.",2);
-
-       if (/^DCC SEND (\S+) (\S+)$/) {
-           my ($nick,$file) = ($1,$2);
-           if (exists $dcc{'SEND'}{$who}) {
-               &msg($nick, "DCC already active.");
-           } else {
-               &DEBUG("shm: dcc sending $2 to $1.");
-               $conn->new_send($1,$2);
-               $dcc{'SEND'}{$who} = time();
-           }
-       } elsif (/^SET FORKPID (\S+) (\S+)/) {
-           $forked{$1}{PID} = $2;
-       } elsif (/^DELETE FORK (\S+)$/) {
-           delete $forked{$1};
-       } elsif (/^EVAL (.*)$/) {
-           &DEBUG("evaling '$1'.");
-           eval $1;
-       } else {
-           &DEBUG("shm: unknown msg. ($_)");
-       }
-    }
-
-    &shmWrite($shm,"") if ($shmmsg ne "");
-}
-
 ### this is semi-scheduled
 sub getNickInUse {
-    if ($ident eq $param{'ircNick'}) {
-       &status("okay, got my nick back.");
-       return;
-    }
-
-    if (@_) {
-       &ScheduleThis(30, "getNickInUse");
-       return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"getNickInUse"}{RUNNING};
-    }
-
-    &status("Trying to get my nick back.");
-    &nick( $param{'ircNick'} );
+# FIXME: broken for multiple connects
+#    if ($ident eq $param{'ircNick'}) {
+#      &status("okay, got my nick back.");
+#      return;
+#    }
+#
+#    if (@_) {
+#      &ScheduleThis(30, 'getNickInUse');
+#      return if ($_[0] eq '2');       # defer.
+#    }
+#
+#    &nick( $param{'ircNick'} );
 }
 
 sub uptimeLoop {
-    return unless &IsChanConf("uptime");
+    return if (!defined &uptimeWriteFile);
+#    return unless &IsParam('Uptime');
 
     if (@_) {
-       &ScheduleThis(60, "uptimeLoop");
-       return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"uptimeLoop"}{RUNNING};
+       &ScheduleThis(60, 'uptimeLoop');
+       return if ($_[0] eq '2');       # defer.
     }
 
     &uptimeWriteFile();
@@ -888,16 +866,14 @@ sub uptimeLoop {
 sub slashdotLoop {
 
     if (@_) {
-       &ScheduleThis(60, "slashdotLoop");
-       return if ($_[0] eq "2");
-    } else {
-       delete $sched{"slashdotLoop"}{RUNNING};
+       &ScheduleThis(60, 'slashdotLoop');
+       return if ($_[0] eq '2');
     }
 
-    my @chans = &ChanConfList("slashdotAnnounce");
+    my @chans = &ChanConfList('slashdotAnnounce');
     return unless (scalar @chans);
 
-    &Forker("slashdot", sub {
+    &Forker('slashdot', sub {
        my $line = &Slashdot::slashdotAnnounce();
        return unless (defined $line);
 
@@ -910,41 +886,39 @@ sub slashdotLoop {
     } );
 }
 
-sub freshmeatLoop {
+sub plugLoop {
+
     if (@_) {
-       &ScheduleThis(60, "freshmeatLoop");
-       return if ($_[0] eq "2");
-    } else {
-       delete $sched{"freshmeatLoop"}{RUNNING};
+       &ScheduleThis(60, 'plugLoop');
+       return if ($_[0] eq '2');
     }
 
-    my @chans = &ChanConfList("freshmeatAnnounce");
+    my @chans = &ChanConfList('plugAnnounce');
     return unless (scalar @chans);
 
-    &Forker("freshmeat", sub {
-       my $data = &Freshmeat::freshmeatAnnounce();
+    &Forker('Plug', sub {
+       my $line = &Plug::plugAnnounce();
+       return unless (defined $line);
 
        foreach (@chans) {
            next unless (&::validChan($_));
 
-           &::status("sending freshmeat update to $_.");
-           &msg($_, $data);
+           &::status("sending plug update to $_.");
+           &notice($_, "Plug: $line");
        }
     } );
 }
 
 sub kernelLoop {
     if (@_) {
-       &ScheduleThis(240, "kernelLoop");
-       return if ($_[0] eq "2");
-    } else {
-       delete $sched{"kernelLoop"}{RUNNING};
+       &ScheduleThis(240, 'kernelLoop');
+       return if ($_[0] eq '2');
     }
 
-    my @chans = &ChanConfList("kernelAnnounce");
+    my @chans = &ChanConfList('kernelAnnounce');
     return unless (scalar @chans);
 
-    &Forker("kernel", sub {
+    &Forker('Kernel', sub {
        my @data = &Kernel::kernelAnnounce();
 
        foreach (@chans) {
@@ -960,17 +934,17 @@ sub kernelLoop {
 }
 
 sub wingateCheck {
-    return unless &IsChanConf("wingate");
+    return unless &IsChanConf('Wingate') > 0;
 
     ### FILE CACHE OF OFFENDING WINGATES.
     foreach (grep /^$host$/, @wingateBad) {
        &status("Wingate: RUNNING ON $host BY $who");
-       &ban("*!*\@$host", "") if &IsChanConf("wingateBan");
+       &ban("*!*\@$host", '') if &IsChanConf('wingateBan') > 0;
 
-       my $reason      = &getChanConf("wingateKick");
+       my $reason      = &getChanConf('wingateKick');
 
        next unless ($reason);
-       &kick($who, "", $reason)
+       &kick($who, '', $reason)
     }
 
     ### RUN CACHE OF TRIED WINGATES.
@@ -981,31 +955,29 @@ sub wingateCheck {
        &DEBUG("Already scanned $host. good.");
     }
 
-    my $interval = &getChanConfDefault("wingateInterval", 60); # seconds.
-    return if (defined $forked{'wingate'});
+    my $interval = &getChanConfDefault('wingateInterval', 60, $chan); # seconds.
+    return if (defined $forked{'Wingate'});
     return if (time() - $wingaterun <= $interval);
     return unless (scalar(keys %wingateToDo));
 
     $wingaterun = time();
 
-    &Forker("wingate", sub { &Wingate::Wingates(keys %wingateToDo); } );
+    &Forker('Wingate', sub { &Wingate::Wingates(keys %wingateToDo); } );
     undef @wingateNow;
 }
 
-### TODO.
+### TODO: ??
 sub wingateWriteFile {
     if (@_) {
-       &ScheduleThis(60, "wingateWriteFile");
-       return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"wingateWriteFile"}{RUNNING};
+       &ScheduleThis(60, 'wingateWriteFile');
+       return if ($_[0] eq '2');       # defer.
     }
 
     return unless (scalar @wingateCache);
 
     my $file = "$bot_base_dir/$param{'ircUser'}.wingate";
     if ($bot_pid != $$) {
-       &DEBUG("wingateWriteFile: Reorganising!");
+       &DEBUG('wingateWriteFile: Reorganising!');
 
        open(IN, $file);
        while (<IN>) {
@@ -1019,31 +991,43 @@ sub wingateWriteFile {
        @wingateNow = sort keys %hash;
     }
 
-    &DEBUG("wingateWF: writing...");
+    &DEBUG('wingateWF: writing...');
     open(OUT, ">$file");
     foreach (@wingateNow) {
        print OUT "$_\n";
     }
     close OUT;
-
 }
 
 sub factoidCheck {
     if (@_) {
-       &ScheduleThis(1440, "factoidCheck");
-       return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"factoidCheck"}{RUNNING};
+       &ScheduleThis(720, 'factoidCheck');
+       return if ($_[0] eq '2');       # defer.
+    }
+
+    my @list   = &searchTable('factoids', 'factoid_key', 'factoid_key', " #DEL#");
+    my $stale  = &getChanConfDefault('factoidDeleteDelay', 14, $chan) *60*60*24;
+    if ($stale < 1) {
+       # disable it since it's 'illegal'.
+       return;
     }
 
-    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");    
+       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("del factoid: old cruft (no time): $_");
+           &delFactoid($_);
            next;
        }
 
@@ -1051,24 +1035,22 @@ sub factoidCheck {
 
        my $fix = $_;
        $fix =~ s/ #DEL#$//g;
-       &DEBUG("safedel: Removing $fix ($_) for good.");
+       my $agestr = &Time2String($time - $age);
+       &status("safedel: Removing '$_' for good. [$agestr old]");
 
        &delFactoid($_);
     }
-
 }
 
 sub dccStatus {
     return unless (scalar keys %{ $dcc{CHAT} });
 
     if (@_) {
-       &ScheduleThis(10, "dccStatus");
-       return if ($_[0] eq "2");       # defer.
-    } else {
-       delete $sched{"dccStatus"}{RUNNING};
+       &ScheduleThis(10, 'dccStatus');
+       return if ($_[0] eq '2');       # defer.
     }
 
-    my $time = strftime("%H:%M", localtime(time()) );
+    my $time = strftime("%H:%M", gmtime(time()) );
 
     my $c;
     foreach (keys %channels) {
@@ -1092,71 +1074,53 @@ sub scheduleList {
     #  b - weird time.
     ###
 
-    &DEBUG("sched:");
-    foreach (keys %{ $irc->{_queue} }) {
+    my $reply = "sched:";
+    foreach (keys %{ $irc->{_queue}}) {
        my $q = $_;
-
+       my $coderef = $irc->{_queue}->{$q}->[1];
        my $sched;
        foreach (keys %sched) {
-           next unless ($q eq $sched{$_});
-           $sched = $_;
+           my $schedname = $_;
+           next unless defined(\&$schedname);
+           next unless ($coderef eq \&$schedname);
+           $sched = $schedname;
            last;
        }
 
        my $time = $irc->{_queue}->{$q}->[0] - time();
 
        if (defined $sched) {
-           &DEBUG("   $sched($q): ".&Time2String($time) );
+           $reply = "$reply, $sched($q):" . &Time2String($time);
        } else {
-           &DEBUG("   NULL($q): ".&Time2String($time) );
+           $reply = "$reply, NULL($q):" . &Time2String($time);
        }
     }
 
-    &DEBUG("end of sList.");
-}
-
-sub getChanConfDefault {
-    my($what, $default, $chan) = @_;
-
-    if (exists $param{$what}) {
-       if (!exists $cache{config}{$what}) {
-           &status("conf: backward-compat: found param{$what} ($param{$what}) instead.");
-           $cache{config}{$what} = 1;
-       }
-
-       return $param{$what};
-    }
-
-    my $val = &getChanConf($what, $chan);
-    if (defined $val) {
-       return $val;
-    }
-
-    $param{$what}      = $default;
-    &status("conf: auto-setting param{$what} = $default");
-    $cache{config}{$what} = 1;
-
-    return $default;
+    &DEBUG("$reply");
 }
 
 sub mkBackup {
-    my($file, $time) = @_;
-    my $backup = 0;
+    my($file, $time)   = @_;
+    my $backup         = 0;
 
     if (! -f $file) {
-       &WARN("mkB: file $file don't exist.");
+       &VERB("mkB: file '$file' does not exist.",2);
        return;
     }
 
+    my $age    = 'New';
     if ( -e "$file~" ) {
-       $backup++ if ((stat $file)[9] - (stat "$file~")[9] > $time);
+       $backup++       if ((stat $file)[9] - (stat "$file~")[9] > $time);
+       my $delta       = time() - (stat "$file~")[9];
+       $age            = &Time2String($delta);
     } else {
        $backup++;
     }
+
     return unless ($backup);
 
     ### TODO: do internal copying.
-    &status("Backup: $file to $file~");
+    &status("Backup: $file ($age)");
     CORE::system("/bin/cp $file $file~");
 }