]> git.donarmstrong.com Git - infobot.git/blob - src/IRC/Schedulers.pl
- added dccStatus
[infobot.git] / src / IRC / Schedulers.pl
1 #
2 # ProcessExtra.pl: Extensions to Process.pl
3 #          Author: dms
4 #         Version: v0.5 (20010124)
5 #         Created: 20000117
6 #
7
8 if (&IsParam("useStrict")) { use strict; }
9
10 use POSIX qw(strftime);
11 use vars qw(%sched);
12
13 #
14 # is there a point in ScheduleChecked()?
15 # not yet... not unless we run setupSchedulers more than once.
16 #
17
18 sub setupSchedulers {
19     &VERB("Starting schedulers...",2);
20
21     # ONCE OFF.
22
23     # REPETITIVE.
24     &uptimeCycle(1);
25     &randomQuote(2);
26     &randomFactoid(2);
27     &randomFreshmeat(2);
28     &logCycle(1);
29     &chanlimitCheck(1);
30     &netsplitCheck(1);  # mandatory
31     &floodCycle(1);     # mandatory
32     &seenFlush(1);
33     &leakCheck(1);      # mandatory
34     &ignoreCheck(1);    # mandatory
35     &seenFlushOld(1);
36     &ircCheck(1);       # mandatory
37     &miscCheck(1);      # mandatory
38     &shmFlush(1);       # mandatory
39     &slashdotCycle(2);
40     &freshmeatCycle(2);
41     &kernelCycle(2);
42     &wingateWriteFile(1);
43     &factoidCheck(1);
44
45 #    my $count = map { exists $sched{$_}{RUNNING} } keys %sched;
46     my $count   = 0;
47     foreach (keys %sched) {
48         next unless (exists $sched{$_}{RUNNING});
49         $count++;
50     }
51
52     &status("Schedulers: $count will be running.");
53 ###    &scheduleList();
54 }
55
56 sub ScheduleThis {
57     my ($interval, $codename, @args) = @_;
58     my $waittime = &getRandomInt($interval);
59
60     if (!defined $waittime) {
61         &WARN("interval == waittime == UNDEF for $codename.");
62         return;
63     }
64
65     if (exists $sched{$codename}) {
66         &WARN("Sched for $codename already exists.");
67         return;
68     }
69
70     &VERB("Scheduling \&$codename() for ".&Time2String($waittime),3);
71     my $retval = $conn->schedule($waittime, \&$codename, @args);
72     $sched{$codename}{LABEL}    = $retval;
73     $sched{$codename}{TIME}     = time()+$waittime;
74     $sched{$codename}{RUNNING}  = 1;
75 }
76
77 sub ScheduleChecked {
78     my ($codename) = shift;
79
80     # what the hell is this for?
81     if (exists $sched{$codename}{RUNNING}) {
82         &DEBUG("SC: Removed $codename.");
83         delete $sched{$codename}{RUNNING};
84     } else {
85 ###     &WARN("sched $codename already removed.");
86     }
87 }
88
89 ####
90 #### LET THE FUN BEGIN.
91 ####
92
93 sub randomQuote {
94     my $interval = $param{'randomQuoteInterval'} || 60;
95     &ScheduleThis($interval, "randomQuote") if (@_);
96     return if ($_[0] eq "2");   # defer.
97     &ScheduleChecked("randomQuote");
98
99     my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.randtext");
100     if (!defined $line) {
101         &ERROR("random Quote: weird error?");
102         return;
103     }
104
105     foreach ( &ChanConfList("randomQuote") ) {
106         next unless (&validChan($_));   # ???
107
108         &status("sending random Quote to $_.");
109         &action($_, "Ponders: ".$line);
110     }
111     ### TODO: if there were no channels, don't reschedule until channel
112     ###         configuration is modified.
113 }
114
115 sub randomFactoid {
116     my ($key,$val);
117     my $error = 0;
118
119     my $interval = $param{'randomFactoidInterval'} || 60; # FIXME.
120     &ScheduleThis($interval, "randomFactoid") if (@_);
121     return if ($_[0] eq "2");   # defer.
122     &ScheduleChecked("randomFactoid");
123
124     while (1) {
125         ($key,$val) = &randKey("factoids","factoid_key,factoid_value");
126 ###     $val =~ tr/^[A-Z]/[a-z]/;       # blah is Good => blah is good.
127         last if ($val !~ /^</);
128         $error++;
129         if ($error == 5) {
130             &ERROR("rF: tried 5 times but failed.");
131             return;
132         }
133     }
134
135     foreach ( &ChanConfList("randomFactoid") ) {
136         next unless (&validChan($_));   # ???
137
138         &status("sending random Factoid to $_.");
139         &action($_, "Thinks: \037$key\037 is $val");
140         ### FIXME: Use &getReply() on above to format factoid properly?
141         $good++;
142     }
143 }
144
145 sub randomFreshmeat {
146     my $interval = $param{'randomFresheatInterval'} || 60;
147     &ScheduleThis($interval, "randomFreshmeat") if (@_);
148     return if ($_[0] eq "2");   # defer.
149     &ScheduleChecked("randomFreshmeat");
150
151     my @chans = &ChanConfList("randomFreshmeat");
152     return unless (scalar @chans);
153
154     &Forker("freshmeat", sub {
155         my $retval = &Freshmeat::randPackage();
156
157         foreach (@chans) {
158             next unless (&validChan($_));       # ???
159
160             &status("sending random Freshmeat to $_.");
161             &say($_, $line);
162         }
163     } );
164 }
165
166 sub logCycle {
167     if (@_) {
168         &ScheduleThis(60, "logCycle");
169         return if ($_[0] eq "2");       # defer.
170         &ScheduleChecked("logCycle");
171     }
172
173     return unless (defined fileno LOG);
174     return unless (&IsParam("logfile"));
175     return unless (&IsParam("maxLogSize"));
176
177     ### check if current size is too large.
178     if ( -s $file{log} > $param{'maxLogSize'}) {
179         my $date = sprintf("%04d%02d%02d", (localtime)[5,4,3]);
180         $file{log} = $param{'logfile'} ."-". $date;
181         &status("cycling log file.");
182
183         if ( -e $file{log}) {
184             my $i = 1;
185             my $newlog;
186             while () {
187                 $newlog = $file{log}."-".$i;
188                 last if (! -e $newlog);
189                 $i++;
190             }
191             $file{log} = $newlog;
192         }
193
194         &closeLog();
195         system("/bin/mv '$param{'logfile'}' '$file{log}'");
196         &compress($file{log});
197         &openLog();
198         &status("cycling log file.");
199     }
200
201     ### check if all the logs exceed size.
202     my $logdir = "$bot_base_dir/log/";
203     if (opendir(LOGS, $logdir)) {
204         my $tsize = 0;
205         my (%age, %size);
206
207         while (defined($_ = readdir LOGS)) {
208             my $logfile         = "$logdir/$_";
209
210             next unless ( -f $logfile);
211             my $size            = -s $logfile;
212             my $age             = (stat $logfile)[9];
213
214             $age{$age}          = $logfile;
215             $size{$logfile}     = $size;
216
217             $tsize              += $size;
218         }
219         closedir LOGS;
220
221         my $delete      = 0;
222         while ($tsize > $param{'maxLogSize'}) {
223             &status("LOG: current size > max ($tsize > $param{'maxLogSize'})");
224             my $oldest  = (sort {$a <=> $b} keys %age)[0];
225             &status("LOG: unlinking $age{$oldest}.");
226             unlink $age{$oldest};
227             $tsize      -= $oldest;
228             $delete++;
229         }
230
231         ### TODO: add how many b,kb,mb removed?
232         &status("LOG: removed $delete logs.") if ($delete);
233     } else {
234         &WARN("could not open dir $logdir");
235     }
236
237 }
238
239 sub seenFlushOld {
240     if (@_) {
241         &ScheduleThis(1440, "seenFlushOld");
242         return if ($_[0] eq "2");       # defer.
243         &ScheduleChecked("seenFlushOld");
244     }
245
246     # is this global-only?
247     return unless (&IsChanConf("seen") > 0);
248     return unless (&IsChanConf("seenFlushInterval") > 0);
249
250     my $max_time = ($chanconf{_default}{'seenMaxDays'} || 30)
251                                 *60*60*24; # global.
252     my $delete   = 0;
253
254     if ($param{'DBType'} =~ /^pg|postgres|mysql/i) {
255         my $query = "SELECT nick,time FROM seen GROUP BY nick HAVING UNIX_TIMESTAMP() - time > $max_time";
256         my $sth = $dbh->prepare($query);
257         $sth->execute;
258
259         while (my @row = $sth->fetchrow_array) {
260             my ($nick,$time) = @row;
261
262             &dbDel("seen","nick",$nick);
263             $delete++;
264         }
265         $sth->finish;
266     } elsif ($param{'DBType'} =~ /^dbm/i) {
267         my $time = time();
268
269         foreach (keys %seen) {
270             my $delta_time = $time - &dbGet("seen", "NULL", $_, "time");
271             next unless ($delta_time > $max_time);
272
273             &DEBUG("seenFlushOld: ".&Time2String($delta_time) );
274             delete $seen{$_};
275             $delete++;
276         }
277     } else {
278         &FIXME("seenFlushOld: for PG/NO-DB.");
279     }
280     &VERB("SEEN deleted $delete seen entries.",2);
281
282 }
283
284 sub chanlimitCheck {
285     if (@_) {
286         my $interval = &getChanConf("chanlimitcheckInterval") || 10;
287         &ScheduleThis($interval, "chanlimitCheck");
288         return if ($_[0] eq "2");
289         &ScheduleChecked("chanlimitCheck");
290     }
291
292     foreach ( &ChanConfList("chanlimitcheck") ) {
293         next unless (&validChan($_));   # ???
294
295         my $limitplus   = &getChanConf("chanlimitcheckPlus",$_) || 5;
296         my $newlimit    = scalar(keys %{$channels{$_}{''}}) + $limitplus;
297         my $limit       = $channels{$_}{'l'};
298
299         if (scalar keys %{$channels{$_}{''}} > $limit) {
300             &status("LIMIT: set too low!!! FIXME");
301             ### run NAMES again and flush it.
302         }
303
304         next unless (!defined $limit or $limit != $newlimit);
305
306         if (!exists $channels{$_}{'o'}{$ident}) {
307             &ERROR("chanlimitcheck: dont have ops on $_.");
308             next;
309         }
310         &rawout("MODE $_ +l $newlimit");
311     }
312
313 }
314
315 sub netsplitCheck {
316     my ($s1,$s2);
317
318     if (@_) {
319         &ScheduleThis(30, "netsplitCheck");
320         return if ($_[0] eq "2");
321         &ScheduleChecked("netsplitCheck");
322     }
323
324     foreach $s1 (keys %netsplitservers) {
325         foreach $s2 (keys %{$netsplitservers{$s1}}) {
326             if (time() - $netsplitservers{$s1}{$s2} > 3600) {
327                 &status("netsplit between $s1 and $s2 appears to be stale.");
328                 delete $netsplitservers{$s1}{$s2};
329             }
330         }
331     }
332
333     # %netsplit hash checker.
334     foreach (keys %netsplit) {
335         if (&IsNickInAnyChan($_)) {
336             &DEBUG("netsplitC: $_ is in some chan; removing from netsplit list.");
337             delete $netsplit{$_};
338         }
339         next unless (time() - $netsplit{$_} > 60*60*2); # 2 hours.
340         next if (&IsNickInAnyChan($_));
341
342         &DEBUG("netsplitC: $_ didn't come back from netsplit in 2 hours; removing from netsplit list.");
343         delete $netsplit{$_};
344     }
345 }
346
347 sub floodCycle {
348     my $delete   = 0;
349     my $who;
350
351     if (@_) {
352         &ScheduleThis(60, "floodCycle");        # minutes.
353         return if ($_[0] eq "2");
354         &ScheduleChecked("floodCycle");
355     }
356
357     my $time    = time();
358     foreach $who (keys %flood) {
359         foreach (keys %{$flood{$who}}) {
360             if ($time - $flood{$who}{$_} > $interval) {
361                 delete $flood{$who}{$_};
362                 $delete++;
363             }
364         }
365     }
366     &VERB("floodCycle: deleted $delete items.",2);
367
368 }
369
370 sub seenFlush {
371     my %stats;
372     my $nick;
373     my $flushed         = 0;
374     $stats{'count_old'} = &countKeys("seen");
375     $stats{'new'}       = 0;
376     $stats{'old'}       = 0;
377
378     if (@_) {
379         my $interval = $param{'seenFlushInterval'} || 60;
380         &ScheduleThis($interval, "seenFlush");
381         return if ($_[0] eq "2");
382         &ScheduleChecked("seenFlush");
383     }
384
385     if ($param{'DBType'} =~ /^mysql|pg|postgres/i) {
386         foreach $nick (keys %seencache) {
387             my $exists = &dbGet("seen","nick", $nick, "nick");
388
389             if (defined $exists and $exists) {
390                 &dbUpdate("seen", "nick", $nick, (
391                         "time" => $seencache{$nick}{'time'},
392                         "host" => $seencache{$nick}{'host'},
393                         "channel" => $seencache{$nick}{'chan'},
394                         "message" => $seencache{$nick}{'msg'},
395                 ) );
396                 $stats{'old'}++;
397             } else {
398                 my $retval = &dbInsert("seen", $nick, (
399                         "nick" => $seencache{$nick}{'nick'},
400                         "time" => $seencache{$nick}{'time'},
401                         "host" => $seencache{$nick}{'host'},
402                         "channel" => $seencache{$nick}{'chan'},
403                         "message" => $seencache{$nick}{'msg'},
404                 ) );
405                 $stats{'new'}++;
406
407                 ### TODO: put bad nick into a list and don't do it again!
408                 &FIXME("Should never happen! (nick => $nick)") if !$retval;
409             }
410
411             delete $seencache{$nick};
412             $flushed++;
413         }
414
415     } elsif ($param{'DBType'} =~ /^dbm/i) {
416
417         foreach $nick (keys %seencache) {
418             my $retval = &dbInsert("seen", $nick, (
419                 "nick" => $seencache{$nick}{'nick'},
420                 "time" => $seencache{$nick}{'time'},
421                 "host" => $seencache{$nick}{'host'},
422                 "channel" => $seencache{$nick}{'chan'},
423                 "message" => $seencache{$nick}{'msg'},
424             ) );
425
426             ### TODO: put bad nick into a list and don't do it again!
427             &FIXME("Should never happen! (nick => $nick)") if !$retval;
428
429             delete $seencache{$nick};
430             $flushed++;
431         }
432     } else {
433         &DEBUG("seenFlush: NO VALID FACTOID SUPPORT?");
434     }
435
436     &status("Flushed $flushed seen entries.")           if ($flushed);
437     &VERB(sprintf("  new seen: %03.01f%% (%d/%d)",
438         $stats{'new'}*100/$stats{'count_old'},
439         $stats{'new'}, $stats{'count_old'} ), 2)        if ($stats{'new'});
440     &VERB(sprintf("  now seen: %3.1f%% (%d/%d)",
441         $stats{'old'}*100/&countKeys("seen"),
442         $stats{'old'}, &countKeys("seen") ), 2)         if ($stats{'old'});
443
444     &WARN("scalar keys seenflush != 0!")        if (scalar keys %seenflush);
445
446 }
447
448 sub leakCheck {
449     my ($blah1,$blah2);
450     my $count = 0;
451
452     if (@_) {
453         &ScheduleThis(60, "leakCheck");
454         return if ($_[0] eq "2");
455         &ScheduleChecked("leakCheck");
456     }
457
458     # flood.
459     foreach $blah1 (keys %flood) {
460         foreach $blah2 (keys %{$flood{$blah1}}) {
461             $count += scalar(keys %{$flood{$blah1}{$blah2}});
462         }
463     }
464     &VERB("\%flood has $count total keys.",2);
465
466     my $chan;
467     foreach $chan (grep /[A-Z]/, keys %channels) {
468         &DEBUG("leak: chan => '$chan'.");
469         my ($i,$j);
470         foreach $i (keys %{$channels{$chan}}) {
471             foreach (keys %{$channels{$chan}{$i}}) {
472                 &DEBUG("leak:   \$channels{$chan}{$i}{$_} ...");
473             }
474         }
475     }
476 }
477
478 sub ignoreCheck {
479     my $time    = time();
480     my $count   = 0;
481
482     foreach (keys %ignore) {
483         my $chan = $_;
484
485         foreach (keys %{ $ignore{$chan} }) {
486             my @array = $ignore{$chan}{$_};
487
488             foreach (@array) {
489                 &DEBUG("  => $_");
490             }
491
492             next;
493             next unless ($time > $ignore{$_});
494
495             delete $ignore{$chan}{$_};
496             &status("ignore: $_/$chan has expired.");
497             $count++;
498         }
499     }
500     &VERB("ignore: $count items deleted.",2);
501
502     &ScheduleThis(60, "ignoreCheck") if (@_);
503 }
504
505 sub ircCheck {
506     my @array = grep !/^_default$/, keys %chanconf;
507     my $iconf = scalar(@array);
508     my $inow  = scalar(keys %channels);
509     if ($iconf > 2 and $inow * 2 <= $iconf) {
510         &FIXME("ircCheck: current channels * 2 <= config channels. FIXME.");
511     }
512
513     if (!$conn->connected and time - $msgtime > 3600) {
514         &WARN("ircCheck: no msg for 3600 and disco'd! reconnecting!");
515         $msgtime = time();      # just in case.
516         &ircloop();
517     }
518
519     if ($ident !~ /^\Q$param{ircNick}\E$/) {
520         &WARN("ircCheck: ident($ident) != param{ircNick}($param{IrcNick}).");
521     }
522
523     &joinNextChan();
524         # if scalar @joinnext => join more channels
525         # else check for chanserv.
526
527     if (grep /^\s*$/, keys %channels) {
528         &WARN("we have a NULL chan in hash channels? removing!");
529         delete $channels{''};
530         &status("channels now:");
531         foreach (keys %channels) {
532             &status("  $_");
533         }
534     }
535
536     ### USER FILE.
537     if ($utime_userfile > $wtime_userfile and time() - $wtime_userfile > 3600) {
538         &writeUserFile();
539         $wtime_userfile = time();
540     }
541     ### CHAN FILE.
542     if ($utime_chanfile > $wtime_chanfile and time() - $wtime_chanfile > 3600) {
543         &writeChanFile();
544         $wtime_chanfile = time();
545     }
546
547     &ScheduleThis(240, "ircCheck") if (@_);
548 }
549
550 sub miscCheck {
551     # SHM check.
552     my @ipcs;
553     if ( -x "/usr/bin/ipcs") {
554         @ipcs = `/usr/bin/ipcs`;
555     } else {
556         &WARN("ircCheck: no 'ipcs' binary.");
557     }
558
559     # shmid stale remove.
560     foreach (@ipcs) {
561         chop;
562
563         # key, shmid, owner, perms, bytes, nattch
564         next unless (/^(0x\d+) (\d+)\s+(\S+)\s+(\d+)\s+(\d+)\s+/);
565
566         my ($shmid, $size) = ($2,$5);
567         next unless ($shmid != $shm and $size == 2000);
568
569         &status("SHM: nuking shmid $shmid");
570         system("/usr/bin/ipcrm shm $shmid >/dev/null");
571     }
572
573     ### check modules if they've been modified. might be evil.
574     &reloadAllModules();
575
576     &ScheduleThis(240, "miscCheck") if (@_);
577 }
578
579 sub shmFlush {
580     if (@_) {
581         &ScheduleThis(5, "shmFlush");
582         return if ($_[0] eq "2");
583         &ScheduleChecked("shmFlush");
584     }
585
586     my $shmmsg = &shmRead($shm);
587     $shmmsg =~ s/\0//g;         # remove padded \0's.
588
589     return if ($$ != $::bot_pid); # fork protection.
590
591     foreach (split '\|\|', $shmmsg) {
592         &VERB("shm: Processing '$_'.",2);
593
594         if (/^DCC SEND (\S+) (\S+)$/) {
595             my ($nick,$file) = ($1,$2);
596             if (exists $dcc{'SEND'}{$who}) {
597                 &msg($nick,"DCC already active.");
598             } else {
599                 &DEBUG("shm: dcc sending $2 to $1.");
600                 $conn->new_send($1,$2);
601                 $dcc{'SEND'}{$who} = time();
602             }
603         } elsif (/^SET FORKPID (\S+) (\S+)/) {
604             $forked{$1}{PID} = $2;
605         } elsif (/^DELETE FORK (\S+)$/) {
606             delete $forked{$1};
607         } elsif (/^EVAL (.*)$/) {
608             &DEBUG("evaling '$1'.");
609             eval $1;
610         } else {
611             &DEBUG("shm: unknown msg. ($_)");
612         }
613     }
614
615     &shmWrite($shm,"") if ($shmmsg ne "");
616 }
617
618 ### this is semi-scheduled
619 sub getNickInUse {
620     if ($ident eq $param{'ircNick'}) {
621         &status("okay, got my nick back.");
622         return;
623     }
624
625     &status("Trying to get my nick back.");
626     &nick($param{'ircNick'});
627
628     &ScheduleThis(5, "getNickInUse") if (@_);
629 }
630
631 sub uptimeCycle {
632     &uptimeWriteFile();
633
634     &ScheduleThis(60, "uptimeCycle") if (@_);
635 }
636
637 sub slashdotCycle {
638     &ScheduleThis(60, "slashdotCycle") if (@_);
639     return if ($_[0] eq "2");
640     &ScheduleChecked("slashdotCycle");
641
642     my @chans = &ChanConfList("slashdotAnnounce");
643     return unless (scalar @chans);
644
645     &Forker("slashdot", sub {
646         my @data = &Slashdot::slashdotAnnounce();
647
648         foreach (@chans) {
649             next unless (&::validChan($_));
650
651             &::status("sending slashdot update to $_.");
652             my $c = $_;
653             foreach (@data) {
654                 &notice($c, "Slashdot: $_");
655             }
656         }
657     } );
658 }
659
660 sub freshmeatCycle {
661     &ScheduleThis(60, "freshmeatCycle") if (@_);
662     return if ($_[0] eq "2");
663     &ScheduleChecked("freshmeatCycle");
664
665     my @chans = &ChanConfList("freshmeatAnnounce");
666     return unless (scalar @chans);
667
668     &Forker("freshmeat", sub {
669         my $data = &Freshmeat::freshmeatAnnounce();
670
671         foreach (@chans) {
672             next unless (&::validChan($_));
673
674             &::status("sending freshmeat update to $_.");
675             &msg($_, $data);
676         }
677     } );
678 }
679
680 sub kernelCycle {
681     &ScheduleThis(240, "kernelCycle") if (@_);
682     return if ($_[0] eq "2");
683     &ScheduleChecked("kernelCycle");
684
685     my @chans = &ChanConfList("kernelAnnounce");
686     return unless (scalar @chans);
687
688     &Forker("kernel", sub {
689         my @data = &Kernel::kernelAnnounce();
690
691         foreach (@chans) {
692             next unless (&::validChan($_));
693
694             &::status("sending kernel update to $_.");
695             my $c = $_;
696             foreach (@data) {
697                 &notice($c, "Kernel: $_");
698             }
699         }
700     } );
701 }
702
703 sub wingateCheck {
704     return unless &IsParam("wingate");
705     return unless ($param{'wingate'} =~ /^(.*\s+)?$chan(\s+.*)?$/i);
706
707     ### FILE CACHE OF OFFENDING WINGATES.
708     foreach (grep /^$host$/, @wingateBad) {
709         &status("Wingate: RUNNING ON $host BY $who");
710         &ban("*!*\@$host", "") if &IsParam("wingateBan");
711
712         next unless (&IsParam("wingateKick"));
713         &kick($who, "", $param{'wingateKick'})
714     }
715
716     ### RUN CACHE OF TRIED WINGATES.
717     if (grep /^$host$/, @wingateCache) {
718         push(@wingateNow, $host);       # per run.
719         push(@wingateCache, $host);     # cache per run.
720     } else {
721         &DEBUG("Already scanned $host. good.");
722     }
723
724     my $interval = $param{'wingateInterval'} || 60;     # seconds.
725     return if (defined $forked{'wingate'});
726     return if (time() - $wingaterun <= $interval);
727     return unless (scalar(keys %wingateToDo));
728
729     $wingaterun = time();
730
731     &Forker("wingate", sub { &Wingate::Wingates(keys %wingateToDo); } );
732     undef @wingateNow;
733 }
734
735 ### TODO.
736 sub wingateWriteFile {
737     return unless (scalar @wingateCache);
738
739     my $file = "$bot_base_dir/$param{'ircUser'}.wingate";
740     if ($bot_pid != $$) {
741         &DEBUG("wingateWriteFile: Reorganising!");
742
743         open(IN, $file);
744         while (<IN>) {
745             chop;
746             push(@wingateNow, $_);
747         }
748         close IN;
749
750         # very lame hack.
751         my %hash = map { $_ => 1 } @wingateNow;
752         @wingateNow = sort keys %hash;
753     }
754
755     &DEBUG("wingateWF: writing...");
756     open(OUT, ">$file");
757     foreach (@wingateNow) {
758         print OUT "$_\n";
759     }
760     close OUT;
761
762     &ScheduleThis(60, "wingateWriteFile") if (@_);
763 }
764
765 sub factoidCheck {
766     my @list = &searchTable("factoids", "factoid_key", "factoid_key", " #DEL#");
767     my $stale = $param{'factoidDeleteDelay'}*60*60*24;
768
769     foreach (@list) {
770         my $age = &getFactInfo($_, "modified_time");    
771         next unless (time() - $age > $stale);
772
773         my $fix = $_;
774         $fix =~ s/ #DEL#$//g;
775         &VERB("safedel: Removing $fix for good.",2);
776         &delFactoid($_);
777     }
778
779     &ScheduleThis(1440, "factoidCheck") if (@_);
780 }
781
782 sub dccStatus {
783     my $time = strftime("%H:%M", localtime(time()) );
784
785     return unless (scalar keys %{ $DCC{CHAT} });
786
787     foreach (keys %channels) {
788         &DCCBroadcast("[$time] $_: $users members ($chops chops), $bans bans","+o");
789     }
790
791     &ScheduleThis(10, "dccStatus") if (@_);
792 }
793
794 sub schedulerSTUB {
795
796     &ScheduleThis(TIME_IN_MINUTES, "FUNCTION") if (@_);
797 }
798
799 sub scheduleList {
800     ###
801     # custom:
802     #   a - time == now.
803     #   b - weird time.
804     ###
805
806     &DEBUG("sched:");
807     foreach (keys %{ $irc->{_queue} }) {
808         my $q = $_;
809
810         my $sched;
811         foreach (keys %sched) {
812             next unless ($q eq $sched{$_});
813             $sched = $_;
814             last;
815         }
816
817         my $time = $irc->{_queue}->{$q}->[0] - time();
818
819         if (defined $sched) {
820             &DEBUG("   $sched($q): ".&Time2String($time) );
821         } else {
822             &DEBUG("   NULL($q): ".&Time2String($time) );
823         }
824     }
825
826     &DEBUG("end of sList.");
827 }
828
829 1;