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 &sqlDelete("seen", { nick => $nick } );
309 &FIXME("seenFlushOld: for bad DBType:" . $param{'DBType'} . ".");
311 &VERB("SEEN deleted $delete seen entries.",2);
317 &ScheduleThis(60, "newsFlush");
318 return if ($_[0] eq "2"); # defer.
321 if (!&ChanConfList("news")) {
322 &DEBUG("newsFlush: news disabled? (chan => $chan)");
329 foreach $chan (keys %::news) {
331 my $total = scalar(keys %{ $::news{$chan} });
334 delete $::news{$chan};
338 foreach $item (keys %{ $::news{$chan} }) {
339 my $t = $::news{$chan}{$item}{Expire};
341 my $tadd = $::news{$chan}{$item}{Time};
342 $oldest = $tadd if ($oldest > $tadd);
344 next if ($t == 0 or $t == -1);
346 &status("newsFlush: Fixed Expire time for $chan/$item, should not happen anyway.");
347 $::news{$chan}{$item}{Expire} = time() + $t*60*60*24;
351 my $delta = $t - time();
353 next unless (time() > $t);
355 # todo: show how old it was.
356 delete $::news{$chan}{$item};
357 &status("NEWS: (newsflush) deleted '$item'");
362 &status("NEWS (newsflush) {$chan}: deleted [$i/$total] news entries.") if ($i);
363 $none{$chan} = 1 if ($total == $i);
366 # todo: flush users aswell.
368 foreach $chan (keys %::newsuser) {
369 next if (exists $none{$chan});
371 foreach (keys %{ $::newsuser{$chan} }) {
372 my $t = $::newsuser{$chan}{$_};
373 if (!defined $t or ($t > 2 and $t < 1000)) {
374 &DEBUG("something wrong with newsuser{$chan}{$_} => $t");
378 next unless ($oldest > $t);
380 delete $::newsuser{$chan}{$_};
384 my $i = scalar(keys %{ $::newsuser{$chan} });
385 delete $::newsuser{$chan} unless ($i);
388 if ($delete or $duser) {
389 &status("NewsFlush: deleted: $delete news entries; $duser user cache.");
394 my $interval = &getChanConfDefault("chanlimitcheckInterval", 10);
397 &ScheduleThis($interval, "chanlimitCheck");
398 return if ($_[0] eq "2");
401 my $str = join(' ', &ChanConfList("chanlimitcheck") );
403 foreach $chan ( &ChanConfList("chanlimitcheck") ) {
404 next unless (&validChan($chan));
406 if ($chan eq "_default") {
407 &WARN("chanlimit: we're doing $chan!! HELP ME!");
411 my $limitplus = &getChanConfDefault("chanlimitcheckPlus", 5, $chan);
412 my $newlimit = scalar(keys %{ $channels{$chan}{''} }) + $limitplus;
413 my $limit = $channels{$chan}{'l'};
415 if (scalar keys %netsplitservers) {
416 if (defined $limit) {
417 &status("chanlimit: netsplit; removing it for $chan.");
418 $conn->mode($chan, "-l");
419 $cache{chanlimitChange}{$chan} = time();
420 &status("chanlimit: netsplit; removed.");
426 if (defined $limit and scalar keys %{ $channels{$chan}{''} } > $limit) {
427 &FIXME("LIMIT: set too low!!! FIXME");
428 ### run NAMES again and flush it.
431 if (defined $limit and $limit == $newlimit) {
432 $cache{chanlimitChange}{$chan} = time();
436 if (!exists $channels{$chan}{'o'}{$ident}) {
437 &status("chanlimit: dont have ops on $chan.") unless (exists $cache{warn}{chanlimit}{$chan});
438 $cache{warn}{chanlimit}{$chan} = 1;
439 &chanServCheck($chan);
442 delete $cache{warn}{chanlimit}{$chan};
444 if (!defined $limit) {
445 &status("chanlimit: setting for first time or from netsplit, for $chan");
448 if (exists $cache{chanlimitChange}{$chan}) {
449 my $delta = time() - $cache{chanlimitChange}{$chan};
450 if ($delta < $interval*60) {
451 &DEBUG("chanlimit: not going to change chanlimit! ($delta<$interval*60)");
456 $conn->mode($chan, "+l", $newlimit);
457 $cache{chanlimitChange}{$chan} = time();
465 &ScheduleThis(15, "netsplitCheck");
466 return if ($_[0] eq "2");
469 $cache{'netsplitCache'}++;
470 # &DEBUG("running netsplitCheck... $cache{netsplitCache}");
472 if (!scalar %netsplit and scalar %netsplitservers) {
473 &DEBUG("nsC: !hash netsplit but hash netsplitservers <- removing!");
474 undef %netsplitservers;
478 # well... this shouldn't happen since %netsplit code does it anyway.
479 foreach $s1 (keys %netsplitservers) {
481 foreach $s2 (keys %{ $netsplitservers{$s1} }) {
482 my $delta = time() - $netsplitservers{$s1}{$s2};
484 if ($delta > 60*30) {
485 &status("netsplit between $s1 and $s2 appears to be stale.");
486 delete $netsplitservers{$s1}{$s2};
491 my $i = scalar(keys %{ $netsplitservers{$s1} });
492 delete $netsplitservers{$s1} unless ($i);
495 # %netsplit hash checker.
496 my $count = scalar keys %netsplit;
498 foreach (keys %netsplit) {
499 if (&IsNickInAnyChan($_)) { # why would this happen?
500 # &DEBUG("nsC: $_ is in some chan; removing from netsplit list.");
501 delete $netsplit{$_};
506 next unless (time() - $netsplit{$_} > 60*15);
509 delete $netsplit{$_};
513 foreach (keys %channels) {
514 my $i = $cache{maxpeeps}{$chan} || 0;
515 my $j = scalar(keys %{ $channels{$chan} });
516 next unless ($i > 10 and 0.25*$i > $j);
518 &DEBUG("netsplit: 0.25*max($i) > current($j); possible netsplit?");
522 my $j = scalar(keys %netsplit);
523 &status("nsC: removed from netsplit list: (before: $count; after: $j)");
526 if (!scalar %netsplit and scalar %netsplitservers) {
527 &DEBUG("nsC: ok hash netsplit is NULL; purging hash netsplitservers");
528 undef %netsplitservers;
531 if ($count and !scalar keys %netsplit) {
532 &DEBUG("nsC: netsplit is hopefully gone. reinstating chanlimit check.");
542 &ScheduleThis(60, "floodLoop"); # minutes.
543 return if ($_[0] eq "2");
547 my $interval = &getChanConfDefault("floodCycle",60);
549 foreach $who (keys %flood) {
550 foreach (keys %{ $flood{$who} }) {
551 if (!exists $flood{$who}{$_}) {
552 &WARN("flood{$who}{$_} undefined?");
556 if ($time - $flood{$who}{$_} > $interval) {
557 delete $flood{$who}{$_};
562 &VERB("floodLoop: deleted $delete items.",2);
567 my $interval = &getChanConfDefault("seenFlushInterval", 60);
568 &ScheduleThis($interval, "seenFlush");
569 return if ($_[0] eq "2");
575 $stats{'count_old'} = &countKeys("seen") || 0;
579 if ($param{'DBType'} =~ /^(mysql|pgsql|sqlite)$/i) {
580 foreach $nick (keys %seencache) {
581 my $retval = &sqlReplace("seen", {
582 nick => lc $seencache{$nick}{'nick'},
583 time => $seencache{$nick}{'time'},
584 host => $seencache{$nick}{'host'},
585 channel => $seencache{$nick}{'chan'},
586 message => $seencache{$nick}{'msg'},
589 delete $seencache{$nick};
593 &DEBUG("seenFlush: NO VALID FACTOID SUPPORT?");
596 &status("Seen: Flushed $flushed entries.") if ($flushed);
597 &VERB(sprintf(" new seen: %03.01f%% (%d/%d)",
598 $stats{'new'}*100/($stats{'count_old'} || 1),
599 $stats{'new'}, ( $stats{'count_old'} || 1) ), 2) if ($stats{'new'});
600 &VERB(sprintf(" now seen: %3.1f%% (%d/%d)",
601 $stats{'old'}*100 / ( &countKeys("seen") || 1),
602 $stats{'old'}, &countKeys("seen") ), 2) if ($stats{'old'});
604 &WARN("scalar keys seenflush != 0!") if (scalar keys %seenflush);
612 &ScheduleThis(240, "leakCheck");
613 return if ($_[0] eq "2");
616 # flood. this is dealt with in floodLoop()
617 foreach $blah1 (keys %flood) {
618 foreach $blah2 (keys %{ $flood{$blah1} }) {
619 $count += scalar(keys %{ $flood{$blah1}{$blah2} });
622 &VERB("leak: hash flood has $count total keys.",2);
626 foreach $blah1 (keys %floodjoin) {
627 foreach $blah2 (keys %{ $floodjoin{$blah1} }) {
628 $count += scalar(keys %{ $floodjoin{$blah1}{$blah2} });
631 &VERB("leak: hash floodjoin has $count total keys.",2);
634 $count = scalar(keys %floodwarn);
635 &VERB("leak: hash floodwarn has $count total keys.",2);
638 foreach $chan (grep /[A-Z]/, keys %channels) {
639 &DEBUG("leak: chan => '$chan'.");
641 foreach $i (keys %{ $channels{$chan} }) {
642 foreach (keys %{ $channels{$chan}{$i} }) {
643 &DEBUG("leak: \$channels{$chan}{$i}{$_} ...");
649 $count = scalar(keys %chanstats);
650 &VERB("leak: hash chanstats has $count total keys.",2);
654 foreach (keys %nuh) {
655 next if (&IsNickInAnyChan($_));
656 next if (exists $dcc{CHAT}{$_});
662 &status("leak: $delete nuh{} items deleted; now have ".
663 scalar(keys %nuh) ) if ($delete);
668 &ScheduleThis(60, "ignoreCheck");
669 return if ($_[0] eq "2"); # defer.
675 foreach (keys %ignore) {
678 foreach (keys %{ $ignore{$chan} }) {
679 my @array = @{ $ignore{$chan}{$_} };
681 next unless ($array[0] and $time > $array[0]);
683 delete $ignore{$chan}{$_};
684 &status("ignore: $_/$chan has expired.");
689 $cache{ignoreCheckTime} = time();
691 &VERB("ignore: $count items deleted.",2);
696 &ScheduleThis(15, "ircCheck");
697 return if ($_[0] eq "2"); # defer.
700 $cache{statusSafe} = 1;
702 my @x = &getJoinChans();
703 my $iconf = scalar( @x );
704 my $inow = scalar( keys %channels );
705 if ($iconf > 2 and $inow * 2 <= $iconf) {
706 &FIXME("ircCheck: current channels * 2 <= config channels. FIXME.");
707 &FIXME("ircCheck: iconf = $iconf");
708 &FIXME("ircCheck: inow = $inow");
713 if (!$conn->connected or time() - $msgtime > 3600) {
714 # todo: shouldn't we use cache{connect} somewhere?
715 if (exists $cache{connect}) {
716 &WARN("ircCheck: no msg for 3600 and disco'd! reconnecting!");
717 $msgtime = time(); # just in case.
719 delete $cache{connect};
721 &status("IRCTEST: possible lost in space; checking. ".
723 &msg($ident, "TEST");
724 $cache{connect} = time();
728 if ($ident !~ /^\Q$param{ircNick}\E$/) {
729 # this does not work unfortunately.
730 &WARN("ircCheck: ident($ident) != param{ircNick}($param{ircNick}).");
732 # this check is misleading... perhaps we should do a notify.
733 if (! &IsNickInAnyChan( $param{ircNick} ) ) {
734 &DEBUG("$param{ircNick} not in use... changing!");
735 &nick( $param{ircNick} );
737 &WARN("$param{ircNick} is still in use...");
741 if (grep /^\s*$/, keys %channels) {
742 &WARN("ircCheck: we have a NULL chan in hash channels? removing!");
743 if (!exists $channels{''}) {
744 &DEBUG("ircCheck: this should never happen!");
747 delete $channels{''};
750 $cache{statusSafe} = 0;
753 if ($utime_userfile > $wtime_userfile and time() - $wtime_userfile > 3600) {
755 $wtime_userfile = time();
758 if ($utime_chanfile > $wtime_chanfile and time() - $wtime_chanfile > 3600) {
760 $wtime_chanfile = time();
766 &ScheduleThis(120, "miscCheck");
767 return if ($_[0] eq "2"); # defer.
772 if ( -x "/usr/bin/ipcs") {
773 @ipcs = `/usr/bin/ipcs`;
775 &WARN("ircCheck: no 'ipcs' binary.");
779 # shmid stale remove.
783 # key, shmid, owner, perms, bytes, nattch
784 next unless (/^(0x\d+) (\d+)\s+(\S+)\s+(\d+)\s+(\d+)\s+/);
786 my ($shmid, $size) = ($2,$5);
787 next unless ($shmid != $shm and $size == 2000);
788 my $z = &shmRead($shmid);
789 if ($z =~ /^(\d+): /) {
791 next if (time() - $time < 60*60);
794 # &DEBUG("shm: $shmid is not ours or old blootbot => ($z)");
798 &status("SHM: nuking shmid $shmid");
799 CORE::system("/usr/bin/ipcrm shm $shmid >/dev/null");
802 # make backup of important files.
803 &mkBackup( $bot_state_dir."/blootbot.chan", 60*60*24*3);
804 &mkBackup( $bot_state_dir."/blootbot.users", 60*60*24*3);
805 &mkBackup( $bot_base_dir."/blootbot-news.txt", 60*60*24*1);
807 # flush cache{lobotomy}
808 foreach (keys %{ $cache{lobotomy} }) {
809 next unless (time() - $cache{lobotomy}{$_} > 60*60);
810 delete $cache{lobotomy}{$_};
813 ### check modules if they've been modified. might be evil.
819 &ScheduleThis(240, "miscCheck2");
820 return if ($_[0] eq "2"); # defer.
824 opendir(DEBIAN, "$bot_state_dir/debian");
825 foreach ( grep /gz$/, readdir(DEBIAN) ) {
826 my $exit = CORE::system("gzip -t $bot_state_dir/debian/$_");
829 &status("debian: unlinking file => $_");
830 unlink "$bot_state_dir/debian/$_";
834 # compress logs that should have been compressed.
835 # todo: use strftime?
836 my ($day,$month,$year) = (gmtime(time()))[3,4,5];
837 my $date = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
839 if (!opendir(DIR,"$bot_log_dir")) {
840 &ERROR("misccheck2: log dir $bot_log_dir does not exist.");
845 while (my $f = readdir(DIR)) {
846 next unless ( -f "$bot_log_dir/$f");
847 next if ($f =~ /gz|bz2/);
848 next unless ($f =~ /(\d{8})/);
849 next if ($date eq $1);
851 &compress("$bot_log_dir/$f");
856 ### this is semi-scheduled
858 if ($ident eq $param{'ircNick'}) {
859 &status("okay, got my nick back.");
864 &ScheduleThis(30, "getNickInUse");
865 return if ($_[0] eq "2"); # defer.
868 &nick( $param{'ircNick'} );
872 return if (!defined &uptimeWriteFile);
873 # return unless &IsChanConf("uptime");
876 &ScheduleThis(60, "uptimeLoop");
877 return if ($_[0] eq "2"); # defer.
886 &ScheduleThis(60, "slashdotLoop");
887 return if ($_[0] eq "2");
890 my @chans = &ChanConfList("slashdotAnnounce");
891 return unless (scalar @chans);
893 &Forker("slashdot", sub {
894 my $line = &Slashdot::slashdotAnnounce();
895 return unless (defined $line);
898 next unless (&::validChan($_));
900 &::status("sending slashdot update to $_.");
901 ¬ice($_, "Slashdot: $line");
909 &ScheduleThis(60, "plugLoop");
910 return if ($_[0] eq "2");
913 my @chans = &ChanConfList("plugAnnounce");
914 return unless (scalar @chans);
916 &Forker("plug", sub {
917 my $line = &Plug::plugAnnounce();
918 return unless (defined $line);
921 next unless (&::validChan($_));
923 &::status("sending plug update to $_.");
924 ¬ice($_, "Plug: $line");
931 &ScheduleThis(60, "freshmeatLoop");
932 return if ($_[0] eq "2");
935 my @chans = &ChanConfList("freshmeatAnnounce");
936 return unless (scalar @chans);
938 &Forker("freshmeat", sub {
939 my $data = &Freshmeat::freshmeatAnnounce();
942 next unless (&::validChan($_));
944 &::status("sending freshmeat update to $_.");
952 &ScheduleThis(240, "kernelLoop");
953 return if ($_[0] eq "2");
956 my @chans = &ChanConfList("kernelAnnounce");
957 return unless (scalar @chans);
959 &Forker("kernel", sub {
960 my @data = &Kernel::kernelAnnounce();
963 next unless (&::validChan($_));
965 &::status("sending kernel update to $_.");
968 ¬ice($c, "Kernel: $_");
975 return unless &IsChanConf("wingate");
977 ### FILE CACHE OF OFFENDING WINGATES.
978 foreach (grep /^$host$/, @wingateBad) {
979 &status("Wingate: RUNNING ON $host BY $who");
980 &ban("*!*\@$host", "") if &IsChanConf("wingateBan");
982 my $reason = &getChanConf("wingateKick");
984 next unless ($reason);
985 &kick($who, "", $reason)
988 ### RUN CACHE OF TRIED WINGATES.
989 if (grep /^$host$/, @wingateCache) {
990 push(@wingateNow, $host); # per run.
991 push(@wingateCache, $host); # cache per run.
993 &DEBUG("Already scanned $host. good.");
996 my $interval = &getChanConfDefault("wingateInterval", 60); # seconds.
997 return if (defined $forked{'wingate'});
998 return if (time() - $wingaterun <= $interval);
999 return unless (scalar(keys %wingateToDo));
1001 $wingaterun = time();
1003 &Forker("wingate", sub { &Wingate::Wingates(keys %wingateToDo); } );
1008 sub wingateWriteFile {
1010 &ScheduleThis(60, "wingateWriteFile");
1011 return if ($_[0] eq "2"); # defer.
1014 return unless (scalar @wingateCache);
1016 my $file = "$bot_base_dir/$param{'ircUser'}.wingate";
1017 if ($bot_pid != $$) {
1018 &DEBUG("wingateWriteFile: Reorganising!");
1023 push(@wingateNow, $_);
1028 my %hash = map { $_ => 1 } @wingateNow;
1029 @wingateNow = sort keys %hash;
1032 &DEBUG("wingateWF: writing...");
1033 open(OUT, ">$file");
1034 foreach (@wingateNow) {
1042 &ScheduleThis(720, "factoidCheck");
1043 return if ($_[0] eq "2"); # defer.
1046 my @list = &searchTable("factoids", "factoid_key", "factoid_key", " #DEL#");
1047 my $stale = &getChanConfDefault("factoidDeleteDelay", 14) *60*60*24;
1049 # disable it since it's "illegal".
1056 my $age = &getFactInfo($_, "modified_time");
1058 if (!defined $age or $age !~ /^\d+$/) {
1059 if (scalar @list > 50) {
1060 if (!$cache{warnDel}) {
1061 &WARN("list is over 50 (".scalar(@list)."... giving it a miss.");
1062 $cache{warnDel} = 1;
1067 &WARN("del factoid: old cruft (no time): $_");
1072 next unless ($time - $age > $stale);
1075 $fix =~ s/ #DEL#$//g;
1076 my $agestr = &Time2String($time - $age);
1077 &status("safedel: Removing '$_' for good. [$agestr old]");
1084 return unless (scalar keys %{ $dcc{CHAT} });
1087 &ScheduleThis(10, "dccStatus");
1088 return if ($_[0] eq "2"); # defer.
1091 my $time = strftime("%H:%M", gmtime(time()) );
1094 foreach (keys %channels) {
1096 my $users = keys %{ $channels{$c}{''} };
1097 my $chops = keys %{ $channels{$c}{o} };
1098 my $bans = keys %{ $channels{$c}{b} };
1100 my $txt = "[$time] $c: $users members ($chops chops), $bans bans";
1101 foreach (keys %{ $dcc{'CHAT'} }) {
1102 next unless (exists $channels{$c}{''}{lc $_});
1103 $conn->privmsg($dcc{'CHAT'}{$_}, $txt);
1116 foreach (keys %{ $irc->{_queue} }) {
1120 foreach (keys %sched) {
1121 next unless ($q eq $sched{$_});
1126 my $time = $irc->{_queue}->{$q}->[0] - time();
1128 if (defined $sched) {
1129 &DEBUG(" $sched($q): ".&Time2String($time) );
1131 &DEBUG(" NULL($q): ".&Time2String($time) );
1135 &DEBUG("end of sList.");
1139 my($file, $time) = @_;
1143 &VERB("mkB: file '$file' does not exist.",2);
1148 if ( -e "$file~" ) {
1149 $backup++ if ((stat $file)[9] - (stat "$file~")[9] > $time);
1150 my $delta = time() - (stat "$file~")[9];
1151 $age = &Time2String($delta);
1156 return unless ($backup);
1158 ### TODO: do internal copying.
1159 &status("Backup: $file ($age)");
1160 CORE::system("/bin/cp $file $file~");