]> git.donarmstrong.com Git - infobot.git/blobdiff - src/IRC/Schedulers.pl
- endofnames: chanserv ops should work now.
[infobot.git] / src / IRC / Schedulers.pl
index 6a5a3486ece717a943e6954af3ad8e513a31b991..cbe2a0cb371b5be58571765c95230de21897ff57 100644 (file)
 #
 # ProcessExtra.pl: Extensions to Process.pl
 #          Author: dms
-#         Version: v0.3 (20000707)
+#         Version: v0.5 (20010124)
 #         Created: 20000117
 #
 
 if (&IsParam("useStrict")) { use strict; }
 
+use POSIX qw(strftime);
+use vars qw(%sched);
+
 sub setupSchedulers {
     &VERB("Starting schedulers...",2);
 
     # ONCE OFF.
 
     # REPETITIVE.
-    &uptimeCycle(1)    if (&IsParam("uptime"));
-    &randomQuote(1)    if (&IsParam("randomQuote"));
-    &randomFactoid(1)  if (&IsParam("randomFactoid"));
-    &logCycle(1)       if ($loggingstatus and &IsParam("logFile") and &IsParam("maxLogSize"));
-    &limitCheck(1)     if (&IsParam("limitcheck"));
+    &uptimeLoop(1);
+    &randomQuote(2);
+    &randomFactoid(2);
+    &randomFreshmeat(2);
+    &logLoop(1);
+    &chanlimitCheck(1);
     &netsplitCheck(1); # mandatory
-    &floodCycle(1);    # mandatory
-    &seenFlush(1)      if (&IsParam("seen") and &IsParam("seenFlushInterval"));
-    &leakCheck(1);     # mandatory
-    &ignoreListCheck(1);# mandatory
-    &seenFlushOld(1)   if (&IsParam("seen"));
+    &floodLoop(1);     # mandatory
+    &seenFlush(1);
+    &leakCheck(2);     # mandatory
+    &ignoreCheck(1);   # mandatory
+    &seenFlushOld(1);
     &ircCheck(1);      # mandatory
+    &miscCheck(2);     # mandatory
     &shmFlush(1);      # mandatory
-    &slashdotCycle(1)  if (&IsParam("slashdot") and &IsParam("slashdotAnnounce"));
-    &freshmeatCycle(1) if (&IsParam("freshmeat") and &IsParam("freshmeatAnnounce"));
-    &kernelCycle(1)    if (&IsParam("kernel") and &IsParam("kernelAnnounce"));
-    &wingateWriteFile(1) if (&IsParam("wingate"));
+    &slashdotLoop(2);
+    &freshmeatLoop(2);
+    &kernelLoop(2);
+    &wingateWriteFile(1);
+    &factoidCheck(1);
+
+#    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());
+
+       $count++;
+    }
+
+    &status("Schedulers: $count will be running.");
+###    &scheduleList();
 }
 
 sub ScheduleThis {
     my ($interval, $codename, @args) = @_;
     my $waittime = &getRandomInt($interval);
 
-    &VERB("Scheduling \&$codename() for ".&Time2String($waittime),3);
-    $conn->schedule($waittime, \&$codename, @args);
+    if (!defined $waittime) {
+       &WARN("interval == waittime == UNDEF for $codename.");
+       return;
+    }
+
+    my $time = $sched{$codename}{TIME};
+    if (defined $time and $time > time()) {
+       &WARN("Sched for $codename already exists.");
+       return;
+    }
+
+#    &VERB("Scheduling \&$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;
 }
 
+####
+#### LET THE FUN BEGIN.
+####
+
 sub randomQuote {
+    my $interval = &getChanConfDefault("randomQuoteInterval", 60);
+    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;
     }
 
