2 # ProcessExtra.pl: Extensions to Process.pl
4 # Version: v0.5 (20010124)
10 use POSIX qw(strftime);
11 use vars qw(%sched %schedule);
13 # format: function name = (
14 # str chanconfdefault,
15 # int internaldefault,
17 # int next run, (optional)
21 # uptimeLoop => ('', 60, 1),
24 sub setupSchedulersII {
25 foreach (keys %schedule) {
26 &queueTask($_, @{ $schedule{$_} });
31 my($codename, $chanconfdef, $intervaldef, $defer) = @_;
32 my $t = &getChanConfDefault($chanconfdef, $intervaldef, $chan);
33 my $waittime = &getRandomInt($t);
35 if (!defined $waittime) {
36 &WARN("interval == waittime == UNDEF for $codename.");
40 my $time = $schedule{$codename}[3];
41 if (defined $time and $time > time()) {
42 &WARN("Sched for $codename already exists in " . &Time2String(time() - $time) . ".");
46 #&VERB("Scheduling \&$codename() for " . &Time2String($waittime),3);
48 my $retval = $conn->schedule($waittime, sub {
54 &VERB( 'Starting schedulers...', 2 );
63 &leakCheck(2); # mandatory
65 &miscCheck2(2); # mandatory
70 &factoidCheck(2); # takes a couple of seconds on a 486. defer it
72 # TODO: convert to new format... or nuke altogether.
76 # 1 for run straight away
80 &netsplitCheck(1); # mandatory
81 &floodLoop(1); # mandatory
82 &ignoreCheck(1); # mandatory
83 &miscCheck(1); # mandatory
84 &shmFlush(1); # mandatory
86 &ircCheck(1); # mandatory
88 # TODO: squeeze this into a one-liner.
89 # my $count = map { exists $sched{$_}{TIME} } keys %sched;
91 foreach ( keys %sched ) {
92 my $time = $sched{$_}{TIME};
93 next unless ( defined $time and $time > time() );
98 &status("Schedulers: $count will be running.");
103 my ( $interval, $codename, @args ) = @_;
105 # Set to supllied value plus a random 0-60 seconds to avoid simultaneous runs
107 &getRandomInt( "$interval-" . ( $interval + &getRandomInt(60) ) );
109 if ( !defined $waittime ) {
110 &WARN("interval == waittime == UNDEF for $codename.");
114 my $time = $sched{$codename}{TIME};
115 if ( defined $time and $time > time() ) {
116 &WARN( "Sched for $codename already exists in "
117 . &Time2String( time() - $time )
123 "Scheduling \&$codename() "
124 . \&$codename . ' for '
125 . &Time2String($waittime),
129 my $retval = $conn->schedule( $waittime, \&$codename, @args );
130 $sched{$codename}{LABEL} = $retval;
131 $sched{$codename}{TIME} = time() + $waittime;
132 $sched{$codename}{LOOP} = 1;
136 #### LET THE FUN BEGIN.
140 my $interval = $param{'rssFeedTime'} || 30;
142 &ScheduleThis( $interval * 60, 'rssFeeds' ); # minutes
143 return if ( $_[0] eq '2' ); # defer.
148 my $line = &RSSFeeds::RSS();
149 return unless ( defined $line );
156 my $interval = &getChanConfDefault( 'randomQuoteInterval', 60, $chan );
158 &ScheduleThis( $interval * 60, 'randomQuote' ); # every hour
159 return if ( $_[0] eq '2' ); # defer.
162 foreach ( &ChanConfList('randomQuote') ) {
163 next unless ( &validChan($_) );
166 &getRandomLineFromFile( $bot_data_dir . '/infobot.randtext' );
167 if ( !defined $line ) {
168 &ERROR('random Quote: weird error?');
172 &status("sending random Quote to $_.");
173 &action( $_, 'Ponders: ' . $line );
175 ### TODO: if there were no channels, don't reschedule until channel
176 ### configuration is modified.
183 my $interval = &getChanConfDefault( 'randomFactoidInterval', 60, $chan );
185 &ScheduleThis( $interval * 60, 'randomFactoid' ); # minutes
186 return if ( $_[0] eq '2' ); # defer.
189 foreach ( &ChanConfList('randomFactoid') ) {
190 next unless ( &validChan($_) );
192 &status("sending random Factoid to $_.");
195 &randKey( 'factoids', 'factoid_key,factoid_value' );
196 &DEBUG("rF: $key, $val");
197 ### $val =~ tr/^[A-Z]/[a-z]/; # blah is Good => blah is good.
199 if ( ( defined $val )
201 and ( $key !~ /\#DEL\#/ )
202 and ( $key !~ /^cmd:/ ) );
206 &ERROR('rF: tried 5 times but failed.');
210 &action( $_, "Thinks: \037$key\037 is $val" );
211 ### FIXME: Use &getReply() on above to format factoid properly?
218 &ScheduleThis( 3600, 'logLoop' ); # 1 hour
219 return if ( $_[0] eq '2' ); # defer.
222 return unless ( defined fileno LOG );
223 return unless ( &IsParam('logfile') );
224 return unless ( &IsParam('maxLogSize') );
226 ### check if current size is too large.
227 if ( -s $file{log} > $param{'maxLogSize'} ) {
228 my $date = sprintf( '%04d%02d%02d', (gmtime)[ 5, 4, 3 ] );
229 $file{log} = $param{'logfile'} . '-' . $date;
230 &status('cycling log file.');
232 if ( -e $file{log} ) {
236 $newlog = $file{log} . '-' . $i;
237 last if ( !-e $newlog );
240 $file{log} = $newlog;
244 CORE::system("/bin/mv '$param{'logfile'}' '$file{log}'");
245 &compress( $file{log} );
247 &status('cycling log file.');
250 ### check if all the logs exceed size.
251 if ( !opendir( LOGS, $bot_log_dir ) ) {
252 &WARN("logLoop: could not open dir '$bot_log_dir'");
258 while ( defined( $_ = readdir LOGS ) ) {
259 my $logfile = "$bot_log_dir/$_";
261 next unless ( -f $logfile );
263 my $size = -s $logfile;
264 my $age = ( stat $logfile )[9];
265 $age{$age} = $logfile;
266 $size{$logfile} = $size;
272 while ( $tsize > $param{'maxLogSize'} ) {
273 &status("LOG: current size > max ($tsize > $param{'maxLogSize'})");
274 my $oldest = ( sort { $a <=> $b } keys %age )[0];
275 &status("LOG: unlinking $age{$oldest}.");
276 unlink $age{$oldest};
281 ### TODO: add how many b,kb,mb removed?
282 &status("LOG: removed $delete logs.") if ($delete);
287 &ScheduleThis( 86400, 'seenFlushOld' ); # 1 day
288 return if ( $_[0] eq '2' ); # defer.
291 # is this global-only?
292 return unless ( &IsChanConf('seen') > 0 );
293 return unless ( &IsChanConf('seenFlushInterval') > 0 );
295 # global setting. does not make sense for per-channel.
297 &getChanConfDefault( 'seenMaxDays', 30, $chan ) * 60 * 60 * 24;
300 if ( $param{'DBType'} =~ /^(pgsql|mysql|sqlite(2)?)$/i ) {
303 if ( $param{'DBType'} =~ /^mysql$/i ) {
305 'SELECT nick,time FROM seen GROUP BY nick HAVING '
306 . "UNIX_TIMESTAMP() - time > $max_time";
308 elsif ( $param{'DBType'} =~ /^sqlite(2)?$/i ) {
310 'SELECT nick,time FROM seen GROUP BY nick HAVING '
311 . "strftime('%s','now','localtime') - time > $max_time";
315 'SELECT nick,time FROM seen WHERE '
316 . "extract(epoch from timestamp 'now') - time > $max_time";
319 my $sth = $dbh->prepare($query);
320 if ( $sth->execute ) {
321 while ( my @row = $sth->fetchrow_array ) {
322 my ( $nick, $time ) = @row;
324 &sqlDelete( 'seen', { nick => $nick } );
331 &FIXME( 'seenFlushOld: for bad DBType:' . $param{'DBType'} . '.' );
333 &VERB( "SEEN deleted $delete seen entries.", 2 );
339 &ScheduleThis( 3600, 'newsFlush' ); # 1 hour
340 return if ( $_[0] eq '2' ); # defer.
343 if ( !&ChanConfList('News') ) {
344 &DEBUG("newsFlush: news disabled? (chan => $chan)");
351 foreach $chan ( keys %::news ) {
353 my $total = scalar( keys %{ $::news{$chan} } );
356 delete $::news{$chan};
360 foreach $item ( keys %{ $::news{$chan} } ) {
361 my $t = $::news{$chan}{$item}{Expire};
363 my $tadd = $::news{$chan}{$item}{Time};
364 $oldest = $tadd if ( $oldest > $tadd );
366 next if ( $t == 0 or $t == -1 );
369 "newsFlush: Fixed Expire time for $chan/$item, should not happen anyway."
371 $::news{$chan}{$item}{Expire} = time() + $t * 60 * 60 * 24;
375 my $delta = $t - time();
377 next unless ( time() > $t );
379 # TODO: show how old it was.
380 delete $::news{$chan}{$item};
381 &status("NEWS: (newsflush) deleted '$item'");
386 &status("NEWS (newsflush) {$chan}: deleted [$i/$total] news entries.")
388 $none{$chan} = 1 if ( $total == $i );
391 # TODO: flush users aswell.
393 foreach $chan ( keys %::newsuser ) {
394 next if ( exists $none{$chan} );
396 foreach ( keys %{ $::newsuser{$chan} } ) {
397 my $t = $::newsuser{$chan}{$_};
398 if ( !defined $t or ( $t > 2 and $t < 1000 ) ) {
399 &DEBUG("something wrong with newsuser{$chan}{$_} => $t");
403 next unless ( $oldest > $t );
405 delete $::newsuser{$chan}{$_};
409 my $i = scalar( keys %{ $::newsuser{$chan} } );
410 delete $::newsuser{$chan} unless ($i);
413 if ( $delete or $duser ) {
414 &status("NewsFlush: deleted: $delete news entries; $duser user cache.");
419 my $interval = &getChanConfDefault( 'chanlimitcheckInterval', 10, $chan );
420 my $mynick = $conn->nick();
423 &ScheduleThis( $interval * 60, 'chanlimitCheck' ); # default 10 minutes
424 return if ( $_[0] eq '2' );
427 my $str = join( ' ', &ChanConfList('chanlimitcheck') );
429 foreach $chan ( &ChanConfList('chanlimitcheck') ) {
430 next unless ( &validChan($chan) );
432 if ( $chan eq '_default' ) {
433 &WARN("chanlimit: we're doing $chan!! HELP ME!");
437 my $limitplus = &getChanConfDefault( 'chanlimitcheckPlus', 5, $chan );
438 my $newlimit = scalar( keys %{ $channels{$chan}{''} } ) + $limitplus;
439 my $limit = $channels{$chan}{'l'};
441 if ( scalar keys %netsplitservers ) {
442 if ( defined $limit ) {
443 &status("chanlimit: netsplit; removing it for $chan.");
444 $conn->mode( $chan, '-l' );
445 $cache{chanlimitChange}{$chan} = time();
446 &status('chanlimit: netsplit; removed.');
452 if ( defined $limit and scalar keys %{ $channels{$chan}{''} } > $limit )
454 &FIXME('LIMIT: set too low!!!');
455 ### run NAMES again and flush it.
458 if ( defined $limit and $limit == $newlimit ) {
459 $cache{chanlimitChange}{$chan} = time();
463 if ( !exists $channels{$chan}{'o'}{$mynick} ) {
464 &status("chanlimit: dont have ops on $chan.")
465 unless ( exists $cache{warn}{chanlimit}{$chan} );
466 $cache{warn}{chanlimit}{$chan} = 1;
467 &chanServCheck($chan);
470 delete $cache{warn}{chanlimit}{$chan};
472 if ( !defined $limit ) {
474 "chanlimit: $chan: setting for first time or from netsplit.");
477 if ( exists $cache{chanlimitChange}{$chan} ) {
478 my $delta = time() - $cache{chanlimitChange}{$chan};
479 if ( $delta < $interval * 60 ) {
481 "chanlimit: not going to change chanlimit! ($delta<$interval*60)"
487 $conn->mode( $chan, '+l', $newlimit );
488 $cache{chanlimitChange}{$chan} = time();
496 &ScheduleThis( 300, 'netsplitCheck' ); # every 5 minutes
497 return if ( $_[0] eq '2' );
500 $cache{'netsplitCache'}++;
502 # &DEBUG("running netsplitCheck... $cache{netsplitCache}");
504 if ( !scalar %netsplit and scalar %netsplitservers ) {
505 &DEBUG('nsC: !hash netsplit but hash netsplitservers <- removing!');
506 undef %netsplitservers;
510 # well... this shouldn't happen since %netsplit code does it anyway.
511 foreach $s1 ( keys %netsplitservers ) {
513 foreach $s2 ( keys %{ $netsplitservers{$s1} } ) {
514 my $delta = time() - $netsplitservers{$s1}{$s2};
516 if ( $delta > 60 * 30 ) {
517 &status("netsplit between $s1 and $s2 appears to be stale.");
518 delete $netsplitservers{$s1}{$s2};
523 my $i = scalar( keys %{ $netsplitservers{$s1} } );
524 delete $netsplitservers{$s1} unless ($i);
527 # %netsplit hash checker.
528 my $count = scalar keys %netsplit;
530 foreach ( keys %netsplit ) {
531 if ( &IsNickInAnyChan($_) ) { # why would this happen?
533 # &DEBUG("nsC: $_ is in some chan; removing from netsplit list.");
534 delete $netsplit{$_};
539 next unless ( time() - $netsplit{$_} > 60 * 15 );
542 delete $netsplit{$_};
546 # FIXED: $ch should be used rather than $chan since it creates NULL channels in the hash
547 foreach my $ch ( keys %channels ) {
548 my $i = $cache{maxpeeps}{$ch} || 0;
549 my $j = scalar( keys %{ $channels{$ch} } );
550 next unless ( $i > 10 and 0.25 * $i > $j );
552 &DEBUG("netsplit: 0.25*max($i) > current($j); possible netsplit?");
556 my $j = scalar( keys %netsplit );
557 &status("nsC: removed from netsplit list: (before: $count; after: $j)");
560 if ( !scalar %netsplit and scalar %netsplitservers ) {
561 &DEBUG('nsC: ok hash netsplit is NULL; purging hash netsplitservers');
562 undef %netsplitservers;
565 if ( $count and !scalar keys %netsplit ) {
566 &DEBUG('nsC: netsplit is hopefully gone. reinstating chanlimit check.');
576 &ScheduleThis( 60, 'floodLoop' ); # 1 minute
577 return if ( $_[0] eq '2' );
581 my $interval = &getChanConfDefault( 'floodCycle', 60, $chan );
583 foreach $who ( keys %flood ) {
584 foreach ( keys %{ $flood{$who} } ) {
585 if ( !exists $flood{$who}{$_} ) {
586 &WARN("flood{$who}{$_} undefined?");
590 if ( $time - $flood{$who}{$_} > $interval ) {
591 delete $flood{$who}{$_};
596 &VERB( "floodLoop: deleted $delete items.", 2 );
601 my $interval = &getChanConfDefault( 'seenFlushInterval', 60, $chan );
602 &ScheduleThis( $interval * 60, 'seenFlush' ); # minutes
603 return if ( $_[0] eq '2' );
609 $stats{'count_old'} = &countKeys('seen') || 0;
613 if ( $param{'DBType'} =~ /^(mysql|pgsql|sqlite(2)?)$/i ) {
614 foreach $nick ( keys %seencache ) {
615 my $retval = &sqlSet(
617 { 'nick' => lc $seencache{$nick}{'nick'} },
619 time => $seencache{$nick}{'time'},
620 host => $seencache{$nick}{'host'},
621 channel => $seencache{$nick}{'chan'},
622 message => $seencache{$nick}{'msg'},
626 delete $seencache{$nick};
631 &DEBUG('seenFlush: NO VALID FACTOID SUPPORT?');
634 &status("Seen: Flushed $flushed entries.") if ($flushed);
637 ' new seen: %03.01f%% (%d/%d)',
638 $stats{'new'} * 100 / ( $stats{'count_old'} || 1 ),
640 ( $stats{'count_old'} || 1 )
643 ) if ( $stats{'new'} );
646 ' now seen: %3.1f%% (%d/%d)',
647 $stats{'old'} * 100 / ( &countKeys('seen') || 1 ), $stats{'old'},
651 ) if ( $stats{'old'} );
653 &WARN('scalar keys seenflush != 0!') if ( scalar keys %seenflush );
657 my ( $blah1, $blah2 );
661 &ScheduleThis( 14400, 'leakCheck' ); # every 4 hours
662 return if ( $_[0] eq '2' );
665 # flood. this is dealt with in floodLoop()
666 foreach $blah1 ( keys %flood ) {
667 foreach $blah2 ( keys %{ $flood{$blah1} } ) {
668 $count += scalar( keys %{ $flood{$blah1}{$blah2} } );
671 &VERB( "leak: hash flood has $count total keys.", 2 );
675 foreach $blah1 ( keys %floodjoin ) {
676 foreach $blah2 ( keys %{ $floodjoin{$blah1} } ) {
677 $count += scalar( keys %{ $floodjoin{$blah1}{$blah2} } );
680 &VERB( "leak: hash floodjoin has $count total keys.", 2 );
683 $count = scalar( keys %floodwarn );
684 &VERB( "leak: hash floodwarn has $count total keys.", 2 );
687 foreach $chan ( grep /[A-Z]/, keys %channels ) {
688 &DEBUG("leak: chan => '$chan'.");
690 foreach $i ( keys %{ $channels{$chan} } ) {
691 foreach ( keys %{ $channels{$chan}{$i} } ) {
692 &DEBUG("leak: \$channels{$chan}{$i}{$_} ...");
698 $count = scalar( keys %chanstats );
699 &VERB( "leak: hash chanstats has $count total keys.", 2 );
703 foreach ( keys %nuh ) {
704 next if ( &IsNickInAnyChan($_) );
705 next if ( exists $dcc{CHAT}{$_} );
712 "leak: $delete nuh{} items deleted; now have " . scalar( keys %nuh ) )
718 &ScheduleThis( 60, 'ignoreCheck' ); # once every minute
719 return if ( $_[0] eq '2' ); # defer.
725 foreach ( keys %ignore ) {
728 foreach ( keys %{ $ignore{$chan} } ) {
729 my @array = @{ $ignore{$chan}{$_} };
731 next unless ( $array[0] and $time > $array[0] );
733 delete $ignore{$chan}{$_};
734 &status("ignore: $_/$chan has expired.");
739 $cache{ignoreCheckTime} = time();
741 &VERB( "ignore: $count items deleted.", 2 );
746 &ScheduleThis( 300, 'ircCheck' ); # every 5 minutes
747 return if ( $_[0] eq '2' ); # defer.
750 $cache{statusSafe} = 1;
751 foreach ( sort keys %conns ) {
753 my $mynick = $conn->nick();
754 &DEBUG("ircCheck for $_");
755 # Display with min of 900sec delay between redisplay
756 # FIXME: should only use 900sec when we are on the LAST %conns
757 my @join = &getJoinChans(900);
758 if ( scalar @join ) {
759 &FIXME( 'ircCheck: found channels to join! ' . join( ',', @join ) );
763 # TODO: fix on_disconnect()
765 if ( time() - $msgtime > 3600 ) {
767 # TODO: shouldn't we use cache{connect} somewhere?
768 if ( exists $cache{connect} ) {
769 &WARN("ircCheck: no msg for 3600 and disco'd! reconnecting!");
770 $msgtime = time(); # just in case.
772 delete $cache{connect};
775 &status( 'ircCheck: possible lost in space; checking.'
777 &msg( $mynick, 'TEST' );
778 $cache{connect} = time();
783 if ( grep /^\s*$/, keys %channels ) {
784 &WARN('ircCheck: we have a NULL chan in hash channels? removing!');
785 if ( !exists $channels{''} ) {
786 &DEBUG('ircCheck: this should never happen!');
789 if ( $ident !~ /^\Q$param{ircNick}\E$/ ) {
791 # this does not work unfortunately.
792 &WARN("ircCheck: ident($ident) != param{ircNick}($param{ircNick}).");
794 # this check is misleading... perhaps we should do a notify.
795 if ( !&IsNickInAnyChan( $param{ircNick} ) ) {
796 &DEBUG("$param{ircNick} not in use... changing!");
797 &nick( $param{ircNick} );
800 &WARN("$param{ircNick} is still in use...");
804 $cache{statusSafe} = 0;
807 if ( $utime_userfile > $wtime_userfile and time() - $wtime_userfile > 3600 )
810 $wtime_userfile = time();
813 if ( $utime_chanfile > $wtime_chanfile and time() - $wtime_chanfile > 3600 )
816 $wtime_chanfile = time();
822 &ScheduleThis( 7200, 'miscCheck' ); # every 2 hours
823 return if ( $_[0] eq '2' ); # defer.
828 if ( -x '/usr/bin/ipcs' ) {
829 @ipcs = `/usr/bin/ipcs`;
832 &WARN("ircCheck: no 'ipcs' binary.");
836 # make backup of important files.
837 &mkBackup( $bot_state_dir . '/infobot.chan', 60 * 60 * 24 * 3 );
838 &mkBackup( $bot_state_dir . '/infobot.users', 60 * 60 * 24 * 3 );
839 &mkBackup( $bot_base_dir . '/infobot-news.txt', 60 * 60 * 24 * 1 );
841 # flush cache{lobotomy}
842 foreach ( keys %{ $cache{lobotomy} } ) {
843 next unless ( time() - $cache{lobotomy}{$_} > 60 * 60 );
844 delete $cache{lobotomy}{$_};
847 ### check modules if they've been modified. might be evil.
850 # shmid stale remove.
854 # key, shmid, owner, perms, bytes, nattch
855 next unless (/^(0x\d+) (\d+)\s+(\S+)\s+(\d+)\s+(\d+)\s+/);
857 my ( $shmid, $size ) = ( $2, $5 );
858 next unless ( $shmid != $shm and $size == 2000 );
859 my $z = &shmRead($shmid);
860 if ( $z =~ /^(\S+):(\d+):(\d+): / ) {
864 next if ( time() - $time < 60 * 60 );
866 # FIXME remove not-pid shm if parent process dead
867 next if ( $pid == $bot_pid );
869 # don't touch other bots, if they're running.
870 next unless ( $param{ircUser} =~ /^\Q$n\E$/ );
873 &DEBUG("shm: $shmid is not ours or old infobot => ($z)");
877 &status("SHM: nuking shmid $shmid");
878 CORE::system("/usr/bin/ipcrm shm $shmid >/dev/null");
884 &ScheduleThis( 14400, 'miscCheck2' ); # every 4 hours
885 return if ( $_[0] eq '2' ); # defer.
889 opendir( DEBIAN, "$bot_state_dir/debian" );
890 foreach ( grep /gz$/, readdir(DEBIAN) ) {
891 my $exit = CORE::system("gzip -t $bot_state_dir/debian/$_");
894 &status("debian: unlinking file => $_");
895 unlink "$bot_state_dir/debian/$_";
899 # compress logs that should have been compressed.
900 # TODO: use strftime?
901 my ( $day, $month, $year ) = ( gmtime( time() ) )[ 3, 4, 5 ];
902 my $date = sprintf( '%04d%02d%02d', $year + 1900, $month + 1, $day );
904 if ( !opendir( DIR, "$bot_log_dir" ) ) {
905 &ERROR("misccheck2: log dir $bot_log_dir does not exist.");
910 while ( my $f = readdir(DIR) ) {
911 next unless ( -f "$bot_log_dir/$f" );
912 next if ( $f =~ /gz|bz2/ );
913 next unless ( $f =~ /(\d{8})/ );
914 next if ( $date eq $1 );
916 &compress("$bot_log_dir/$f");
921 ### this is semi-scheduled
924 # FIXME: broken for multiple connects
925 # if ($ident eq $param{'ircNick'}) {
926 # &status('okay, got my nick back.');
931 # &ScheduleThis(30, 'getNickInUse');
932 # return if ($_[0] eq '2'); # defer.
935 # &nick( $param{'ircNick'} );
939 return if ( !defined &uptimeWriteFile );
941 # return unless &IsParam('Uptime');
944 &ScheduleThis( 3600, 'uptimeLoop' ); # once per hour
945 return if ( $_[0] eq '2' ); # defer.
954 &ScheduleThis( 3600, 'slashdotLoop' ); # once per hour
955 return if ( $_[0] eq '2' );
958 my @chans = &ChanConfList('slashdotAnnounce');
959 return unless ( scalar @chans );
964 my $line = &Slashdot::slashdotAnnounce();
965 return unless ( defined $line );
968 next unless ( &::validChan($_) );
970 &::status("sending slashdot update to $_.");
971 ¬ice( $_, "Slashdot: $line" );
980 &ScheduleThis( 3600, 'plugLoop' ); # once per hour
981 return if ( $_[0] eq '2' );
984 my @chans = &ChanConfList('plugAnnounce');
985 return unless ( scalar @chans );
990 my $line = &Plug::plugAnnounce();
991 return unless ( defined $line );
994 next unless ( &::validChan($_) );
996 &::status("sending plug update to $_.");
997 ¬ice( $_, "Plug: $line" );
1005 &ScheduleThis( 14400, 'kernelLoop' ); # once every 4 hours
1006 return if ( $_[0] eq '2' );
1009 my @chans = &ChanConfList('kernelAnnounce');
1010 return unless ( scalar @chans );
1015 my @data = &Kernel::kernelAnnounce();
1018 next unless ( &::validChan($_) );
1020 &::status("sending kernel update to $_.");
1023 ¬ice( $c, "Kernel: $_" );
1031 return unless &IsChanConf('Wingate') > 0;
1033 ### FILE CACHE OF OFFENDING WINGATES.
1034 foreach ( grep /^$host$/, @wingateBad ) {
1035 &status("Wingate: RUNNING ON $host BY $who");
1036 &ban( "*!*\@$host", '' ) if &IsChanConf('wingateBan') > 0;
1038 my $reason = &getChanConf('wingateKick');
1040 next unless ($reason);
1041 &kick( $who, '', $reason );
1044 ### RUN CACHE OF TRIED WINGATES.
1045 if ( grep /^$host$/, @wingateCache ) {
1046 push( @wingateNow, $host ); # per run.
1047 push( @wingateCache, $host ); # cache per run.
1050 &DEBUG("Already scanned $host. good.");
1054 &getChanConfDefault( 'wingateInterval', 60, $chan ); # seconds.
1055 return if ( defined $forked{'Wingate'} );
1056 return if ( time() - $wingaterun <= $interval );
1057 return unless ( scalar( keys %wingateToDo ) );
1059 $wingaterun = time();
1061 &Forker( 'Wingate', sub { &Wingate::Wingates( keys %wingateToDo ); } );
1066 sub wingateWriteFile {
1068 &ScheduleThis( 3600, 'wingateWriteFile' ); # once per hour
1069 return if ( $_[0] eq '2' ); # defer.
1072 return unless ( scalar @wingateCache );
1074 my $file = "$bot_base_dir/$param{'ircUser'}.wingate";
1075 if ( $bot_pid != $$ ) {
1076 &DEBUG('wingateWriteFile: Reorganising!');
1081 push( @wingateNow, $_ );
1086 my %hash = map { $_ => 1 } @wingateNow;
1087 @wingateNow = sort keys %hash;
1090 &DEBUG('wingateWF: writing...');
1091 open( OUT, ">$file" );
1092 foreach (@wingateNow) {
1100 &ScheduleThis( 43200, 'factoidCheck' ); # ever 12 hours
1101 return if ( $_[0] eq '2' ); # defer.
1105 &searchTable( 'factoids', 'factoid_key', 'factoid_key', ' #DEL#' );
1107 &getChanConfDefault( 'factoidDeleteDelay', 14, $chan ) * 60 * 60 * 24;
1110 # disable it since it's 'illegal'.
1117 my $age = &getFactInfo( $_, 'modified_time' );
1119 if ( !defined $age or $age !~ /^\d+$/ ) {
1120 if ( scalar @list > 50 ) {
1121 if ( !$cache{warnDel} ) {
1122 &WARN( 'list is over 50 ('
1124 . '... giving it a miss.' );
1125 $cache{warnDel} = 1;
1130 &WARN("del factoid: old cruft (no time): $_");
1135 next unless ( $time - $age > $stale );
1138 $fix =~ s/ #DEL#$//g;
1139 my $agestr = &Time2String( $time - $age );
1140 &status("safedel: Removing '$_' for good. [$agestr old]");
1147 return unless ( scalar keys %{ $dcc{CHAT} } );
1150 &ScheduleThis( 600, 'dccStatus' ); # every 10 minutes
1151 return if ( $_[0] eq '2' ); # defer.
1154 my $time = strftime( '%H:%M', gmtime( time() ) );
1157 foreach ( keys %channels ) {
1159 my $users = keys %{ $channels{$c}{''} };
1160 my $chops = keys %{ $channels{$c}{o} };
1161 my $bans = keys %{ $channels{$c}{b} };
1163 my $txt = "[$time] $c: $users members ($chops chops), $bans bans";
1164 foreach ( keys %{ $dcc{'CHAT'} } ) {
1165 next unless ( exists $channels{$c}{''}{ lc $_ } );
1166 $conn->privmsg( $dcc{'CHAT'}{$_}, $txt );
1178 my $reply = 'sched:';
1179 foreach ( keys %{ $irc->{_queue} } ) {
1181 my $coderef = $irc->{_queue}->{$q}->[1];
1183 foreach ( keys %sched ) {
1185 next unless defined( \&$schedname );
1186 next unless ( $coderef eq \&$schedname );
1187 $sched = $schedname;
1191 my $time = $irc->{_queue}->{$q}->[0] - time();
1193 if ( defined $sched ) {
1194 $reply = "$reply, $sched($q):" . &Time2String($time);
1197 $reply = "$reply, NULL($q):" . &Time2String($time);
1205 my ( $file, $time ) = @_;
1209 &VERB( "mkB: file '$file' does not exist.", 2 );
1214 if ( -e "$file~" ) {
1215 $backup++ if ( ( stat $file )[9] - ( stat "$file~" )[9] > $time );
1216 my $delta = time() - ( stat "$file~" )[9];
1217 $age = &Time2String($delta);
1223 return unless ($backup);
1225 ### TODO: do internal copying.
1226 &status("Backup: $file ($age)");
1227 CORE::system("/bin/cp $file $file~");
1232 # vim:ts=4:sw=4:expandtab:tw=80