]> git.donarmstrong.com Git - infobot.git/blob - src/IRC/Schedulers.pl
c1d49758600466863e781fd671d7b23714c2335b
[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     &ScheduleChecked("randomQuote");
97     return if ($_[0] eq "2");   # defer.
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     &ScheduleChecked("randomFactoid");
122     return if ($_[0] eq "2");   # defer.
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     &ScheduleChecked("randomFreshmeat");
149     return if ($_[0] eq "2");   # defer.
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         &ScheduleChecked("logCycle");
170         return if ($_[0] eq "2");       # defer.
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         &ScheduleChecked("seenFlushOld");
243         return if ($_[0] eq "2");       # defer.
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         &ScheduleChecked("chanlimitCheck");
289         return if ($_[0] eq "2");
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         &ScheduleChecked("netsplitCheck");
321         return if ($_[0] eq "2");
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         &ScheduleChecked("floodCycle");
354         return if ($_[0] eq "2");
355     }
356
357     my $time    = time();
358     foreach $who (keys %flood) {
359         foreach (keys %{$flood{$who}}) {
360             if (!exists $flood{$who}{$_} or defined $flood{$who}{$_}) {
361                 &WARN("flood{$who}{$_} undefined?");
362                 next;
363             }
364
365             if ($time - $flood{$who}{$_} > $interval) {
366                 delete $flood{$who}{$_};
367                 $delete++;
368             }
369         }
370     }
371     &VERB("floodCycle: deleted $delete items.",2);
372
373 }
374
375 sub seenFlush {
376     my %stats;
377     my $nick;
378     my $flushed         = 0;
379     $stats{'count_old'} = &countKeys("seen");
380     $stats{'new'}       = 0;
381     $stats{'old'}       = 0;
382
383     if (@_) {
384         my $interval = $param{'seenFlushInterval'} || 60;
385         &ScheduleThis($interval, "seenFlush");
386         &ScheduleChecked("seenFlush");
387         return if ($_[0] eq "2");
388     }
389
390     if ($param{'DBType'} =~ /^mysql|pg|postgres/i) {
391         foreach $nick (keys %seencache) {
392             my $exists = &dbGet("seen","nick", $nick, "nick");
393
394             if (defined $exists and $exists) {
395                 &dbUpdate("seen", "nick", $nick, (
396                         "time" => $seencache{$nick}{'time'},
397                         "host" => $seencache{$nick}{'host'},
398                         "channel" => $seencache{$nick}{'chan'},
399                         "message" => $seencache{$nick}{'msg'},
400                 ) );
401                 $stats{'old'}++;
402             } else {
403                 my $retval = &dbInsert("seen", $nick, (
404                         "nick" => $seencache{$nick}{'nick'},
405                         "time" => $seencache{$nick}{'time'},
406                         "host" => $seencache{$nick}{'host'},
407                         "channel" => $seencache{$nick}{'chan'},
408                         "message" => $seencache{$nick}{'msg'},
409                 ) );
410                 $stats{'new'}++;
411
412                 ### TODO: put bad nick into a list and don't do it again!
413                 &FIXME("Should never happen! (nick => $nick)") if !$retval;
414             }
415
416             delete $seencache{$nick};
417             $flushed++;
418         }
419
420     } elsif ($param{'DBType'} =~ /^dbm/i) {
421
422         foreach $nick (keys %seencache) {
423             my $retval = &dbInsert("seen", $nick, (
424                 "nick" => $seencache{$nick}{'nick'},
425                 "time" => $seencache{$nick}{'time'},
426                 "host" => $seencache{$nick}{'host'},
427                 "channel" => $seencache{$nick}{'chan'},
428                 "message" => $seencache{$nick}{'msg'},
429             ) );
430
431             ### TODO: put bad nick into a list and don't do it again!
432             &FIXME("Should never happen! (nick => $nick)") if !$retval;
433
434             delete $seencache{$nick};
435             $flushed++;
436         }
437     } else {
438         &DEBUG("seenFlush: NO VALID FACTOID SUPPORT?");
439     }
440
441     &status("Flushed $flushed seen entries.")           if ($flushed);
442     &VERB(sprintf("  new seen: %03.01f%% (%d/%d)",
443         $stats{'new'}*100/$stats{'count_old'},
444         $stats{'new'}, $stats{'count_old'} ), 2)        if ($stats{'new'});
445     &VERB(sprintf("  now seen: %3.1f%% (%d/%d)",
446         $stats{'old'}*100/&countKeys("seen"),
447         $stats{'old'}, &countKeys("seen") ), 2)         if ($stats{'old'});
448
449     &WARN("scalar keys seenflush != 0!")        if (scalar keys %seenflush);
450
451 }
452
453 sub leakCheck {
454     my ($blah1,$blah2);
455     my $count = 0;
456
457     if (@_) {
458         &ScheduleThis(60, "leakCheck");
459         &ScheduleChecked("leakCheck");
460         return if ($_[0] eq "2");
461     }
462
463     # flood.
464     foreach $blah1 (keys %flood) {
465         foreach $blah2 (keys %{$flood{$blah1}}) {
466             $count += scalar(keys %{$flood{$blah1}{$blah2}});
467         }
468     }
469     &VERB("\%flood has $count total keys.",2);
470
471     my $chan;
472     foreach $chan (grep /[A-Z]/, keys %channels) {
473         &DEBUG("leak: chan => '$chan'.");
474         my ($i,$j);
475         foreach $i (keys %{$channels{$chan}}) {
476             foreach (keys %{$channels{$chan}{$i}}) {
477                 &DEBUG("leak:   \$channels{$chan}{$i}{$_} ...");
478             }
479         }
480     }
481 }
482
483 sub ignoreCheck {
484     my $time    = time();
485     my $count   = 0;
486
487     foreach (keys %ignore) {
488         my $chan = $_;
489
490         foreach (keys %{ $ignore{$chan} }) {
491             my @array = $ignore{$chan}{$_};
492
493             foreach (@array) {
494                 &DEBUG("  => $_");
495             }
496
497             next;
498             next unless ($time > $ignore{$_});
499
500             delete $ignore{$chan}{$_};
501             &status("ignore: $_/$chan has expired.");
502             $count++;
503         }
504     }
505     &VERB("ignore: $count items deleted.",2);
506
507     &ScheduleThis(60, "ignoreCheck") if (@_);
508 }
509
510 sub ircCheck {
511     my @array = grep !/^_default$/, keys %chanconf;
512     my $iconf = scalar(@array);
513     my $inow  = scalar(keys %channels);
514     if ($iconf > 2 and $inow * 2 <= $iconf) {
515         &FIXME("ircCheck: current channels * 2 <= config channels. FIXME.");
516     }
517
518     if (!$conn->connected and time - $msgtime > 3600) {
519         &WARN("ircCheck: no msg for 3600 and disco'd! reconnecting!");
520         $msgtime = time();      # just in case.
521         &ircloop();
522     }
523
524     if ($ident !~ /^\Q$param{ircNick}\E$/) {
525         &WARN("ircCheck: ident($ident) != param{ircNick}($param{IrcNick}).");
526     }
527
528     &joinNextChan();
529         # if scalar @joinnext => join more channels
530         # else check for chanserv.
531
532     if (grep /^\s*$/, keys %channels) {
533         &WARN("we have a NULL chan in hash channels? removing!");
534         delete $channels{''};
535         &status("channels now:");
536         foreach (keys %channels) {
537             &status("  $_");
538         }
539     }
540
541     ### USER FILE.
542     if ($utime_userfile > $wtime_userfile and time() - $wtime_userfile > 3600) {
543         &writeUserFile();
544         $wtime_userfile = time();
545     }
546     ### CHAN FILE.
547     if ($utime_chanfile > $wtime_chanfile and time() - $wtime_chanfile > 3600) {
548         &writeChanFile();
549         $wtime_chanfile = time();
550     }
551
552     &ScheduleThis(240, "ircCheck") if (@_);
553 }
554
555 sub miscCheck {
556     &ScheduleThis(240, "miscCheck") if (@_);
557
558     # SHM check.
559     my @ipcs;
560     if ( -x "/usr/bin/ipcs") {
561         @ipcs = `/usr/bin/ipcs`;
562     } else {
563         &WARN("ircCheck: no 'ipcs' binary.");
564         return;
565     }
566
567     # shmid stale remove.
568     foreach (@ipcs) {
569         chop;
570
571         # key, shmid, owner, perms, bytes, nattch
572         next unless (/^(0x\d+) (\d+)\s+(\S+)\s+(\d+)\s+(\d+)\s+/);
573
574         my ($shmid, $size) = ($2,$5);
575         next unless ($shmid != $shm and $size == 2000);
576
577 ###     &status("SHM: nuking shmid $shmid");
578 ###     system("/usr/bin/ipcrm shm $shmid >/dev/null");
579     }
580
581     ### check modules if they've been modified. might be evil.
582     &reloadAllModules();
583 }
584
585 sub shmFlush {
586     if (@_) {
587         &ScheduleThis(5, "shmFlush");
588         &ScheduleChecked("shmFlush");
589         return if ($_[0] eq "2");
590     }
591
592     my $shmmsg = &shmRead($shm);
593     $shmmsg =~ s/\0//g;         # remove padded \0's.
594
595     return if ($$ != $::bot_pid); # fork protection.
596
597     foreach (split '\|\|', $shmmsg) {
598         &VERB("shm: Processing '$_'.",2);
599
600         if (/^DCC SEND (\S+) (\S+)$/) {
601             my ($nick,$file) = ($1,$2);
602             if (exists $dcc{'SEND'}{$who}) {
603                 &msg($nick,"DCC already active.");
604             } else {
605                 &DEBUG("shm: dcc sending $2 to $1.");
606                 $conn->new_send($1,$2);
607                 $dcc{'SEND'}{$who} = time();
608             }
609         } elsif (/^SET FORKPID (\S+) (\S+)/) {
610             $forked{$1}{PID} = $2;
611         } elsif (/^DELETE FORK (\S+)$/) {
612             delete $forked{$1};
613         } elsif (/^EVAL (.*)$/) {
614             &DEBUG("evaling '$1'.");
615             eval $1;
616         } else {
617             &DEBUG("shm: unknown msg. ($_)");
618         }
619     }
620
621     &shmWrite($shm,"") if ($shmmsg ne "");
622 }
623
624 ### this is semi-scheduled
625 sub getNickInUse {
626     if ($ident eq $param{'ircNick'}) {
627         &status("okay, got my nick back.");
628         return;
629     }
630
631     &status("Trying to get my nick back.");
632     &nick($param{'ircNick'});
633
634     &ScheduleThis(5, "getNickInUse") if (@_);
635 }
636
637 sub uptimeCycle {
638     &uptimeWriteFile();
639
640     &ScheduleThis(60, "uptimeCycle") if (@_);
641 }
642
643 sub slashdotCycle {
644     &ScheduleThis(60, "slashdotCycle") if (@_);
645     &ScheduleChecked("slashdotCycle");
646     return if ($_[0] eq "2");
647
648     my @chans = &ChanConfList("slashdotAnnounce");
649     return unless (scalar @chans);
650
651     &Forker("slashdot", sub {
652         my @data = &Slashdot::slashdotAnnounce();
653
654         foreach (@chans) {
655             next unless (&::validChan($_));
656
657             &::status("sending slashdot update to $_.");
658             my $c = $_;
659             foreach (@data) {
660                 &notice($c, "Slashdot: $_");
661             }
662         }
663     } );
664 }
665
666 sub freshmeatCycle {
667     &ScheduleThis(60, "freshmeatCycle") if (@_);
668     &ScheduleChecked("freshmeatCycle");
669     return if ($_[0] eq "2");
670
671     my @chans = &ChanConfList("freshmeatAnnounce");
672     return unless (scalar @chans);
673
674     &Forker("freshmeat", sub {
675         my $data = &Freshmeat::freshmeatAnnounce();
676
677         foreach (@chans) {
678             next unless (&::validChan($_));
679
680             &::status("sending freshmeat update to $_.");
681             &msg($_, $data);
682         }
683     } );
684 }
685
686 sub kernelCycle {
687     &ScheduleThis(240, "kernelCycle") if (@_);
688     &ScheduleChecked("kernelCycle");
689     return if ($_[0] eq "2");
690
691     my @chans = &ChanConfList("kernelAnnounce");
692     return unless (scalar @chans);
693
694     &Forker("kernel", sub {
695         my @data = &Kernel::kernelAnnounce();
696
697         foreach (@chans) {
698             next unless (&::validChan($_));
699
700             &::status("sending kernel update to $_.");
701             my $c = $_;
702             foreach (@data) {
703                 &notice($c, "Kernel: $_");
704             }
705         }
706     } );
707 }
708
709 sub wingateCheck {
710     return unless &IsParam("wingate");
711     return unless ($param{'wingate'} =~ /^(.*\s+)?$chan(\s+.*)?$/i);
712
713     ### FILE CACHE OF OFFENDING WINGATES.
714     foreach (grep /^$host$/, @wingateBad) {
715         &status("Wingate: RUNNING ON $host BY $who");
716         &ban("*!*\@$host", "") if &IsParam("wingateBan");
717
718         next unless (&IsParam("wingateKick"));
719         &kick($who, "", $param{'wingateKick'})
720     }
721
722     ### RUN CACHE OF TRIED WINGATES.
723     if (grep /^$host$/, @wingateCache) {
724         push(@wingateNow, $host);       # per run.
725         push(@wingateCache, $host);     # cache per run.
726     } else {
727         &DEBUG("Already scanned $host. good.");
728     }
729
730     my $interval = $param{'wingateInterval'} || 60;     # seconds.
731     return if (defined $forked{'wingate'});
732     return if (time() - $wingaterun <= $interval);
733     return unless (scalar(keys %wingateToDo));
734
735     $wingaterun = time();
736
737     &Forker("wingate", sub { &Wingate::Wingates(keys %wingateToDo); } );
738     undef @wingateNow;
739 }
740
741 ### TODO.
742 sub wingateWriteFile {
743     return unless (scalar @wingateCache);
744
745     my $file = "$bot_base_dir/$param{'ircUser'}.wingate";
746     if ($bot_pid != $$) {
747         &DEBUG("wingateWriteFile: Reorganising!");
748
749         open(IN, $file);
750         while (<IN>) {
751             chop;
752             push(@wingateNow, $_);
753         }
754         close IN;
755
756         # very lame hack.
757         my %hash = map { $_ => 1 } @wingateNow;
758         @wingateNow = sort keys %hash;
759     }
760
761     &DEBUG("wingateWF: writing...");
762     open(OUT, ">$file");
763     foreach (@wingateNow) {
764         print OUT "$_\n";
765     }
766     close OUT;
767
768     &ScheduleThis(60, "wingateWriteFile") if (@_);
769 }
770
771 sub factoidCheck {
772     my @list = &searchTable("factoids", "factoid_key", "factoid_key", " #DEL#");
773     my $stale = ($param{'factoidDeleteDelay'} || 7)*60*60*24;
774
775     foreach (@list) {
776         my $age = &getFactInfo($_, "modified_time");    
777         next unless (time() - $age > $stale);
778
779         my $fix = $_;
780         $fix =~ s/ #DEL#$//g;
781         &VERB("safedel: Removing $fix for good.",2);
782         &delFactoid($_);
783     }
784
785     &ScheduleThis(1440, "factoidCheck") if (@_);
786 }
787
788 sub dccStatus {
789     my $time = strftime("%H:%M", localtime(time()) );
790
791     return unless (scalar keys %{ $DCC{CHAT} });
792
793     foreach (keys %channels) {
794         &DCCBroadcast("[$time] $_: $users members ($chops chops), $bans bans","+o");
795     }
796
797     &ScheduleThis(10, "dccStatus") if (@_);
798 }
799
800 sub schedulerSTUB {
801
802     &ScheduleThis(TIME_IN_MINUTES, "FUNCTION") if (@_);
803 }
804
805 sub scheduleList {
806     ###
807     # custom:
808     #   a - time == now.
809     #   b - weird time.
810     ###
811
812     &DEBUG("sched:");
813     foreach (keys %{ $irc->{_queue} }) {
814         my $q = $_;
815
816         my $sched;
817         foreach (keys %sched) {
818             next unless ($q eq $sched{$_});
819             $sched = $_;
820             last;
821         }
822
823         my $time = $irc->{_queue}->{$q}->[0] - time();
824
825         if (defined $sched) {
826             &DEBUG("   $sched($q): ".&Time2String($time) );
827         } else {
828             &DEBUG("   NULL($q): ".&Time2String($time) );
829         }
830     }
831
832     &DEBUG("end of sList.");
833 }
834
835 sub getChanConfDefault {
836     my($what, $chan, $default) = @_;
837
838     if (exists $param{$what}) {
839         return $param{$what};
840     }
841
842     my $val = &getChanConf($what, $chan);
843     if (defined $val) {
844         return $val;
845     }
846     &DEBUG("returning default $default for $what");
847     ### TODO: set some vars?
848     return $default;
849 }
850
851 1;