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 foreach $s1 (keys %netsplitservers) {
423 foreach $s2 (keys %{ $netsplitservers{$s1} }) {
424 if (time() - $netsplitservers{$s1}{$s2} > 3600) {
425 &status("netsplit between $s1 and $s2 appears to be stale.");
426 delete $netsplitservers{$s1}{$s2};
431 # %netsplit hash checker.
432 my $count = scalar keys %netsplit;
433 foreach (keys %netsplit) {
434 if (&IsNickInAnyChan($_)) {
435 &DEBUG("netsplitC: $_ is in some chan; removing from netsplit list.");
436 delete $netsplit{$_};
439 next unless (time() - $netsplit{$_} > 60*10);
441 &DEBUG("netsplitC: $_ didn't come back from netsplit; removing from netsplit list.");
442 delete $netsplit{$_};
445 &DEBUG("nsC: netsplitservers: ".scalar(keys %netsplitservers) );
446 &DEBUG("nsC: netsplit: ".scalar(keys %netsplit) );
448 if ($count and !scalar keys %netsplit) {
449 &DEBUG("nsC: netsplit is hopefully gone. reinstating chanlimit check.");
459 &ScheduleThis(60, "floodLoop"); # minutes.
460 return if ($_[0] eq "2");
464 my $interval = &getChanConfDefault("floodCycle",60);
466 foreach $who (keys %flood) {
467 foreach (keys %{ $flood{$who} }) {
468 if (!exists $flood{$who}{$_}) {
469 &WARN("flood{$who}{$_} undefined?");
473 if ($time - $flood{$who}{$_} > $interval) {
474 delete $flood{$who}{$_};
479 &VERB("floodLoop: deleted $delete items.",2);
484 my $interval = &getChanConfDefault("seenFlushInterval", 60);
485 &ScheduleThis($interval, "seenFlush");
486 return if ($_[0] eq "2");
492 $stats{'count_old'} = &countKeys("seen") || 0;
496 if ($param{'DBType'} =~ /^mysql|pg|postgres/i) {
497 foreach $nick (keys %seencache) {
498 my $retval = &dbReplace("seen", "nick", $nick, (
499 "nick" => $seencache{$nick}{'nick'},
500 "time" => $seencache{$nick}{'time'},
501 "host" => $seencache{$nick}{'host'},
502 "channel" => $seencache{$nick}{'chan'},
503 "message" => $seencache{$nick}{'msg'},
505 delete $seencache{$nick};
514 my $exists = &dbGet("seen","nick", $nick, "nick");
516 if (defined $exists and $exists) {
517 &dbUpdate("seen", "nick", $nick, (
518 "time" => $seencache{$nick}{'time'},
519 "host" => $seencache{$nick}{'host'},
520 "channel" => $seencache{$nick}{'chan'},
521 "message" => $seencache{$nick}{'msg'},
525 my $retval = &dbInsert("seen", $nick, (
526 "nick" => $seencache{$nick}{'nick'},
527 "time" => $seencache{$nick}{'time'},
528 "host" => $seencache{$nick}{'host'},
529 "channel" => $seencache{$nick}{'chan'},
530 "message" => $seencache{$nick}{'msg'},
534 ### TODO: put bad nick into a list and don't do it again!
535 &FIXME("Should never happen! (nick => $nick)") if !$retval;
538 delete $seencache{$nick};
542 } elsif ($param{'DBType'} =~ /^dbm/i) {
544 foreach $nick (keys %seencache) {
545 my $retval = &dbInsert("seen", $nick, (
546 "nick" => $seencache{$nick}{'nick'},
547 "time" => $seencache{$nick}{'time'},
548 "host" => $seencache{$nick}{'host'},
549 "channel" => $seencache{$nick}{'chan'},
550 "message" => $seencache{$nick}{'msg'},
553 ### TODO: put bad nick into a list and don't do it again!
554 &FIXME("Should never happen! (nick => $nick)") if !$retval;
556 delete $seencache{$nick};
560 &DEBUG("seenFlush: NO VALID FACTOID SUPPORT?");
563 &status("Flushed $flushed seen entries.") if ($flushed);
564 &VERB(sprintf(" new seen: %03.01f%% (%d/%d)",
565 $stats{'new'}*100/($stats{'count_old'} || 1),
566 $stats{'new'}, ( $stats{'count_old'} || 1) ), 2) if ($stats{'new'});
567 &VERB(sprintf(" now seen: %3.1f%% (%d/%d)",
568 $stats{'old'}*100 / ( &countKeys("seen") || 1),
569 $stats{'old'}, &countKeys("seen") ), 2) if ($stats{'old'});
571 &WARN("scalar keys seenflush != 0!") if (scalar keys %seenflush);
579 &ScheduleThis(240, "leakCheck");
580 return if ($_[0] eq "2");
583 # flood. this is dealt with in floodLoop()
584 foreach $blah1 (keys %flood) {
585 foreach $blah2 (keys %{ $flood{$blah1} }) {
586 $count += scalar(keys %{ $flood{$blah1}{$blah2} });
589 &DEBUG("leak: hash flood has $count total keys.",2);
593 foreach $blah1 (keys %floodjoin) {
594 foreach $blah2 (keys %{ $floodjoin{$blah1} }) {
595 $count += scalar(keys %{ $floodjoin{$blah1}{$blah2} });
598 &DEBUG("leak: hash floodjoin has $count total keys.",2);
601 $count = scalar(keys %floodwarn);
602 &DEBUG("leak: hash floodwarn has $count total keys.",2);
605 foreach $chan (grep /[A-Z]/, keys %channels) {
606 &DEBUG("leak: chan => '$chan'.");
608 foreach $i (keys %{ $channels{$chan} }) {
609 foreach (keys %{ $channels{$chan}{$i} }) {
610 &DEBUG("leak: \$channels{$chan}{$i}{$_} ...");
616 $count = scalar(keys %chanstats);
617 &DEBUG("leak: hash chanstats has $count total keys.",2);
621 foreach (keys %nuh) {
622 next if (&IsNickInAnyChan($_));
623 next if (exists $dcc{CHAT}{$_});
629 &status("leak: $delete nuh{} items deleted; now have ".
630 scalar(keys %nuh) ) if ($delete);
635 &ScheduleThis(60, "ignoreCheck");
636 return if ($_[0] eq "2"); # defer.
642 foreach (keys %ignore) {
645 foreach (keys %{ $ignore{$chan} }) {
646 my @array = @{ $ignore{$chan}{$_} };
648 next unless ($array[0] and $time > $array[0]);
650 delete $ignore{$chan}{$_};
651 &status("ignore: $_/$chan has expired.");
655 &VERB("ignore: $count items deleted.",2);
660 &ScheduleThis(60, "ircCheck");
661 return if ($_[0] eq "2"); # defer.
664 my @x = &getJoinChans();
665 my $iconf = scalar( @x );
666 my $inow = scalar( keys %channels );
667 if ($iconf > 2 and $inow * 2 <= $iconf) {
668 &FIXME("ircCheck: current channels * 2 <= config channels. FIXME.");
673 if (!$conn->connected or time() - $msgtime > 3600) {
674 # todo: shouldn't we use cache{connect} somewhere?
675 if (exists $cache{connect}) {
676 &WARN("ircCheck: no msg for 3600 and disco'd! reconnecting!");
677 $msgtime = time(); # just in case.
679 delete $cache{connect};
681 &status("IRCTEST: possible lost in space; checking. ".
683 &msg($ident, "TEST");
684 $cache{connect} = time();
688 if ($ident !~ /^\Q$param{ircNick}\E$/) {
689 # this does not work unfortunately.
690 &WARN("ircCheck: ident($ident) != param{ircNick}($param{IrcNick}).");
692 # this check is misleading... perhaps we should do a notify.
693 if (! &IsNickInAnyChan( $param{ircNick} ) ) {
694 &DEBUG("$param{ircNick} not in use... changing!");
695 &nick( $param{ircNick} );
697 &WARN("$param{ircNick} is still in use...");
701 if (grep /^\s*$/, keys %channels) {
702 &WARN("ircCheck: we have a NULL chan in hash channels? removing!");
703 if (exists $channels{''}) {
704 &DEBUG("ircCheck: ok it existed!");
706 &DEBUG("ircCheck: this hsould never happen!");
709 delete $channels{''};
712 &DEBUG("ircstats...");
713 &DEBUG(" pubsleep: $pubsleep");
714 &DEBUG(" msgsleep: $msgsleep");
715 &DEBUG(" notsleep: $notsleep");
718 if ($utime_userfile > $wtime_userfile and time() - $wtime_userfile > 3600) {
720 $wtime_userfile = time();
723 if ($utime_chanfile > $wtime_chanfile and time() - $wtime_chanfile > 3600) {
725 $wtime_chanfile = time();
731 &ScheduleThis(240, "miscCheck");
732 return if ($_[0] eq "2"); # defer.
737 if ( -x "/usr/bin/ipcs") {
738 @ipcs = `/usr/bin/ipcs`;
740 &WARN("ircCheck: no 'ipcs' binary.");
744 # shmid stale remove.
748 # key, shmid, owner, perms, bytes, nattch
749 next unless (/^(0x\d+) (\d+)\s+(\S+)\s+(\d+)\s+(\d+)\s+/);
751 my ($shmid, $size) = ($2,$5);
752 next unless ($shmid != $shm and $size == 2000);
753 my $z = &shmRead($shmid);
754 if ($z =~ /^(\d+): /) {
756 next if (time() - $time < 60*60);
759 # &DEBUG("shm: $shmid is not ours or old blootbot => ($z)");
763 &status("SHM: nuking shmid $shmid");
764 CORE::system("/usr/bin/ipcrm shm $shmid >/dev/null");
767 # make backup of important files.
768 &mkBackup( $bot_misc_dir."/blootbot.chan", 60*60*24*3);
769 &mkBackup( $bot_misc_dir."/blootbot.users", 60*60*24*3);
770 &mkBackup( $bot_base_dir."/blootbot-news.txt", 60*60*24*1);
772 # flush cache{lobotomy}
773 foreach (keys %{ $cache{lobotomy} }) {
774 next unless (time() - $cache{lobotomy}{$_} > 60*60);
775 delete $cache{lobotomy}{$_};
778 ### check modules if they've been modified. might be evil.
784 &ScheduleThis(240, "miscCheck2");
785 return if ($_[0] eq "2"); # defer.
788 &DEBUG("miscCheck2: Doing debian checking...");
791 opendir(DEBIAN, "$bot_base_dir/debian");
792 foreach ( grep /gz$/, readdir(DEBIAN) ) {
793 my $exit = CORE::system("gzip -t $bot_base_dir/debian/$_");
796 &status("debian: unlinking file => $_");
797 unlink "$bot_base_dir/debian/$_";
801 # compress logs that should have been compressed.
802 # todo: use strftime?
803 my ($day,$month,$year) = (localtime(time()))[3,4,5];
804 my $date = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
806 opendir(DIR,"$bot_base_dir/log");
807 while (my $f = readdir(DIR)) {
808 next unless ( -f "$bot_base_dir/log/$f");
809 next if ($f =~ /gz|bz2/);
810 next unless ($f =~ /(\d{8})/);
811 next if ($date eq $1);
813 &compress("$bot_base_dir/log/$f");
819 return if ($$ != $::bot_pid); # fork protection.
822 &ScheduleThis(5, "shmFlush");
823 return if ($_[0] eq "2");
827 my $shmmsg = &shmRead($shm);
828 $shmmsg =~ s/\0//g; # remove padded \0's.
829 if ($shmmsg =~ s/^(\d+): //) {
833 foreach (split '\|\|', $shmmsg) {
835 &VERB("shm: Processing '$_'.",2);
837 if (/^DCC SEND (\S+) (\S+)$/) {
838 my ($nick,$file) = ($1,$2);
839 if (exists $dcc{'SEND'}{$who}) {
840 &msg($nick, "DCC already active.");
842 &DEBUG("shm: dcc sending $2 to $1.");
843 $conn->new_send($1,$2);
844 $dcc{'SEND'}{$who} = time();
846 } elsif (/^SET FORKPID (\S+) (\S+)/) {
847 $forked{$1}{PID} = $2;
848 } elsif (/^DELETE FORK (\S+)$/) {
850 } elsif (/^EVAL (.*)$/) {
851 &DEBUG("evaling '$1'.");
854 &DEBUG("shm: unknown msg. ($_)");
858 &shmWrite($shm,"") if ($shmmsg ne "");
861 ### this is semi-scheduled
863 if ($ident eq $param{'ircNick'}) {
864 &status("okay, got my nick back.");
869 &ScheduleThis(30, "getNickInUse");
870 return if ($_[0] eq "2"); # defer.
873 &nick( $param{'ircNick'} );
877 return unless &IsChanConf("uptime");
880 &ScheduleThis(60, "uptimeLoop");
881 return if ($_[0] eq "2"); # defer.
890 &ScheduleThis(60, "slashdotLoop");
891 return if ($_[0] eq "2");
894 my @chans = &ChanConfList("slashdotAnnounce");
895 return unless (scalar @chans);
897 &Forker("slashdot", sub {
898 my $line = &Slashdot::slashdotAnnounce();
899 return unless (defined $line);
902 next unless (&::validChan($_));
904 &::status("sending slashdot update to $_.");
905 ¬ice($_, "Slashdot: $line");
912 &ScheduleThis(60, "freshmeatLoop");
913 return if ($_[0] eq "2");
916 my @chans = &ChanConfList("freshmeatAnnounce");
917 return unless (scalar @chans);
919 &Forker("freshmeat", sub {
920 my $data = &Freshmeat::freshmeatAnnounce();
923 next unless (&::validChan($_));
925 &::status("sending freshmeat update to $_.");
933 &ScheduleThis(240, "kernelLoop");
934 return if ($_[0] eq "2");
937 my @chans = &ChanConfList("kernelAnnounce");
938 return unless (scalar @chans);
940 &Forker("kernel", sub {
941 my @data = &Kernel::kernelAnnounce();
944 next unless (&::validChan($_));
946 &::status("sending kernel update to $_.");
949 ¬ice($c, "Kernel: $_");
956 return unless &IsChanConf("wingate");
958 ### FILE CACHE OF OFFENDING WINGATES.
959 foreach (grep /^$host$/, @wingateBad) {
960 &status("Wingate: RUNNING ON $host BY $who");
961 &ban("*!*\@$host", "") if &IsChanConf("wingateBan");
963 my $reason = &getChanConf("wingateKick");
965 next unless ($reason);
966 &kick($who, "", $reason)
969 ### RUN CACHE OF TRIED WINGATES.
970 if (grep /^$host$/, @wingateCache) {
971 push(@wingateNow, $host); # per run.
972 push(@wingateCache, $host); # cache per run.
974 &DEBUG("Already scanned $host. good.");
977 my $interval = &getChanConfDefault("wingateInterval", 60); # seconds.
978 return if (defined $forked{'wingate'});
979 return if (time() - $wingaterun <= $interval);
980 return unless (scalar(keys %wingateToDo));
982 $wingaterun = time();
984 &Forker("wingate", sub { &Wingate::Wingates(keys %wingateToDo); } );
989 sub wingateWriteFile {
991 &ScheduleThis(60, "wingateWriteFile");
992 return if ($_[0] eq "2"); # defer.
995 return unless (scalar @wingateCache);
997 my $file = "$bot_base_dir/$param{'ircUser'}.wingate";
998 if ($bot_pid != $$) {
999 &DEBUG("wingateWriteFile: Reorganising!");
1004 push(@wingateNow, $_);
1009 my %hash = map { $_ => 1 } @wingateNow;
1010 @wingateNow = sort keys %hash;
1013 &DEBUG("wingateWF: writing...");
1014 open(OUT, ">$file");
1015 foreach (@wingateNow) {
1023 &ScheduleThis(1440, "factoidCheck");
1024 return if ($_[0] eq "2"); # defer.
1027 my @list = &searchTable("factoids", "factoid_key", "factoid_key", " #DEL#");
1028 my $stale = &getChanConfDefault("factoidDeleteDelay", 30) *60*60*24;
1032 my $age = &getFactInfo($_, "modified_time");
1034 if (!defined $age or $age !~ /^\d+$/) {
1035 if (scalar @list > 50) {
1036 if (!$cache{warnDel}) {
1037 &WARN("list is over 50 (".scalar(@list)."... giving it a miss.");
1038 $cache{warnDel} = 1;
1043 &WARN("old cruft (no time): $_");
1048 next unless ($time - $age > $stale);
1051 $fix =~ s/ #DEL#$//g;
1052 &DEBUG("safedel: Removing $fix ($_) for good.");
1060 return unless (scalar keys %{ $dcc{CHAT} });
1063 &ScheduleThis(10, "dccStatus");
1064 return if ($_[0] eq "2"); # defer.
1067 my $time = strftime("%H:%M", localtime(time()) );
1070 foreach (keys %channels) {
1072 my $users = keys %{ $channels{$c}{''} };
1073 my $chops = keys %{ $channels{$c}{o} };
1074 my $bans = keys %{ $channels{$c}{b} };
1076 my $txt = "[$time] $c: $users members ($chops chops), $bans bans";
1077 foreach (keys %{ $dcc{'CHAT'} }) {
1078 next unless (exists $channels{$c}{''}{lc $_});
1079 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
1092 foreach (keys %{ $irc->{_queue} }) {
1096 foreach (keys %sched) {
1097 next unless ($q eq $sched{$_});
1102 my $time = $irc->{_queue}->{$q}->[0] - time();
1104 if (defined $sched) {
1105 &DEBUG(" $sched($q): ".&Time2String($time) );
1107 &DEBUG(" NULL($q): ".&Time2String($time) );
1111 &DEBUG("end of sList.");
1114 sub getChanConfDefault {
1115 my($what, $default, $chan) = @_;
1117 if (exists $param{$what}) {
1118 if (!exists $cache{config}{$what}) {
1119 &status("conf: backward-compat: found param{$what} ($param{$what}) instead.");
1120 $cache{config}{$what} = 1;
1123 return $param{$what};
1126 my $val = &getChanConf($what, $chan);
1131 $param{$what} = $default;
1132 &status("conf: auto-setting param{$what} = $default");
1133 $cache{config}{$what} = 1;
1139 my($file, $time) = @_;
1143 &WARN("mkB: file '$file' does not exist.");
1147 if ( -e "$file~" ) {
1148 $backup++ if ((stat $file)[9] - (stat "$file~")[9] > $time);
1152 return unless ($backup);
1154 # should delta be time(file) - time(file~)?
1155 my $delta = time() - (stat "$file~")[9];
1156 my $age = &Time2String($delta);
1158 ### TODO: do internal copying.
1159 &status("Backup: $file ($age)");
1160 CORE::system("/bin/cp $file $file~");