-    my @channels = split(/[\s\t]+/, lc $param{'randomQuoteChannels'});
-    @channels    = keys(%channels) unless (scalar @channels);
-
-    my $good = 0;
-    foreach (@channels) {
-       next unless (&validChan($_));
+    foreach ( &ChanConfList("randomQuote") ) {
+       next unless (&validChan($_));   # ???
 
        &status("sending random Quote to $_.");
        &action($_, "Ponders: ".$line);
-       $good++;
     }
-
-    if (!$good) {
-       &WARN("randomQuote: no valid channels?");
-       return;
-    }
-
-    my $interval = $param{'randomQuoteInterval'} || 60;
-    &ScheduleThis($interval, "randomQuote") if (@_);
+    ### TODO: if there were no channels, don't reschedule until channel
+    ###                configuration is modified.
 }
 
 sub randomFactoid {
     my ($key,$val);
     my $error = 0;
+
+    my $interval = &getChanConfDefault("randomFactoidInterval", 60);
+    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.
@@ -82,31 +126,54 @@ sub randomFactoid {
        }
     }
 
-    my @channels = split(/[\s\t]+/, lc $param{'randomFactoidChannels'});
-    @channels    = keys(%channels) unless (scalar @channels);
-
-    my $good = 0;
-    foreach (@channels) {
-       next unless (&validChan($_));
+    foreach ( &ChanConfList("randomFactoid") ) {
+       next unless (&validChan($_));   # ???
 
        &status("sending random Factoid to $_.");
-###    &msg($_, "$key is $val");
        &action($_, "Thinks: \037$key\037 is $val");
        ### FIXME: Use &getReply() on above to format factoid properly?
        $good++;
     }
+}
 
