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(1); # 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}{RUNNING} = 1;
81 #### LET THE FUN BEGIN.
85 my $interval = &getChanConfDefault("randomQuoteInterval", 60);
87 &ScheduleThis($interval, "randomQuote");
88 return if ($_[0] eq "2"); # defer.
90 delete $sched{"randomQuote"}{RUNNING};
93 my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.randtext");
95 &ERROR("random Quote: weird error?");
99 foreach ( &ChanConfList("randomQuote") ) {
100 next unless (&validChan($_));
102 &status("sending random Quote to $_.");
103 &action($_, "Ponders: ".$line);
105 ### TODO: if there were no channels, don't reschedule until channel
106 ### configuration is modified.
113 my $interval = &getChanConfDefault("randomFactoidInterval", 60);
115 &ScheduleThis($interval, "randomFactoid");
116 return if ($_[0] eq "2"); # defer.
118 delete $sched{"randomFactoid"}{RUNNING};
122 ($key,$val) = &randKey("factoids","factoid_key,factoid_value");
123 ### $val =~ tr/^[A-Z]/[a-z]/; # blah is Good => blah is good.
124 last if (defined $val and $val !~ /^</);
128 &ERROR("rF: tried 5 times but failed.");
133 foreach ( &ChanConfList("randomFactoid") ) {
134 next unless (&validChan($_));
136 &status("sending random Factoid to $_.");
137 &action($_, "Thinks: \037$key\037 is $val");
138 ### FIXME: Use &getReply() on above to format factoid properly?
143 sub randomFreshmeat {
144 my $interval = &getChanConfDefault("randomFresheatInterval", 60);
147 &ScheduleThis($interval, "randomFreshmeat");
148 return if ($_[0] eq "2"); # defer.
150 delete $sched{"randomFreshmeat"}{RUNNING};
153 my @chans = &ChanConfList("randomFreshmeat");
154 return unless (scalar @chans);
156 &Forker("freshmeat", sub {
157 my $retval = &Freshmeat::randPackage();
160 next unless (&validChan($_));
162 &status("sending random Freshmeat to $_.");
170 &ScheduleThis(60, "logLoop");
171 return if ($_[0] eq "2"); # defer.
173 delete $sched{"logLoop"}{RUNNING};
176 return unless (defined fileno LOG);
177 return unless (&IsParam("logfile"));
178 return unless (&IsParam("maxLogSize"));
180 ### check if current size is too large.
181 if ( -s $file{log} > $param{'maxLogSize'}) {
182 my $date = sprintf("%04d%02d%02d", (localtime)[5,4,3]);
183 $file{log} = $param{'logfile'} ."-". $date;
184 &status("cycling log file.");
186 if ( -e $file{log}) {
190 $newlog = $file{log}."-".$i;
191 last if (! -e $newlog);
194 $file{log} = $newlog;
198 CORE::system("/bin/mv '$param{'logfile'}' '$file{log}'");
199 &compress($file{log});
201 &status("cycling log file.");
204 ### check if all the logs exceed size.
205 my $logdir = "$bot_base_dir/log/";
206 if (opendir(LOGS, $logdir)) {
210 while (defined($_ = readdir LOGS)) {
211 my $logfile = "$logdir/$_";
213 next unless ( -f $logfile);
214 my $size = -s $logfile;
215 my $age = (stat $logfile)[9];
217 $age{$age} = $logfile;
218 $size{$logfile} = $size;
225 while ($tsize > $param{'maxLogSize'}) {
226 &status("LOG: current size > max ($tsize > $param{'maxLogSize'})");
227 my $oldest = (sort {$a <=> $b} keys %age)[0];
228 &status("LOG: unlinking $age{$oldest}.");
229 unlink $age{$oldest};
234 ### TODO: add how many b,kb,mb removed?
235 &status("LOG: removed $delete logs.") if ($delete);
237 &WARN("could not open dir $logdir");
244 &ScheduleThis(1440, "seenFlushOld");
245 return if ($_[0] eq "2"); # defer.
247 delete $sched{"seenFlushOld"}{RUNNING};
250 # is this global-only?
251 return unless (&IsChanConf("seen") > 0);
252 return unless (&IsChanConf("seenFlushInterval") > 0);
254 # global setting. does not make sense for per-channel.
255 my $max_time = &getChanConfDefault("seenMaxDays", 30) *60*60*24;
258 if ($param{'DBType'} =~ /^pg|postgres|mysql/i) {
259 my $query = "SELECT nick,time FROM seen GROUP BY nick HAVING UNIX_TIMESTAMP() - time > $max_time";
260 my $sth = $dbh->prepare($query);
263 while (my @row = $sth->fetchrow_array) {
264 my ($nick,$time) = @row;
266 &dbDel("seen","nick",$nick);
270 } elsif ($param{'DBType'} =~ /^dbm/i) {
273 foreach (keys %seen) {
274 my $delta_time = $time - &dbGet("seen", "NULL", $_, "time");
275 next unless ($delta_time > $max_time);
277 &DEBUG("seenFlushOld: ".&Time2String($delta_time) );
282 &FIXME("seenFlushOld: for PG/NO-DB.");
284 &VERB("SEEN deleted $delete seen entries.",2);
290 &ScheduleThis(1440, "newsFlush");
291 return if ($_[0] eq "2"); # defer.
293 delete $sched{"newsFlush"}{RUNNING};
296 return unless (&IsChanConf("news") > 0);
300 foreach $chan (keys %::news) {
301 foreach $item (keys %{ $::news{$chan} }) {
302 my $t = $::news{$chan}{$item}{Expire};
304 my $tadd = $::news{$chan}{$item}{Time};
305 $oldest = $tadd if ($oldest > $tadd);
307 next if ($t == 0 or $t == -1);
309 &status("newsFlush: Fixed Expire time for $chan/$item, should not happen anyway.");
310 $::news{$chan}{$item}{Expire} = time() + $t*60*60*24;
314 next unless (time() > $t);
315 # todo: show how old it was.
316 delete $::news{$chan}{$item};
317 &VERB("NEWS: deleted '$item'", 2);
322 # todo: flush users aswell.
324 foreach $chan (keys %::newsuser) {
325 foreach (keys %{ $::newsuser{$chan} }) {
326 my $t = $::newsuser{$chan}{$_};
327 if (!defined $t or ($t > 2 and $t < 1000)) {
328 &DEBUG("something wrong with newsuser{$chan}{$_} => $t");
332 next unless ($oldest > $t);
334 delete $::newsuser{$chan}{$_};
339 # &VERB("NEWS deleted $delete seen entries.",2);
340 &status("NEWS deleted: $delete news entries; $duser user cache.");
346 my $interval = &getChanConfDefault("chanlimitcheckInterval", 10);
349 &ScheduleThis($interval, "chanlimitCheck");
350 return if ($_[0] eq "2");
352 delete $sched{"chanlimitCheck"}{RUNNING};
355 foreach $chan ( &ChanConfList("chanlimitcheck") ) {
356 next unless (&validChan($chan));
358 my $limitplus = &getChanConfDefault("chanlimitcheckPlus", 5, $chan);
359 my $newlimit = scalar(keys %{$channels{$chan}{''}}) + $limitplus;
360 my $limit = $channels{$chan}{'l'};
362 if (defined $limit and scalar keys %{$channels{$chan}{''}} > $limit) {
363 &FIXME("LIMIT: set too low!!! FIXME");
364 ### run NAMES again and flush it.
367 next unless (!defined $limit or $limit != $newlimit);
369 if (!exists $channels{$chan}{'o'}{$ident}) {
370 &status("ChanLimit: dont have ops on $chan.") unless (exists $cache{warn}{chanlimit}{$chan});
371 $cache{warn}{chanlimit}{$chan} = 1;
372 ### TODO: check chanserv?
375 delete $cache{warn}{chanlimit}{$chan};
377 if (!defined $limit) {
378 &status("ChanLimit: setting for first time or from netsplit, for $chan");
381 &rawout("MODE $chan +l $newlimit");
389 &ScheduleThis(30, "netsplitCheck");
390 return if ($_[0] eq "2");
392 delete $sched{"netsplitCheck"}{RUNNING};
395 foreach $s1 (keys %netsplitservers) {
396 foreach $s2 (keys %{$netsplitservers{$s1}}) {
397 if (time() - $netsplitservers{$s1}{$s2} > 3600) {
398 &status("netsplit between $s1 and $s2 appears to be stale.");
399 delete $netsplitservers{$s1}{$s2};
404 # %netsplit hash checker.
405 my $count = scalar keys %netsplit;
406 foreach (keys %netsplit) {
407 if (&IsNickInAnyChan($_)) {
408 &DEBUG("netsplitC: $_ is in some chan; removing from netsplit list.");
409 delete $netsplit{$_};
412 next unless (time() - $netsplit{$_} > 60*10);
414 &DEBUG("netsplitC: $_ didn't come back from netsplit; removing from netsplit list.");
415 delete $netsplit{$_};
418 if ($count and !scalar keys %netsplit) {
419 &DEBUG("ok, netsplit is hopefully gone. reinstating chanlimit check.");
429 &ScheduleThis(60, "floodLoop"); # minutes.
430 return if ($_[0] eq "2");
432 delete $sched{"floodLoop"}{RUNNING};
436 my $interval = &getChanConfDefault("floodCycle",60);
438 foreach $who (keys %flood) {
439 foreach (keys %{$flood{$who}}) {
440 if (!exists $flood{$who}{$_}) {
441 &WARN("flood{$who}{$_} undefined?");
445 if ($time - $flood{$who}{$_} > $interval) {
446 delete $flood{$who}{$_};
451 &VERB("floodLoop: deleted $delete items.",2);
456 my $interval = &getChanConfDefault("seenFlushInterval", 60);
457 &ScheduleThis($interval, "seenFlush");
458 return if ($_[0] eq "2");
460 delete $sched{"seenFlush"}{RUNNING};
466 $stats{'count_old'} = &countKeys("seen") || 0;
470 if ($param{'DBType'} =~ /^mysql|pg|postgres/i) {
471 foreach $nick (keys %seencache) {
474 my $retval = &dbReplace("seen", "nick", $nick, (
475 "nick" => $seencache{$nick}{'nick'},
476 "time" => $seencache{$nick}{'time'},
477 "host" => $seencache{$nick}{'host'},
478 "channel" => $seencache{$nick}{'chan'},
479 "message" => $seencache{$nick}{'msg'},
481 &DEBUG("retval => $retval.");
482 delete $seencache{$nick};
489 my $exists = &dbGet("seen","nick", $nick, "nick");
491 if (defined $exists and $exists) {
492 &dbUpdate("seen", "nick", $nick, (
493 "time" => $seencache{$nick}{'time'},
494 "host" => $seencache{$nick}{'host'},
495 "channel" => $seencache{$nick}{'chan'},
496 "message" => $seencache{$nick}{'msg'},
500 my $retval = &dbInsert("seen", $nick, (
501 "nick" => $seencache{$nick}{'nick'},
502 "time" => $seencache{$nick}{'time'},
503 "host" => $seencache{$nick}{'host'},
504 "channel" => $seencache{$nick}{'chan'},
505 "message" => $seencache{$nick}{'msg'},
509 ### TODO: put bad nick into a list and don't do it again!
510 &FIXME("Should never happen! (nick => $nick)") if !$retval;
513 delete $seencache{$nick};
517 } elsif ($param{'DBType'} =~ /^dbm/i) {
519 foreach $nick (keys %seencache) {
520 my $retval = &dbInsert("seen", $nick, (
521 "nick" => $seencache{$nick}{'nick'},
522 "time" => $seencache{$nick}{'time'},
523 "host" => $seencache{$nick}{'host'},
524 "channel" => $seencache{$nick}{'chan'},
525 "message" => $seencache{$nick}{'msg'},
528 ### TODO: put bad nick into a list and don't do it again!
529 &FIXME("Should never happen! (nick => $nick)") if !$retval;
531 delete $seencache{$nick};
535 &DEBUG("seenFlush: NO VALID FACTOID SUPPORT?");
538 &status("Flushed $flushed seen entries.") if ($flushed);
539 &VERB(sprintf(" new seen: %03.01f%% (%d/%d)",
540 $stats{'new'}*100/($stats{'count_old'} || 1),
541 $stats{'new'}, $stats{'count_old'} ), 2) if ($stats{'new'});
542 &VERB(sprintf(" now seen: %3.1f%% (%d/%d)",
543 $stats{'old'}*100/&countKeys("seen"),
544 $stats{'old'}, &countKeys("seen") ), 2) if ($stats{'old'});
546 &WARN("scalar keys seenflush != 0!") if (scalar keys %seenflush);
554 &ScheduleThis(240, "leakCheck");
555 return if ($_[0] eq "2");
557 delete $sched{"leakCheck"}{RUNNING};
561 foreach $blah1 (keys %flood) {
562 foreach $blah2 (keys %{ $flood{$blah1} }) {
563 $count += scalar(keys %{ $flood{$blah1}{$blah2} });
566 &DEBUG("leak: hash flood has $count total keys.",2);
570 foreach $blah1 (keys %floodjoin) {
571 foreach $blah2 (keys %{ $floodjoin{$blah1} }) {
572 $count += scalar(keys %{ $floodjoin{$blah1}{$blah2} });
575 &DEBUG("leak: hash flood has $count total keys.",2);
578 $count = scalar(keys %floodwarn);
579 &DEBUG("leak: hash floodwarn has $count total keys.",2);
582 foreach $chan (grep /[A-Z]/, keys %channels) {
583 &DEBUG("leak: chan => '$chan'.");
585 foreach $i (keys %{ $channels{$chan} }) {
586 foreach (keys %{ $channels{$chan}{$i} }) {
587 &DEBUG("leak: \$channels{$chan}{$i}{$_} ...");
593 foreach (keys %nuh) {
594 next if (&IsNickInAnyChan($_));
595 next if (exists $dcc{CHAT}{$_});
601 &status("leak: $delete nuh{} items deleted; now have ".
602 scalar(keys %nuh) ) if ($delete);
607 &ScheduleThis(60, "ignoreCheck");
608 return if ($_[0] eq "2"); # defer.
610 delete $sched{"ignoreCheck"}{RUNNING};
616 foreach (keys %ignore) {
619 foreach (keys %{ $ignore{$chan} }) {
620 my @array = @{ $ignore{$chan}{$_} };
622 next unless ($array[0] and $time > $array[0]);
624 delete $ignore{$chan}{$_};
625 &status("ignore: $_/$chan has expired.");
629 &VERB("ignore: $count items deleted.",2);
635 &ScheduleThis(60, "ircCheck");
636 return if ($_[0] eq "2"); # defer.
638 delete $sched{"ircCheck"}{RUNNING};
641 my @array = grep !/^_default$/, keys %chanconf;
642 my $iconf = scalar(@array);
643 my $inow = scalar(keys %channels);
644 if ($iconf > 2 and $inow * 2 <= $iconf) {
645 &FIXME("ircCheck: current channels * 2 <= config channels. FIXME.");
649 foreach ( &ChanConfList("chanServ_ops") ) {
650 next if (exists $channels{$chan}{'o'}{$ident});
652 &status("ChanServ ==> Requesting ops for $chan.");
653 &rawout("PRIVMSG ChanServ :OP $chan $ident");
656 if (!$conn->connected or time - $msgtime > 3600) {
657 # todo: shouldn't we use cache{connect} somewhere?
658 if (exists $cache{connect}) {
659 &WARN("ircCheck: no msg for 3600 and disco'd! reconnecting!");
660 $msgtime = time(); # just in case.
662 delete $cache{connect};
664 &status("IRCTEST: possible lost in space; checking. ".
666 &msg($ident, "TEST");
667 $cache{connect} = time();
671 if ($ident !~ /^\Q$param{ircNick}\E$/) {
672 &WARN("ircCheck: ident($ident) != param{ircNick}($param{IrcNick}).");
673 if (! &IsNickInAnyChan( $param{ircNick} ) ) {
674 &DEBUG("$param{ircNick} not in use... changing!");
675 &nick( $param{ircNick} );
677 &WARN("$param{ircNick} is still in use...");
682 # if scalar @joinnext => join more channels
683 # else check for chanserv.
685 if (grep /^\s*$/, keys %channels) {
686 &WARN("we have a NULL chan in hash channels? removing!");
687 delete $channels{''};
689 &DEBUG("channels now:");
690 foreach (keys %channels) {
694 &DEBUG("channels END");
698 if ($utime_userfile > $wtime_userfile and time() - $wtime_userfile > 3600) {
700 $wtime_userfile = time();
703 if ($utime_chanfile > $wtime_chanfile and time() - $wtime_chanfile > 3600) {
705 $wtime_chanfile = time();
711 &ScheduleThis(240, "miscCheck");
712 return if ($_[0] eq "2"); # defer.
714 delete $sched{"miscCheck"}{RUNNING};
719 if ( -x "/usr/bin/ipcs") {
720 @ipcs = `/usr/bin/ipcs`;
722 &WARN("ircCheck: no 'ipcs' binary.");
726 # shmid stale remove.
730 # key, shmid, owner, perms, bytes, nattch
731 next unless (/^(0x\d+) (\d+)\s+(\S+)\s+(\d+)\s+(\d+)\s+/);
733 my ($shmid, $size) = ($2,$5);
734 next unless ($shmid != $shm and $size == 2000);
735 my $z = &shmRead($shmid);
736 if ($z =~ /^(\d+): /) {
738 next if (time() - $time < 60*60);
741 # &DEBUG("shm: $shmid is not ours or old blootbot => ($z)");
745 &status("SHM: nuking shmid $shmid");
746 CORE::system("/usr/bin/ipcrm shm $shmid >/dev/null");
749 # make backup of important files.
750 &mkBackup( $bot_misc_dir."/blootbot.chan", 60*60*24*1);
751 &mkBackup( $bot_misc_dir."/blootbot.users", 60*60*24*1);
752 &mkBackup( $bot_base_dir."/blootbot-news.txt", 60*60*24*1);
754 # flush cache{lobotomy}
755 foreach (keys %{ $cache{lobotomy} }) {
756 next unless (time() - $cache{lobotomy}{$_} > 60*60);
757 delete $cache{lobotomy}{$_};
760 ### check modules if they've been modified. might be evil.
766 &ScheduleThis(240, "miscCheck2");
767 return if ($_[0] eq "2"); # defer.
769 delete $sched{"miscCheck2"}{RUNNING};
772 &DEBUG("miscCheck2: Doing debian checking...");
775 opendir(DEBIAN, "$bot_base_dir/debian");
776 foreach ( grep /gz$/, readdir(DEBIAN) ) {
777 my $exit = CORE::system("gzip -t $bot_base_dir/debian/$_");
780 &status("debian: unlinking file => $_");
781 unlink "$bot_base_dir/debian/$_";
785 # compress logs that should have been compressed.
786 # todo: use strftime?
787 my ($day,$month,$year) = (localtime(time()))[3,4,5];
788 my $date = sprintf("%04d%02d%02d",$year+1900,$month+1,$day);
790 opendir(DIR,"$bot_base_dir/log");
791 while (my $f = readdir(DIR)) {
792 next unless ( -f "$bot_base_dir/log/$f");
793 next if ($f =~ /gz|bz2/);
794 next unless ($f =~ /(\d{8})/);
795 next if ($date eq $1);
797 &compress("$bot_base_dir/log/$f");
803 return if ($$ != $::bot_pid); # fork protection.
806 &ScheduleThis(5, "shmFlush");
807 return if ($_[0] eq "2");
809 delete $sched{"shmFlush"}{RUNNING};
813 my $shmmsg = &shmRead($shm);
814 $shmmsg =~ s/\0//g; # remove padded \0's.
815 if ($shmmsg =~ s/^(\d+): //) {
819 foreach (split '\|\|', $shmmsg) {
821 &VERB("shm: Processing '$_'.",2);
823 if (/^DCC SEND (\S+) (\S+)$/) {
824 my ($nick,$file) = ($1,$2);
825 if (exists $dcc{'SEND'}{$who}) {
826 &msg($nick, "DCC already active.");
828 &DEBUG("shm: dcc sending $2 to $1.");
829 $conn->new_send($1,$2);
830 $dcc{'SEND'}{$who} = time();
832 } elsif (/^SET FORKPID (\S+) (\S+)/) {
833 $forked{$1}{PID} = $2;
834 } elsif (/^DELETE FORK (\S+)$/) {
836 } elsif (/^EVAL (.*)$/) {
837 &DEBUG("evaling '$1'.");
840 &DEBUG("shm: unknown msg. ($_)");
844 &shmWrite($shm,"") if ($shmmsg ne "");
847 ### this is semi-scheduled
850 &ScheduleThis(30, "getNickInUse");
851 return if ($_[0] eq "2"); # defer.
853 delete $sched{"getNickInUse"}{RUNNING};
856 if ($ident eq $param{'ircNick'}) {
857 &status("okay, got my nick back.");
861 &status("Trying to get my nick back.");
862 &nick( $param{'ircNick'} );
866 return unless &IsChanConf("uptime");
869 &ScheduleThis(60, "uptimeLoop");
870 return if ($_[0] eq "2"); # defer.
872 delete $sched{"uptimeLoop"}{RUNNING};
881 &ScheduleThis(60, "slashdotLoop");
882 return if ($_[0] eq "2");
884 delete $sched{"slashdotLoop"}{RUNNING};
887 my @chans = &ChanConfList("slashdotAnnounce");
888 return unless (scalar @chans);
890 &Forker("slashdot", sub {
891 my $line = &Slashdot::slashdotAnnounce();
892 return unless (defined $line);
895 next unless (&::validChan($_));
897 &::status("sending slashdot update to $_.");
898 ¬ice($_, "Slashdot: $line");
905 &ScheduleThis(60, "freshmeatLoop");
906 return if ($_[0] eq "2");
908 delete $sched{"freshmeatLoop"}{RUNNING};
911 my @chans = &ChanConfList("freshmeatAnnounce");
912 return unless (scalar @chans);
914 &Forker("freshmeat", sub {
915 my $data = &Freshmeat::freshmeatAnnounce();
918 next unless (&::validChan($_));
920 &::status("sending freshmeat update to $_.");
928 &ScheduleThis(240, "kernelLoop");
929 return if ($_[0] eq "2");
931 delete $sched{"kernelLoop"}{RUNNING};
934 my @chans = &ChanConfList("kernelAnnounce");
935 return unless (scalar @chans);
937 &Forker("kernel", sub {
938 my @data = &Kernel::kernelAnnounce();
941 next unless (&::validChan($_));
943 &::status("sending kernel update to $_.");
946 ¬ice($c, "Kernel: $_");
953 return unless &IsChanConf("wingate");
955 ### FILE CACHE OF OFFENDING WINGATES.
956 foreach (grep /^$host$/, @wingateBad) {
957 &status("Wingate: RUNNING ON $host BY $who");
958 &ban("*!*\@$host", "") if &IsChanConf("wingateBan");
960 my $reason = &getChanConf("wingateKick");
962 next unless ($reason);
963 &kick($who, "", $reason)
966 ### RUN CACHE OF TRIED WINGATES.
967 if (grep /^$host$/, @wingateCache) {
968 push(@wingateNow, $host); # per run.
969 push(@wingateCache, $host); # cache per run.
971 &DEBUG("Already scanned $host. good.");
974 my $interval = &getChanConfDefault("wingateInterval", 60); # seconds.
975 return if (defined $forked{'wingate'});
976 return if (time() - $wingaterun <= $interval);
977 return unless (scalar(keys %wingateToDo));
979 $wingaterun = time();
981 &Forker("wingate", sub { &Wingate::Wingates(keys %wingateToDo); } );
986 sub wingateWriteFile {
988 &ScheduleThis(60, "wingateWriteFile");
989 return if ($_[0] eq "2"); # defer.
991 delete $sched{"wingateWriteFile"}{RUNNING};
994 return unless (scalar @wingateCache);
996 my $file = "$bot_base_dir/$param{'ircUser'}.wingate";
997 if ($bot_pid != $$) {
998 &DEBUG("wingateWriteFile: Reorganising!");
1003 push(@wingateNow, $_);
1008 my %hash = map { $_ => 1 } @wingateNow;
1009 @wingateNow = sort keys %hash;
1012 &DEBUG("wingateWF: writing...");
1013 open(OUT, ">$file");
1014 foreach (@wingateNow) {
1023 &ScheduleThis(1440, "factoidCheck");
1024 return if ($_[0] eq "2"); # defer.
1026 delete $sched{"factoidCheck"}{RUNNING};
1029 my @list = &searchTable("factoids", "factoid_key", "factoid_key", " #DEL#");
1030 my $stale = &getChanConfDefault("factoidDeleteDelay", 30) *60*60*24;
1034 my $age = &getFactInfo($_, "modified_time");
1035 if (!defined $age or $age !~ /^\d+$/) {
1036 &WARN("age == NULL or not numeric.");
1040 next unless ($time - $age > $stale);
1043 $fix =~ s/ #DEL#$//g;
1044 &DEBUG("safedel: Removing $fix ($_) for good.");
1052 return unless (scalar keys %{ $dcc{CHAT} });
1055 &ScheduleThis(10, "dccStatus");
1056 return if ($_[0] eq "2"); # defer.
1058 delete $sched{"dccStatus"}{RUNNING};
1061 my $time = strftime("%H:%M", localtime(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);
1086 foreach (keys %{ $irc->{_queue} }) {
1090 foreach (keys %sched) {
1091 next unless ($q eq $sched{$_});
1096 my $time = $irc->{_queue}->{$q}->[0] - time();
1098 if (defined $sched) {
1099 &DEBUG(" $sched($q): ".&Time2String($time) );
1101 &DEBUG(" NULL($q): ".&Time2String($time) );
1105 &DEBUG("end of sList.");
1108 sub getChanConfDefault {
1109 my($what, $default, $chan) = @_;
1111 if (exists $param{$what}) {
1112 if (!exists $cache{config}{$what}) {
1113 &status("conf: backward-compat: found param{$what} ($param{$what}) instead.");
1114 $cache{config}{$what} = 1;
1117 return $param{$what};
1120 my $val = &getChanConf($what, $chan);
1125 $param{$what} = $default;
1126 &status("conf: auto-setting param{$what} = $default");
1127 $cache{config}{$what} = 1;
1133 my($file, $time) = @_;
1137 &WARN("mkB: file $file don't exist.");
1141 if ( -e "$file~" ) {
1142 $backup++ if ((stat $file)[9] - (stat "$file~")[9] > $time);
1146 return unless ($backup);
1148 ### TODO: do internal copying.
1149 &status("Backup: $file to $file~");
1150 CORE::system("/bin/cp $file $file~");