]> git.donarmstrong.com Git - infobot.git/blobdiff - src/IRC/Schedulers.pl
wingate -> Wingate
[infobot.git] / src / IRC / Schedulers.pl
index e1646dae42d7381fb9958f69361ae9f293826154..9fbe0c8cc5c64c03780ff9bfc3171b9b2c5794e7 100644 (file)
@@ -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;
@@ -128,15 +131,15 @@ sub 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") ) {
        next unless (&validChan($_));
 
+       my $line = &getRandomLineFromFile($bot_data_dir. "/blootbot.randtext");
+       if (!defined $line) {
+           &ERROR("random Quote: weird error?");
+           return;
+       }
+
        &status("sending random Quote to $_.");
        &action($_, "Ponders: ".$line);
     }
@@ -154,22 +157,22 @@ sub 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;
-       }
-    }
-
     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++;
@@ -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});
@@ -367,6 +370,7 @@ sub newsFlush {
 
 sub chanlimitCheck {
     my $interval = &getChanConfDefault("chanlimitcheckInterval", 10);
+    my $mynick=$conn->nick();
 
     if (@_) {
        &ScheduleThis($interval, "chanlimitCheck");
@@ -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);
@@ -673,60 +677,41 @@ sub ircCheck {
     }
 
     $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();
-    }
-
-    # debug. needed for testing
-    open(OUT, ">>irccheck.log");
-    my $t = scalar(localtime);
-    printf OUT "%s: conn->connected = %s\n", $t, $conn->connected();
-    printf OUT "%s: time()-msgtime  = %d\n", $t, time() - $msgtime;
-    close OUT;
-
-    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!');
+           }
 
-       delete $channels{''};
+           delete $channels{''};
+       }
     }
 
     $cache{statusSafe} = 0;
@@ -758,6 +743,20 @@ sub miscCheck {
        return;
     }
 
+    # 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();
+
     # shmid stale remove.
     foreach (@ipcs) {
        chop;
@@ -768,32 +767,23 @@ 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 blootbot => ($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 {
@@ -814,7 +804,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);
 
@@ -837,26 +827,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 &IsChanConf('Uptime');
 
     if (@_) {
-       &ScheduleThis(60, "uptimeLoop");
-       return if ($_[0] eq "2");       # defer.
+       &ScheduleThis(60, 'uptimeLoop');
+       return if ($_[0] eq '2');       # defer.
     }
 
     &uptimeWriteFile();
@@ -865,14 +856,14 @@ sub uptimeLoop {
 sub slashdotLoop {
 
     if (@_) {
-       &ScheduleThis(60, "slashdotLoop");
+       &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);
 
@@ -888,14 +879,14 @@ sub slashdotLoop {
 sub plugLoop {
 
     if (@_) {
-       &ScheduleThis(60, "plugLoop");
+       &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);
 
@@ -933,14 +924,14 @@ sub kernelLoop {
 }
 
 sub wingateCheck {
-    return unless &IsChanConf("wingate");
+    return unless &IsChanConf('Wingate');
 
     ### 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');
 
-       my $reason      = &getChanConf("wingateKick");
+       my $reason      = &getChanConf('wingateKick');
 
        next unless ($reason);
        &kick($who, "", $reason)
@@ -954,21 +945,21 @@ sub wingateCheck {
        &DEBUG("Already scanned $host. good.");
     }
 
-    my $interval = &getChanConfDefault("wingateInterval", 60); # seconds.
-    return if (defined $forked{'wingate'});
+    my $interval = &getChanConfDefault('wingateInterval', 60); # 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");
+       &ScheduleThis(60, 'wingateWriteFile');
        return if ($_[0] eq "2");       # defer.
     }
 
@@ -976,7 +967,7 @@ sub wingateWriteFile {
 
     my $file = "$bot_base_dir/$param{'ircUser'}.wingate";
     if ($bot_pid != $$) {
-       &DEBUG("wingateWriteFile: Reorganising!");
+       &DEBUG('wingateWriteFile: Reorganising!');
 
        open(IN, $file);
        while (<IN>) {
@@ -990,7 +981,7 @@ sub wingateWriteFile {
        @wingateNow = sort keys %hash;
     }
 
-    &DEBUG("wingateWF: writing...");
+    &DEBUG('wingateWF: writing...');
     open(OUT, ">$file");
     foreach (@wingateNow) {
        print OUT "$_\n";
@@ -1014,7 +1005,7 @@ sub factoidCheck {
     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) {
@@ -1073,27 +1064,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 {