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 &floodCycle(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{$_}{RUNNING} } keys %sched;
47 foreach (keys %sched) {
48 next unless (exists $sched{$_}{RUNNING});
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}) {
66 &WARN("Sched for $codename already exists.");
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 = $param{'randomQuoteInterval'} || 60;
95 &ScheduleThis($interval, "randomQuote") if (@_);
96 return if ($_[0] eq "2"); # defer.
97 &ScheduleChecked("randomQuote");
99 my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.randtext");
100 if (!defined $line) {
101 &ERROR("random Quote: weird error?");
105 foreach ( &ChanConfList("randomQuote") ) {
106 next unless (&validChan($_)); # ???
108 &status("sending random Quote to $_.");
109 &action($_, "Ponders: ".$line);
111 ### TODO: if there were no channels, don't reschedule until channel
112 ### configuration is modified.
119 my $interval = $param{'randomFactoidInterval'} || 60; # FIXME.
120 &ScheduleThis($interval, "randomFactoid") if (@_);
121 return if ($_[0] eq "2"); # defer.
122 &ScheduleChecked("randomFactoid");
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 !~ /^</);
130 &ERROR("rF: tried 5 times but failed.");
135 foreach ( &ChanConfList("randomFactoid") ) {
136 next unless (&validChan($_)); # ???
138 &status("sending random Factoid to $_.");
139 &action($_, "Thinks: \037$key\037 is $val");
140 ### FIXME: Use &getReply() on above to format factoid properly?
145 sub randomFreshmeat {
146 my $interval = $param{'randomFresheatInterval'} || 60;
147 &ScheduleThis($interval, "randomFreshmeat") if (@_);
148 return if ($_[0] eq "2"); # defer.
149 &ScheduleChecked("randomFreshmeat");
151 my @chans = &ChanConfList("randomFreshmeat");
152 return unless (scalar @chans);
154 &Forker("freshmeat", sub {
155 my $retval = &Freshmeat::randPackage();
158 next unless (&validChan($_)); # ???
160 &status("sending random Freshmeat to $_.");
168 &ScheduleThis(60, "logCycle");
169 return if ($_[0] eq "2"); # defer.
170 &ScheduleChecked("logCycle");
173 return unless (defined fileno LOG);
174 return unless (&IsParam("logfile"));
175 return unless (&IsParam("maxLogSize"));
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.");
183 if ( -e $file{log}) {
187 $newlog = $file{log}."-".$i;
188 last if (! -e $newlog);
191 $file{log} = $newlog;
195 system("/bin/mv '$param{'logfile'}' '$file{log}'");
196 &compress($file{log});
198 &status("cycling log file.");
201 ### check if all the logs exceed size.
202 my $logdir = "$bot_base_dir/log/";
203 if (opendir(LOGS, $logdir)) {
207 while (defined($_ = readdir LOGS)) {
208 my $logfile = "$logdir/$_";
210 next unless ( -f $logfile);
211 my $size = -s $logfile;
212 my $age = (stat $logfile)[9];
214 $age{$age} = $logfile;
215 $size{$logfile} = $size;
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};
231 ### TODO: add how many b,kb,mb removed?
232 &status("LOG: removed $delete logs.") if ($delete);
234 &WARN("could not open dir $logdir");
241 &ScheduleThis(1440, "seenFlushOld");
242 return if ($_[0] eq "2"); # defer.
243 &ScheduleChecked("seenFlushOld");
246 # is this global-only?
247 return unless (&IsChanConf("seen") > 0);
248 return unless (&IsChanConf("seenFlushInterval") > 0);
250 my $max_time = ($chanconf{_default}{'seenMaxDays'} || 30)
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);
259 while (my @row = $sth->fetchrow_array) {
260 my ($nick,$time) = @row;
262 &dbDel("seen","nick",$nick);
266 } elsif ($param{'DBType'} =~ /^dbm/i) {
269 foreach (keys %seen) {
270 my $delta_time = $time - &dbGet("seen", "NULL", $_, "time");
271 next unless ($delta_time > $max_time);
273 &DEBUG("seenFlushOld: ".&Time2String($delta_time) );
278 &FIXME("seenFlushOld: for PG/NO-DB.");
280 &VERB("SEEN deleted $delete seen entries.",2);
286 my $interval = &getChanConf("chanlimitcheckInterval") || 10;
287 &ScheduleThis($interval, "chanlimitCheck");
288 return if ($_[0] eq "2");
289 &ScheduleChecked("chanlimitCheck");
292 foreach ( &ChanConfList("chanlimitcheck") ) {
293 next unless (&validChan($_)); # ???
295 my $limitplus = &getChanConf("chanlimitcheckPlus",$_) || 5;
296 my $newlimit = scalar(keys %{$channels{$_}{''}}) + $limitplus;
297 my $limit = $channels{$_}{'l'};
299 if (scalar keys %{$channels{$_}{''}} > $limit) {
300 &status("LIMIT: set too low!!! FIXME");
301 ### run NAMES again and flush it.
304 next unless (!defined $limit or $limit != $newlimit);
306 if (!exists $channels{$_}{'o'}{$ident}) {
307 &ERROR("chanlimitcheck: dont have ops on $_.");
310 &rawout("MODE $_ +l $newlimit");
319 &ScheduleThis(30, "netsplitCheck");
320 return if ($_[0] eq "2");
321 &ScheduleChecked("netsplitCheck");
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};
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{$_};
339 next unless (time() - $netsplit{$_} > 60*60*2); # 2 hours.
340 next if (&IsNickInAnyChan($_));
342 &DEBUG("netsplitC: $_ didn't come back from netsplit in 2 hours; removing from netsplit list.");
343 delete $netsplit{$_};
352 &ScheduleThis(60, "floodCycle"); # minutes.
353 return if ($_[0] eq "2");
354 &ScheduleChecked("floodCycle");
358 foreach $who (keys %flood) {
359 foreach (keys %{$flood{$who}}) {
360 if ($time - $flood{$who}{$_} > $interval) {
361 delete $flood{$who}{$_};
366 &VERB("floodCycle: deleted $delete items.",2);
374 $stats{'count_old'} = &countKeys("seen");
379 my $interval = $param{'seenFlushInterval'} || 60;
380 &ScheduleThis($interval, "seenFlush");
381 return if ($_[0] eq "2");
382 &ScheduleChecked("seenFlush");
385 if ($param{'DBType'} =~ /^mysql|pg|postgres/i) {
386 foreach $nick (keys %seencache) {
387 my $exists = &dbGet("seen","nick", $nick, "nick");
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'},
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'},
407 ### TODO: put bad nick into a list and don't do it again!
408 &FIXME("Should never happen! (nick => $nick)") if !$retval;
411 delete $seencache{$nick};
415 } elsif ($param{'DBType'} =~ /^dbm/i) {
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'},
426 ### TODO: put bad nick into a list and don't do it again!
427 &FIXME("Should never happen! (nick => $nick)") if !$retval;
429 delete $seencache{$nick};
433 &DEBUG("seenFlush: NO VALID FACTOID SUPPORT?");
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'});
444 &WARN("scalar keys seenflush != 0!") if (scalar keys %seenflush);
453 &ScheduleThis(60, "leakCheck");
454 return if ($_[0] eq "2");
455 &ScheduleChecked("leakCheck");
459 foreach $blah1 (keys %flood) {
460 foreach $blah2 (keys %{$flood{$blah1}}) {
461 $count += scalar(keys %{$flood{$blah1}{$blah2}});
464 &VERB("\%flood has $count total keys.",2);
467 foreach $chan (grep /[A-Z]/, keys %channels) {
468 &DEBUG("leak: chan => '$chan'.");
470 foreach $i (keys %{$channels{$chan}}) {
471 foreach (keys %{$channels{$chan}{$i}}) {
472 &DEBUG("leak: \$channels{$chan}{$i}{$_} ...");
482 foreach (keys %ignore) {
485 foreach (keys %{ $ignore{$chan} }) {
486 my @array = $ignore{$chan}{$_};
493 next unless ($time > $ignore{$_});
495 delete $ignore{$chan}{$_};
496 &status("ignore: $_/$chan has expired.");
500 &VERB("ignore: $count items deleted.",2);
502 &ScheduleThis(60, "ignoreCheck") if (@_);
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.");
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.
519 if ($ident !~ /^\Q$param{ircNick}\E$/) {
520 &WARN("ircCheck: ident($ident) != param{ircNick}($param{IrcNick}).");
524 # if scalar @joinnext => join more channels
525 # else check for chanserv.
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) {
537 if ($utime_userfile > $wtime_userfile and time() - $wtime_userfile > 3600) {
539 $wtime_userfile = time();
542 if ($utime_chanfile > $wtime_chanfile and time() - $wtime_chanfile > 3600) {
544 $wtime_chanfile = time();
547 &ScheduleThis(240, "ircCheck") if (@_);
553 if ( -x "/usr/bin/ipcs") {
554 @ipcs = `/usr/bin/ipcs`;
556 &WARN("ircCheck: no 'ipcs' binary.");
559 # shmid stale remove.
563 # key, shmid, owner, perms, bytes, nattch
564 next unless (/^(0x\d+) (\d+)\s+(\S+)\s+(\d+)\s+(\d+)\s+/);
566 my ($shmid, $size) = ($2,$5);
567 next unless ($shmid != $shm and $size == 2000);
569 &status("SHM: nuking shmid $shmid");
570 system("/usr/bin/ipcrm shm $shmid >/dev/null");
573 ### check modules if they've been modified. might be evil.
576 &ScheduleThis(240, "miscCheck") if (@_);
581 &ScheduleThis(5, "shmFlush");
582 return if ($_[0] eq "2");
583 &ScheduleChecked("shmFlush");
586 my $shmmsg = &shmRead($shm);
587 $shmmsg =~ s/\0//g; # remove padded \0's.
589 return if ($$ != $::bot_pid); # fork protection.
591 foreach (split '\|\|', $shmmsg) {
592 &VERB("shm: Processing '$_'.",2);
594 if (/^DCC SEND (\S+) (\S+)$/) {
595 my ($nick,$file) = ($1,$2);
596 if (exists $dcc{'SEND'}{$who}) {
597 &msg($nick,"DCC already active.");
599 &DEBUG("shm: dcc sending $2 to $1.");
600 $conn->new_send($1,$2);
601 $dcc{'SEND'}{$who} = time();
603 } elsif (/^SET FORKPID (\S+) (\S+)/) {
604 $forked{$1}{PID} = $2;
605 } elsif (/^DELETE FORK (\S+)$/) {
607 } elsif (/^EVAL (.*)$/) {
608 &DEBUG("evaling '$1'.");
611 &DEBUG("shm: unknown msg. ($_)");
615 &shmWrite($shm,"") if ($shmmsg ne "");
618 ### this is semi-scheduled
620 if ($ident eq $param{'ircNick'}) {
621 &status("okay, got my nick back.");
625 &status("Trying to get my nick back.");
626 &nick($param{'ircNick'});
628 &ScheduleThis(5, "getNickInUse") if (@_);
634 &ScheduleThis(60, "uptimeCycle") if (@_);
638 &ScheduleThis(60, "slashdotCycle") if (@_);
639 return if ($_[0] eq "2");
640 &ScheduleChecked("slashdotCycle");
642 my @chans = &ChanConfList("slashdotAnnounce");
643 return unless (scalar @chans);
645 &Forker("slashdot", sub {
646 my @data = &Slashdot::slashdotAnnounce();
649 next unless (&::validChan($_));
651 &::status("sending slashdot update to $_.");
654 ¬ice($c, "Slashdot: $_");
661 &ScheduleThis(60, "freshmeatCycle") if (@_);
662 return if ($_[0] eq "2");
663 &ScheduleChecked("freshmeatCycle");
665 my @chans = &ChanConfList("freshmeatAnnounce");
666 return unless (scalar @chans);
668 &Forker("freshmeat", sub {
669 my $data = &Freshmeat::freshmeatAnnounce();
672 next unless (&::validChan($_));
674 &::status("sending freshmeat update to $_.");
681 &ScheduleThis(240, "kernelCycle") if (@_);
682 return if ($_[0] eq "2");
683 &ScheduleChecked("kernelCycle");
685 my @chans = &ChanConfList("kernelAnnounce");
686 return unless (scalar @chans);
688 &Forker("kernel", sub {
689 my @data = &Kernel::kernelAnnounce();
692 next unless (&::validChan($_));
694 &::status("sending kernel update to $_.");
697 ¬ice($c, "Kernel: $_");
704 return unless &IsParam("wingate");
705 return unless ($param{'wingate'} =~ /^(.*\s+)?$chan(\s+.*)?$/i);
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");
712 next unless (&IsParam("wingateKick"));
713 &kick($who, "", $param{'wingateKick'})
716 ### RUN CACHE OF TRIED WINGATES.
717 if (grep /^$host$/, @wingateCache) {
718 push(@wingateNow, $host); # per run.
719 push(@wingateCache, $host); # cache per run.
721 &DEBUG("Already scanned $host. good.");
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));
729 $wingaterun = time();
731 &Forker("wingate", sub { &Wingate::Wingates(keys %wingateToDo); } );
736 sub wingateWriteFile {
737 return unless (scalar @wingateCache);
739 my $file = "$bot_base_dir/$param{'ircUser'}.wingate";
740 if ($bot_pid != $$) {
741 &DEBUG("wingateWriteFile: Reorganising!");
746 push(@wingateNow, $_);
751 my %hash = map { $_ => 1 } @wingateNow;
752 @wingateNow = sort keys %hash;
755 &DEBUG("wingateWF: writing...");
757 foreach (@wingateNow) {
762 &ScheduleThis(60, "wingateWriteFile") if (@_);
766 my @list = &searchTable("factoids", "factoid_key", "factoid_key", " #DEL#");
767 my $stale = $param{'factoidDeleteDelay'}*60*60*24;
770 my $age = &getFactInfo($_, "modified_time");
771 next unless (time() - $age > $stale);
774 $fix =~ s/ #DEL#$//g;
775 &VERB("safedel: Removing $fix for good.",2);
779 &ScheduleThis(1440, "factoidCheck") if (@_);
783 my $time = strftime("%H:%M", localtime(time()) );
785 return unless (scalar keys %{ $DCC{CHAT} });
787 foreach (keys %channels) {
788 &DCCBroadcast("[$time] $_: $users members ($chops chops), $bans bans","+o");
791 &ScheduleThis(10, "dccStatus") if (@_);
796 &ScheduleThis(TIME_IN_MINUTES, "FUNCTION") if (@_);
807 foreach (keys %{ $irc->{_queue} }) {
811 foreach (keys %sched) {
812 next unless ($q eq $sched{$_});
817 my $time = $irc->{_queue}->{$q}->[0] - time();
819 if (defined $sched) {
820 &DEBUG(" $sched($q): ".&Time2String($time) );
822 &DEBUG(" NULL($q): ".&Time2String($time) );
826 &DEBUG("end of sList.");