2 # core.pl: Important functions stuff...
4 # Version: v0.4 (20000718)
10 # dynamic scalar. MUST BE REDUCED IN SIZE!!!
13 $bot_misc_dir $bot_pid $bot_base_dir $bot_src_dir
14 $answer $correction_plausible $talkchannel $bot_release
15 $statcount $memusage $user $memusageOld $bot_version $dbh
16 $shm $host $msg $noreply $conn $irc $learnok $nick $ident
17 $force_public_reply $addrchar $userHandle $addressedother
18 $floodwho $chan $msgtime $server $firsttime $wingaterun
19 $flag_quit $msgType $no_syscall
20 $utime_userfile $wtime_userfile $ucount_userfile
21 $utime_chanfile $wtime_chanfile $ucount_chanfile
22 $pubsize $pubcount $pubtime
23 $msgsize $msgcount $msgtime
24 $notsize $notcount $nottime
29 use vars qw(@joinchan @ircServers @wingateBad @wingateNow @wingateCache
32 ### dynamic hash. MUST BE REDUCED IN SIZE!!!
34 use vars qw(%count %netsplit %netsplitservers %flood %dcc %orig
35 %nuh %talkWho %seen %floodwarn %param %dbh %ircPort
36 %topic %moduleAge %last %time %mask %file
37 %forked %chanconf %channels
41 $SIG{'HUP'} = 'restart'; # 1.
42 $SIG{'INT'} = 'doExit'; # 2.
43 $SIG{'KILL'} = 'doExit'; # 9. DOES NOT WORK. 'man perlipc' for details.
44 $SIG{'TERM'} = 'doExit'; # 15.
45 $SIG{'__WARN__'} = 'doWarn';
47 # initialize variables.
51 $userHandle = "default";
73 use POSIX qw(strftime);
74 $bot_release = strftime("cvs (%Y%m%d)", localtime( (stat("CVS"))[9] ) );
76 $bot_release = "1.0.10 (2001xxxx)";
78 $bot_version = "blootbot $bot_release -- $^O";
88 if (defined $flag_quit) {
89 &WARN("doExit: quit already called.");
94 if (!defined $bot_pid) { # independent.
96 } elsif ($bot_pid == $$) { # parent.
97 &status("parent caught SIG$sig (pid $$).") if (defined $sig);
99 &status("--- Start of quit.");
100 $ident ||= "blootbot"; # lame hack.
106 &quit($param{'quitMsg'}) if (&whatInterface() =~ /IRC/);
109 &uptimeWriteFile() if (&ChanConfList("uptime"));
110 &News::writeNews() if (&ChanConfList("news"));
113 &dumpallvars() if (&IsParam("dumpvarsAtExit"));
115 &closeSQLDebug() if (&IsParam("SQLDebug"));
117 &status("--- QUIT.");
119 &status("child caught SIG$sig (pid $$).");
126 $SIG{__WARN__} = sub { warn $_[0]; };
132 $SIG{__WARN__} = 'doWarn'; # ???
135 # Usage: &IsParam($param);
136 # blootbot.config specific.
140 return 0 unless (defined $param);
141 return 0 unless (exists $param{$param});
142 return 0 unless ($param{$param});
143 return 0 if $param{$param} =~ /^false$/i;
148 # Usage: &ChanConfList($param)
149 # About: gets channels with 'param' enabled. (!!!)
150 # Return: array of channels
153 return unless (defined $param);
154 my %chan = &getChanConfList($param);
156 if (exists $chan{_default}) {
157 return keys %chanconf;
164 # Usage: &getChanConfList($param)
165 # About: gets channels with 'param' enabled, internal use only.
166 # Return: hash of channels
167 sub getChanConfList {
171 return unless (defined $param);
173 foreach (keys %chanconf) {
175 # &DEBUG("chan => $chan");
176 my @array = grep /^$param$/, keys %{ $chanconf{$chan} };
178 next unless (scalar @array);
180 if (scalar @array > 1) {
181 &WARN("multiple items found?");
184 if ($array[0] eq "0") {
195 # Usage: &IsChanConf($param);
196 # About: Check for 'param' on the basis of channel config.
197 # Return: 1 for enabled, 0 for passive disable, -1 for active disable.
200 my $debug = 0; # knocked tons of bugs with this! :)
202 if (!defined $param) {
203 &WARN("IsChanConf: param == NULL.");
207 $chan ||= "_default";
210 if ($chan =~ tr/A-Z/a-z/) {
211 &WARN("IsChanConf: lowercased chan. ($old)");
214 ### TODO: VERBOSITY on how chanconf returned 1 or 0 or -1.
215 my %chan = &getChanConfList($param);
217 if (!defined $msgType) {
220 $nomatch++ if ($msgType eq "");
221 $nomatch++ unless ($msgType =~ /^(public|private)$/i);
224 ### debug purposes only.
225 # &DEBUG("param => $param, msgType => $msgType.");
226 # foreach (keys %chan) {
227 # &DEBUG(" $_ => $chan{$_}");
232 &DEBUG("ICC: other: $chan{$chan} (_default/$param)") if ($debug);
233 } elsif ($chan{_default}) {
234 &DEBUG("ICC: other: $chan{_default} (_default/$param)") if ($debug);
236 &DEBUG("ICC: other: 0 ($param)") if ($debug);
239 return $chan{$chan} || $chan{_default} || 0;
242 if ($msgType eq "public") {
244 &DEBUG("ICC: public: $chan{$chan} ($chan/$param)") if ($debug);
245 } elsif ($chan{_default}) {
246 &DEBUG("ICC: public: $chan{_default} (_default/$param)") if ($debug);
248 &DEBUG("ICC: public: 0 ($param)") if ($debug);
251 return $chan{$chan} || $chan{_default} || 0;
254 if ($msgType eq "private") {
255 if ($chan{_default}) {
256 &DEBUG("ICC: private: $chan{_default} (_default/$param)") if ($debug);
257 } elsif ($chan{$chan}) {
258 &DEBUG("ICC: private: $chan{$chan} ($chan/$param) (hack)") if ($debug);
260 &DEBUG("ICC: private: 0 ($param)") if ($debug);
263 return $chan{$chan} || $chan{_default} || 0;
266 &DEBUG("ICC: no-match: 0/$param (msgType = $msgType)");
272 # Usage: &getChanConf($param);
273 # About: Retrieve value for 'param' value in current/default chan.
274 # Return: scalar for success, undef for failure.
278 if (!defined $param) {
279 &WARN("param == NULL.");
284 if (0 and !defined $chan) {
285 &DEBUG("gCC: ok !chan... doing _default instead.");
290 $c = "_default" if ($c eq "*"); # fix!
291 my @c = grep /^$c$/i, keys %chanconf;
294 if (0 and $c[0] ne $c) {
295 &WARN("c ne chan ($c[0] ne $chan)");
297 return $chanconf{$c[0]}{$param};
300 # &DEBUG("gCC: returning _default... ");
301 return $chanconf{"_default"}{$param};
305 # Usage: &findChanConf($param);
306 # About: Retrieve value for 'param' value from any chan.
307 # Return: scalar for success, undef for failure.
311 if (!defined $param) {
312 &WARN("param == NULL.");
317 foreach $c (keys %chanconf) {
318 foreach (keys %{ $chanconf{$c} }) {
319 next unless (/^$param$/);
321 &DEBUG("chanconf{$c}{$_} ...");
322 return $chanconf{$c}{$_};
330 my ($prefix) = $_[0] || "";
332 if (!open(IN, "/proc/$$/status")) {
333 &ERROR("cannot open '/proc/$$/status'.");
337 if ($^O eq "linux") {
339 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
343 } elsif ($^O eq "netbsd") {
344 $memusage = (stat "/proc/$$/mem")[7]/1024;
346 } elsif ($^O =~ /^(free|open)bsd$/) {
347 my @info = split /\s+/, `/bin/ps -l -p $$`;
348 $memusage = $info[20];
351 $memusage = "UNKNOWN";
355 if (defined $memusageOld and &IsParam("DEBUG")) {
356 # it's always going to be increase.
357 my $delta = $memusage - $memusageOld;
361 } elsif ($delta > 500) {
362 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
363 } elsif ($delta > 0) {
364 $str = "MEM:$prefix increased by $delta kB";
365 } else { # delta < 0.
367 # never knew RSS could decrease, probably Size can't?
368 $str = "MEM:$prefix decreased by $delta kB. YES YES YES";
373 $memusageOld = $memusage;
381 &showProc(" (\&openLog before)");
382 &openLog(); # write, append.
383 &status("--- Started logging.");
386 my $dir = "$bot_base_dir/$_/";
388 &status("Making dir $_");
393 &loadLang($bot_data_dir. "/blootbot.lang");
397 &loadMyModulesNow(); # must be after chan file.
400 &openSQLDebug() if (&IsParam("SQLDebug"));
401 &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
404 &status("Setup: ". &countKeys("factoids") ." factoids.");
405 &News::readNews() if (&ChanConfList("news"));
406 &getChanConfDefault("sendPrivateLimitLines", 3);
407 &getChanConfDefault("sendPrivateLimitBytes", 1000);
408 &getChanConfDefault("sendPublicLimitLines", 3);
409 &getChanConfDefault("sendPublicLimitBytes", 1000);
410 &getChanConfDefault("sendNoticeLimitLines", 3);
411 &getChanConfDefault("sendNoticeLimitBytes", 1000);
413 $param{tempDir} =~ s#\~/#$ENV{HOME}/#;
415 &status("Initial memory usage: $memusage kB");
416 &status("-------------------------------------------------------");
420 $param{'VERBOSITY'} = 1;
421 &loadConfig($bot_config_dir."/blootbot.config");
423 foreach ("ircNick", "ircUser", "ircName", "DBType", "tempDir") {
424 next if &IsParam($_);
425 &ERROR("Parameter $_ has not been defined.");
429 if ($param{tempDir} =~ s#\~/#$ENV{HOME}/#) {
430 &VERB("Fixing up tempDir.",2);
433 if ($param{tempDir} =~ /~/) {
434 &ERROR("parameter tempDir still contains tilde.");
438 if (! -d $param{tempDir}) {
439 &status("making $param{tempDir}...");
440 system("mkdir $param{tempDir}");
443 # static scalar variables.
444 $file{utm} = "$bot_state_dir/$param{'ircUser'}.uptime";
445 $file{PID} = "$bot_run_dir/$param{'ircUser'}.pid";
449 if (&IsParam("DEBUG")) {
450 &status("enabling debug diagnostics.");
451 ### I thought disabling this reduced memory usage by 1000 kB.
455 $count{'Question'} = 0;
456 $count{'Update'} = 0;
462 # reverse order of &setup().
463 &status("--- shutdown called.");
465 $ident ||= "blootbot"; # hack.
467 # opened files must be written to on shutdown/hup/whatever
468 # unless they're write-only, like uptime.
471 &News::writeNews() if (&ChanConfList("news"));
474 &closeSHM($shm); # aswell. TODO: use this in &doExit?
481 if ($$ == $bot_pid) {
482 &status("--- $sig called.");
484 ### crappy bug in Net::IRC?
485 if (!$conn->connected and time - $msgtime > 900) {
486 &status("reconnecting because of uncaught disconnect \@ ".scalar(localtime) );
493 &ircCheck(); # heh, evil!
495 &DCCBroadcast("-HUP called.","m");
497 &loadConfig($bot_config_dir."/blootbot.config");
498 &reloadAllModules() if (&IsParam("DEBUG"));
501 &status("--- End of $sig.");
503 &status("$sig called; ignoring restart.");
507 # File: Configuration.
511 if (!open(FILE, $file)) {
512 &ERROR("FAILED loadConfig ($file): $!");
513 &status("Please copy files/sample.config to files/blootbot.config");
514 &status(" and edit files/blootbot.config, modify to tastes.");
523 my ($set,$key,$val) = split(/\s+/, $_, 3);
526 &status("loadConfig: invalid line '$_'.");
530 # perform variable interpolation
531 $val =~ s/(\$(\w+))/$param{$2}/g;
540 &status("Loaded config $file ($count items)");