2 # ProcessExtra.pl: Extensions to Process.pl
4 # Version: v0.5 (20010124)
10 use POSIX qw(strftime);
11 use vars qw(%sched %schedule);
13 # format: function name = (
14 # str chanconfdefault,
15 # int internaldefault,
17 # int next run, (optional)
21 # uptimeLoop => ("", 60, 1),
24 sub setupSchedulersII {
25 foreach (keys %schedule) {
26 &queueTask($_, @{ $schedule{$_} });
31 my($codename, $chanconfdef, $intervaldef, $defer) = @_;
32 my $t = &getChanConfDefault($chanconfdef, $intervaldef);
33 my $waittime = &getRandomInt($t);
35 if (!defined $waittime) {
36 &WARN("interval == waittime == UNDEF for $codename.");
40 my $time = $schedule{$codename}[3];
41 if (defined $time and $time > time()) {
42 &WARN("Sched for $codename already exists.");
46 # &VERB("Scheduling \&$codename() for ".&Time2String($waittime),3);
48 my $retval = $conn->schedule($waittime, sub {
54 &VERB("Starting schedulers...",2);
59 # 1 for run straight away, 2 for on next-run.
66 &netsplitCheck(1); # mandatory
67 &floodLoop(1); # mandatory
69 &leakCheck(2); # mandatory
70 &ignoreCheck(1); # mandatory
72 &ircCheck(1); # mandatory
73 &miscCheck(1); # mandatory
74 &miscCheck2(2); # mandatory
75 &shmFlush(1); # mandatory
81 &factoidCheck(2); # takes a couple of seconds on a 486. defer it
82 # todo: convert to new format... or nuke altogether.
85 # todo: squeeze this into a one-liner.
86 # my $count = map { exists $sched{$_}{TIME} } keys %sched;
88 foreach (keys %sched) {
89 my $time = $sched{$_}{TIME};
90 next unless (defined $time and $time > time());
95 &status("Schedulers: $count will be running.");
100 my ($interval, $codename, @args) = @_;
101 my $waittime = &getRandomInt($interval);
103 if (!defined $waittime) {
104 &WARN("interval == waittime == UNDEF for $codename.");
108 my $time = $sched{$codename}{TIME};
109 if (defined $time and $time > time()) {
110 &WARN("Sched for $codename already exists.");
114 # &VERB("Scheduling \&$codename() for ".&Time2String($waittime),3);
116 my $retval = $conn->schedule($waittime, \&$codename, @args);
117 $sched{$codename}{LABEL} = $retval;
118 $sched{$codename}{TIME} = time()+$waittime;
119 $sched{$codename}{LOOP} = 1;
123 #### LET THE FUN BEGIN.
127 my $interval = &getChanConfDefault("randomQuoteInterval", 60);
129 &ScheduleThis($interval, "randomQuote");
130 return if ($_[0] eq "2"); # defer.
133 my $line = &getRandomLineFromFile($bot_data_dir. "/blootbot.randtext");
134 if (!defined $line) {
135 &ERROR("random Quote: weird error?");
139 foreach ( &ChanConfList("randomQuote") ) {
140 next unless (&validChan($_));
142 &status("sending random Quote to $_.");
143 &action($_, "Ponders: ".$line);
145 ### TODO: if there were no channels, don't reschedule until channel
146 ### configuration is modified.
153 my $interval = &getChanConfDefault("randomFactoidInterval", 60);
155 &ScheduleThis($interval, "randomFactoid");
156 return if ($_[0] eq "2"); # defer.
160 ($key,$val) = &randKey("factoids","factoid_key,factoid_value");
161 ### $val =~ tr/^[A-Z]/[a-z]/; # blah is Good => blah is good.
162 last if (defined $val and $val !~ /^</);
166 &ERROR("rF: tried 5 times but failed.");
171 foreach ( &ChanConfList("randomFactoid") ) {
172 next unless (&validChan($_));
174 &status("sending random Factoid to $_.");
175 &action($_, "Thinks: \037$key\037 is $val");
176 ### FIXME: Use &getReply() on above to format factoid properly?
181 sub randomFreshmeat {
182 my $interval = &getChanConfDefault("randomFreshmeatInterval", 60);
185 &ScheduleThis($interval, "randomFreshmeat");
186 return if ($_[0] eq "2"); # defer.
189 my @chans = &ChanConfList("randomFreshmeat");
190 return unless (scalar @chans);
192 &Forker("freshmeat", sub {
193 my $retval = &Freshmeat::randPackage();
196 next unless (&validChan($_));
198 &status("sending random Freshmeat to $_.");
206 &ScheduleThis(60, "logLoop");
207 return if ($_[0] eq "2"); # defer.
210 return unless (defined fileno LOG);
211 return unless (&IsParam("logfile"));
212 return unless (&IsParam("maxLogSize"));
214 ### check if current size is too large.
215 if ( -s $file{log} > $param{'maxLogSize'}) {
216 my $date = sprintf("%04d%02d%02d", (gmtime)[5,4,3]);
217 $file{log} = $param{'logfile'} ."-". $date;
218 &status("cycling log file.");
220 if ( -e $file{log}) {
224 $newlog = $file{log}."-".$i;
225 last if (! -e $newlog);
228 $file{log} = $newlog;
232 CORE::system("/bin/mv '$param{'logfile'}' '$file{log}'");
233 &compress($file{log});
235 &status("cycling log file.");
238 ### check if all the logs exceed size.
239 if (!opendir(LOGS, $bot_log_dir)) {
240 &WARN("logLoop: could not open dir '$bot_log_dir'");
246 while (defined($_ = readdir LOGS)) {
247 my $logfile = "$bot_log_dir/$_";
249 next unless ( -f $logfile);
251 my $size = -s $logfile;
252 my $age = (stat $logfile)[9];
253 $age{$age} = $logfile;
254 $size{$logfile} = $size;
260 while ($tsize > $param{'maxLogSize'}) {
261 &status("LOG: current size > max ($tsize > $param{'maxLogSize'})");
262 my $oldest = (sort {$a <=> $b} keys %age)[0];
263 &status("LOG: unlinking $age{$oldest}.");
264 unlink $age{$oldest};
269 ### TODO: add how many b,kb,mb removed?
270 &status("LOG: removed $delete logs.") if ($delete);
275 &ScheduleThis(1440, "seenFlushOld");
276 return if ($_[0] eq "2"); # defer.
279 # is this global-only?
280 return unless (&IsChanConf("seen") > 0);
281 return unless (&IsChanConf("seenFlushInterval") > 0);
283 # global setting. does not make sense for per-channel.
284 my $max_time = &getChanConfDefault("seenMaxDays", 30) *60*60*24;
287 if ($param{'DBType'} =~ /^pgsql|mysql|sqlite/i) {
290 if ($param{'DBType'} =~ /^mysql|sqlite$/i) {
291 $query = "SELECT nick,time FROM seen GROUP BY nick HAVING ".
292 "UNIX_TIMESTAMP() - time > $max_time";
294 $query = "SELECT nick,time FROM seen WHERE ".
295 "extract(epoch from timestamp 'now') - time > $max_time";
298 my $sth = $dbh->prepare($query);
300 while (my @row = $sth->fetchrow_array) {
301 my ($nick,$time) = @row;
303 &dbDel("seen",{"nick"=>$nick});
308 } elsif ($param{'DBType'} =~ /^dbm/i) {
311 foreach (keys %seen) {
312 my $delta_time = $time - &dbGet("seen", "time", "nick", $_);
313 next unless ($delta_time > $max_time);
315 &DEBUG("seenFlushOld: ".&Time2String($delta_time) );
320 &FIXME("seenFlushOld: for PG/NO-DB.");
322 &VERB("SEEN deleted $delete seen entries.",2);
328 &ScheduleThis(60, "newsFlush");
329 return if ($_[0] eq "2"); # defer.
332 if (!&ChanConfList("news")) {
333 &DEBUG("newsFlush: news disabled? (chan => $chan)");
340 foreach $chan (keys %::news) {
342 my $total = scalar(keys %{ $::news{$chan} });
345 delete $::news{$chan};
349 foreach $item (keys %{ $::news{$chan} }) {
350 my $t = $::news{$chan}{$item}{Expire};
352 my $tadd = $::news{$chan}{$item}{Time};
353 $oldest = $tadd if ($oldest > $tadd);
355 next if ($t == 0 or $t == -1);
357 &status("newsFlush: Fixed Expire time for $chan/$item, should not happen anyway.");
358 $::news{$chan}{$item}{Expire} = time() + $t*60*60*24;
362 my $delta = $t - time();
364 next unless (time() > $t);
366 # todo: show how old it was.
367 delete $::news{$chan}{$item};
368 &status("NEWS: (newsflush) deleted '$item'");
373 &status("NEWS (newsflush) {$chan}: deleted [$i/$total] news entries.") if ($i);
374 $none{$chan} = 1 if ($total == $i);
377 # todo: flush users aswell.
379 foreach $chan (keys %::newsuser) {
380 next if (exists $none{$chan});
382 foreach (keys %{ $::newsuser{$chan} }) {
383 my $t = $::newsuser{$chan}{$_};
384 if (!defined $t or ($t > 2 and $t < 1000)) {
385 &DEBUG("something wrong with newsuser{$chan}{$_} => $t");
389 next unless ($oldest > $t);
391 delete $::newsuser{$chan}{$_};
395 my $i = scalar(keys %{ $::newsuser{$chan} });
396 delete $::newsuser{$chan} unless ($i);
399 if ($delete or $duser) {
400 &status("NewsFlush: deleted: $delete news entries; $duser user cache.");
405 my $interval = &getChanConfDefault("chanlimitcheckInterval", 10);
408 &ScheduleThis($interval, "chanlimitCheck");
409 return if ($_[0] eq "2");
412 my $str = join(' ', &ChanConfList("chanlimitcheck") );
414 foreach $chan ( &ChanConfList("chanlimitcheck") ) {
415 next unless (&validChan($chan));
417 if ($chan eq "_default") {
418 &WARN("chanlimit: we're doing $chan!! HELP ME!");
422 my $limitplus = &getChanConfDefault("chanlimitcheckPlus", 5, $chan);
423 my $newlimit = scalar(keys %{ $channels{$chan}{''} }) + $limitplus;
424 my $limit = $channels{$chan}{'l'};
426 if (scalar keys %netsplitservers) {
427 if (defined $limit) {
428 &status("chanlimit: netsplit; removing it for $chan.");
429 $conn->mode($chan, "-l");
430 $cache{chanlimitChange}{$chan} = time();
431 &status("chanlimit: netsplit; removed.");
437 if (defined $limit and scalar keys %{ $channels{$chan}{''} } > $limit) {
438 &FIXME("LIMIT: set too low!!! FIXME");
439 ### run NAMES again and flush it.
442 if (defined $limit and $limit == $newlimit) {
443 $cache{chanlimitChange}{$chan} = time();
447 if (!exists $channels{$chan}{'o'}{$ident}) {
448 &status("chanlimit: dont have ops on $chan.") unless (exists $cache{warn}{chanlimit}{$chan});
449 $cache{warn}{chanlimit}{$chan} = 1;
450 &chanServCheck($chan);
453 delete $cache{warn}{chanlimit}{$chan};
455 if (!defined $limit) {
456 &status("chanlimit: setting for first time or from netsplit, for $chan");
459 if (exists $cache{chanlimitChange}{$chan}) {
460 my $delta = time() - $cache{chanlimitChange}{$chan};
461 if ($delta < $interval*60) {
462 &DEBUG("chanlimit: not going to change chanlimit! ($delta<$interval*60)");
467 $conn->mode($chan, "+l", $newlimit);
468 $cache{chanlimitChange}{$chan} = time();
476 &ScheduleThis(15, "netsplitCheck");
477 return if ($_[0] eq "2");
480 $cache{'netsplitCache'}++;
481 # &DEBUG("running netsplitCheck... $cache{netsplitCache}");
483 if (!scalar %netsplit and scalar %netsplitservers) {
484 &DEBUG("nsC: !hash netsplit but hash netsplitservers <- removing!");
485 undef %netsplitservers;
489 # well... this shouldn't happen since %netsplit code does it anyway.
490 foreach $s1 (keys %netsplitservers) {
492 foreach $s2 (keys %{ $netsplitservers{$s1} }) {
493 my $delta = time() - $netsplitservers{$s1}{$s2};
495 if ($delta > 60*30) {
496 &status("netsplit between $s1 and $s2 appears to be stale.");
497 delete $netsplitservers{$s1}{$s2};
502 my $i = scalar(keys %{ $netsplitservers{$s1} });
503 delete $netsplitservers{$s1} unless ($i);
506 # %netsplit hash checker.
507 my $count = scalar keys %netsplit;
509 foreach (keys %netsplit) {
510 if (&IsNickInAnyChan($_)) { # why would this happen?
511 # &DEBUG("nsC: $_ is in some chan; removing from netsplit list.");
512 delete $netsplit{$_};
517 next unless (time() - $netsplit{$_} > 60*15);
520 delete $netsplit{$_};
524 foreach (keys %channels) {
525 my $i = $cache{maxpeeps}{$chan} || 0;
526 my $j = scalar(keys %{ $channels{$chan} });
527 next unless ($i > 10 and 0.25*$i > $j);
529 &DEBUG("netsplit: 0.25*max($i) > current($j); possible netsplit?");
533 my $j = scalar(keys %netsplit);
534 &status("nsC: removed from netsplit list: (before: $count; after: $j)");
537 if (!scalar %netsplit and scalar %netsplitservers) {
538 &DEBUG("nsC: ok hash netsplit is NULL; purging hash netsplitservers");
539 undef %netsplitservers;
542 if ($count and !scalar keys %netsplit) {
543 &DEBUG("nsC: netsplit is hopefully gone. reinstating chanlimit check.");
553 &ScheduleThis(60, "floodLoop"); # minutes.
554 return if ($_[0] eq "2");
558 my $interval = &getChanConfDefault("floodCycle",60);
560 foreach $who (keys %flood) {
561 foreach (keys %{ $flood{$who} }) {
562 if (!exists $flood{$who}{$_}) {
563 &WARN("flood{$who}{$_} undefined?");
567 if ($time - $flood{$who}{$_} > $interval) {
568 delete $flood{$who}{$_};
573 &VERB("floodLoop: deleted $delete items.",2);
578 my $interval = &getChanConfDefault("seenFlushInterval", 60);
579 &ScheduleThis($interval, "seenFlush");
580 return if ($_[0] eq "2");
586 $stats{'count_old'} = &countKeys("seen") || 0;
590 if ($param{'DBType'} =~ /^(mysql|pgsql|sqlite|dbm)$/i) {
591 foreach $nick (keys %seencache) {
592 my $retval = &sqlReplace("seen", {
593 nick => lc $seencache{$nick}{'nick'},
594 time => $seencache{$nick}{'time'},
595 host => $seencache{$nick}{'host'},
596 channel => $seencache{$nick}{'chan'},
597 message => $seencache{$nick}{'msg'},
600 delete $seencache{$nick};
604 &DEBUG("seenFlush: NO VALID FACTOID SUPPORT?");
607 &status("Seen: Flushed $flushed entries.") if ($flushed);
608 &VERB(sprintf(" new seen: %03.01f%% (%d/%d)",
609 $stats{'new'}*100/($stats{'count_old'} || 1),
610 $stats{'new'}, ( $stats{'count_old'} || 1) ), 2) if ($stats{'new'});
611 &VERB(sprintf(" now seen: %3.1f%% (%d/%d)",
612 $stats{'old'}*100 / ( &countKeys("seen") || 1),
613 $stats{'old'}, &countKeys("seen") ), 2) if ($stats{'old'});
615 &WARN("scalar keys seenflush != 0!") if (scalar keys %seenflush);
623 &ScheduleThis(240, "leakCheck");
624 return if ($_[0] eq "2");
627 # flood. this is dealt with in floodLoop()
628 foreach $blah1 (keys %flood) {
629 foreach $blah2 (keys %{ $flood{$blah1} }) {
630 $count += scalar(keys %{ $flood{$blah1}{$blah2} });
633 &VERB("leak: hash flood has $count total keys.",2);
637 foreach $blah1 (keys %floodjoin) {
638 foreach $blah2 (keys %{ $floodjoin{$blah1} }) {
639 $count += scalar(keys %{ $floodjoin{$blah1}{$blah2} });
642 &VERB("leak: hash floodjoin has $count total keys.",2);
645 $count = scalar(keys %floodwarn);
646 &VERB("leak: hash floodwarn has $count total keys.",2);
649 foreach $chan (grep /[A-Z]/, keys %channels) {
650 &DEBUG("leak: chan => '$chan'.");
652 foreach $i (keys %{ $channels{$chan} }) {
653 foreach (keys %{ $channels{$chan}{$i} }) {
654 &DEBUG("leak: \$channels{$chan}{$i}{$_} ...");
660 $count = scalar(keys %chanstats);
661 &VERB("leak: hash chanstats has $count total keys.",2);
665 foreach (keys %nuh) {
666 next if (&IsNickInAnyChan($_));
667 next if (exists $dcc{CHAT}{$_});
673 &status("leak: $delete nuh{} items deleted; now have ".
674 scalar(keys %nuh) ) if ($delete);
679 &ScheduleThis(60, "ignoreCheck");
680 return if ($_[0] eq "2"); # defer.
686 foreach (keys %ignore) {
689 foreach (keys %{ $ignore{$chan} }) {
690 my @array = @{ $ignore{$chan}{$_} };
692 next unless ($array[0] and $time > $array[0]);
694 delete $ignore{$chan}{$_};
695 &status("ignore: $_/$chan has expired.");
700 $cache{ignoreCheckTime} = time();
702 &VERB("ignore: $count items deleted.",2);
707 &ScheduleThis(15, "ircCheck");
708 return if ($_[0] eq "2"); # defer.
711 $cache{statusSafe} = 1;
713 my @x = &getJoinChans();
714 my $iconf = scalar( @x );
715 my $inow = scalar( keys %channels );
716 if ($iconf > 2 and $inow * 2 <= $iconf) {
717 &FIXME("ircCheck: current channels * 2 <= config channels. FIXME.");
718 &FIXME("ircCheck: iconf = $iconf");
719 &FIXME("ircCheck: inow = $inow");
724 if (!$conn->connected or time() - $msgtime > 3600) {
725 # todo: shouldn't we use cache{connect} somewhere?
726 if (exists $cache{connect}) {
727 &WARN("ircCheck: no msg for 3600 and disco'd! reconnecting!");
728 $msgtime = time(); # just in case.
730 delete $cache{connect};
732 &status("IRCTEST: possible lost in space; checking. ".
734 &msg($ident, "TEST");
735 $cache{connect} = time();
739 if ($ident !~ /^\Q$param{ircNick}\E$/) {
740 # this does not work unfortunately.
741 &WARN("ircCheck: ident($ident) != param{ircNick}($param{ircNick}).");
743 # this check is misleading... perhaps we should do a notify.
744 if (! &IsNickInAnyChan( $param{ircNick} ) ) {
745 &DEBUG("$param{ircNick} not in use... changing!");
746 &nick( $param{ircNick} );
748 &WARN("$param{ircNick} is still in use...");
752 if (grep /^\s*$/, keys %channels) {
753 &WARN("ircCheck: we have a NULL chan in hash channels? removing!");
754 if (!exists $channels{''}) {
755 &DEBUG("ircCheck: this should never happen!");
758 delete $channels{''};
761 $cache{statusSafe} = 0;
764 if ($utime_userfile > $wtime_userfile and time() - $wtime_userfile > 3600) {
766 $wtime_userfile = time();
769 if ($utime_chanfile > $wtime_chanfile and time() - $wtime_chanfile > 3600) {
771 $wtime_chanfile = time();
777 &ScheduleThis(120, "miscCheck");
778 return if ($_[0] eq "2"); # defer.
783 if ( -x "/usr/bin/ipcs") {
784 @ipcs = `/usr/bin/ipcs`;
786 &WARN("ircCheck: no 'ipcs' binary.");
790 # shmid stale remove.
794 # key, shmid, owner, perms, bytes, nattch
795 next unless (/^(0x\d+) (\d+)\s+(\S+)\s+(\d+)\s+(\d+)\s+/);
797 my ($shmid, $size) = ($2,$5);
798 next unless ($shmid != $shm and $size == 2000);
799 my $z = &shmRead($shmid);
800 if ($z =~ /^(\d+): /) {
802 next if (time() - $time < 60*60);
805 # &DEBUG("shm: $shmid is not ours or old blootbot => ($z)");
809 &status("SHM: nuking shmid $shmid");
810 CORE::system("/usr/bin/ipcrm shm $shmid >/dev/null");
813 # make backup of important files.
814 &mkBackup( $bot_state_dir."/blootbot.chan", 60*60*24*3);
815 &mkBackup( $bot_state_dir."/blootbot.users", 60*60*24*3);
816 &mkBackup( $bot_base_dir."/blootbot-news.txt", 60*60*24*1);
818 # flush cache{lobotomy}
819 foreach (keys %{ $cache{lobotomy} }) {
820 next unless (time() - $cache{lobotomy}{$_} > 60*60);
821 delete $cache{lobotomy}{$_};
824 ### check modules if they've been modified. might be evil.
830 &ScheduleThis(240, "miscCheck2");
831 return if ($_[0] eq "2"); # defer.
835 opendir(DEBIAN, "$bot_state_dir/debian");
836 foreach ( grep /gz$/, readdir(DEBIAN) ) {
837 my $exit = CORE::system("gzip -t $bot_state_dir/debian/$_");
840 &status("debian: unlinking file => $_");
841 unlink "$bot_state_dir/debian/$_";
845 # compress logs that should have been compressed.
846 # todo: use strftime?
847 my ($day,$month,$year) = (gmtime(time()))[3,4,5];
848 my $date = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
850 if (!opendir(DIR,"$bot_log_dir")) {
851 &ERROR("misccheck2: log dir $bot_log_dir does not exist.");
856 while (my $f = readdir(DIR)) {
857 next unless ( -f "$bot_log_dir/$f");
858 next if ($f =~ /gz|bz2/);
859 next unless ($f =~ /(\d{8})/);
860 next if ($date eq $1);
862 &compress("$bot_log_dir/$f");
867 ### this is semi-scheduled
869 if ($ident eq $param{'ircNick'}) {
870 &status("okay, got my nick back.");
875 &ScheduleThis(30, "getNickInUse");
876 return if ($_[0] eq "2"); # defer.
879 &nick( $param{'ircNick'} );
883 return if (!defined &uptimeWriteFile);
884 # return unless &IsChanConf("uptime");
887 &ScheduleThis(60, "uptimeLoop");
888 return if ($_[0] eq "2"); # defer.
897 &ScheduleThis(60, "slashdotLoop");
898 return if ($_[0] eq "2");
901 my @chans = &ChanConfList("slashdotAnnounce");
902 return unless (scalar @chans);
904 &Forker("slashdot", sub {
905 my $line = &Slashdot::slashdotAnnounce();
906 return unless (defined $line);
909 next unless (&::validChan($_));
911 &::status("sending slashdot update to $_.");
912 ¬ice($_, "Slashdot: $line");
920 &ScheduleThis(60, "plugLoop");
921 return if ($_[0] eq "2");
924 my @chans = &ChanConfList("plugAnnounce");
925 return unless (scalar @chans);
927 &Forker("plug", sub {
928 my $line = &Plug::plugAnnounce();
929 return unless (defined $line);
932 next unless (&::validChan($_));
934 &::status("sending plug update to $_.");
935 ¬ice($_, "Plug: $line");
942 &ScheduleThis(60, "freshmeatLoop");
943 return if ($_[0] eq "2");
946 my @chans = &ChanConfList("freshmeatAnnounce");
947 return unless (scalar @chans);
949 &Forker("freshmeat", sub {
950 my $data = &Freshmeat::freshmeatAnnounce();
953 next unless (&::validChan($_));
955 &::status("sending freshmeat update to $_.");
963 &ScheduleThis(240, "kernelLoop");
964 return if ($_[0] eq "2");
967 my @chans = &ChanConfList("kernelAnnounce");
968 return unless (scalar @chans);
970 &Forker("kernel", sub {
971 my @data = &Kernel::kernelAnnounce();
974 next unless (&::validChan($_));
976 &::status("sending kernel update to $_.");
979 ¬ice($c, "Kernel: $_");
986 return unless &IsChanConf("wingate");
988 ### FILE CACHE OF OFFENDING WINGATES.
989 foreach (grep /^$host$/, @wingateBad) {
990 &status("Wingate: RUNNING ON $host BY $who");
991 &ban("*!*\@$host", "") if &IsChanConf("wingateBan");
993 my $reason = &getChanConf("wingateKick");
995 next unless ($reason);
996 &kick($who, "", $reason)
999 ### RUN CACHE OF TRIED WINGATES.
1000 if (grep /^$host$/, @wingateCache) {
1001 push(@wingateNow, $host); # per run.
1002 push(@wingateCache, $host); # cache per run.
1004 &DEBUG("Already scanned $host. good.");
1007 my $interval = &getChanConfDefault("wingateInterval", 60); # seconds.
1008 return if (defined $forked{'wingate'});
1009 return if (time() - $wingaterun <= $interval);
1010 return unless (scalar(keys %wingateToDo));
1012 $wingaterun = time();
1014 &Forker("wingate", sub { &Wingate::Wingates(keys %wingateToDo); } );
1019 sub wingateWriteFile {
1021 &ScheduleThis(60, "wingateWriteFile");
1022 return if ($_[0] eq "2"); # defer.
1025 return unless (scalar @wingateCache);
1027 my $file = "$bot_base_dir/$param{'ircUser'}.wingate";
1028 if ($bot_pid != $$) {
1029 &DEBUG("wingateWriteFile: Reorganising!");
1034 push(@wingateNow, $_);
1039 my %hash = map { $_ => 1 } @wingateNow;
1040 @wingateNow = sort keys %hash;
1043 &DEBUG("wingateWF: writing...");
1044 open(OUT, ">$file");
1045 foreach (@wingateNow) {
1053 &ScheduleThis(720, "factoidCheck");
1054 return if ($_[0] eq "2"); # defer.
1057 my @list = &searchTable("factoids", "factoid_key", "factoid_key", " #DEL#");
1058 my $stale = &getChanConfDefault("factoidDeleteDelay", 14) *60*60*24;
1060 # disable it since it's "illegal".
1067 my $age = &getFactInfo($_, "modified_time");
1069 if (!defined $age or $age !~ /^\d+$/) {
1070 if (scalar @list > 50) {
1071 if (!$cache{warnDel}) {
1072 &WARN("list is over 50 (".scalar(@list)."... giving it a miss.");
1073 $cache{warnDel} = 1;
1078 &WARN("del factoid: old cruft (no time): $_");
1083 next unless ($time - $age > $stale);
1086 $fix =~ s/ #DEL#$//g;
1087 my $agestr = &Time2String($time - $age);
1088 &status("safedel: Removing '$_' for good. [$agestr old]");
1095 return unless (scalar keys %{ $dcc{CHAT} });
1098 &ScheduleThis(10, "dccStatus");
1099 return if ($_[0] eq "2"); # defer.
1102 my $time = strftime("%H:%M", gmtime(time()) );
1105 foreach (keys %channels) {
1107 my $users = keys %{ $channels{$c}{''} };
1108 my $chops = keys %{ $channels{$c}{o} };
1109 my $bans = keys %{ $channels{$c}{b} };
1111 my $txt = "[$time] $c: $users members ($chops chops), $bans bans";
1112 foreach (keys %{ $dcc{'CHAT'} }) {
1113 next unless (exists $channels{$c}{''}{lc $_});
1114 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
1127 foreach (keys %{ $irc->{_queue} }) {
1131 foreach (keys %sched) {
1132 next unless ($q eq $sched{$_});
1137 my $time = $irc->{_queue}->{$q}->[0] - time();
1139 if (defined $sched) {
1140 &DEBUG(" $sched($q): ".&Time2String($time) );
1142 &DEBUG(" NULL($q): ".&Time2String($time) );
1146 &DEBUG("end of sList.");
1150 my($file, $time) = @_;
1154 &VERB("mkB: file '$file' does not exist.",2);
1159 if ( -e "$file~" ) {
1160 $backup++ if ((stat $file)[9] - (stat "$file~")[9] > $time);
1161 my $delta = time() - (stat "$file~")[9];
1162 $age = &Time2String($delta);
1167 return unless ($backup);
1169 ### TODO: do internal copying.
1170 &status("Backup: $file ($age)");
1171 CORE::system("/bin/cp $file $file~");