2 # ProcessExtra.pl: Extensions to Process.pl
4 # Version: v0.5 (20010124)
8 if (&IsParam("useStrict")) { use strict; }
10 use POSIX qw(strftime);
14 # is there a point in ScheduleChecked()?
15 # not yet... not unless we run setupSchedulers more than once.
19 &VERB("Starting schedulers...",2);
30 &netsplitCheck(1); # mandatory
31 &floodLoop(1); # mandatory
33 &leakCheck(1); # mandatory
34 &ignoreCheck(1); # mandatory
36 &ircCheck(1); # mandatory
37 &miscCheck(1); # mandatory
38 &shmFlush(1); # mandatory
45 # my $count = map { exists $sched{$_}{TIME} } keys %sched;
47 foreach (keys %sched) {
48 next unless (exists $sched{$_}{TIME});
52 &status("Schedulers: $count will be running.");
57 my ($interval, $codename, @args) = @_;
58 my $waittime = &getRandomInt($interval);
60 if (!defined $waittime) {
61 &WARN("interval == waittime == UNDEF for $codename.");
65 if (exists $sched{$codename}{RUNNING}) {
66 &WARN("Sched for $codename already exists. forgot ScheduleChecked?");
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;
78 my ($codename) = shift;
80 # what the hell is this for?
81 if (exists $sched{$codename}{RUNNING}) {
82 ### &DEBUG("SC: Removed $codename.");
83 delete $sched{$codename}{RUNNING};
85 ### &WARN("sched $codename already removed.");
90 #### LET THE FUN BEGIN.
94 my $interval = &getChanConfDefault("randomQuoteInterval", 60);
96 &ScheduleThis($interval, "randomQuote");
97 &ScheduleChecked("randomQuote");
98 return if ($_[0] eq "2"); # defer.
101 my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.randtext");
102 if (!defined $line) {
103 &ERROR("random Quote: weird error?");
107 foreach ( &ChanConfList("randomQuote") ) {
108 next unless (&validChan($_)); # ???
110 &status("sending random Quote to $_.");
111 &action($_, "Ponders: ".$line);
113 ### TODO: if there were no channels, don't reschedule until channel
114 ### configuration is modified.
121 my $interval = &getChanConfDefault("randomFactoidInterval", 60);
123 &ScheduleThis($interval, "randomFactoid");
124 &ScheduleChecked("randomFactoid");
125 return if ($_[0] eq "2"); # defer.
129 ($key,$val) = &randKey("factoids","factoid_key,factoid_value");
130 ### $val =~ tr/^[A-Z]/[a-z]/; # blah is Good => blah is good.
131 last if ($val !~ /^</);
134 &ERROR("rF: tried 5 times but failed.");
139 foreach ( &ChanConfList("randomFactoid") ) {
140 next unless (&validChan($_)); # ???
142 &status("sending random Factoid to $_.");
143 &action($_, "Thinks: \037$key\037 is $val");
144 ### FIXME: Use &getReply() on above to format factoid properly?
149 sub randomFreshmeat {
150 my $interval = &getChanConfDefault("randomFresheatInterval", 60);
153 &ScheduleThis($interval, "randomFreshmeat");
154 &ScheduleChecked("randomFreshmeat");
155 return if ($_[0] eq "2"); # defer.
158 my @chans = &ChanConfList("randomFreshmeat");
159 return unless (scalar @chans);
161 &Forker("freshmeat", sub {
162 my $retval = &Freshmeat::randPackage();
165 next unless (&validChan($_)); # ???
167 &status("sending random Freshmeat to $_.");
175 &ScheduleThis(60, "logLoop");
176 &ScheduleChecked("logLoop");
177 return if ($_[0] eq "2"); # defer.
180 return unless (defined fileno LOG);
181 return unless (&IsParam("logfile"));
182 return unless (&IsParam("maxLogSize"));
184 ### check if current size is too large.
185 if ( -s $file{log} > $param{'maxLogSize'}) {
186 my $date = sprintf("%04d%02d%02d", (localtime)[5,4,3]);
187 $file{log} = $param{'logfile'} ."-". $date;
188 &status("cycling log file.");
190 if ( -e $file{log}) {
194 $newlog = $file{log}."-".$i;
195 last if (! -e $newlog);
198 $file{log} = $newlog;
202 system("/bin/mv '$param{'logfile'}' '$file{log}'");
203 &compress($file{log});
205 &status("cycling log file.");
208 ### check if all the logs exceed size.
209 my $logdir = "$bot_base_dir/log/";
210 if (opendir(LOGS, $logdir)) {
214 while (defined($_ = readdir LOGS)) {
215 my $logfile = "$logdir/$_";
217 next unless ( -f $logfile);
218 my $size = -s $logfile;
219 my $age = (stat $logfile)[9];
221 $age{$age} = $logfile;
222 $size{$logfile} = $size;
229 while ($tsize > $param{'maxLogSize'}) {
230 &status("LOG: current size > max ($tsize > $param{'maxLogSize'})");
231 my $oldest = (sort {$a <=> $b} keys %age)[0];
232 &status("LOG: unlinking $age{$oldest}.");
233 unlink $age{$oldest};
238 ### TODO: add how many b,kb,mb removed?
239 &status("LOG: removed $delete logs.") if ($delete);
241 &WARN("could not open dir $logdir");
248 &ScheduleThis(1440, "seenFlushOld");
249 &ScheduleChecked("seenFlushOld");
250 return if ($_[0] eq "2"); # defer.
253 # is this global-only?
254 return unless (&IsChanConf("seen") > 0);
255 return unless (&IsChanConf("seenFlushInterval") > 0);
257 my $max_time = ($chanconf{_default}{'seenMaxDays'} || 30)
261 if ($param{'DBType'} =~ /^pg|postgres|mysql/i) {
262 my $query = "SELECT nick,time FROM seen GROUP BY nick HAVING UNIX_TIMESTAMP() - time > $max_time";
263 my $sth = $dbh->prepare($query);
266 while (my @row = $sth->fetchrow_array) {
267 my ($nick,$time) = @row;
269 &dbDel("seen","nick",$nick);
273 } elsif ($param{'DBType'} =~ /^dbm/i) {
276 foreach (keys %seen) {
277 my $delta_time = $time - &dbGet("seen", "NULL", $_, "time");
278 next unless ($delta_time > $max_time);
280 &DEBUG("seenFlushOld: ".&Time2String($delta_time) );
285 &FIXME("seenFlushOld: for PG/NO-DB.");
287 &VERB("SEEN deleted $delete seen entries.",2);
293 my $interval = &getChanConf("chanlimitcheckInterval") || 10;
294 &ScheduleThis($interval, "chanlimitCheck");
295 &ScheduleChecked("chanlimitCheck");
296 return if ($_[0] eq "2");
299 foreach ( &ChanConfList("chanlimitcheck") ) {
300 next unless (&validChan($_)); # ???
302 my $limitplus = &getChanConf("chanlimitcheckPlus",$_) || 5;
303 my $newlimit = scalar(keys %{$channels{$_}{''}}) + $limitplus;
304 my $limit = $channels{$_}{'l'};
306 if (scalar keys %{$channels{$_}{''}} > $limit) {
307 &status("LIMIT: set too low!!! FIXME");
308 ### run NAMES again and flush it.
311 next unless (!defined $limit or $limit != $newlimit);
313 if (!exists $channels{$_}{'o'}{$ident}) {
314 &ERROR("chanlimitcheck: dont have ops on $_.");
317 &rawout("MODE $_ +l $newlimit");
326 &ScheduleThis(30, "netsplitCheck");
327 &ScheduleChecked("netsplitCheck");
328 return if ($_[0] eq "2");
331 foreach $s1 (keys %netsplitservers) {
332 foreach $s2 (keys %{$netsplitservers{$s1}}) {
333 if (time() - $netsplitservers{$s1}{$s2} > 3600) {
334 &status("netsplit between $s1 and $s2 appears to be stale.");
335 delete $netsplitservers{$s1}{$s2};
340 # %netsplit hash checker.
341 foreach (keys %netsplit) {
342 if (&IsNickInAnyChan($_)) {
343 &DEBUG("netsplitC: $_ is in some chan; removing from netsplit list.");
344 delete $netsplit{$_};
346 next unless (time() - $netsplit{$_} > 60*60*2); # 2 hours.
347 next if (&IsNickInAnyChan($_));
349 &DEBUG("netsplitC: $_ didn't come back from netsplit in 2 hours; removing from netsplit list.");
350 delete $netsplit{$_};
359 &ScheduleThis(60, "floodLoop"); # minutes.
360 &ScheduleChecked("floodLoop");
361 return if ($_[0] eq "2");
365 my $interval = &getChanConfDefault("floodCycle",60);
367 foreach $who (keys %flood) {
368 foreach (keys %{$flood{$who}}) {
369 if (!exists $flood{$who}{$_}) {
370 &WARN("flood{$who}{$_} undefined?");
374 if ($time - $flood{$who}{$_} > $interval) {
375 delete $flood{$who}{$_};
380 &VERB("floodLoop: deleted $delete items.",2);
387 $stats{'count_old'} = &countKeys("seen") || 0;
392 my $interval = &getChanConfDefault("seenFlushInterval", 60);
393 &ScheduleThis($interval, "seenFlush");
394 &ScheduleChecked("seenFlush");
395 return if ($_[0] eq "2");
398 if ($param{'DBType'} =~ /^mysql|pg|postgres/i) {
399 foreach $nick (keys %seencache) {
400 my $exists = &dbGet("seen","nick", $nick, "nick");
402 if (defined $exists and $exists) {
403 &dbUpdate("seen", "nick", $nick, (
404 "time" => $seencache{$nick}{'time'},
405 "host" => $seencache{$nick}{'host'},
406 "channel" => $seencache{$nick}{'chan'},
407 "message" => $seencache{$nick}{'msg'},
411 my $retval = &dbInsert("seen", $nick, (
412 "nick" => $seencache{$nick}{'nick'},
413 "time" => $seencache{$nick}{'time'},
414 "host" => $seencache{$nick}{'host'},
415 "channel" => $seencache{$nick}{'chan'},
416 "message" => $seencache{$nick}{'msg'},
420 ### TODO: put bad nick into a list and don't do it again!
421 &FIXME("Should never happen! (nick => $nick)") if !$retval;
424 delete $seencache{$nick};
428 } elsif ($param{'DBType'} =~ /^dbm/i) {
430 foreach $nick (keys %seencache) {
431 my $retval = &dbInsert("seen", $nick, (
432 "nick" => $seencache{$nick}{'nick'},
433 "time" => $seencache{$nick}{'time'},
434 "host" => $seencache{$nick}{'host'},
435 "channel" => $seencache{$nick}{'chan'},
436 "message" => $seencache{$nick}{'msg'},
439 ### TODO: put bad nick into a list and don't do it again!
440 &FIXME("Should never happen! (nick => $nick)") if !$retval;
442 delete $seencache{$nick};
446 &DEBUG("seenFlush: NO VALID FACTOID SUPPORT?");
449 &status("Flushed $flushed seen entries.") if ($flushed);
450 &VERB(sprintf(" new seen: %03.01f%% (%d/%d)",
451 $stats{'new'}*100/$stats{'count_old'},
452 $stats{'new'}, $stats{'count_old'} ), 2) if ($stats{'new'});
453 &VERB(sprintf(" now seen: %3.1f%% (%d/%d)",
454 $stats{'old'}*100/&countKeys("seen"),
455 $stats{'old'}, &countKeys("seen") ), 2) if ($stats{'old'});
457 &WARN("scalar keys seenflush != 0!") if (scalar keys %seenflush);
466 &ScheduleThis(60, "leakCheck");
467 &ScheduleChecked("leakCheck");
468 return if ($_[0] eq "2");
472 foreach $blah1 (keys %flood) {
473 foreach $blah2 (keys %{$flood{$blah1}}) {
474 $count += scalar(keys %{$flood{$blah1}{$blah2}});
477 &VERB("\%flood has $count total keys.",2);
480 foreach $chan (grep /[A-Z]/, keys %channels) {
481 &DEBUG("leak: chan => '$chan'.");
483 foreach $i (keys %{$channels{$chan}}) {
484 foreach (keys %{$channels{$chan}{$i}}) {
485 &DEBUG("leak: \$channels{$chan}{$i}{$_} ...");
491 foreach (keys %nuh) {
492 next if (&IsNickInAnyChan($_));
497 &DEBUG("$delete nuh{} items deleted; now have ".
498 scalar(keys %nuh) ) if ($delete);
503 &ScheduleThis(60, "ignoreCheck");
504 &ScheduleChecked("ignoreCheck");
510 foreach (keys %ignore) {
513 foreach (keys %{ $ignore{$chan} }) {
514 my @array = @{ $ignore{$chan}{$_} };
516 next unless ($array[0] and $time > $array[0]);
518 delete $ignore{$chan}{$_};
519 &status("ignore: $_/$chan has expired.");
523 &VERB("ignore: $count items deleted.",2);
527 my @array = grep !/^_default$/, keys %chanconf;
528 my $iconf = scalar(@array);
529 my $inow = scalar(keys %channels);
530 if ($iconf > 2 and $inow * 2 <= $iconf) {
531 &FIXME("ircCheck: current channels * 2 <= config channels. FIXME.");
535 foreach ( &ChanConfList("chanServ_ops") ) {
536 next if (exists $channels{$chan}{'o'}{$ident});
538 &status("ChanServ ==> Requesting ops for $chan.");
539 &rawout("PRIVMSG ChanServ :OP $chan $ident");
542 if (!$conn->connected and time - $msgtime > 3600) {
543 &WARN("ircCheck: no msg for 3600 and disco'd! reconnecting!");
544 $msgtime = time(); # just in case.
548 if ($ident !~ /^\Q$param{ircNick}\E$/) {
549 &WARN("ircCheck: ident($ident) != param{ircNick}($param{IrcNick}).");
550 ### TODO: schedule check for own nick if taken?
554 # if scalar @joinnext => join more channels
555 # else check for chanserv.
557 if (grep /^\s*$/, keys %channels) {
558 &WARN("we have a NULL chan in hash channels? removing!");
559 delete $channels{''}; # ???
560 &DEBUG("channels now:");
561 foreach (keys %channels) {
564 &DEBUG("channels END");
568 if ($utime_userfile > $wtime_userfile and time() - $wtime_userfile > 3600) {
570 $wtime_userfile = time();
573 if ($utime_chanfile > $wtime_chanfile and time() - $wtime_chanfile > 3600) {
575 $wtime_chanfile = time();
578 &ScheduleThis(240, "ircCheck") if (@_);
582 &ScheduleThis(240, "miscCheck") if (@_);
586 if ( -x "/usr/bin/ipcs") {
587 @ipcs = `/usr/bin/ipcs`;
589 &WARN("ircCheck: no 'ipcs' binary.");
593 # shmid stale remove.
597 # key, shmid, owner, perms, bytes, nattch
598 next unless (/^(0x\d+) (\d+)\s+(\S+)\s+(\d+)\s+(\d+)\s+/);
600 my ($shmid, $size) = ($2,$5);
601 next unless ($shmid != $shm and $size == 2000);
603 ### &status("SHM: nuking shmid $shmid");
604 ### system("/usr/bin/ipcrm shm $shmid >/dev/null");
607 ### check modules if they've been modified. might be evil.
613 &ScheduleThis(5, "shmFlush");
614 &ScheduleChecked("shmFlush");
615 return if ($_[0] eq "2");
618 my $shmmsg = &shmRead($shm);
619 $shmmsg =~ s/\0//g; # remove padded \0's.
621 return if ($$ != $::bot_pid); # fork protection.
623 foreach (split '\|\|', $shmmsg) {
624 &VERB("shm: Processing '$_'.",2);
626 if (/^DCC SEND (\S+) (\S+)$/) {
627 my ($nick,$file) = ($1,$2);
628 if (exists $dcc{'SEND'}{$who}) {
629 &msg($nick,"DCC already active.");
631 &DEBUG("shm: dcc sending $2 to $1.");
632 $conn->new_send($1,$2);
633 $dcc{'SEND'}{$who} = time();
635 } elsif (/^SET FORKPID (\S+) (\S+)/) {
636 $forked{$1}{PID} = $2;
637 } elsif (/^DELETE FORK (\S+)$/) {
639 } elsif (/^EVAL (.*)$/) {
640 &DEBUG("evaling '$1'.");
643 &DEBUG("shm: unknown msg. ($_)");
647 &shmWrite($shm,"") if ($shmmsg ne "");
650 ### this is semi-scheduled
652 if ($ident eq $param{'ircNick'}) {
653 &status("okay, got my nick back.");
657 &status("Trying to get my nick back.");
658 &nick( $param{'ircNick'} );
660 &ScheduleThis(30, "getNickInUse") if (@_);
665 &ScheduleThis(60, "uptimeLoop");
666 &ScheduleChecked("uptimeLoop");
673 &ScheduleThis(60, "slashdotLoop") if (@_);
674 &ScheduleChecked("slashdotLoop");
675 return if ($_[0] eq "2");
677 my @chans = &ChanConfList("slashdotAnnounce");
678 return unless (scalar @chans);
680 &Forker("slashdot", sub {
681 my @data = &Slashdot::slashdotAnnounce();
684 next unless (&::validChan($_));
686 &::status("sending slashdot update to $_.");
689 ¬ice($c, "Slashdot: $_");
697 &ScheduleThis(60, "freshmeatLoop");
698 &ScheduleChecked("freshmeatLoop");
699 return if ($_[0] eq "2");
702 my @chans = &ChanConfList("freshmeatAnnounce");
703 return unless (scalar @chans);
705 &Forker("freshmeat", sub {
706 my $data = &Freshmeat::freshmeatAnnounce();
709 next unless (&::validChan($_));
711 &::status("sending freshmeat update to $_.");
719 &ScheduleThis(240, "kernelLoop");
720 &ScheduleChecked("kernelLoop");
721 return if ($_[0] eq "2");
724 my @chans = &ChanConfList("kernelAnnounce");
725 return unless (scalar @chans);
727 &Forker("kernel", sub {
728 my @data = &Kernel::kernelAnnounce();
731 next unless (&::validChan($_));
733 &::status("sending kernel update to $_.");
736 ¬ice($c, "Kernel: $_");
743 return unless &IsChanConf("wingate");
745 ### FILE CACHE OF OFFENDING WINGATES.
746 foreach (grep /^$host$/, @wingateBad) {
747 &status("Wingate: RUNNING ON $host BY $who");
748 &ban("*!*\@$host", "") if &IsChanConf("wingateBan");
750 my $reason = &getChanConf("wingateKick");
752 next unless ($reason);
753 &kick($who, "", $reason)
756 ### RUN CACHE OF TRIED WINGATES.
757 if (grep /^$host$/, @wingateCache) {
758 push(@wingateNow, $host); # per run.
759 push(@wingateCache, $host); # cache per run.
761 &DEBUG("Already scanned $host. good.");
764 my $interval = &getChanConfDefault("wingateInterval", 60); # seconds.
765 return if (defined $forked{'wingate'});
766 return if (time() - $wingaterun <= $interval);
767 return unless (scalar(keys %wingateToDo));
769 $wingaterun = time();
771 &Forker("wingate", sub { &Wingate::Wingates(keys %wingateToDo); } );
776 sub wingateWriteFile {
777 return unless (scalar @wingateCache);
779 my $file = "$bot_base_dir/$param{'ircUser'}.wingate";
780 if ($bot_pid != $$) {
781 &DEBUG("wingateWriteFile: Reorganising!");
786 push(@wingateNow, $_);
791 my %hash = map { $_ => 1 } @wingateNow;
792 @wingateNow = sort keys %hash;
795 &DEBUG("wingateWF: writing...");
797 foreach (@wingateNow) {
802 &ScheduleThis(60, "wingateWriteFile") if (@_);
806 my @list = &searchTable("factoids", "factoid_key", "factoid_key", " #DEL#");
807 my $stale = &getChanConfDefault("factoidDeleteDelay", 7)*60*60*24;
810 my $age = &getFactInfo($_, "modified_time");
811 next unless (time() - $age > $stale);
814 $fix =~ s/ #DEL#$//g;
815 &VERB("safedel: Removing $fix for good.",2);
819 &ScheduleThis(1440, "factoidCheck") if (@_);
823 my $time = strftime("%H:%M", localtime(time()) );
825 return unless (scalar keys %{ $DCC{CHAT} });
827 foreach (keys %channels) {
828 &DCCBroadcast("[$time] $_: $users members ($chops chops), $bans bans","+o");
831 &ScheduleThis(10, "dccStatus") if (@_);
836 &ScheduleThis(TIME_IN_MINUTES, "FUNCTION") if (@_);
847 foreach (keys %{ $irc->{_queue} }) {
851 foreach (keys %sched) {
852 next unless ($q eq $sched{$_});
857 my $time = $irc->{_queue}->{$q}->[0] - time();
859 if (defined $sched) {
860 &DEBUG(" $sched($q): ".&Time2String($time) );
862 &DEBUG(" NULL($q): ".&Time2String($time) );
866 &DEBUG("end of sList.");
869 sub getChanConfDefault {
870 my($what, $default, $chan) = @_;
872 if (exists $param{$what}) {
873 &DEBUG("backward-compat: found param{$what} instead.");
874 return $param{$what};
877 my $val = &getChanConf($what, $chan);
881 $param{$what} = $default;
882 &status("gCCD: not configured; setting param{$what} = $default");
883 ### TODO: set some vars?