2 # ProcessExtra.pl: Extensions to Process.pl
4 # Version: v0.5 (20010124)
8 if (&IsParam("useStrict")) { use strict; }
10 use POSIX qw(strftime);
14 &VERB("Starting schedulers...",2);
19 # 1 for run straight away, 2 for on next-run.
26 &netsplitCheck(1); # mandatory
27 &floodLoop(1); # mandatory
29 &leakCheck(2); # mandatory
30 &ignoreCheck(1); # mandatory
32 &ircCheck(2); # mandatory
33 &miscCheck(1); # mandatory
34 &miscCheck2(2); # mandatory
35 &shmFlush(1); # mandatory
43 # my $count = map { exists $sched{$_}{TIME} } keys %sched;
45 foreach (keys %sched) {
46 # next unless (exists $sched{$_}{TIME});
47 my $time = $sched{$_}{TIME};
48 next unless (defined $time and $time > time());
53 &status("Schedulers: $count will be running.");
58 my ($interval, $codename, @args) = @_;
59 my $waittime = &getRandomInt($interval);
61 if (!defined $waittime) {
62 &WARN("interval == waittime == UNDEF for $codename.");
66 my $time = $sched{$codename}{TIME};
67 if (defined $time and $time > time()) {
68 &WARN("Sched for $codename already exists.");
72 # &VERB("Scheduling \&$codename() for ".&Time2String($waittime),3);
74 my $retval = $conn->schedule($waittime, \&$codename, @args);
75 $sched{$codename}{LABEL} = $retval;
76 $sched{$codename}{TIME} = time()+$waittime;
77 $sched{$codename}{LOOP} = 1;
81 #### LET THE FUN BEGIN.
85 my $interval = &getChanConfDefault("randomQuoteInterval", 60);
87 &ScheduleThis($interval, "randomQuote");
88 return if ($_[0] eq "2"); # defer.
91 my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.randtext");
93 &ERROR("random Quote: weird error?");
97 foreach ( &ChanConfList("randomQuote") ) {
98 next unless (&validChan($_));
100 &status("sending random Quote to $_.");
101 &action($_, "Ponders: ".$line);
103 ### TODO: if there were no channels, don't reschedule until channel
104 ### configuration is modified.
111 my $interval = &getChanConfDefault("randomFactoidInterval", 60);
113 &ScheduleThis($interval, "randomFactoid");
114 return if ($_[0] eq "2"); # defer.
118 ($key,$val) = &randKey("factoids","factoid_key,factoid_value");
119 ### $val =~ tr/^[A-Z]/[a-z]/; # blah is Good => blah is good.
120 last if (defined $val and $val !~ /^</);
124 &ERROR("rF: tried 5 times but failed.");
129 foreach ( &ChanConfList("randomFactoid") ) {
130 next unless (&validChan($_));
132 &status("sending random Factoid to $_.");
133 &action($_, "Thinks: \037$key\037 is $val");
134 ### FIXME: Use &getReply() on above to format factoid properly?
139 sub randomFreshmeat {
140 my $interval = &getChanConfDefault("randomFresheatInterval", 60);
143 &ScheduleThis($interval, "randomFreshmeat");
144 return if ($_[0] eq "2"); # defer.
147 my @chans = &ChanConfList("randomFreshmeat");
148 return unless (scalar @chans);
150 &Forker("freshmeat", sub {
151 my $retval = &Freshmeat::randPackage();
154 next unless (&validChan($_));
156 &status("sending random Freshmeat to $_.");
164 &ScheduleThis(60, "logLoop");
165 return if ($_[0] eq "2"); # defer.
168 return unless (defined fileno LOG);
169 return unless (&IsParam("logfile"));
170 return unless (&IsParam("maxLogSize"));
172 ### check if current size is too large.
173 if ( -s $file{log} > $param{'maxLogSize'}) {
174 my $date = sprintf("%04d%02d%02d", (localtime)[5,4,3]);
175 $file{log} = $param{'logfile'} ."-". $date;
176 &status("cycling log file.");
178 if ( -e $file{log}) {
182 $newlog = $file{log}."-".$i;
183 last if (! -e $newlog);
186 $file{log} = $newlog;
190 CORE::system("/bin/mv '$param{'logfile'}' '$file{log}'");
191 &compress($file{log});
193 &status("cycling log file.");
196 ### check if all the logs exceed size.
197 my $logdir = "$bot_base_dir/log/";
198 if (opendir(LOGS, $logdir)) {
202 while (defined($_ = readdir LOGS)) {
203 my $logfile = "$logdir/$_";
205 next unless ( -f $logfile);
206 my $size = -s $logfile;
207 my $age = (stat $logfile)[9];
209 $age{$age} = $logfile;
210 $size{$logfile} = $size;
217 while ($tsize > $param{'maxLogSize'}) {
218 &status("LOG: current size > max ($tsize > $param{'maxLogSize'})");
219 my $oldest = (sort {$a <=> $b} keys %age)[0];
220 &status("LOG: unlinking $age{$oldest}.");
221 unlink $age{$oldest};
226 ### TODO: add how many b,kb,mb removed?
227 &status("LOG: removed $delete logs.") if ($delete);
229 &WARN("could not open dir $logdir");
236 &ScheduleThis(1440, "seenFlushOld");
237 return if ($_[0] eq "2"); # defer.
240 # is this global-only?
241 return unless (&IsChanConf("seen") > 0);
242 return unless (&IsChanConf("seenFlushInterval") > 0);
244 # global setting. does not make sense for per-channel.
245 my $max_time = &getChanConfDefault("seenMaxDays", 30) *60*60*24;
248 if ($param{'DBType'} =~ /^pg|postgres|mysql/i) {
249 my $query = "SELECT nick,time FROM seen GROUP BY nick HAVING UNIX_TIMESTAMP() - time > $max_time";
250 my $sth = $dbh->prepare($query);
253 while (my @row = $sth->fetchrow_array) {
254 my ($nick,$time) = @row;
256 &dbDel("seen","nick",$nick);
260 } elsif ($param{'DBType'} =~ /^dbm/i) {
263 foreach (keys %seen) {
264 my $delta_time = $time - &dbGet("seen", "NULL", $_, "time");
265 next unless ($delta_time > $max_time);
267 &DEBUG("seenFlushOld: ".&Time2String($delta_time) );
272 &FIXME("seenFlushOld: for PG/NO-DB.");
274 &VERB("SEEN deleted $delete seen entries.",2);
280 &ScheduleThis(60, "newsFlush");
281 return if ($_[0] eq "2"); # defer.
284 if (&ChanConfList("news")) {
286 &DEBUG("newsFlush: news disabled? (chan => $chan)");
293 foreach $chan (keys %::news) {
295 my $total = scalar(keys %{ $::news{$chan} });
297 &DEBUG("newsFlush: chan => $chan");
299 foreach $item (keys %{ $::news{$chan} }) {
300 my $t = $::news{$chan}{$item}{Expire};
302 my $tadd = $::news{$chan}{$item}{Time};
303 $oldest = $tadd if ($oldest > $tadd);
305 next if ($t == 0 or $t == -1);
307 &status("newsFlush: Fixed Expire time for $chan/$item, should not happen anyway.");
308 $::news{$chan}{$item}{Expire} = time() + $t*60*60*24;
312 my $delta = $t - time();
314 next unless (time() > $t);
316 # todo: show how old it was.
317 delete $::news{$chan}{$item};
318 &status("NEWS: (newsflush) deleted '$item'");
323 &status("NEWS (newsflush) {$chan}: deleted [$i/$total] news entries.");
324 $none{$chan} = 1 if ($total == $i);
327 # todo: flush users aswell.
329 foreach $chan (keys %::newsuser) {
330 next if (exists $none{$chan});
332 foreach (keys %{ $::newsuser{$chan} }) {
333 my $t = $::newsuser{$chan}{$_};
334 if (!defined $t or ($t > 2 and $t < 1000)) {
335 &DEBUG("something wrong with newsuser{$chan}{$_} => $t");
339 next unless ($oldest > $t);
341 delete $::newsuser{$chan}{$_};
346 if ($delete or $duser) {
347 &DEBUG("newsF: Writing news.");
349 &status("NEWS (newsflush) deleted: $delete news entries; $duser user cache.");
354 my $interval = &getChanConfDefault("chanlimitcheckInterval", 10);
357 &ScheduleThis($interval, "chanlimitCheck");
358 return if ($_[0] eq "2");
361 foreach $chan ( &ChanConfList("chanlimitcheck") ) {
362 next unless (&validChan($chan));
364 my $limitplus = &getChanConfDefault("chanlimitcheckPlus", 5, $chan);
365 my $newlimit = scalar(keys %{ $channels{$chan}{''} }) + $limitplus;
366 my $limit = $channels{$chan}{'l'};
368 if (scalar keys %netsplitservers) {
369 if (defined $limit) {
370 &DEBUG("chanlimit: removing it for $chan.");
371 &rawout("MODE $chan -l");
372 $cache{chanlimitChange}{$chan} = time();
378 if (defined $limit and scalar keys %{ $channels{$chan}{''} } > $limit) {
379 &FIXME("LIMIT: set too low!!! FIXME");
380 ### run NAMES again and flush it.
383 next unless (!defined $limit);
384 if (defined $limit and $limit == $newlimit) {
385 $cache{chanlimitChange}{$chan} = time();
389 if (!exists $channels{$chan}{'o'}{$ident}) {
390 &status("ChanLimit: dont have ops on $chan.") unless (exists $cache{warn}{chanlimit}{$chan});
391 $cache{warn}{chanlimit}{$chan} = 1;
392 &chanServCheck($chan);
395 delete $cache{warn}{chanlimit}{$chan};
397 if (!defined $limit) {
398 &status("ChanLimit: setting for first time or from netsplit, for $chan");
401 if (exists $cache{chanlimitChange}{$chan}) {
402 my $delta = time() - $cache{chanlimitChange}{$chan};
403 if ($delta < $interval*60) {
404 &DEBUG("not going to change chanlimit! ($delta<$interval*60)");
409 &rawout("MODE $chan +l $newlimit");
410 $cache{chanlimitChange}{$chan} = time();
418 &ScheduleThis(30, "netsplitCheck");
419 return if ($_[0] eq "2");
422 &DEBUG("running netsplitCheck...");
424 foreach $s1 (keys %netsplitservers) {
425 &DEBUG("nsC: s1 => $s1");
427 foreach $s2 (keys %{ $netsplitservers{$s1} }) {
428 my $delta = time() - $netsplitservers{$s1}{$s2};
429 &DEBUG("nss{$s1}{$s2} = $delta");
431 if (time() - $netsplitservers{$s1}{$s2} > 3600) {
432 &status("netsplit between $s1 and $s2 appears to be stale.");
433 delete $netsplitservers{$s1}{$s2};
438 # %netsplit hash checker.
439 my $count = scalar keys %netsplit;
440 foreach (keys %netsplit) {
441 if (&IsNickInAnyChan($_)) {
442 &DEBUG("netsplitC: $_ is in some chan; removing from netsplit list.");
443 delete $netsplit{$_};
446 next unless (time() - $netsplit{$_} > 60*10);
448 &DEBUG("netsplitC: $_ didn't come back from netsplit; removing from netsplit list.");
449 delete $netsplit{$_};
452 &DEBUG("nsC: netsplitservers: ".scalar(keys %netsplitservers) );
453 &DEBUG("nsC: netsplit: ".scalar(keys %netsplit) );
455 if ($count and !scalar keys %netsplit) {
456 &DEBUG("nsC: netsplit is hopefully gone. reinstating chanlimit check.");
466 &ScheduleThis(60, "floodLoop"); # minutes.
467 return if ($_[0] eq "2");
471 my $interval = &getChanConfDefault("floodCycle",60);
473 foreach $who (keys %flood) {
474 foreach (keys %{ $flood{$who} }) {
475 if (!exists $flood{$who}{$_}) {
476 &WARN("flood{$who}{$_} undefined?");
480 if ($time - $flood{$who}{$_} > $interval) {
481 delete $flood{$who}{$_};
486 &VERB("floodLoop: deleted $delete items.",2);
491 my $interval = &getChanConfDefault("seenFlushInterval", 60);
492 &ScheduleThis($interval, "seenFlush");
493 return if ($_[0] eq "2");
499 $stats{'count_old'} = &countKeys("seen") || 0;
503 if ($param{'DBType'} =~ /^mysql|pg|postgres/i) {
504 foreach $nick (keys %seencache) {
505 my $retval = &dbReplace("seen", "nick", $nick, (
506 ### "nick" => $seencache{$nick}{'nick'},
507 "time" => $seencache{$nick}{'time'},
508 "host" => $seencache{$nick}{'host'},
509 "channel" => $seencache{$nick}{'chan'},
510 "message" => $seencache{$nick}{'msg'},
512 delete $seencache{$nick};
521 my $exists = &dbGet("seen","nick", $nick, "nick");
523 if (defined $exists and $exists) {
524 &dbUpdate("seen", "nick", $nick, (
525 "time" => $seencache{$nick}{'time'},
526 "host" => $seencache{$nick}{'host'},
527 "channel" => $seencache{$nick}{'chan'},
528 "message" => $seencache{$nick}{'msg'},
532 my $retval = &dbInsert("seen", $nick, (
533 "nick" => $seencache{$nick}{'nick'},
534 "time" => $seencache{$nick}{'time'},
535 "host" => $seencache{$nick}{'host'},
536 "channel" => $seencache{$nick}{'chan'},
537 "message" => $seencache{$nick}{'msg'},
541 ### TODO: put bad nick into a list and don't do it again!
542 &FIXME("Should never happen! (nick => $nick)") if !$retval;
545 delete $seencache{$nick};
549 } elsif ($param{'DBType'} =~ /^dbm/i) {
551 foreach $nick (keys %seencache) {
552 my $retval = &dbInsert("seen", $nick, (
553 "nick" => $seencache{$nick}{'nick'},
554 "time" => $seencache{$nick}{'time'},
555 "host" => $seencache{$nick}{'host'},
556 "channel" => $seencache{$nick}{'chan'},
557 "message" => $seencache{$nick}{'msg'},
560 ### TODO: put bad nick into a list and don't do it again!
561 &FIXME("Should never happen! (nick => $nick)") if !$retval;
563 delete $seencache{$nick};
567 &DEBUG("seenFlush: NO VALID FACTOID SUPPORT?");
570 &status("Flushed $flushed seen entries.") if ($flushed);
571 &VERB(sprintf(" new seen: %03.01f%% (%d/%d)",
572 $stats{'new'}*100/($stats{'count_old'} || 1),
573 $stats{'new'}, ( $stats{'count_old'} || 1) ), 2) if ($stats{'new'});
574 &VERB(sprintf(" now seen: %3.1f%% (%d/%d)",
575 $stats{'old'}*100 / ( &countKeys("seen") || 1),
576 $stats{'old'}, &countKeys("seen") ), 2) if ($stats{'old'});
578 &WARN("scalar keys seenflush != 0!") if (scalar keys %seenflush);
586 &ScheduleThis(240, "leakCheck");
587 return if ($_[0] eq "2");
590 # flood. this is dealt with in floodLoop()
591 foreach $blah1 (keys %flood) {
592 foreach $blah2 (keys %{ $flood{$blah1} }) {
593 $count += scalar(keys %{ $flood{$blah1}{$blah2} });
596 &DEBUG("leak: hash flood has $count total keys.",2);
600 foreach $blah1 (keys %floodjoin) {
601 foreach $blah2 (keys %{ $floodjoin{$blah1} }) {
602 $count += scalar(keys %{ $floodjoin{$blah1}{$blah2} });
605 &DEBUG("leak: hash floodjoin has $count total keys.",2);
608 $count = scalar(keys %floodwarn);
609 &DEBUG("leak: hash floodwarn has $count total keys.",2);
612 foreach $chan (grep /[A-Z]/, keys %channels) {
613 &DEBUG("leak: chan => '$chan'.");
615 foreach $i (keys %{ $channels{$chan} }) {
616 foreach (keys %{ $channels{$chan}{$i} }) {
617 &DEBUG("leak: \$channels{$chan}{$i}{$_} ...");
623 $count = scalar(keys %chanstats);
624 &DEBUG("leak: hash chanstats has $count total keys.",2);
628 foreach (keys %nuh) {
629 next if (&IsNickInAnyChan($_));
630 next if (exists $dcc{CHAT}{$_});
636 &status("leak: $delete nuh{} items deleted; now have ".
637 scalar(keys %nuh) ) if ($delete);
642 &ScheduleThis(60, "ignoreCheck");
643 return if ($_[0] eq "2"); # defer.
649 foreach (keys %ignore) {
652 foreach (keys %{ $ignore{$chan} }) {
653 my @array = @{ $ignore{$chan}{$_} };
655 next unless ($array[0] and $time > $array[0]);
657 delete $ignore{$chan}{$_};
658 &status("ignore: $_/$chan has expired.");
662 &VERB("ignore: $count items deleted.",2);
667 &ScheduleThis(60, "ircCheck");
668 return if ($_[0] eq "2"); # defer.
671 my @x = &getJoinChans();
672 my $iconf = scalar( @x );
673 my $inow = scalar( keys %channels );
674 if ($iconf > 2 and $inow * 2 <= $iconf) {
675 &FIXME("ircCheck: current channels * 2 <= config channels. FIXME.");
680 if (!$conn->connected or time() - $msgtime > 3600) {
681 # todo: shouldn't we use cache{connect} somewhere?
682 if (exists $cache{connect}) {
683 &WARN("ircCheck: no msg for 3600 and disco'd! reconnecting!");
684 $msgtime = time(); # just in case.
686 delete $cache{connect};
688 &status("IRCTEST: possible lost in space; checking. ".
690 &msg($ident, "TEST");
691 $cache{connect} = time();
695 if ($ident !~ /^\Q$param{ircNick}\E$/) {
696 # this does not work unfortunately.
697 &WARN("ircCheck: ident($ident) != param{ircNick}($param{IrcNick}).");
699 # this check is misleading... perhaps we should do a notify.
700 if (! &IsNickInAnyChan( $param{ircNick} ) ) {
701 &DEBUG("$param{ircNick} not in use... changing!");
702 &nick( $param{ircNick} );
704 &WARN("$param{ircNick} is still in use...");
708 if (grep /^\s*$/, keys %channels) {
709 &WARN("ircCheck: we have a NULL chan in hash channels? removing!");
710 if (exists $channels{''}) {
711 &DEBUG("ircCheck: ok it existed!");
713 &DEBUG("ircCheck: this hsould never happen!");
716 delete $channels{''};
719 &DEBUG("ircstats...");
720 &DEBUG(" pubsleep: $pubsleep");
721 &DEBUG(" msgsleep: $msgsleep");
722 &DEBUG(" notsleep: $notsleep");
725 if ($utime_userfile > $wtime_userfile and time() - $wtime_userfile > 3600) {
727 $wtime_userfile = time();
730 if ($utime_chanfile > $wtime_chanfile and time() - $wtime_chanfile > 3600) {
732 $wtime_chanfile = time();
738 &ScheduleThis(240, "miscCheck");
739 return if ($_[0] eq "2"); # defer.
744 if ( -x "/usr/bin/ipcs") {
745 @ipcs = `/usr/bin/ipcs`;
747 &WARN("ircCheck: no 'ipcs' binary.");
751 # shmid stale remove.
755 # key, shmid, owner, perms, bytes, nattch
756 next unless (/^(0x\d+) (\d+)\s+(\S+)\s+(\d+)\s+(\d+)\s+/);
758 my ($shmid, $size) = ($2,$5);
759 next unless ($shmid != $shm and $size == 2000);
760 my $z = &shmRead($shmid);
761 if ($z =~ /^(\d+): /) {
763 next if (time() - $time < 60*60);
766 # &DEBUG("shm: $shmid is not ours or old blootbot => ($z)");
770 &status("SHM: nuking shmid $shmid");
771 CORE::system("/usr/bin/ipcrm shm $shmid >/dev/null");
774 # make backup of important files.
775 &mkBackup( $bot_misc_dir."/blootbot.chan", 60*60*24*3);
776 &mkBackup( $bot_misc_dir."/blootbot.users", 60*60*24*3);
777 &mkBackup( $bot_base_dir."/blootbot-news.txt", 60*60*24*1);
779 # flush cache{lobotomy}
780 foreach (keys %{ $cache{lobotomy} }) {
781 next unless (time() - $cache{lobotomy}{$_} > 60*60);
782 delete $cache{lobotomy}{$_};
785 ### check modules if they've been modified. might be evil.
791 &ScheduleThis(240, "miscCheck2");
792 return if ($_[0] eq "2"); # defer.
796 opendir(DEBIAN, "$bot_base_dir/debian");
797 foreach ( grep /gz$/, readdir(DEBIAN) ) {
798 my $exit = CORE::system("gzip -t $bot_base_dir/debian/$_");
801 &status("debian: unlinking file => $_");
802 unlink "$bot_base_dir/debian/$_";
806 # compress logs that should have been compressed.
807 # todo: use strftime?
808 my ($day,$month,$year) = (localtime(time()))[3,4,5];
809 my $date = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
811 opendir(DIR,"$bot_base_dir/log");
812 while (my $f = readdir(DIR)) {
813 next unless ( -f "$bot_base_dir/log/$f");
814 next if ($f =~ /gz|bz2/);
815 next unless ($f =~ /(\d{8})/);
816 next if ($date eq $1);
818 &compress("$bot_base_dir/log/$f");
824 return if ($$ != $::bot_pid); # fork protection.
827 &ScheduleThis(5, "shmFlush");
828 return if ($_[0] eq "2");
832 my $shmmsg = &shmRead($shm);
833 $shmmsg =~ s/\0//g; # remove padded \0's.
834 if ($shmmsg =~ s/^(\d+): //) {
838 foreach (split '\|\|', $shmmsg) {
840 &VERB("shm: Processing '$_'.",2);
842 if (/^DCC SEND (\S+) (\S+)$/) {
843 my ($nick,$file) = ($1,$2);
844 if (exists $dcc{'SEND'}{$who}) {
845 &msg($nick, "DCC already active.");
847 &DEBUG("shm: dcc sending $2 to $1.");
848 $conn->new_send($1,$2);
849 $dcc{'SEND'}{$who} = time();
851 } elsif (/^SET FORKPID (\S+) (\S+)/) {
852 $forked{$1}{PID} = $2;
853 } elsif (/^DELETE FORK (\S+)$/) {
855 } elsif (/^EVAL (.*)$/) {
856 &DEBUG("evaling '$1'.");
859 &DEBUG("shm: unknown msg. ($_)");
863 &shmWrite($shm,"") if ($shmmsg ne "");
866 ### this is semi-scheduled
868 if ($ident eq $param{'ircNick'}) {
869 &status("okay, got my nick back.");
874 &ScheduleThis(30, "getNickInUse");
875 return if ($_[0] eq "2"); # defer.
878 &nick( $param{'ircNick'} );
882 return unless &IsChanConf("uptime");
885 &ScheduleThis(60, "uptimeLoop");
886 return if ($_[0] eq "2"); # defer.
895 &ScheduleThis(60, "slashdotLoop");
896 return if ($_[0] eq "2");
899 my @chans = &ChanConfList("slashdotAnnounce");
900 return unless (scalar @chans);
902 &Forker("slashdot", sub {
903 my $line = &Slashdot::slashdotAnnounce();
904 return unless (defined $line);
907 next unless (&::validChan($_));
909 &::status("sending slashdot update to $_.");
910 ¬ice($_, "Slashdot: $line");
917 &ScheduleThis(60, "freshmeatLoop");
918 return if ($_[0] eq "2");
921 my @chans = &ChanConfList("freshmeatAnnounce");
922 return unless (scalar @chans);
924 &Forker("freshmeat", sub {
925 my $data = &Freshmeat::freshmeatAnnounce();
928 next unless (&::validChan($_));
930 &::status("sending freshmeat update to $_.");
938 &ScheduleThis(240, "kernelLoop");
939 return if ($_[0] eq "2");
942 my @chans = &ChanConfList("kernelAnnounce");
943 return unless (scalar @chans);
945 &Forker("kernel", sub {
946 my @data = &Kernel::kernelAnnounce();
949 next unless (&::validChan($_));
951 &::status("sending kernel update to $_.");
954 ¬ice($c, "Kernel: $_");
961 return unless &IsChanConf("wingate");
963 ### FILE CACHE OF OFFENDING WINGATES.
964 foreach (grep /^$host$/, @wingateBad) {
965 &status("Wingate: RUNNING ON $host BY $who");
966 &ban("*!*\@$host", "") if &IsChanConf("wingateBan");
968 my $reason = &getChanConf("wingateKick");
970 next unless ($reason);
971 &kick($who, "", $reason)
974 ### RUN CACHE OF TRIED WINGATES.
975 if (grep /^$host$/, @wingateCache) {
976 push(@wingateNow, $host); # per run.
977 push(@wingateCache, $host); # cache per run.
979 &DEBUG("Already scanned $host. good.");
982 my $interval = &getChanConfDefault("wingateInterval", 60); # seconds.
983 return if (defined $forked{'wingate'});
984 return if (time() - $wingaterun <= $interval);
985 return unless (scalar(keys %wingateToDo));
987 $wingaterun = time();
989 &Forker("wingate", sub { &Wingate::Wingates(keys %wingateToDo); } );
994 sub wingateWriteFile {
996 &ScheduleThis(60, "wingateWriteFile");
997 return if ($_[0] eq "2"); # defer.
1000 return unless (scalar @wingateCache);
1002 my $file = "$bot_base_dir/$param{'ircUser'}.wingate";
1003 if ($bot_pid != $$) {
1004 &DEBUG("wingateWriteFile: Reorganising!");
1009 push(@wingateNow, $_);
1014 my %hash = map { $_ => 1 } @wingateNow;
1015 @wingateNow = sort keys %hash;
1018 &DEBUG("wingateWF: writing...");
1019 open(OUT, ">$file");
1020 foreach (@wingateNow) {
1028 &ScheduleThis(1440, "factoidCheck");
1029 return if ($_[0] eq "2"); # defer.
1032 my @list = &searchTable("factoids", "factoid_key", "factoid_key", " #DEL#");
1033 my $stale = &getChanConfDefault("factoidDeleteDelay", 30) *60*60*24;
1037 my $age = &getFactInfo($_, "modified_time");
1039 if (!defined $age or $age !~ /^\d+$/) {
1040 if (scalar @list > 50) {
1041 if (!$cache{warnDel}) {
1042 &WARN("list is over 50 (".scalar(@list)."... giving it a miss.");
1043 $cache{warnDel} = 1;
1048 &WARN("old cruft (no time): $_");
1053 next unless ($time - $age > $stale);
1056 $fix =~ s/ #DEL#$//g;
1057 &DEBUG("safedel: Removing $fix ($_) for good.");
1065 return unless (scalar keys %{ $dcc{CHAT} });
1068 &ScheduleThis(10, "dccStatus");
1069 return if ($_[0] eq "2"); # defer.
1072 my $time = strftime("%H:%M", localtime(time()) );
1075 foreach (keys %channels) {
1077 my $users = keys %{ $channels{$c}{''} };
1078 my $chops = keys %{ $channels{$c}{o} };
1079 my $bans = keys %{ $channels{$c}{b} };
1081 my $txt = "[$time] $c: $users members ($chops chops), $bans bans";
1082 foreach (keys %{ $dcc{'CHAT'} }) {
1083 next unless (exists $channels{$c}{''}{lc $_});
1084 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
1097 foreach (keys %{ $irc->{_queue} }) {
1101 foreach (keys %sched) {
1102 next unless ($q eq $sched{$_});
1107 my $time = $irc->{_queue}->{$q}->[0] - time();
1109 if (defined $sched) {
1110 &DEBUG(" $sched($q): ".&Time2String($time) );
1112 &DEBUG(" NULL($q): ".&Time2String($time) );
1116 &DEBUG("end of sList.");
1119 sub getChanConfDefault {
1120 my($what, $default, $chan) = @_;
1122 if (exists $param{$what}) {
1123 if (!exists $cache{config}{$what}) {
1124 &status("conf: backward-compat: found param{$what} ($param{$what}) instead.");
1125 $cache{config}{$what} = 1;
1128 return $param{$what};
1131 my $val = &getChanConf($what, $chan);
1136 $param{$what} = $default;
1137 &status("conf: auto-setting param{$what} = $default");
1138 $cache{config}{$what} = 1;
1144 my($file, $time) = @_;
1148 &WARN("mkB: file '$file' does not exist.");
1152 if ( -e "$file~" ) {
1153 $backup++ if ((stat $file)[9] - (stat "$file~")[9] > $time);
1157 return unless ($backup);
1159 # should delta be time(file) - time(file~)?
1160 my $delta = time() - (stat "$file~")[9];
1161 my $age = &Time2String($delta);
1163 ### TODO: do internal copying.
1164 &status("Backup: $file ($age)");
1165 CORE::system("/bin/cp $file $file~");