]> 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 84b8da5038193c456a36a11c59ef8689f624c7bb..2e6bc8f877589260222b2b2595218b3e598c114e 100644 (file)
@@ -18,7 +18,7 @@ use vars qw(%sched %schedule);
 # )
 
 #%schedule = {
-#      uptimeLoop => ("", 60, 1),
+#      uptimeLoop => ('', 60, 1),
 #};
 
 sub setupSchedulersII {
@@ -29,7 +29,7 @@ sub setupSchedulersII {
 
 sub queueTask {
     my($codename, $chanconfdef, $intervaldef, $defer) = @_;
-    my $t = &getChanConfDefault($chanconfdef, $intervaldef);
+    my $t = &getChanConfDefault($chanconfdef, $intervaldef, $chan);
     my $waittime = &getRandomInt($t);
 
     if (!defined $waittime) {
@@ -39,11 +39,11 @@ sub queueTask {
 
     my $time = $schedule{$codename}[3];
     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);
+    #&VERB("Scheduling \&$codename() for " . &Time2String($waittime),3);
 
     my $retval = $conn->schedule($waittime, sub {
                \&$codename;
@@ -56,31 +56,34 @@ 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);
-    &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);
     &plugLoop(2);
     &kernelLoop(2);
     &wingateWriteFile(2);
     &factoidCheck(2);  # takes a couple of seconds on a 486. defer it
-# todo: convert to new format... or nuke altogether.
-    &newsFlush(1);
+# 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.
+    # TODO: squeeze this into a one-liner.
 #    my $count = map { exists $sched{$_}{TIME} } keys %sched;
     my $count  = 0;
     foreach (keys %sched) {
@@ -91,7 +94,7 @@ sub setupSchedulers {
     }
 
     &status("Schedulers: $count will be running.");
-###    &scheduleList();
+    &scheduleList();
 }
 
 sub ScheduleThis {
@@ -105,11 +108,11 @@ 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;
@@ -122,21 +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.
+       &ScheduleThis($interval, 'randomQuote');
+       return if ($_[0] eq '2');       # defer.
     }
 
-    my $line = &getRandomLineFromFile($bot_data_dir. "/blootbot.randtext");
-    if (!defined $line) {
-       &ERROR("random Quote: weird error?");
-       return;
-    }
-
-    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);
     }
@@ -148,28 +151,28 @@ 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.
-    }
-
-    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++;
@@ -178,13 +181,13 @@ sub randomFactoid {
 
 sub logLoop {
     if (@_) {
-       &ScheduleThis(60, "logLoop");
-       return if ($_[0] eq "2");       # defer.
+       &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'}) {
@@ -247,22 +250,22 @@ sub logLoop {
 
 sub seenFlushOld {
     if (@_) {
-       &ScheduleThis(1440, "seenFlushOld");
-       return if ($_[0] eq "2");       # defer.
+       &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'} =~ /^(pgsql|mysql|sqlite)/i) {
+    if ($param{'DBType'} =~ /^(pgsql|mysql|sqlite(2)?)$/i) {
        my $query;
 
-       if ($param{'DBType'} =~ /^mysql|sqlite$/i) {
+       if ($param{'DBType'} =~ /^(mysql|sqlite(2)?)$/i) {
            $query = "SELECT nick,time FROM seen GROUP BY nick HAVING ".
                        "UNIX_TIMESTAMP() - time > $max_time";
        } else {        # pgsql.
@@ -275,7 +278,7 @@ sub seenFlushOld {
            while (my @row = $sth->fetchrow_array) {
                my ($nick,$time) = @row;
 
-               &sqlDelete("seen", { nick => $nick } );
+               &sqlDelete('seen', { nick => $nick } );
                $delete++;
            }
            $sth->finish;
@@ -289,11 +292,11 @@ sub seenFlushOld {
 
 sub newsFlush {
     if (@_) {
-       &ScheduleThis(60, "newsFlush");
-       return if ($_[0] eq "2");       # defer.
+       &ScheduleThis(60, 'newsFlush');
+       return if ($_[0] eq '2');       # defer.
     }
 
-    if (!&ChanConfList("news")) {
+    if (!&ChanConfList('News')) {
        &DEBUG("newsFlush: news disabled? (chan => $chan)");
        return;
     }
@@ -327,7 +330,7 @@ sub newsFlush {
 
            next unless (time() > $t);
 
-           # todo: show how old it was.
+           # TODO: show how old it was.
            delete $::news{$chan}{$item};
            &status("NEWS: (newsflush) deleted '$item'");
            $delete++;
@@ -338,7 +341,7 @@ sub newsFlush {
        $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});
@@ -366,24 +369,25 @@ sub newsFlush {
 }
 
 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");
+       &ScheduleThis($interval, 'chanlimitCheck');
+       return if ($_[0] eq '2');
     }
 
-    my $str = join(' ', &ChanConfList("chanlimitcheck") );
+    my $str = join(' ', &ChanConfList('chanlimitcheck') );
 
-    foreach $chan ( &ChanConfList("chanlimitcheck") ) {
+    foreach $chan ( &ChanConfList('chanlimitcheck') ) {
        next unless (&validChan($chan));
 
-       if ($chan eq "_default") {
+       if ($chan eq '_default') {
            &WARN("chanlimit: we're doing $chan!! HELP ME!");
            next;
        }
 
-       my $limitplus   = &getChanConfDefault("chanlimitcheckPlus", 5, $chan);
+       my $limitplus   = &getChanConfDefault('chanlimitcheckPlus', 5, $chan);
        my $newlimit    = scalar(keys %{ $channels{$chan}{''} }) + $limitplus;
        my $limit       = $channels{$chan}{'l'};
 
@@ -399,7 +403,7 @@ sub chanlimitCheck {
        }
 
        if (defined $limit and scalar keys %{ $channels{$chan}{''} } > $limit) {
-           &FIXME("LIMIT: set too low!!! FIXME");
+           &FIXME("LIMIT: set too low!!!");
            ### run NAMES again and flush it.
        }
 
@@ -408,7 +412,7 @@ sub chanlimitCheck {
            next;
        }
 
-       if (!exists $channels{$chan}{'o'}{$ident}) {
+       if (!exists $channels{$chan}{'o'}{$mynick}) {
            &status("chanlimit: dont have ops on $chan.") unless (exists $cache{warn}{chanlimit}{$chan});
            $cache{warn}{chanlimit}{$chan} = 1;
            &chanServCheck($chan);
@@ -437,8 +441,8 @@ sub netsplitCheck {
     my ($s1,$s2);
 
     if (@_) {
-       &ScheduleThis(15, "netsplitCheck");
-       return if ($_[0] eq "2");
+       &ScheduleThis(15, 'netsplitCheck');
+       return if ($_[0] eq '2');
     }
 
     $cache{'netsplitCache'}++;
@@ -485,9 +489,10 @@ sub netsplitCheck {
     }
 
     # yet another hack.
-    foreach (keys %channels) {
-       my $i = $cache{maxpeeps}{$chan} || 0;
-       my $j = scalar(keys %{ $channels{$chan} });
+    # 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?");
@@ -514,12 +519,12 @@ sub floodLoop {
     my $who;
 
     if (@_) {
-       &ScheduleThis(60, "floodLoop"); # minutes.
-       return if ($_[0] eq "2");
+       &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} }) {
@@ -539,22 +544,21 @@ sub floodLoop {
 
 sub seenFlush {
     if (@_) {
-       my $interval = &getChanConfDefault("seenFlushInterval", 60);
-       &ScheduleThis($interval, "seenFlush");
-       return if ($_[0] eq "2");
+       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|pgsql|sqlite)$/i) {
+    if ($param{'DBType'} =~ /^(mysql|pgsql|sqlite(2)?)$/i) {
        foreach $nick (keys %seencache) {
-           my $retval = &sqlReplace("seen", {
-                       nick    => lc $seencache{$nick}{'nick'},
+           my $retval = &sqlSet('seen', {'nick' => lc $seencache{$nick}{'nick'}}, {
                        time    => $seencache{$nick}{'time'},
                        host    => $seencache{$nick}{'host'},
                        channel => $seencache{$nick}{'chan'},
@@ -573,8 +577,8 @@ sub seenFlush {
        $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") || 1),
-       $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);
 }
@@ -584,8 +588,8 @@ sub leakCheck {
     my $count = 0;
 
     if (@_) {
-       &ScheduleThis(240, "leakCheck");
-       return if ($_[0] eq "2");
+       &ScheduleThis(240, 'leakCheck');
+       return if ($_[0] eq '2');
     }
 
     # flood. this is dealt with in floodLoop()
@@ -640,8 +644,8 @@ sub leakCheck {
 
 sub ignoreCheck {
     if (@_) {
-       &ScheduleThis(60, "ignoreCheck");
-       return if ($_[0] eq "2");       # defer.
+       &ScheduleThis(60, 'ignoreCheck');
+       return if ($_[0] eq '2');       # defer.
     }
 
     my $time   = time();
@@ -668,58 +672,56 @@ sub ignoreCheck {
 
 sub ircCheck {
     if (@_) {
-       &ScheduleThis(15, "ircCheck");
-       return if ($_[0] eq "2");       # defer.
+       &ScheduleThis(15, 'ircCheck');
+       return if ($_[0] eq '2');       # defer.
     }
 
     $cache{statusSafe} = 1;
-
-    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.");
-       &FIXME("ircCheck: iconf = $iconf");
-       &FIXME("ircCheck: inow  = $inow");
-#      @joinchan       = @x;
-       &joinNextChan();
-    }
-
-    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(gmtime) );
-           &msg($ident, "TEST");
-           $cache{connect} = time();
+    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();
        }
-    }
 
-    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...");
+       # TODO: fix on_disconnect()
+
+       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 (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 (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...");
        }
-
-       delete $channels{''};
     }
 
     $cache{statusSafe} = 0;
@@ -738,8 +740,8 @@ sub ircCheck {
 
 sub miscCheck {
     if (@_) {
-       &ScheduleThis(120, "miscCheck");
-       return if ($_[0] eq "2");       # defer.
+       &ScheduleThis(120, 'miscCheck');
+       return if ($_[0] eq '2');       # defer.
     }
 
     # SHM check.
@@ -751,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;
@@ -761,38 +777,29 @@ 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_state_dir."/blootbot.chan", 60*60*24*3);
-    &mkBackup( $bot_state_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.
+       &ScheduleThis(240, 'miscCheck2');
+       return if ($_[0] eq '2');       # defer.
     }
 
     # debian check.
@@ -807,7 +814,7 @@ sub miscCheck2 {
     closedir DEBIAN;
 
     # compress logs that should have been compressed.
-    # todo: use strftime?
+    # TODO: use strftime?
     my ($day,$month,$year) = (gmtime(time()))[3,4,5];
     my $date = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
 
@@ -830,26 +837,27 @@ sub miscCheck2 {
 
 ### 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.
-    }
-
-    &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 if (!defined &uptimeWriteFile);
-#    return unless &IsChanConf("uptime");
+#    return unless &IsParam('Uptime');
 
     if (@_) {
-       &ScheduleThis(60, "uptimeLoop");
-       return if ($_[0] eq "2");       # defer.
+       &ScheduleThis(60, 'uptimeLoop');
+       return if ($_[0] eq '2');       # defer.
     }
 
     &uptimeWriteFile();
@@ -858,14 +866,14 @@ sub uptimeLoop {
 sub slashdotLoop {
 
     if (@_) {
-       &ScheduleThis(60, "slashdotLoop");
-       return if ($_[0] eq "2");
+       &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);
 
@@ -881,14 +889,14 @@ sub slashdotLoop {
 sub plugLoop {
 
     if (@_) {
-       &ScheduleThis(60, "plugLoop");
-       return if ($_[0] eq "2");
+       &ScheduleThis(60, 'plugLoop');
+       return if ($_[0] eq '2');
     }
 
-    my @chans = &ChanConfList("plugAnnounce");
+    my @chans = &ChanConfList('plugAnnounce');
     return unless (scalar @chans);
 
-    &Forker("plug", sub {
+    &Forker('Plug', sub {
        my $line = &Plug::plugAnnounce();
        return unless (defined $line);
 
@@ -903,14 +911,14 @@ sub plugLoop {
 
 sub kernelLoop {
     if (@_) {
-       &ScheduleThis(240, "kernelLoop");
-       return if ($_[0] eq "2");
+       &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) {
@@ -926,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.
@@ -947,29 +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.
+       &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>) {
@@ -983,7 +991,7 @@ sub wingateWriteFile {
        @wingateNow = sort keys %hash;
     }
 
-    &DEBUG("wingateWF: writing...");
+    &DEBUG('wingateWF: writing...');
     open(OUT, ">$file");
     foreach (@wingateNow) {
        print OUT "$_\n";
@@ -993,21 +1001,21 @@ sub wingateWriteFile {
 
 sub factoidCheck {
     if (@_) {
-       &ScheduleThis(720, "factoidCheck");
-       return if ($_[0] eq "2");       # defer.
+       &ScheduleThis(720, 'factoidCheck');
+       return if ($_[0] eq '2');       # defer.
     }
 
-    my @list   = &searchTable("factoids", "factoid_key", "factoid_key", " #DEL#");
-    my $stale  = &getChanConfDefault("factoidDeleteDelay", 14) *60*60*24;
+    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".
+       # disable it since it's 'illegal'.
        return;
     }
 
     my $time   = time();
 
     foreach (@list) {
-       my $age = &getFactInfo($_, "modified_time");    
+       my $age = &getFactInfo($_, 'modified_time');
 
        if (!defined $age or $age !~ /^\d+$/) {
            if (scalar @list > 50) {
@@ -1038,8 +1046,8 @@ sub dccStatus {
     return unless (scalar keys %{ $dcc{CHAT} });
 
     if (@_) {
-       &ScheduleThis(10, "dccStatus");
-       return if ($_[0] eq "2");       # defer.
+       &ScheduleThis(10, 'dccStatus');
+       return if ($_[0] eq '2');       # defer.
     }
 
     my $time = strftime("%H:%M", gmtime(time()) );
@@ -1066,27 +1074,29 @@ 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.");
+    &DEBUG("$reply");
 }
 
 sub mkBackup {
@@ -1098,9 +1108,9 @@ sub mkBackup {
        return;
     }
 
-    my $age    = "New";
+    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 {