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 &shmFlush(1); # mandatory
29 &slashdotCycle(1) if (&IsParam("slashdot") and &IsParam("slashdotAnnounce"));
30 &freshmeatCycle(1) if (&IsParam("freshmeat") and &IsParam("freshmeatAnnounce"));
31 &kernelCycle(1) if (&IsParam("kernel") and &IsParam("kernelAnnounce"));
32 &wingateWriteFile(1) if (&IsParam("wingate"));
36 my ($interval, $codename, @args) = @_;
37 my $waittime = &getRandomInt($interval);
39 &VERB("Scheduling \&$codename() for ".&Time2String($waittime),3);
40 $conn->schedule($waittime, \&$codename, @args);
44 my $line = &getRandomLineFromFile($bot_misc_dir. "/blootbot.randtext");
46 &ERROR("random Quote: weird error?");
50 my @channels = split(/[\s\t]+/, lc $param{'randomQuoteChannels'});
51 @channels = keys(%channels) unless (scalar @channels);
55 next unless (&validChan($_));
57 &status("sending random Quote to $_.");
58 &action($_, "Ponders: ".$line);
63 &WARN("randomQuote: no valid channels?");
67 my $interval = $param{'randomQuoteInterval'} || 60;
68 &ScheduleThis($interval, "randomQuote") if (@_);
75 ($key,$val) = &randKey("factoids","factoid_key,factoid_value");
76 ### $val =~ tr/^[A-Z]/[a-z]/; # blah is Good => blah is good.
77 last if ($val !~ /^</);
80 &ERROR("rF: tried 5 times but failed.");
85 my @channels = split(/[\s\t]+/, lc $param{'randomFactoidChannels'});
86 @channels = keys(%channels) unless (scalar @channels);
90 next unless (&validChan($_));
92 &status("sending random Factoid to $_.");
93 ### &msg($_, "$key is $val");
94 &action($_, "Thinks: \037$key\037 is $val");
95 ### FIXME: Use &getReply() on above to format factoid properly?
100 &WARN("randomFactoid: no valid channels?");
104 my $interval = $param{'randomFactoidInterval'} || 60;
105 &ScheduleThis($interval, "randomFactoid") if (@_);
109 # check if current size is too large.
110 if ( -s $file{log} > $param{'maxLogSize'}) {
111 my $date = sprintf("%04d%02d%02d", (localtime)[5,4,3]);
112 $file{log} = $param{'logfile'} ."-". $date;
113 &status("cycling log file.");
115 if ( -e $file{log}) {
119 $newlog = $file{log}."-".$i;
120 last if (! -e $newlog);
123 $file{log} = $newlog;
127 system("/bin/mv '$param{'logfile'}' '$file{log}'");
128 &compress($file{log});
130 &status("cycling log file.");
133 # check if all the logs exceed size.
134 my $logdir = "$bot_base_dir/log/";
135 if (opendir(LOGS, $logdir)) {
139 while (defined($_ = readdir LOGS)) {
140 my $logfile = "$logdir/$_";
142 next unless ( -f $logfile);
143 my $size = -s $logfile;
144 my $age = (stat $logfile)[9]; ### or 8 ?
146 $age{$age} = $logfile;
147 $size{$logfile} = $size;
154 while ($tsize > $param{'maxLogSize'}) {
155 &status("LOG: current size > max ($tsize > $param{'maxLogSize'})");
156 my $oldest = (sort {$a <=> $b} keys %age)[0];
157 &status("LOG: unlinking $age{$oldest}.");
159 # unlink $age{$oldest};
164 ### TODO: add how many b,kb,mb removed?
166 &status("LOG: removed $delete logs.");
169 &WARN("could not open dir $logdir");
172 &ScheduleThis(60, "logCycle") if (@_);
176 my $max_time = $param{'seenMaxDays'}*60*60*24;
179 if ($param{'DBType'} =~ /^pg|postgres|mysql/i) {
180 my $query = "SELECT nick,time FROM seen GROUP BY nick HAVING UNIX_TIMESTAMP() - time > $max_time";
181 my $sth = $dbh->prepare($query);
184 while (my @row = $sth->fetchrow_array) {
185 my ($nick,$time) = @row;
187 &dbDel("seen","nick",$nick);
191 } elsif ($param{'DBType'} =~ /^dbm/i) {
194 foreach (keys %seen) {
195 my $delta_time = $time - &dbGet("seen", "NULL", $_, "time");
196 next unless ($delta_time > $max_time);
198 &DEBUG("seenFlushOld: ".&Time2String($delta_time) );
203 &FIXME("seenFlushOld: for PG/NO-DB.");
205 &VERB("SEEN deleted $delete seen entries.",2);
207 &ScheduleThis(1440, "seenFlushOld") if (@_);
211 my $limitplus = $param{'limitcheckPlus'} || 5;
213 if (scalar keys %netsplit) {
214 &status("limitcheck: netsplit active.");
218 my @channels = split(/[\s\t]+/, lc $param{'limitcheck'});
220 foreach (@channels) {
221 next unless (&validChan($_));
223 if (!exists $channels{$_}{'o'}{$ident}) {
224 &ERROR("limitcheck: dont have ops on $_.");
228 my $newlimit = scalar(keys %{$channels{$_}{''}}) + $limitplus;
229 my $limit = $channels{$_}{'l'};
231 next unless (!defined $limit or $limit != $newlimit);
233 &rawout("MODE $_ +l $newlimit");
236 my $interval = $param{'limitcheckInterval'} || 10;
237 &ScheduleThis($interval, "limitCheck") if (@_);
243 foreach $s1 (keys %netsplitservers) {
244 foreach $s2 (keys %{$netsplitservers{$s1}}) {
245 if (time() - $netsplitservers{$s1}{$s2} > 3600) {
246 &status("netsplit between $s1 and $s2 appears to be stale.");
247 delete $netsplitservers{$s1}{$s2};
252 # %netsplit hash checker.
253 foreach (keys %netsplit) {
254 if (&IsNickInAnyChan($_)) {
255 &DEBUG("netsplitC: $_ is in some chan; removing from netsplit list.");
256 delete $netsplit{$_};
258 next unless (time() - $netsplit{$_} > 60*60*2); # 2 hours.
260 if (!&IsNickInAnyChan($_)) {
261 &DEBUG("netsplitC: $_ didn't come back from netsplit in 2 hours; removing from netsplit list.");
262 delete $netsplit{$_};
266 &ScheduleThis(30, "netsplitCheck") if (@_);
270 my $interval = $param{'floodInterval'} || 60; # seconds.
274 foreach $who (keys %flood) {
275 foreach (keys %{$flood{$who}}) {
276 if (time() - $flood{$who}{$_} > $interval) {
277 delete $flood{$who}{$_};
282 &VERB("floodCycle: deleted $delete items.",2);
284 &ScheduleThis($interval, "floodCycle") if (@_); # minutes.
291 if ($param{'DBType'} =~ /^mysql|pg|postgres/i) {
292 foreach $nick (keys %seencache) {
293 my $exists = &dbGet("seen","nick", $nick, "nick");
295 if (defined $exists and $exists) {
296 &dbUpdate("seen", "nick", $nick, (
297 "time" => $seencache{$nick}{'time'},
298 "host" => $seencache{$nick}{'host'},
299 "channel" => $seencache{$nick}{'chan'},
300 "message" => $seencache{$nick}{'msg'},
303 my $retval = &dbInsert("seen", $nick, (
304 "nick" => $seencache{$nick}{'nick'},
305 "time" => $seencache{$nick}{'time'},
306 "host" => $seencache{$nick}{'host'},
307 "channel" => $seencache{$nick}{'chan'},
308 "message" => $seencache{$nick}{'msg'},
311 ### TODO: put bad nick into a list and don't do it again!
313 &ERROR("Should never happen! (nick => $nick) FIXME");
317 delete $seencache{$nick};
321 } elsif ($param{'DBType'} =~ /^dbm/i) {
323 foreach $nick (keys %seencache) {
324 my $retval = &dbInsert("seen", $nick, (
325 "nick" => $seencache{$nick}{'nick'},
326 "time" => $seencache{$nick}{'time'},
327 "host" => $seencache{$nick}{'host'},
328 "channel" => $seencache{$nick}{'chan'},
329 "message" => $seencache{$nick}{'msg'},
332 ### TODO: put bad nick into a list and don't do it again!
334 &ERROR("Should never happen! (nick => $nick) FIXME");
337 delete $seencache{$nick};
341 &DEBUG("seenFlush: NO VALID FACTOID SUPPORT?");
344 &VERB("Flushed $flushed seen entries.",1); # was 2.
345 &DEBUG("seen: ".scalar(keys %seenflush)." remaining.");
347 my $interval = $param{'seenFlushInterval'} || 60;
348 &ScheduleThis($interval, "seenFlush") if (@_);
356 foreach $blah1 (keys %flood) {
357 foreach $blah2 (keys %{$flood{$blah1}}) {
358 $count += scalar(keys %{$flood{$blah1}{$blah2}});
361 &VERB("\%flood has $count total keys.",2);
364 foreach $chan (grep /[A-Z]/, keys %channels) {
365 &DEBUG("leak: chan => '$chan'.");
367 foreach $i (keys %{$channels{$chan}}) {
368 foreach (keys %{$channels{$chan}{$i}}) {
369 &DEBUG("leak: \$channels{$chan}{$i}{$_} ...");
374 &ScheduleThis(60, "leakCheck") if (@_);
377 sub ignoreListCheck {
381 foreach (keys %ignoreList) {
382 next if ($ignoreList{$_} == 1);
383 next unless ($time > $ignoreList{$_});
385 delete $ignoreList{$_};
386 &status("ignore: $_ has expired.");
389 &VERB("ignore: $count items deleted.",2);
391 &ScheduleThis(30, "ignoreListCheck") if (@_);
395 my @array = split /[\t\s]+/, $param{'join_channels'};
396 my $iconf = scalar(@array);
397 my $inow = scalar(keys %channels);
398 if ($iconf > 2 and $inow * 2 <= $iconf) {
399 &FIXME("ircCheck: current channels * 2 <= config channels. FIXME.");
403 if ( -x "/usr/bin/ipcs") {
404 @ipcs = `/usr/bin/ipcs`;
406 &WARN("ircCheck: no 'ipcs' binary.");
409 # shmid stale remove.
413 # key, shmid, owner, perms, bytes, nattch
414 next unless (/^(0x\d+) (\d+)\s+(\S+)\s+(\d+)\s+(\d+)\s+/);
416 my ($shmid, $size) = ($2,$5);
417 next unless ($shmid != $shm and $size == 2000);
419 &status("SHM: nuking shmid $shmid");
420 system("/usr/bin/ipcrm shm $shmid >/dev/null");
423 if (!$conn->connected and time - $msgtime > 3600) {
424 &WARN("ircCheck: no msg for 3600 and disco'd! reconnecting!");
425 $msgtime = time(); # just in case.
429 if ($ident !~ /^\Q$param{ircNick}\E$/) {
430 &WARN("ircCheck: ident($ident) != param{ircNick}($param{IrcNick}).");
433 if (scalar @joinchan) {
434 &WARN("We have some channels to join, doing so.");
438 &ScheduleThis(240, "ircCheck") if (@_);
442 my $shmmsg = &shmRead($shm);
443 $shmmsg =~ s/\0//g; # remove padded \0's.
445 foreach (split '\|\|', $shmmsg) {
446 &status("shm: Processing '$_'.");
448 if (/^DCC SEND (\S+) (\S+)$/) {
449 my ($nick,$file) = ($1,$2);
450 if (exists $dcc{'SEND'}{$who}) {
451 &msg($nick,"DCC already active.");
453 &DEBUG("shm: dcc sending $2 to $1.");
454 $conn->new_send($1,$2);
455 $dcc{'SEND'}{$who} = time();
457 } elsif (/^DELETE FORK (\S+)$/) {
459 } elsif (/^EVAL (.*)$/) {
460 &DEBUG("evaling '$1'.");
463 &DEBUG("shm: unknown msg. ($_)");
467 &shmWrite($shm,"") if ($shmmsg ne "");
469 &ScheduleThis(5, "shmFlush") if (@_);
473 if ($ident eq $param{'ircNick'}) {
474 &status("okay, got my nick back.");
478 &status("Trying to get my nick back.");
479 &nick($param{'ircNick'});
481 &ScheduleThis(5, "getNickInUse") if (@_);
487 &ScheduleThis(60, "uptimeCycle") if (@_);
491 &Forker("slashdot", sub { &Slashdot::slashdotAnnounce(); } );
493 &ScheduleThis(60, "slashdotCycle") if (@_);
497 &Forker("freshmeat", sub { &Freshmeat::freshmeatAnnounce(); } );
499 &ScheduleThis(60, "freshmeatCycle") if (@_);
503 &Forker("kernel", sub { &Kernel::kernelAnnounce(); } );
505 &ScheduleThis(240, "kernelCycle") if (@_);
509 return unless &IsParam("wingate");
510 return unless ($param{'wingate'} =~ /^(.*\s+)?$chan(\s+.*)?$/i);
512 ### FILE CACHE OF OFFENDING WINGATES.
513 foreach (grep /^$host$/, @wingateBad) {
514 &status("Wingate: RUNNING ON $host BY $who");
515 &ban("*!*\@$host", "") if &IsParam("wingateBan");
517 next unless (&IsParam("wingateKick"));
518 &kick($who, "", $param{'wingateKick'})
521 ### RUN CACHE OF TRIED WINGATES.
522 if (grep /^$host$/, @wingateCache) {
523 push(@wingateNow, $host); # per run.
524 push(@wingateCache, $host); # cache per run.
526 &DEBUG("Already scanned $host. good.");
529 my $interval = $param{'wingateInterval'} || 60; # seconds.
530 return if (defined $forked{'wingate'});
531 return if (time() - $wingaterun <= $interval);
532 return unless (scalar(keys %wingateToDo));
534 $wingaterun = time();
536 &Forker("wingate", sub { &Wingate::Wingates(keys %wingateToDo); } );
541 sub wingateWriteFile {
542 return unless (scalar @wingateCache);
544 my $file = "$bot_base_dir/$param{'ircUser'}.wingate";
545 if ($bot_pid != $$) {
546 &DEBUG("wingateWriteFile: Reorganising!");
551 push(@wingateNow, $_);
556 my %hash = map { $_ => 1 } @wingateNow;
557 @wingateNow = sort keys %hash;
560 &DEBUG("wingateWF: writing...");
562 foreach (@wingateNow) {
567 &ScheduleThis(60, "wingateWriteFile") if (@_);