-    if (!$good) {
-       &WARN("randomFactoid: no valid channels?");
-       return;
+sub randomFreshmeat {
+    my $interval = &getChanConfDefault("randomFresheatInterval", 60);
+
+    if (@_) {
+       &ScheduleThis($interval, "randomFreshmeat");
+       return if ($_[0] eq "2");       # defer.
+    } else {
+       delete $sched{"randomFreshmeat"}{RUNNING};
     }
 
-    my $interval = $param{'randomFactoidInterval'} || 60;
-    &ScheduleThis($interval, "randomFactoid") if (@_);
+    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 logCycle {
-    # check if current size is too large.
+sub logLoop {
+    if (@_) {
+       &ScheduleThis(60, "logLoop");
+       return if ($_[0] eq "2");       # defer.
+    } else {
+       delete $sched{"logLoop"}{RUNNING};
+    }
+
+    return unless (defined fileno LOG);
+    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]);
        $file{log} = $param{'logfile'} ."-". $date;
@@ -130,18 +197,18 @@ sub logCycle {
        &status("cycling log file.");
     }
 
-    # check if all the logs exceed size.
+    ### 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/$_";
+           my $logfile         = "$logdir/$_";
 
            next unless ( -f $logfile);
-           my $size = -s $logfile;
-           my $age = (stat $logfile)[9]; ### or 8 ?
+           my $size            = -s $logfile;
+           my $age             = (stat $logfile)[9];
 
            $age{$age}          = $logfile;
            $size{$logfile}     = $size;
@@ -150,30 +217,38 @@ sub logCycle {
        }
        closedir LOGS;
 
-       my $delete = 0;
+       my $delete      = 0;
        while ($tsize > $param{'maxLogSize'}) {
            &status("LOG: current size > max ($tsize > $param{'maxLogSize'})");
-           my $oldest = (sort {$a <=> $b} keys %age)[0];
+           my $oldest  = (sort {$a <=> $b} keys %age)[0];
            &status("LOG: unlinking $age{$oldest}.");
-           ### NOT YET.
-           # unlink $age{$oldest};
-           $tsize -= $oldest;
+           unlink $age{$oldest};
+           $tsize      -= $oldest;
            $delete++;
        }
 
        ### TODO: add how many b,kb,mb removed?
-       if ($delete) {
-           &status("LOG: removed $delete logs.");
-       }
+       &status("LOG: removed $delete logs.") if ($delete);
     } else {
        &WARN("could not open dir $logdir");
     }
 
-    &ScheduleThis(60, "logCycle") if (@_);
 }
 
 sub seenFlushOld {
-    my $max_time = $param{'seenMaxDays'}*60*60*24;
+    if (@_) {
+       &ScheduleThis(1440, "seenFlushOld");
+       return if ($_[0] eq "2");       # defer.
+    } else {
+       delete $sched{"seenFlushOld"}{RUNNING};
+    }
+
+    # is this global-only?
+    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 $delete   = 0;
 
     if ($param{'DBType'} =~ /^pg|postgres|mysql/i) {
@@ -204,42 +279,51 @@ sub seenFlushOld {
     }
     &VERB("SEEN deleted $delete seen entries.",2);
 
-    &ScheduleThis(1440, "seenFlushOld") if (@_);
 }
 
-sub limitCheck {
-    my $limitplus = $param{'limitcheckPlus'} || 5;
+sub chanlimitCheck {
+    my $interval = &getChanConfDefault("chanlimitcheckInterval", 10);
 
-    if (scalar keys %netsplit) {
-       &status("limitcheck: netsplit active.");
-       return;
+    if (@_) {
+       &ScheduleThis($interval, "chanlimitCheck");
+       return if ($_[0] eq "2");
+    } else {
+       delete $sched{"chanlimitCheck"}{RUNNING};
     }
 
-    my @channels = split(/[\s\t]+/, lc $param{'limitcheck'});
+    foreach ( &ChanConfList("chanlimitcheck") ) {
+       next unless (&validChan($_));   # ???
 
-    foreach (@channels) {
-       next unless (&validChan($_));
+       my $limitplus   = &getChanConfDefault("chanlimitcheckPlus", 5, $_);
+       my $newlimit    = scalar(keys %{$channels{$_}{''}}) + $limitplus;
+       my $limit       = $channels{$_}{'l'};
 
-       if (!exists $channels{$_}{'o'}{$ident}) {
-           &ERROR("limitcheck: dont have ops on $_.");
-           next;
+       if (scalar keys %{$channels{$_}{''}} > $limit) {
+           &status("LIMIT: set too low!!! FIXME");
+           ### run NAMES again and flush it.
        }
 
-       my $newlimit = scalar(keys %{$channels{$_}{''}}) + $limitplus;
-       my $limit = $channels{$_}{'l'};
-
        next unless (!defined $limit or $limit != $newlimit);
 
+       if (!exists $channels{$_}{'o'}{$ident}) {
+           &ERROR("chanlimitcheck: dont have ops on $_.");
+           next;
+       }
        &rawout("MODE $_ +l $newlimit");
     }
 
-    my $interval = $param{'limitcheckInterval'} || 10;
-    &ScheduleThis($interval, "limitCheck") if (@_);
 }
 
 sub netsplitCheck {
     my ($s1,$s2);
 
+    if (@_) {
+       &ScheduleThis(30, "netsplitCheck");
+       return if ($_[0] eq "2");
+    } else {
+       delete $sched{"netsplitCheck"}{RUNNING};
+    }
+
     foreach $s1 (keys %netsplitservers) {
        foreach $s2 (keys %{$netsplitservers{$s1}}) {
            if (time() - $netsplitservers{$s1}{$s2} > 3600) {
@@ -256,37 +340,58 @@ sub netsplitCheck {
            delete $netsplit{$_};
        }
        next unless (time() - $netsplit{$_} > 60*60*2); # 2 hours.
+       next if (&IsNickInAnyChan($_));
 
-       if (!&IsNickInAnyChan($_)) {
-           &DEBUG("netsplitC: $_ didn't come back from netsplit in 2 hours; removing from netsplit list.");
-           delete $netsplit{$_};
-       }
+       &DEBUG("netsplitC: $_ didn't come back from netsplit in 2 hours; removing from netsplit list.");
+       delete $netsplit{$_};
     }
-
-    &ScheduleThis(30, "netsplitCheck") if (@_);
 }
 
-sub floodCycle {
-    my $interval = $param{'floodInterval'} || 60;      # seconds.
-    my $delete = 0;
-
+sub floodLoop {
+    my $delete   = 0;
     my $who;
+
+    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}}) {
-           if (time() - $flood{$who}{$_} > $interval) {
+           if (!exists $flood{$who}{$_}) {
+               &WARN("flood{$who}{$_} undefined?");
+               next;
+           }
+
+           if ($time - $flood{$who}{$_} > $interval) {
                delete $flood{$who}{$_};
                $delete++;
            }
        }
     }
-    &VERB("floodCycle: deleted $delete items.",2);
-
-    &ScheduleThis($interval, "floodCycle") if (@_);    # minutes.
+    &VERB("floodLoop: deleted $delete items.",2);
 }
 
 sub seenFlush {
+    my %stats;
     my $nick;
-    my $flushed = 0;
+    my $flushed        = 0;
+    $stats{'count_old'} = &countKeys("seen") || 0;
+    $stats{'new'}      = 0;
+    $stats{'old'}      = 0;
+
+    if (@_) {
+       my $interval = &getChanConfDefault("seenFlushInterval", 60);
+       &ScheduleThis($interval, "seenFlush");
+       return if ($_[0] eq "2");
+    } else {
+       delete $sched{"seenFlush"}{RUNNING};
+    }
 
     if ($param{'DBType'} =~ /^mysql|pg|postgres/i) {
        foreach $nick (keys %seencache) {
@@ -299,6 +404,7 @@ sub seenFlush {
                        "channel" => $seencache{$nick}{'chan'},
                        "message" => $seencache{$nick}{'msg'},
                ) );
+               $stats{'old'}++;
            } else {
                my $retval = &dbInsert("seen", $nick, (
                        "nick" => $seencache{$nick}{'nick'},
@@ -307,11 +413,10 @@ sub seenFlush {
                        "channel" => $seencache{$nick}{'chan'},
                        "message" => $seencache{$nick}{'msg'},
                ) );
+               $stats{'new'}++;
 
                ### TODO: put bad nick into a list and don't do it again!
-               if ($retval == 0) {
-                   &ERROR("Should never happen! (nick => $nick) FIXME");
-               }
+               &FIXME("Should never happen! (nick => $nick)") if !$retval;
            }
 
            delete $seencache{$nick};
@@ -330,9 +435,7 @@ sub seenFlush {
            ) );
 
            ### TODO: put bad nick into a list and don't do it again!
-           if ($retval == 0) {
-               &ERROR("Should never happen! (nick => $nick) FIXME");
-           }
+           &FIXME("Should never happen! (nick => $nick)") if !$retval;
 
            delete $seencache{$nick};
            $flushed++;
@@ -341,17 +444,29 @@ sub seenFlush {
        &DEBUG("seenFlush: NO VALID FACTOID SUPPORT?");
     }
 
-    &VERB("Flushed $flushed seen entries.",2);
-    &DEBUG("seen: ".scalar(keys %seenflush)." remaining.");
+    &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'});
+    &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);
 
-    my $interval = $param{'seenFlushInterval'} || 60;
-    &ScheduleThis($interval, "seenFlush") if (@_);
 }
 
 sub leakCheck {
     my ($blah1,$blah2);
     my $count = 0;
 
+    if (@_) {
+       &ScheduleThis(240, "leakCheck");
+       return if ($_[0] eq "2");
+    } else {
+       delete $sched{"leakCheck"}{RUNNING};
+    }
+
     # flood.
     foreach $blah1 (keys %flood) {
        foreach $blah2 (keys %{$flood{$blah1}}) {
@@ -371,39 +486,122 @@ sub leakCheck {
        }
     }
 
-    &ScheduleThis(60, "leakCheck") if (@_);
+    my $delete = 0;
+    foreach (keys %nuh) {
+       next if (&IsNickInAnyChan($_));
+       next if (exists $dcc{CHAT}{$_});
+
+       delete $nuh{$_};
+       $delete++;
+    }
+
+    &DEBUG("$delete nuh{} items deleted; now have ".
+                               scalar(keys %nuh) ) if ($delete);
 }
 
-sub ignoreListCheck {
-    my $time = time();
-    my $count = 0;
+sub ignoreCheck {
+    if (@_) {
+       &ScheduleThis(60, "ignoreCheck");
+       return if ($_[0] eq "2");       # defer.
+    } else {
+       delete $sched{"ignoreCheck"}{RUNNING};
+    }
 
-    foreach (keys %ignoreList) {
-       next if ($ignoreList{$_} == 1);
-       next unless ($time > $ignoreList{$_});
+    my $time   = time();
+    my $count  = 0;
 
-       delete $ignoreList{$_};
-       &status("ignore: $_ has expired.");
-       $count++;
+    foreach (keys %ignore) {
+       my $chan = $_;
+
+       foreach (keys %{ $ignore{$chan} }) {
+           my @array = @{ $ignore{$chan}{$_} };
+
+           next unless ($array[0] and $time > $array[0]);
+
+           delete $ignore{$chan}{$_};
+           &status("ignore: $_/$chan has expired.");
+           $count++;
+       }
     }
     &VERB("ignore: $count items deleted.",2);
-
-    &ScheduleThis(30, "ignoreListCheck") if (@_);
 }
 
 sub ircCheck {
-    my @array = split /[\t\s]+/, $param{'join_channels'};
+
+    if (@_) {
+       &ScheduleThis(120, "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});
+
+       &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 ($ident !~ /^\Q$param{ircNick}\E$/) {
+       &WARN("ircCheck: ident($ident) != param{ircNick}($param{IrcNick}).");
+       ### TODO: schedule check for own nick if taken?
+    }
+
+    &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");
+    }
+
+    ### USER FILE.
+    if ($utime_userfile > $wtime_userfile and time() - $wtime_userfile > 3600) {
+       &writeUserFile();
+       $wtime_userfile = time();
+    }
+    ### CHAN FILE.
+    if ($utime_chanfile > $wtime_chanfile and time() - $wtime_chanfile > 3600) {
+       &writeChanFile();
+       $wtime_chanfile = time();
+    }
+}
+
+sub miscCheck {
+    if (@_) {
+       &ScheduleThis(240, "miscCheck");
+       return if ($_[0] eq "2");       # defer.
+    } else {
+       delete $sched{"miscCheck"}{RUNNING};
+    }
+
+    # SHM check.
     my @ipcs;
     if ( -x "/usr/bin/ipcs") {
        @ipcs = `/usr/bin/ipcs`;
     } else {
        &WARN("ircCheck: no 'ipcs' binary.");
+       return;
     }
 
     # shmid stale remove.
@@ -415,45 +613,67 @@ sub ircCheck {
 
        my ($shmid, $size) = ($2,$5);
        next unless ($shmid != $shm and $size == 2000);
+       my $z   = &shmRead($shmid);
+       if ($z =~ /^(\d+): /) {
+           my $time    = $1;
+           next if (time() - $time < 60*60);
+
+       } else {
+           &DEBUG("shm: $shmid is not ours or old blootbot => ($z)");
+           next;
+       }
 
        &status("SHM: nuking shmid $shmid");
        system("/usr/bin/ipcrm shm $shmid >/dev/null");
     }
 
-    if (!$conn->connected and time - $msgtime > 3600) {
-       &WARN("ircCheck: no msg for 3600 and disco'd! reconnecting!");
-       $msgtime = time();      # just in case.
-       &ircloop();
-    }
-
-    if ($ident !~ /^\Q$param{ircNick}\E$/) {
-       &WARN("ircCheck: ident($ident) != param{ircNick}($param{IrcNick}).");
-    }
+    # debian check.
+    opendir(DEBIAN, "$bot_base_dir/debian");
+    foreach ( grep /gz$/, readdir(DEBIAN) ) {
+       my $exit = system("gzip -t $bot_base_dir/debian/$_");
+       next unless ($exit);
 
-    if (scalar @joinchan) {
-       &WARN("We have some channels to join, doing so.");
-       &joinNextChan();
+       &status("debian: unlinking file => $_");
+       unlink "$bot_base_dir/debian/$_";
     }
+    closedir DEBIAN;
 
-    &ScheduleThis(240, "ircCheck") if (@_);
+    ### check modules if they've been modified. might be evil.
+    &reloadAllModules();
 }
 
 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) {
-       &status("shm: Processing '$_'.");
+       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.");
+               &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 (.*)$/) {
@@ -465,57 +685,123 @@ sub shmFlush {
     }
 
     &shmWrite($shm,"") if ($shmmsg ne "");
-
-    &ScheduleThis(5, "shmFlush") if (@_);
 }
 
+### 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.");
-    &nick($param{'ircNick'});
-
-    &ScheduleThis(5, "getNickInUse") if (@_);
+    &nick( $param{'ircNick'} );
 }
 
-sub uptimeCycle {
-    &uptimeWriteFile();
+sub uptimeLoop {
+    if (@_) {
+       &ScheduleThis(60, "uptimeLoop");
+       return if ($_[0] eq "2");       # defer.
+    } else {
+       delete $sched{"uptimeLoop"}{RUNNING};
+    }
 
-    &ScheduleThis(60, "uptimeCycle") if (@_);
+    &uptimeWriteFile();
 }
 
-sub slashdotCycle {
-    &Forker("slashdot", sub { &Slashdot::slashdotAnnounce(); } );
+sub slashdotLoop {
 
-    &ScheduleThis(60, "slashdotCycle") if (@_);
+    if (@_) {
+       &ScheduleThis(60, "slashdotLoop");
+       return if ($_[0] eq "2");
+    } else {
+       delete $sched{"slashdotLoop"}{RUNNING};
+    }
+
+    my @chans = &ChanConfList("slashdotAnnounce");
+    return unless (scalar @chans);
+
+    &Forker("slashdot", sub {
+       my $line = &Slashdot::slashdotAnnounce();
+       return unless (defined $line);
+
+       foreach (@chans) {
+           next unless (&::validChan($_));
+
+           &::status("sending slashdot update to $_.");
+           &notice($_, "Slashdot: $line");
+       }
+    } );
 }
 
-sub freshmeatCycle {
-    &Forker("freshmeat", sub { &Freshmeat::freshmeatAnnounce(); } );
+sub freshmeatLoop {
+    if (@_) {
+       &ScheduleThis(60, "freshmeatLoop");
+       return if ($_[0] eq "2");
+    } else {
+       delete $sched{"freshmeatLoop"}{RUNNING};
+    }
+
+    my @chans = &ChanConfList("freshmeatAnnounce");
+    return unless (scalar @chans);
 
-    &ScheduleThis(60, "freshmeatCycle") if (@_);
+    &Forker("freshmeat", sub {
+       my $data = &Freshmeat::freshmeatAnnounce();
+
+       foreach (@chans) {
+           next unless (&::validChan($_));
+
+           &::status("sending freshmeat update to $_.");
+           &msg($_, $data);
+       }
+    } );
 }
 
-sub kernelCycle {
-    &Forker("kernel", sub { &Kernel::kernelAnnounce(); } );
+sub kernelLoop {
+    if (@_) {
+       &ScheduleThis(240, "kernelLoop");
+       return if ($_[0] eq "2");
+    } else {
+       delete $sched{"kernelLoop"}{RUNNING};
+    }
+
+    my @chans = &ChanConfList("kernelAnnounce");
+    return unless (scalar @chans);
+
+    &Forker("kernel", sub {
+       my @data = &Kernel::kernelAnnounce();
 
-    &ScheduleThis(240, "kernelCycle") if (@_);
+       foreach (@chans) {
+           next unless (&::validChan($_));
+
+           &::status("sending kernel update to $_.");
+           my $c = $_;
+           foreach (@data) {
+               &notice($c, "Kernel: $_");
+           }
+       }
+    } );
 }
 
 sub wingateCheck {
-    return unless &IsParam("wingate");
-    return unless ($param{'wingate'} =~ /^(.*\s+)?$chan(\s+.*)?$/i);
+    return unless &IsChanConf("wingate");
 
     ### FILE CACHE OF OFFENDING WINGATES.
     foreach (grep /^$host$/, @wingateBad) {
        &status("Wingate: RUNNING ON $host BY $who");
-       &ban("*!*\@$host", "") if &IsParam("wingateBan");
+       &ban("*!*\@$host", "") if &IsChanConf("wingateBan");
 
-       next unless (&IsParam("wingateKick"));
-       &kick($who, "", $param{'wingateKick'})
+       my $reason      = &getChanConf("wingateKick");
+
+       next unless ($reason);
+       &kick($who, "", $reason)
     }
 
     ### RUN CACHE OF TRIED WINGATES.
@@ -526,7 +812,7 @@ sub wingateCheck {
        &DEBUG("Already scanned $host. good.");
     }
 
-    my $interval = $param{'wingateInterval'} || 60;    # seconds.
+    my $interval = &getChanConfDefault("wingateInterval", 60); # seconds.
     return if (defined $forked{'wingate'});
     return if (time() - $wingaterun <= $interval);
     return unless (scalar(keys %wingateToDo));
@@ -539,6 +825,13 @@ sub wingateCheck {
 
 ### TODO.
 sub wingateWriteFile {
+    if (@_) {
+       &ScheduleThis(60, "wingateWriteFile");
+       return if ($_[0] eq "2");       # defer.
+    } else {
+       delete $sched{"wingateWriteFile"}{RUNNING};
+    }
+
     return unless (scalar @wingateCache);
 
     my $file = "$bot_base_dir/$param{'ircUser'}.wingate";
@@ -564,7 +857,116 @@ sub wingateWriteFile {
     }
     close OUT;
 
-    &ScheduleThis(60, "wingateWriteFile") if (@_);
+}
+
+sub factoidCheck {
+    if (@_) {
+       &ScheduleThis(1440, "factoidCheck");
+       return if ($_[0] eq "2");       # defer.
+    } else {
+       delete $sched{"factoidCheck"}{RUNNING};
+    }
+
+    my @list = &searchTable("factoids", "factoid_key", "factoid_key", " #DEL#");
+    my $stale = &getChanConfDefault("factoidDeleteDelay", 7)*60*60*24;
+
+    my $time   = time();
+    foreach (@list) {
+       my $age = &getFactInfo($_, "modified_time");    
+       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);
+       &delFactoid($_);
+    }
+
+}
+
+sub dccStatus {
+    return unless (scalar keys %{ $dcc{CHAT} });
+
+    if (@_) {
+       &ScheduleThis(10, "dccStatus");
+       return if ($_[0] eq "2");       # defer.
+    } else {
+       delete $sched{"dccStatus"}{RUNNING};
+    }
+
+    my $time = strftime("%H:%M", localtime(time()) );
+
+    my $c;
+    foreach (keys %channels) {
+       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);
+       }
+    }
+}
+
+sub scheduleList {
+    ###
+    # custom:
+    #  a - time == now.
+    #  b - weird time.
+    ###
+
+    &DEBUG("sched:");
+    foreach (keys %{ $irc->{_queue} }) {
+       my $q = $_;
+
+       my $sched;
+       foreach (keys %sched) {
+           next unless ($q eq $sched{$_});
+           $sched = $_;
+           last;
+       }
+
+       my $time = $irc->{_queue}->{$q}->[0] - time();
+
+       if (defined $sched) {
+           &DEBUG("   $sched($q): ".&Time2String($time) );
+       } else {
+           &DEBUG("   NULL($q): ".&Time2String($time) );
+       }
+    }
+
+    &DEBUG("end of sList.");
+}
+
+sub getChanConfDefault {
+    my($what, $default, $chan) = @_;
+
+    if (exists $param{$what}) {
+       if (!exists $cache{config}{$what}) {
+           &status("gCCD: 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("gCCD: setting default for param{$what} = $default");
+    $cache{config}{$what} = 1;
+
+    return $default;
 }
 
 1;