2 # core.pl: Important functions stuff...
4 # Version: v0.4 (20000718)
10 # scalar. MUST BE REDUCED IN SIZE!!!
13 $bot_misc_dir $bot_pid $bot_base_dir $bot_src_dir
14 $bot_data_dir $bot_config_dir $bot_state_dir $bot_run_dir
15 $answer $correction_plausible $talkchannel $bot_release
16 $statcount $memusage $user $memusageOld $bot_version $dbh
17 $shm $host $msg $noreply $conn $irc $learnok $nick $ident
18 $force_public_reply $addrchar $userHandle $addressedother
19 $floodwho $chan $msgtime $server $firsttime $wingaterun
20 $flag_quit $msgType $no_syscall
21 $utime_userfile $wtime_userfile $ucount_userfile
22 $utime_chanfile $wtime_chanfile $ucount_chanfile
23 $pubsize $pubcount $pubtime
24 $msgsize $msgcount $msgtime
25 $notsize $notcount $nottime
30 use vars qw(@joinchan @ircServers @wingateBad @wingateNow @wingateCache
33 ### hash. MUST BE REDUCED IN SIZE!!!
35 use vars qw(%count %netsplit %netsplitservers %flood %dcc %orig
36 %nuh %talkWho %seen %floodwarn %param %dbh %ircPort
37 %topic %moduleAge %last %time %mask %file
38 %forked %chanconf %channels
42 $SIG{'HUP'} = 'restart'; # 1.
43 $SIG{'INT'} = 'doExit'; # 2.
44 $SIG{'KILL'} = 'doExit'; # 9. DOES NOT WORK. 'man perlipc' for details.
45 $SIG{'TERM'} = 'doExit'; # 15.
46 $SIG{'__WARN__'} = 'doWarn';
48 # initialize variables.
52 $userHandle = "_default";
74 use POSIX qw(strftime);
75 $bot_release = strftime("cvs (%Y%m%d)", gmtime( (stat("CVS"))[9] ) );
77 $bot_release = "1.0.10 (2001xxxx)";
79 $bot_version = "blootbot $bot_release -- $^O";
89 if (defined $flag_quit) {
90 &WARN("doExit: quit already called.");
95 if (!defined $bot_pid) { # independent.
97 } elsif ($bot_pid == $$) { # parent.
98 &status("parent caught SIG$sig (pid $$).") if (defined $sig);
100 &status("--- Start of quit.");
101 $ident ||= "blootbot"; # lame hack.
103 &status("Memory Usage: $memusage kB");
107 # shutdown IRC and related components.
108 if (&whatInterface() =~ /IRC/) {
111 &quit($param{'quitMsg'});
115 &uptimeWriteFile() if (&IsChanConf("uptime"));
118 &dumpallvars() if (&IsParam("dumpvarsAtExit"));
119 &symdumpAll() if (&IsParam("symdumpAtExit"));
121 &closeSQLDebug() if (&IsParam("SQLDebug"));
123 &status("--- QUIT.");
125 &status("child caught SIG$sig (pid $$).");
132 $SIG{__WARN__} = sub { warn $_[0]; };
138 $SIG{__WARN__} = 'doWarn'; # ???
141 # Usage: &IsParam($param);
142 # blootbot.config specific.
146 return 0 unless (defined $param);
147 return 0 unless (exists $param{$param});
148 return 0 unless ($param{$param});
149 return 0 if $param{$param} =~ /^false$/i;
154 # Usage: &ChanConfList($param)
155 # About: gets channels with 'param' enabled. (!!!)
156 # Return: array of channels
159 return unless (defined $param);
160 my %chan = &getChanConfList($param);
162 if (exists $chan{_default}) {
163 return keys %chanconf;
170 # Usage: &getChanConfList($param)
171 # About: gets channels with 'param' enabled, internal use only.
172 # Return: hash of channels
173 sub getChanConfList {
177 return unless (defined $param);
179 foreach (keys %chanconf) {
181 # &DEBUG("chan => $chan");
182 my @array = grep /^$param$/, keys %{ $chanconf{$chan} };
184 next unless (scalar @array);
186 if (scalar @array > 1) {
187 &WARN("multiple items found?");
190 if ($array[0] eq "0") {
201 # Usage: &IsChanConf($param);
202 # About: Check for 'param' on the basis of channel config.
203 # Return: 1 for enabled, 0 for passive disable, -1 for active disable.
206 my $debug = 0; # knocked tons of bugs with this! :)
208 if (!defined $param) {
209 &WARN("IsChanConf: param == NULL.");
213 # should we use IsParam() externally where needed or hack it in
214 # here just in case? fix it later.
215 if (&IsParam($param)) {
216 &DEBUG("ICC: found '$param' option in main config file.");
220 $chan ||= "_default";
223 if ($chan =~ tr/A-Z/a-z/) {
224 &WARN("IsChanConf: lowercased chan. ($old)");
227 ### TODO: VERBOSITY on how chanconf returned 1 or 0 or -1.
228 my %chan = &getChanConfList($param);
230 if (!defined $msgType) {
233 $nomatch++ if ($msgType eq "");
234 $nomatch++ unless ($msgType =~ /^(public|private)$/i);
237 ### debug purposes only.
238 # &DEBUG("param => $param, msgType => $msgType.");
239 # foreach (keys %chan) {
240 # &DEBUG(" $_ => $chan{$_}");
245 &DEBUG("ICC: other: $chan{$chan} (_default/$param)") if ($debug);
246 } elsif ($chan{_default}) {
247 &DEBUG("ICC: other: $chan{_default} (_default/$param)") if ($debug);
249 &DEBUG("ICC: other: 0 ($param)") if ($debug);
252 return $chan{$chan} || $chan{_default} || 0;
255 if ($msgType eq "public") {
257 &DEBUG("ICC: public: $chan{$chan} ($chan/$param)") if ($debug);
258 } elsif ($chan{_default}) {
259 &DEBUG("ICC: public: $chan{_default} (_default/$param)") if ($debug);
261 &DEBUG("ICC: public: 0 ($param)") if ($debug);
264 return $chan{$chan} || $chan{_default} || 0;
267 if ($msgType eq "private") {
268 if ($chan{_default}) {
269 &DEBUG("ICC: private: $chan{_default} (_default/$param)") if ($debug);
270 } elsif ($chan{$chan}) {
271 &DEBUG("ICC: private: $chan{$chan} ($chan/$param) (hack)") if ($debug);
273 &DEBUG("ICC: private: 0 ($param)") if ($debug);
276 return $chan{$chan} || $chan{_default} || 0;
279 &DEBUG("ICC: no-match: 0/$param (msgType = $msgType)");
285 # Usage: &getChanConf($param);
286 # About: Retrieve value for 'param' value in current/default chan.
287 # Return: scalar for success, undef for failure.
291 if (!defined $param) {
292 &WARN("gCC: param == NULL.");
297 if (0 and !defined $chan) {
298 &DEBUG("gCC: ok !chan... doing _default instead.");
303 $c = "_default" if ($c eq "*"); # fix!
304 my @c = grep /^$c$/i, keys %chanconf;
307 if (0 and $c[0] ne $c) {
308 &WARN("c ne chan ($c[0] ne $chan)");
310 return $chanconf{$c[0]}{$param};
313 # &DEBUG("gCC: returning _default... ");
314 return $chanconf{"_default"}{$param};
317 sub getChanConfDefault {
318 my($what, $default, $chan) = @_;
320 $chan ||= "_default";
322 if (exists $param{$what}) {
323 if (!exists $cache{config}{$what}) {
324 &status("conf: backward-compat: found param{$what} ($param{$what}) for $chan instead.");
325 $cache{config}{$what} = 1;
328 return $param{$what};
330 my $val = &getChanConf($what, $chan);
331 return $val if (defined $val);
333 $param{$what} = $default;
334 &status("conf: $chan auto-setting param{$what} = $default");
335 $cache{config}{$what} = 1;
341 # Usage: &findChanConf($param);
342 # About: Retrieve value for 'param' value from any chan.
343 # Return: scalar for success, undef for failure.
347 if (!defined $param) {
348 &WARN("param == NULL.");
353 foreach $c (keys %chanconf) {
354 foreach (keys %{ $chanconf{$c} }) {
355 next unless (/^$param$/);
357 return $chanconf{$c}{$_};
365 my ($prefix) = $_[0] || "";
367 if ($^O eq "linux") {
368 if (!open(IN, "/proc/$$/status")) {
369 &ERROR("cannot open '/proc/$$/status'.");
374 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
378 } elsif ($^O eq "netbsd") {
379 $memusage = int( (stat "/proc/$$/mem")[7]/1024 );
381 } elsif ($^O =~ /^(free|open)bsd$/) {
382 my @info = split /\s+/, `/bin/ps -l -p $$`;
383 $memusage = $info[20];
386 $memusage = "UNKNOWN";
390 if (defined $memusageOld and &IsParam("DEBUG")) {
391 # it's always going to be increase.
392 my $delta = $memusage - $memusageOld;
396 } elsif ($delta > 500) {
397 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
398 } elsif ($delta > 0) {
399 $str = "MEM:$prefix increased by $delta kB";
400 } else { # delta < 0.
402 # never knew RSS could decrease, probably Size can't?
403 $str = "MEM:$prefix decreased by $delta kB.";
408 $memusageOld = $memusage;
416 &showProc(" (\&openLog before)");
417 &openLog(); # write, append.
418 &status("--- Started logging.");
421 &loadLang($bot_data_dir. "/blootbot.lang");
425 &loadMyModulesNow(); # must be after chan file.
428 &openSQLDebug() if (&IsParam("SQLDebug"));
429 &sqlOpenDB($param{'DBName'}, $param{'DBType'}, $param{'SQLUser'},
433 &status("Setup: ". &countKeys("factoids") ." factoids.");
434 &getChanConfDefault("sendPrivateLimitLines", 3);
435 &getChanConfDefault("sendPrivateLimitBytes", 1000);
436 &getChanConfDefault("sendPublicLimitLines", 3);
437 &getChanConfDefault("sendPublicLimitBytes", 1000);
438 &getChanConfDefault("sendNoticeLimitLines", 3);
439 &getChanConfDefault("sendNoticeLimitBytes", 1000);
441 $param{tempDir} =~ s#\~/#$ENV{HOME}/#;
443 &status("Initial memory usage: $memusage kB");
444 &status("-------------------------------------------------------");
448 $param{'VERBOSITY'} = 1;
449 &loadConfig($bot_config_dir."/blootbot.config");
451 foreach ( qw(ircNick ircUser ircName DBType tempDir) ) {
452 next if &IsParam($_);
453 &ERROR("Parameter $_ has not been defined.");
457 if ($param{tempDir} =~ s#\~/#$ENV{HOME}/#) {
458 &VERB("Fixing up tempDir.",2);
461 if ($param{tempDir} =~ /~/) {
462 &ERROR("parameter tempDir still contains tilde.");
466 if (! -d $param{tempDir}) {
467 &status("making $param{tempDir}...");
468 mkdir $param{tempDir}, 0755;
471 # static scalar variables.
472 $file{utm} = "$bot_state_dir/$param{'ircUser'}.uptime";
473 $file{PID} = "$bot_run_dir/$param{'ircUser'}.pid";
477 if (&IsParam("DEBUG")) {
478 &status("enabling debug diagnostics.");
479 ### I thought disabling this reduced memory usage by 1000 kB.
483 $count{'Question'} = 0;
484 $count{'Update'} = 0;
490 # reverse order of &setup().
491 &status("--- shutdown called.");
493 $ident ||= "blootbot"; # hack.
495 # opened files must be written to on shutdown/hup/whatever
496 # unless they're write-only, like uptime.
501 &closeSHM($shm); # aswell. TODO: use this in &doExit?
508 if ($$ == $bot_pid) {
509 &status("--- $sig called.");
511 ### crappy bug in Net::IRC?
512 my $delta = time() - $msgtime;
513 &DEBUG("restart: dtime = $delta");
514 if (!$conn->connected or time() - $msgtime > 900) {
515 &status("reconnecting because of uncaught disconnect \@ ".scalar(gmtime) );
522 &ircCheck(); # heh, evil!
524 &DCCBroadcast("-HUP called.","m");
526 &loadConfig($bot_config_dir."/blootbot.config");
527 &reloadAllModules() if (&IsParam("DEBUG"));
530 &status("--- End of $sig.");
532 &status("$sig called; ignoring restart.");
536 # File: Configuration.
540 if (!open(FILE, $file)) {
541 &ERROR("Failed to read configuration file ($file): $!");
542 &status("Please read the INSTALL file on how to install and setup this file.");
551 my ($set,$key,$val) = split(/\s+/, $_, 3);
554 &status("loadConfig: invalid line '$_'.");
558 # perform variable interpolation
559 $val =~ s/(\$(\w+))/$param{$2}/g;
568 &status("Loaded config $file ($count items)");