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, $chan);
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 in " . &Time2String(time() - $time) . ".");
46 #&VERB("Scheduling \&$codename() for " . &Time2String($waittime),3);
48 my $retval = $conn->schedule($waittime, sub {
54 &VERB("Starting schedulers...",2);
63 &leakCheck(2); # mandatory
65 &miscCheck2(2); # mandatory
70 &factoidCheck(2); # takes a couple of seconds on a 486. defer it
71 # TODO: convert to new format... or nuke altogether.
74 # 1 for run straight away
78 &netsplitCheck(1); # mandatory
79 &floodLoop(1); # mandatory
80 &ignoreCheck(1); # mandatory
81 &miscCheck(1); # mandatory
82 &shmFlush(1); # mandatory
84 &ircCheck(1); # mandatory
86 # TODO: squeeze this into a one-liner.
87 # my $count = map { exists $sched{$_}{TIME} } keys %sched;
89 foreach (keys %sched) {
90 my $time = $sched{$_}{TIME};
91 next unless (defined $time and $time > time());
96 &status("Schedulers: $count will be running.");
101 my ($interval, $codename, @args) = @_;
102 my $waittime = &getRandomInt($interval);
104 if (!defined $waittime) {
105 &WARN("interval == waittime == UNDEF for $codename.");
109 my $time = $sched{$codename}{TIME};
110 if (defined $time and $time > time()) {
111 &WARN("Sched for $codename already exists in " . &Time2String(time() - $time) . ".");
115 &DEBUG("Scheduling \&$codename() " . \&$codename . " for " . &Time2String($waittime),3);
117 my $retval = $conn->schedule($waittime, \&$codename, @args);
118 $sched{$codename}{LABEL} = $retval;
119 $sched{$codename}{TIME} = time()+$waittime;
120 $sched{$codename}{LOOP} = 1;
124 #### LET THE FUN BEGIN.
128 my $interval = &getChanConfDefault("randomQuoteInterval", 60, $chan);
130 &ScheduleThis($interval, "randomQuote");
131 return if ($_[0] eq "2"); # defer.
134 foreach ( &ChanConfList("randomQuote") ) {
135 next unless (&validChan($_));
137 my $line = &getRandomLineFromFile($bot_data_dir. "/blootbot.randtext");
138 if (!defined $line) {
139 &ERROR("random Quote: weird error?");
143 &status("sending random Quote to $_.");
144 &action($_, "Ponders: ".$line);
146 ### TODO: if there were no channels, don't reschedule until channel
147 ### configuration is modified.
154 my $interval = &getChanConfDefault("randomFactoidInterval", 60, $chan);
156 &ScheduleThis($interval, "randomFactoid");
157 return if ($_[0] eq "2"); # defer.
160 foreach ( &ChanConfList("randomFactoid") ) {
161 next unless (&validChan($_));
163 &status("sending random Factoid to $_.");
165 ($key,$val) = &randKey("factoids","factoid_key,factoid_value");
166 &DEBUG("rF: $key, $val");
167 ### $val =~ tr/^[A-Z]/[a-z]/; # blah is Good => blah is good.
168 last if ((defined $val) and ($val !~ /^</) and ($key !~ /\#DEL\#/) and ($key !~ /^cmd:/));
172 &ERROR("rF: tried 5 times but failed.");
176 &action($_, "Thinks: \037$key\037 is $val");
177 ### FIXME: Use &getReply() on above to format factoid properly?
184 &ScheduleThis(60, "logLoop");
185 return if ($_[0] eq "2"); # defer.
188 return unless (defined fileno LOG);
189 return unless (&IsParam("logfile"));
190 return unless (&IsParam("maxLogSize"));
192 ### check if current size is too large.
193 if ( -s $file{log} > $param{'maxLogSize'}) {
194 my $date = sprintf("%04d%02d%02d", (gmtime)[5,4,3]);
195 $file{log} = $param{'logfile'} ."-". $date;
196 &status("cycling log file.");
198 if ( -e $file{log}) {
202 $newlog = $file{log}."-".$i;
203 last if (! -e $newlog);
206 $file{log} = $newlog;
210 CORE::system("/bin/mv '$param{'logfile'}' '$file{log}'");
211 &compress($file{log});
213 &status("cycling log file.");
216 ### check if all the logs exceed size.
217 if (!opendir(LOGS, $bot_log_dir)) {
218 &WARN("logLoop: could not open dir '$bot_log_dir'");
224 while (defined($_ = readdir LOGS)) {
225 my $logfile = "$bot_log_dir/$_";
227 next unless ( -f $logfile);
229 my $size = -s $logfile;
230 my $age = (stat $logfile)[9];
231 $age{$age} = $logfile;
232 $size{$logfile} = $size;
238 while ($tsize > $param{'maxLogSize'}) {
239 &status("LOG: current size > max ($tsize > $param{'maxLogSize'})");
240 my $oldest = (sort {$a <=> $b} keys %age)[0];
241 &status("LOG: unlinking $age{$oldest}.");
242 unlink $age{$oldest};
247 ### TODO: add how many b,kb,mb removed?
248 &status("LOG: removed $delete logs.") if ($delete);
253 &ScheduleThis(1440, "seenFlushOld");
254 return if ($_[0] eq "2"); # defer.
257 # NO SEEN FLUSHING!!!
259 # is this global-only?
260 return unless (&IsChanConf("seen") > 0);
261 return unless (&IsChanConf("seenFlushInterval") > 0);
263 # global setting. does not make sense for per-channel.
264 my $max_time = &getChanConfDefault("seenMaxDays", 30, $chan) *60*60*24;
267 if ($param{'DBType'} =~ /^(pgsql|mysql|sqlite(2)?)$/i) {
270 if ($param{'DBType'} =~ /^(mysql|sqlite(2)?)$/i) {
271 $query = "SELECT nick,time FROM seen GROUP BY nick HAVING ".
272 "UNIX_TIMESTAMP() - time > $max_time";
274 $query = "SELECT nick,time FROM seen WHERE ".
275 "extract(epoch from timestamp 'now') - time > $max_time";
278 my $sth = $dbh->prepare($query);
280 while (my @row = $sth->fetchrow_array) {
281 my ($nick,$time) = @row;
283 &sqlDelete("seen", { nick => $nick } );
289 &FIXME("seenFlushOld: for bad DBType:" . $param{'DBType'} . ".");
291 &VERB("SEEN deleted $delete seen entries.",2);
297 &ScheduleThis(60, "newsFlush");
298 return if ($_[0] eq "2"); # defer.
301 if (!&ChanConfList('News')) {
302 &DEBUG("newsFlush: news disabled? (chan => $chan)");
309 foreach $chan (keys %::news) {
311 my $total = scalar(keys %{ $::news{$chan} });
314 delete $::news{$chan};
318 foreach $item (keys %{ $::news{$chan} }) {
319 my $t = $::news{$chan}{$item}{Expire};
321 my $tadd = $::news{$chan}{$item}{Time};
322 $oldest = $tadd if ($oldest > $tadd);
324 next if ($t == 0 or $t == -1);
326 &status("newsFlush: Fixed Expire time for $chan/$item, should not happen anyway.");
327 $::news{$chan}{$item}{Expire} = time() + $t*60*60*24;
331 my $delta = $t - time();
333 next unless (time() > $t);
335 # TODO: show how old it was.
336 delete $::news{$chan}{$item};
337 &status("NEWS: (newsflush) deleted '$item'");
342 &status("NEWS (newsflush) {$chan}: deleted [$i/$total] news entries.") if ($i);
343 $none{$chan} = 1 if ($total == $i);
346 # TODO: flush users aswell.
348 foreach $chan (keys %::newsuser) {
349 next if (exists $none{$chan});
351 foreach (keys %{ $::newsuser{$chan} }) {
352 my $t = $::newsuser{$chan}{$_};
353 if (!defined $t or ($t > 2 and $t < 1000)) {
354 &DEBUG("something wrong with newsuser{$chan}{$_} => $t");
358 next unless ($oldest > $t);
360 delete $::newsuser{$chan}{$_};
364 my $i = scalar(keys %{ $::newsuser{$chan} });
365 delete $::newsuser{$chan} unless ($i);
368 if ($delete or $duser) {
369 &status("NewsFlush: deleted: $delete news entries; $duser user cache.");
374 my $interval = &getChanConfDefault("chanlimitcheckInterval", 10, $chan);
375 my $mynick=$conn->nick();
378 &ScheduleThis($interval, "chanlimitCheck");
379 return if ($_[0] eq "2");
382 my $str = join(' ', &ChanConfList("chanlimitcheck") );
384 foreach $chan ( &ChanConfList("chanlimitcheck") ) {
385 next unless (&validChan($chan));
387 if ($chan eq "_default") {
388 &WARN("chanlimit: we're doing $chan!! HELP ME!");
392 my $limitplus = &getChanConfDefault("chanlimitcheckPlus", 5, $chan);
393 my $newlimit = scalar(keys %{ $channels{$chan}{''} }) + $limitplus;
394 my $limit = $channels{$chan}{'l'};
396 if (scalar keys %netsplitservers) {
397 if (defined $limit) {
398 &status("chanlimit: netsplit; removing it for $chan.");
399 $conn->mode($chan, "-l");
400 $cache{chanlimitChange}{$chan} = time();
401 &status("chanlimit: netsplit; removed.");
407 if (defined $limit and scalar keys %{ $channels{$chan}{''} } > $limit) {
408 &FIXME("LIMIT: set too low!!!");
409 ### run NAMES again and flush it.
412 if (defined $limit and $limit == $newlimit) {
413 $cache{chanlimitChange}{$chan} = time();
417 if (!exists $channels{$chan}{'o'}{$mynick}) {
418 &status("chanlimit: dont have ops on $chan.") unless (exists $cache{warn}{chanlimit}{$chan});
419 $cache{warn}{chanlimit}{$chan} = 1;
420 &chanServCheck($chan);
423 delete $cache{warn}{chanlimit}{$chan};
425 if (!defined $limit) {
426 &status("chanlimit: $chan: setting for first time or from netsplit.");
429 if (exists $cache{chanlimitChange}{$chan}) {
430 my $delta = time() - $cache{chanlimitChange}{$chan};
431 if ($delta < $interval*60) {
432 &DEBUG("chanlimit: not going to change chanlimit! ($delta<$interval*60)");
437 $conn->mode($chan, "+l", $newlimit);
438 $cache{chanlimitChange}{$chan} = time();
446 &ScheduleThis(15, "netsplitCheck");
447 return if ($_[0] eq "2");
450 $cache{'netsplitCache'}++;
451 # &DEBUG("running netsplitCheck... $cache{netsplitCache}");
453 if (!scalar %netsplit and scalar %netsplitservers) {
454 &DEBUG("nsC: !hash netsplit but hash netsplitservers <- removing!");
455 undef %netsplitservers;
459 # well... this shouldn't happen since %netsplit code does it anyway.
460 foreach $s1 (keys %netsplitservers) {
462 foreach $s2 (keys %{ $netsplitservers{$s1} }) {
463 my $delta = time() - $netsplitservers{$s1}{$s2};
465 if ($delta > 60*30) {
466 &status("netsplit between $s1 and $s2 appears to be stale.");
467 delete $netsplitservers{$s1}{$s2};
472 my $i = scalar(keys %{ $netsplitservers{$s1} });
473 delete $netsplitservers{$s1} unless ($i);
476 # %netsplit hash checker.
477 my $count = scalar keys %netsplit;
479 foreach (keys %netsplit) {
480 if (&IsNickInAnyChan($_)) { # why would this happen?
481 # &DEBUG("nsC: $_ is in some chan; removing from netsplit list.");
482 delete $netsplit{$_};
487 next unless (time() - $netsplit{$_} > 60*15);
490 delete $netsplit{$_};
494 foreach (keys %channels) {
495 my $i = $cache{maxpeeps}{$chan} || 0;
496 my $j = scalar(keys %{ $channels{$chan} });
497 next unless ($i > 10 and 0.25*$i > $j);
499 &DEBUG("netsplit: 0.25*max($i) > current($j); possible netsplit?");
503 my $j = scalar(keys %netsplit);
504 &status("nsC: removed from netsplit list: (before: $count; after: $j)");
507 if (!scalar %netsplit and scalar %netsplitservers) {
508 &DEBUG("nsC: ok hash netsplit is NULL; purging hash netsplitservers");
509 undef %netsplitservers;
512 if ($count and !scalar keys %netsplit) {
513 &DEBUG("nsC: netsplit is hopefully gone. reinstating chanlimit check.");
523 &ScheduleThis(60, "floodLoop"); # minutes.
524 return if ($_[0] eq "2");
528 my $interval = &getChanConfDefault("floodCycle",60, $chan);
530 foreach $who (keys %flood) {
531 foreach (keys %{ $flood{$who} }) {
532 if (!exists $flood{$who}{$_}) {
533 &WARN("flood{$who}{$_} undefined?");
537 if ($time - $flood{$who}{$_} > $interval) {
538 delete $flood{$who}{$_};
543 &VERB("floodLoop: deleted $delete items.",2);
548 my $interval = &getChanConfDefault("seenFlushInterval", 60, $chan);
549 &ScheduleThis($interval, "seenFlush");
550 return if ($_[0] eq "2");
556 $stats{'count_old'} = &countKeys("seen") || 0;
560 if ($param{'DBType'} =~ /^(mysql|pgsql|sqlite(2)?)$/i) {
561 foreach $nick (keys %seencache) {
562 my $lastcount = &sqlSelect('seen','messagecount',{nick=>lc $seencache{$nick}{'nick'}}) || 0;
563 my $retval = &sqlReplace("seen", {
564 nick => lc $seencache{$nick}{'nick'},
565 time => $seencache{$nick}{'time'},
566 host => $seencache{$nick}{'host'},
567 channel => $seencache{$nick}{'chan'},
568 message => $seencache{$nick}{'msg'},
569 messagecount => $lastcount+$seencache{$nick}{'msgcount'},
573 delete $seencache{$nick};
577 &DEBUG("seenFlush: NO VALID FACTOID SUPPORT?");
580 &status("Seen: Flushed $flushed entries.") if ($flushed);
581 &VERB(sprintf(" new seen: %03.01f%% (%d/%d)",
582 $stats{'new'}*100/($stats{'count_old'} || 1),
583 $stats{'new'}, ( $stats{'count_old'} || 1) ), 2) if ($stats{'new'});
584 &VERB(sprintf(" now seen: %3.1f%% (%d/%d)",
585 $stats{'old'}*100 / ( &countKeys("seen") || 1),
586 $stats{'old'}, &countKeys("seen") ), 2) if ($stats{'old'});
588 &WARN("scalar keys seenflush != 0!") if (scalar keys %seenflush);
596 &ScheduleThis(240, "leakCheck");
597 return if ($_[0] eq "2");
600 # flood. this is dealt with in floodLoop()
601 foreach $blah1 (keys %flood) {
602 foreach $blah2 (keys %{ $flood{$blah1} }) {
603 $count += scalar(keys %{ $flood{$blah1}{$blah2} });
606 &VERB("leak: hash flood has $count total keys.",2);
610 foreach $blah1 (keys %floodjoin) {
611 foreach $blah2 (keys %{ $floodjoin{$blah1} }) {
612 $count += scalar(keys %{ $floodjoin{$blah1}{$blah2} });
615 &VERB("leak: hash floodjoin has $count total keys.",2);
618 $count = scalar(keys %floodwarn);
619 &VERB("leak: hash floodwarn has $count total keys.",2);
622 foreach $chan (grep /[A-Z]/, keys %channels) {
623 &DEBUG("leak: chan => '$chan'.");
625 foreach $i (keys %{ $channels{$chan} }) {
626 foreach (keys %{ $channels{$chan}{$i} }) {
627 &DEBUG("leak: \$channels{$chan}{$i}{$_} ...");
633 $count = scalar(keys %chanstats);
634 &VERB("leak: hash chanstats has $count total keys.",2);
638 foreach (keys %nuh) {
639 next if (&IsNickInAnyChan($_));
640 next if (exists $dcc{CHAT}{$_});
646 &status("leak: $delete nuh{} items deleted; now have ".
647 scalar(keys %nuh) ) if ($delete);
652 &ScheduleThis(60, "ignoreCheck");
653 return if ($_[0] eq "2"); # defer.
659 foreach (keys %ignore) {
662 foreach (keys %{ $ignore{$chan} }) {
663 my @array = @{ $ignore{$chan}{$_} };
665 next unless ($array[0] and $time > $array[0]);
667 delete $ignore{$chan}{$_};
668 &status("ignore: $_/$chan has expired.");
673 $cache{ignoreCheckTime} = time();
675 &VERB("ignore: $count items deleted.",2);
680 &ScheduleThis(15, "ircCheck");
681 return if ($_[0] eq "2"); # defer.
684 $cache{statusSafe} = 1;
685 foreach (sort keys %conns) {
687 my $mynick=$conn->nick();
688 &DEBUG("ircCheck for $_");
689 my @join = &getJoinChans(1);
691 &FIXME('ircCheck: found channels to join! ' . join(',',@join));
695 my @join = &getJoinChans(1);
697 &FIXME('ircCheck: found channels to join! ' . join(',',@join));
701 if (time() - $msgtime > 3600) {
702 # TODO: shouldn't we use cache{connect} somewhere?
703 if (exists $cache{connect}) {
704 &WARN("ircCheck: no msg for 3600 and disco'd! reconnecting!");
705 $msgtime = time(); # just in case.
707 delete $cache{connect};
709 &status('ircCheck: possible lost in space; checking.'.
711 &msg($mynick, "TEST");
712 $cache{connect} = time();
717 if (grep /^\s*$/, keys %channels) {
718 &WARN('ircCheck: we have a NULL chan in hash channels? removing!');
719 if (!exists $channels{''}) {
720 &DEBUG('ircCheck: this should never happen!');
722 if ($ident !~ /^\Q$param{ircNick}\E$/) {
723 # this does not work unfortunately.
724 &WARN("ircCheck: ident($ident) != param{ircNick}($param{ircNick}).");
726 # this check is misleading... perhaps we should do a notify.
727 if (! &IsNickInAnyChan( $param{ircNick} ) ) {
728 &DEBUG("$param{ircNick} not in use... changing!");
729 &nick( $param{ircNick} );
731 &WARN("$param{ircNick} is still in use...");
735 $cache{statusSafe} = 0;
738 if ($utime_userfile > $wtime_userfile and time() - $wtime_userfile > 3600) {
740 $wtime_userfile = time();
743 if ($utime_chanfile > $wtime_chanfile and time() - $wtime_chanfile > 3600) {
745 $wtime_chanfile = time();
751 &ScheduleThis(120, "miscCheck");
752 return if ($_[0] eq "2"); # defer.
757 if ( -x "/usr/bin/ipcs") {
758 @ipcs = `/usr/bin/ipcs`;
760 &WARN("ircCheck: no 'ipcs' binary.");
764 # make backup of important files.
765 &mkBackup( $bot_state_dir."/blootbot.chan", 60*60*24*3);
766 &mkBackup( $bot_state_dir."/blootbot.users", 60*60*24*3);
767 &mkBackup( $bot_base_dir."/blootbot-news.txt", 60*60*24*1);
769 # flush cache{lobotomy}
770 foreach (keys %{ $cache{lobotomy} }) {
771 next unless (time() - $cache{lobotomy}{$_} > 60*60);
772 delete $cache{lobotomy}{$_};
775 ### check modules if they've been modified. might be evil.
778 # shmid stale remove.
782 # key, shmid, owner, perms, bytes, nattch
783 next unless (/^(0x\d+) (\d+)\s+(\S+)\s+(\d+)\s+(\d+)\s+/);
785 my ($shmid, $size) = ($2,$5);
786 next unless ($shmid != $shm and $size == 2000);
787 my $z = &shmRead($shmid);
788 if ($z =~ /^(\S+):(\d+):(\d+): /) {
792 next if (time() - $time < 60*60);
793 # FIXME remove not-pid shm if parent process dead
794 next if ($pid == $bot_pid);
795 # don't touch other bots, if they're running.
796 next unless ($param{ircUser} =~ /^\Q$n\E$/);
798 &DEBUG("shm: $shmid is not ours or old blootbot => ($z)");
802 &status("SHM: nuking shmid $shmid");
803 CORE::system("/usr/bin/ipcrm shm $shmid >/dev/null");
809 &ScheduleThis(240, "miscCheck2");
810 return if ($_[0] eq "2"); # defer.
814 opendir(DEBIAN, "$bot_state_dir/debian");
815 foreach ( grep /gz$/, readdir(DEBIAN) ) {
816 my $exit = CORE::system("gzip -t $bot_state_dir/debian/$_");
819 &status("debian: unlinking file => $_");
820 unlink "$bot_state_dir/debian/$_";
824 # compress logs that should have been compressed.
825 # TODO: use strftime?
826 my ($day,$month,$year) = (gmtime(time()))[3,4,5];
827 my $date = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
829 if (!opendir(DIR,"$bot_log_dir")) {
830 &ERROR("misccheck2: log dir $bot_log_dir does not exist.");
835 while (my $f = readdir(DIR)) {
836 next unless ( -f "$bot_log_dir/$f");
837 next if ($f =~ /gz|bz2/);
838 next unless ($f =~ /(\d{8})/);
839 next if ($date eq $1);
841 &compress("$bot_log_dir/$f");
846 ### this is semi-scheduled
848 # FIXME: broken for multiple connects
849 # if ($ident eq $param{'ircNick'}) {
850 # &status("okay, got my nick back.");
855 # &ScheduleThis(30, "getNickInUse");
856 # return if ($_[0] eq "2"); # defer.
859 # &nick( $param{'ircNick'} );
863 return if (!defined &uptimeWriteFile);
864 # return unless &IsParam('Uptime');
867 &ScheduleThis(60, 'uptimeLoop');
868 return if ($_[0] eq '2'); # defer.
877 &ScheduleThis(60, 'slashdotLoop');
878 return if ($_[0] eq "2");
881 my @chans = &ChanConfList('slashdotAnnounce');
882 return unless (scalar @chans);
884 &Forker('slashdot', sub {
885 my $line = &Slashdot::slashdotAnnounce();
886 return unless (defined $line);
889 next unless (&::validChan($_));
891 &::status("sending slashdot update to $_.");
892 ¬ice($_, "Slashdot: $line");
900 &ScheduleThis(60, 'plugLoop');
901 return if ($_[0] eq "2");
904 my @chans = &ChanConfList('plugAnnounce');
905 return unless (scalar @chans);
907 &Forker('Plug', sub {
908 my $line = &Plug::plugAnnounce();
909 return unless (defined $line);
912 next unless (&::validChan($_));
914 &::status("sending plug update to $_.");
915 ¬ice($_, "Plug: $line");
922 &ScheduleThis(240, "kernelLoop");
923 return if ($_[0] eq "2");
926 my @chans = &ChanConfList("kernelAnnounce");
927 return unless (scalar @chans);
929 &Forker("Kernel", sub {
930 my @data = &Kernel::kernelAnnounce();
933 next unless (&::validChan($_));
935 &::status("sending kernel update to $_.");
938 ¬ice($c, "Kernel: $_");
945 return unless &IsChanConf('Wingate') > 0;
947 ### FILE CACHE OF OFFENDING WINGATES.
948 foreach (grep /^$host$/, @wingateBad) {
949 &status("Wingate: RUNNING ON $host BY $who");
950 &ban("*!*\@$host", "") if &IsChanConf('wingateBan') > 0;
952 my $reason = &getChanConf('wingateKick');
954 next unless ($reason);
955 &kick($who, "", $reason)
958 ### RUN CACHE OF TRIED WINGATES.
959 if (grep /^$host$/, @wingateCache) {
960 push(@wingateNow, $host); # per run.
961 push(@wingateCache, $host); # cache per run.
963 &DEBUG("Already scanned $host. good.");
966 my $interval = &getChanConfDefault('wingateInterval', 60, $chan); # seconds.
967 return if (defined $forked{'Wingate'});
968 return if (time() - $wingaterun <= $interval);
969 return unless (scalar(keys %wingateToDo));
971 $wingaterun = time();
973 &Forker('Wingate', sub { &Wingate::Wingates(keys %wingateToDo); } );
978 sub wingateWriteFile {
980 &ScheduleThis(60, 'wingateWriteFile');
981 return if ($_[0] eq "2"); # defer.
984 return unless (scalar @wingateCache);
986 my $file = "$bot_base_dir/$param{'ircUser'}.wingate";
987 if ($bot_pid != $$) {
988 &DEBUG('wingateWriteFile: Reorganising!');
993 push(@wingateNow, $_);
998 my %hash = map { $_ => 1 } @wingateNow;
999 @wingateNow = sort keys %hash;
1002 &DEBUG('wingateWF: writing...');
1003 open(OUT, ">$file");
1004 foreach (@wingateNow) {
1012 &ScheduleThis(720, "factoidCheck");
1013 return if ($_[0] eq "2"); # defer.
1016 my @list = &searchTable("factoids", "factoid_key", "factoid_key", " #DEL#");
1017 my $stale = &getChanConfDefault("factoidDeleteDelay", 14, $chan) *60*60*24;
1019 # disable it since it's "illegal".
1026 my $age = &getFactInfo($_, "modified_time");
1028 if (!defined $age or $age !~ /^\d+$/) {
1029 if (scalar @list > 50) {
1030 if (!$cache{warnDel}) {
1031 &WARN("list is over 50 (".scalar(@list)."... giving it a miss.");
1032 $cache{warnDel} = 1;
1037 &WARN("del factoid: old cruft (no time): $_");
1042 next unless ($time - $age > $stale);
1045 $fix =~ s/ #DEL#$//g;
1046 my $agestr = &Time2String($time - $age);
1047 &status("safedel: Removing '$_' for good. [$agestr old]");
1054 return unless (scalar keys %{ $dcc{CHAT} });
1057 &ScheduleThis(10, "dccStatus");
1058 return if ($_[0] eq "2"); # defer.
1061 my $time = strftime("%H:%M", gmtime(time()) );
1064 foreach (keys %channels) {
1066 my $users = keys %{ $channels{$c}{''} };
1067 my $chops = keys %{ $channels{$c}{o} };
1068 my $bans = keys %{ $channels{$c}{b} };
1070 my $txt = "[$time] $c: $users members ($chops chops), $bans bans";
1071 foreach (keys %{ $dcc{'CHAT'} }) {
1072 next unless (exists $channels{$c}{''}{lc $_});
1073 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
1085 my $reply = "sched:";
1086 foreach (keys %{ $irc->{_queue}}) {
1088 my $coderef = $irc->{_queue}->{$q}->[1];
1090 foreach (keys %sched) {
1092 next unless defined(\&$schedname);
1093 next unless ($coderef eq \&$schedname);
1094 $sched = $schedname;
1098 my $time = $irc->{_queue}->{$q}->[0] - time();
1100 if (defined $sched) {
1101 $reply = "$reply, $sched($q):" . &Time2String($time);
1103 $reply = "$reply, NULL($q):" . &Time2String($time);
1111 my($file, $time) = @_;
1115 &VERB("mkB: file '$file' does not exist.",2);
1120 if ( -e "$file~" ) {
1121 $backup++ if ((stat $file)[9] - (stat "$file~")[9] > $time);
1122 my $delta = time() - (stat "$file~")[9];
1123 $age = &Time2String($delta);
1128 return unless ($backup);
1130 ### TODO: do internal copying.
1131 &status("Backup: $file ($age)");
1132 CORE::system("/bin/cp $file $file~");