2 # ProcessExtra.pl: Extensions to Process.pl
4 # Version: v0.3 (20000707)
8 if (&IsParam("useStrict")) { use strict; }
11 &VERB("Starting schedulers...",2);
16 &uptimeCycle(1) if (&IsParam("uptime"));
17 &randomQuote(1) if (&IsParam("randomQuote"));
18 &randomFactoid(1) if (&IsParam("randomFactoid"));
19 &logCycle(1) if ($loggingstatus and &IsParam("logFile") and &IsParam("maxLogSize"));
20 &limitCheck(1) if (&IsParam("limitcheck"));
21 &netsplitCheck(1); # mandatory
22 &floodCycle(1); # mandatory
23 &seenFlush(1) if (&IsParam("seen") and &IsParam("seenFlushInterval"));
24 &leakCheck(1); # mandatory
25 &ignoreListCheck(1);# mandatory
26 &seenFlushOld(1) if (&IsParam("seen"));
27 &ircCheck(1); # mandatory
28 &miscCheck(1); # mandatory
29 &shmFlush(1); # mandatory
30 &slashdotCycle(1) if (&IsParam("slashdot") and &IsParam("slashdotAnnounce"));
31 &freshmeatCycle(1) if (&IsParam("freshmeat") and &IsParam("freshmeatAnnounce"));
32 &kernelCycle(1) if (&IsParam("kernel") and &IsParam("kernelAnnounce"));
33 &wingateWriteFile(1) if (&IsParam("wingate"));
37 my ($interval, $codename, @args) = @_;
38 my $waittime = &getRandomInt($interval);
40 &VERB("Scheduling \&$codename() for ".&Time2String($waittime),3);
41 $conn->schedule($waittime, \&$codename, @args);
45 my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.randtext");
47 &ERROR("random Quote: weird error?");
51 my @channels = split(/[\s\t]+/, lc $param{'randomQuoteChannels'});
52 @channels = keys(%channels) unless (scalar @channels);
56 next unless (&validChan($_));
58 &status("sending random Quote to $_.");
59 &action($_, "Ponders: ".$line);
64 &WARN("randomQuote: no valid channels?");
68 my $interval = $param{'randomQuoteInterval'} || 60;
69 &ScheduleThis($interval, "randomQuote") if (@_);
76 ($key,$val) = &randKey("factoids","factoid_key,factoid_value");
77 ### $val =~ tr/^[A-Z]/[a-z]/; # blah is Good => blah is good.
78 last if ($val !~ /^</);
81 &ERROR("rF: tried 5 times but failed.");
86 my @channels = split(/[\s\t]+/, lc $param{'randomFactoidChannels'});
87 @channels = keys(%channels) unless (scalar @channels);
91 next unless (&validChan($_));
93 &status("sending random Factoid to $_.");
94 ### &msg($_, "$key is $val");
95 &action($_, "Thinks: \037$key\037 is $val");
96 ### FIXME: Use &getReply() on above to format factoid properly?
101 &WARN("randomFactoid: no valid channels?");
105 my $interval = $param{'randomFactoidInterval'} || 60;
106 &ScheduleThis($interval, "randomFactoid") if (@_);
110 # check if current size is too large.
111 if ( -s $file{log} > $param{'maxLogSize'}) {
112 my $date = sprintf("%04d%02d%02d", (localtime)[5,4,3]);
113 $file{log} = $param{'logfile'} ."-". $date;
114 &status("cycling log file.");
116 if ( -e $file{log}) {
120 $newlog = $file{log}."-".$i;
121 last if (! -e $newlog);
124 $file{log} = $newlog;
128 system("/bin/mv '$param{'logfile'}' '$file{log}'");
129 &compress($file{log});
131 &status("cycling log file.");
134 # check if all the logs exceed size.
135 my $logdir = "$bot_base_dir/log/";
136 if (opendir(LOGS, $logdir)) {
140 while (defined($_ = readdir LOGS)) {
141 my $logfile = "$logdir/$_";
143 next unless ( -f $logfile);
144 my $size = -s $logfile;
145 my $age = (stat $logfile)[9]; ### or 8 ?
147 $age{$age} = $logfile;
148 $size{$logfile} = $size;
155 while ($tsize > $param{'maxLogSize'}) {
156 &status("LOG: current size > max ($tsize > $param{'maxLogSize'})");
157 my $oldest = (sort {$a <=> $b} keys %age)[0];
158 &status("LOG: unlinking $age{$oldest}.");
160 # unlink $age{$oldest};
165 ### TODO: add how many b,kb,mb removed?
167 &status("LOG: removed $delete logs.");
170 &WARN("could not open dir $logdir");
173 &ScheduleThis(60, "logCycle") if (@_);
177 my $max_time = $param{'seenMaxDays'}*60*60*24;
180 if ($param{'DBType'} =~ /^pg|postgres|mysql/i) {
181 my $query = "SELECT nick,time FROM seen GROUP BY nick HAVING UNIX_TIMESTAMP() - time > $max_time";
182 my $sth = $dbh->prepare($query);
185 while (my @row = $sth->fetchrow_array) {
186 my ($nick,$time) = @row;
188 &dbDel("seen","nick",$nick);
192 } elsif ($param{'DBType'} =~ /^dbm/i) {
195 foreach (keys %seen) {
196 my $delta_time = $time - &dbGet("seen", "NULL", $_, "time");
197 next unless ($delta_time > $max_time);
199 &DEBUG("seenFlushOld: ".&Time2String($delta_time) );
204 &FIXME("seenFlushOld: for PG/NO-DB.");
206 &VERB("SEEN deleted $delete seen entries.",2);
208 &ScheduleThis(1440, "seenFlushOld") if (@_);
212 my $limitplus = $param{'limitcheckPlus'} || 5;
214 if (scalar keys %netsplit) {
215 &status("limitcheck: netsplit active.");
219 my @channels = split(/[\s\t]+/, lc $param{'limitcheck'});
221 foreach (@channels) {
222 next unless (&validChan($_));
224 if (!exists $channels{$_}{'o'}{$ident}) {
225 &ERROR("limitcheck: dont have ops on $_.");
229 my $newlimit = scalar(keys %{$channels{$_}{''}}) + $limitplus;
230 my $limit = $channels{$_}{'l'};
232 next unless (!defined $limit or $limit != $newlimit);
234 &rawout("MODE $_ +l $newlimit");
237 my $interval = $param{'limitcheckInterval'} || 10;
238 &ScheduleThis($interval, "limitCheck") if (@_);
244 foreach $s1 (keys %netsplitservers) {
245 foreach $s2 (keys %{$netsplitservers{$s1}}) {
246 if (time() - $netsplitservers{$s1}{$s2} > 3600) {
247 &status("netsplit between $s1 and $s2 appears to be stale.");
248 delete $netsplitservers{$s1}{$s2};
253 # %netsplit hash checker.
254 foreach (keys %netsplit) {
255 if (&IsNickInAnyChan($_)) {
256 &DEBUG("netsplitC: $_ is in some chan; removing from netsplit list.");
257 delete $netsplit{$_};
259 next unless (time() - $netsplit{$_} > 60*60*2); # 2 hours.
261 if (!&IsNickInAnyChan($_)) {
262 &DEBUG("netsplitC: $_ didn't come back from netsplit in 2 hours; removing from netsplit list.");
263 delete $netsplit{$_};
267 &ScheduleThis(30, "netsplitCheck") if (@_);
271 my $interval = $param{'floodInterval'} || 60; # seconds.
275 foreach $who (keys %flood) {
276 foreach (keys %{$flood{$who}}) {
277 if (time() - $flood{$who}{$_} > $interval) {
278 delete $flood{$who}{$_};
283 &VERB("floodCycle: deleted $delete items.",2);
285 &ScheduleThis($interval, "floodCycle") if (@_); # minutes.
292 if ($param{'DBType'} =~ /^mysql|pg|postgres/i) {
293 foreach $nick (keys %seencache) {
294 my $exists = &dbGet("seen","nick", $nick, "nick");
296 if (defined $exists and $exists) {
297 &dbUpdate("seen", "nick", $nick, (
298 "time" => $seencache{$nick}{'time'},
299 "host" => $seencache{$nick}{'host'},
300 "channel" => $seencache{$nick}{'chan'},
301 "message" => $seencache{$nick}{'msg'},
304 my $retval = &dbInsert("seen", $nick, (
305 "nick" => $seencache{$nick}{'nick'},
306 "time" => $seencache{$nick}{'time'},
307 "host" => $seencache{$nick}{'host'},
308 "channel" => $seencache{$nick}{'chan'},
309 "message" => $seencache{$nick}{'msg'},
312 ### TODO: put bad nick into a list and don't do it again!
314 &ERROR("Should never happen! (nick => $nick) FIXME");
318 delete $seencache{$nick};
322 } elsif ($param{'DBType'} =~ /^dbm/i) {
324 foreach $nick (keys %seencache) {
325 my $retval = &dbInsert("seen", $nick, (
326 "nick" => $seencache{$nick}{'nick'},
327 "time" => $seencache{$nick}{'time'},
328 "host" => $seencache{$nick}{'host'},
329 "channel" => $seencache{$nick}{'chan'},
330 "message" => $seencache{$nick}{'msg'},
333 ### TODO: put bad nick into a list and don't do it again!
335 &ERROR("Should never happen! (nick => $nick) FIXME");
338 delete $seencache{$nick};
342 &DEBUG("seenFlush: NO VALID FACTOID SUPPORT?");
345 &VERB("Flushed $flushed seen entries.",1); # was 2.
346 &DEBUG("seen: ".scalar(keys %seenflush)." remaining.");
348 my $interval = $param{'seenFlushInterval'} || 60;
349 &ScheduleThis($interval, "seenFlush") if (@_);
357 foreach $blah1 (keys %flood) {
358 foreach $blah2 (keys %{$flood{$blah1}}) {
359 $count += scalar(keys %{$flood{$blah1}{$blah2}});
362 &VERB("\%flood has $count total keys.",2);
365 foreach $chan (grep /[A-Z]/, keys %channels) {
366 &DEBUG("leak: chan => '$chan'.");
368 foreach $i (keys %{$channels{$chan}}) {
369 foreach (keys %{$channels{$chan}{$i}}) {
370 &DEBUG("leak: \$channels{$chan}{$i}{$_} ...");
375 &ScheduleThis(60, "leakCheck") if (@_);
378 sub ignoreListCheck {
382 foreach (keys %ignoreList) {
383 next if ($ignoreList{$_} == 1);
384 next unless ($time > $ignoreList{$_});
386 delete $ignoreList{$_};
387 &status("ignore: $_ has expired.");
390 &VERB("ignore: $count items deleted.",2);
392 &ScheduleThis(30, "ignoreListCheck") if (@_);
396 my @array = split /[\t\s]+/, $param{'join_channels'};
397 my $iconf = scalar(@array);
398 my $inow = scalar(keys %channels);
399 if ($iconf > 2 and $inow * 2 <= $iconf) {
400 &FIXME("ircCheck: current channels * 2 <= config channels. FIXME.");
403 if (!$conn->connected and time - $msgtime > 3600) {
404 &WARN("ircCheck: no msg for 3600 and disco'd! reconnecting!");
405 $msgtime = time(); # just in case.
409 if ($ident !~ /^\Q$param{ircNick}\E$/) {
410 &WARN("ircCheck: ident($ident) != param{ircNick}($param{IrcNick}).");
413 if (scalar @joinchan) {
414 &WARN("We have some channels to join, doing so.");
418 if (grep /^\s*$/, keys %channels) {
419 &WARN("we have a NULL chan in hash channels? removing!");
420 delete $channels{''};
421 &status("channels now:");
422 foreach (keys %channels) {
428 &ScheduleThis(240, "ircCheck") if (@_);
434 if ( -x "/usr/bin/ipcs") {
435 @ipcs = `/usr/bin/ipcs`;
437 &WARN("ircCheck: no 'ipcs' binary.");
440 # shmid stale remove.
444 # key, shmid, owner, perms, bytes, nattch
445 next unless (/^(0x\d+) (\d+)\s+(\S+)\s+(\d+)\s+(\d+)\s+/);
447 my ($shmid, $size) = ($2,$5);
448 next unless ($shmid != $shm and $size == 2000);
450 &status("SHM: nuking shmid $shmid");
451 system("/usr/bin/ipcrm shm $shmid >/dev/null");
454 ### check modules if they've been modified. might be evil.
457 &ScheduleThis(240, "miscCheck") if (@_);
461 my $shmmsg = &shmRead($shm);
462 $shmmsg =~ s/\0//g; # remove padded \0's.
464 foreach (split '\|\|', $shmmsg) {
465 &status("shm: Processing '$_'.");
467 if (/^DCC SEND (\S+) (\S+)$/) {
468 my ($nick,$file) = ($1,$2);
469 if (exists $dcc{'SEND'}{$who}) {
470 &msg($nick,"DCC already active.");
472 &DEBUG("shm: dcc sending $2 to $1.");
473 $conn->new_send($1,$2);
474 $dcc{'SEND'}{$who} = time();
476 } elsif (/^DELETE FORK (\S+)$/) {
478 } elsif (/^EVAL (.*)$/) {
479 &DEBUG("evaling '$1'.");
482 &DEBUG("shm: unknown msg. ($_)");
486 &shmWrite($shm,"") if ($shmmsg ne "");
488 &ScheduleThis(5, "shmFlush") if (@_);
492 if ($ident eq $param{'ircNick'}) {
493 &status("okay, got my nick back.");
497 &status("Trying to get my nick back.");
498 &nick($param{'ircNick'});
500 &ScheduleThis(5, "getNickInUse") if (@_);
506 &ScheduleThis(60, "uptimeCycle") if (@_);
510 &Forker("slashdot", sub { &Slashdot::slashdotAnnounce(); } );
512 &ScheduleThis(60, "slashdotCycle") if (@_);
516 &Forker("freshmeat", sub { &Freshmeat::freshmeatAnnounce(); } );
518 &ScheduleThis(60, "freshmeatCycle") if (@_);
522 &Forker("kernel", sub { &Kernel::kernelAnnounce(); } );
524 &ScheduleThis(240, "kernelCycle") if (@_);
528 return unless &IsParam("wingate");
529 return unless ($param{'wingate'} =~ /^(.*\s+)?$chan(\s+.*)?$/i);
531 ### FILE CACHE OF OFFENDING WINGATES.
532 foreach (grep /^$host$/, @wingateBad) {
533 &status("Wingate: RUNNING ON $host BY $who");
534 &ban("*!*\@$host", "") if &IsParam("wingateBan");
536 next unless (&IsParam("wingateKick"));
537 &kick($who, "", $param{'wingateKick'})
540 ### RUN CACHE OF TRIED WINGATES.
541 if (grep /^$host$/, @wingateCache) {
542 push(@wingateNow, $host); # per run.
543 push(@wingateCache, $host); # cache per run.
545 &DEBUG("Already scanned $host. good.");
548 my $interval = $param{'wingateInterval'} || 60; # seconds.
549 return if (defined $forked{'wingate'});
550 return if (time() - $wingaterun <= $interval);
551 return unless (scalar(keys %wingateToDo));
553 $wingaterun = time();
555 &Forker("wingate", sub { &Wingate::Wingates(keys %wingateToDo); } );
560 sub wingateWriteFile {
561 return unless (scalar @wingateCache);
563 my $file = "$bot_base_dir/$param{'ircUser'}.wingate";
564 if ($bot_pid != $$) {
565 &DEBUG("wingateWriteFile: Reorganising!");
570 push(@wingateNow, $_);
575 my %hash = map { $_ => 1 } @wingateNow;
576 @wingateNow = sort keys %hash;
579 &DEBUG("wingateWF: writing...");
581 foreach (@wingateNow) {
586 &ScheduleThis(60, "wingateWriteFile") if (@_);