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 $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 ### dynamic 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)", localtime( (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.
105 # shutdown IRC and related components.
106 if (&whatInterface() =~ /IRC/) {
109 &quit($param{'quitMsg'});
113 if (&IsChanConf("uptime")) {
114 &DEBUG("going to write uptime file info.");
116 &uptimeWriteFile() if (&IsChanConf("uptime"));
117 &News::writeNews() if (&ChanConfList("news"));
120 &dumpallvars() if (&IsParam("dumpvarsAtExit"));
121 &symdumpAll() if (&IsParam("symdumpAtExit"));
123 &closeSQLDebug() if (&IsParam("SQLDebug"));
125 &status("--- QUIT.");
127 &status("child caught SIG$sig (pid $$).");
134 $SIG{__WARN__} = sub { warn $_[0]; };
140 $SIG{__WARN__} = 'doWarn'; # ???
143 # Usage: &IsParam($param);
144 # blootbot.config specific.
148 return 0 unless (defined $param);
149 return 0 unless (exists $param{$param});
150 return 0 unless ($param{$param});
151 return 0 if $param{$param} =~ /^false$/i;
156 # Usage: &ChanConfList($param)
157 # About: gets channels with 'param' enabled. (!!!)
158 # Return: array of channels
161 return unless (defined $param);
162 my %chan = &getChanConfList($param);
164 if (exists $chan{_default}) {
165 return keys %chanconf;
172 # Usage: &getChanConfList($param)
173 # About: gets channels with 'param' enabled, internal use only.
174 # Return: hash of channels
175 sub getChanConfList {
179 return unless (defined $param);
181 foreach (keys %chanconf) {
183 # &DEBUG("chan => $chan");
184 my @array = grep /^$param$/, keys %{ $chanconf{$chan} };
186 next unless (scalar @array);
188 if (scalar @array > 1) {
189 &WARN("multiple items found?");
192 if ($array[0] eq "0") {
203 # Usage: &IsChanConf($param);
204 # About: Check for 'param' on the basis of channel config.
205 # Return: 1 for enabled, 0 for passive disable, -1 for active disable.
208 my $debug = 0; # knocked tons of bugs with this! :)
210 if (!defined $param) {
211 &WARN("IsChanConf: param == NULL.");
215 $chan ||= "_default";
218 if ($chan =~ tr/A-Z/a-z/) {
219 &WARN("IsChanConf: lowercased chan. ($old)");
222 ### TODO: VERBOSITY on how chanconf returned 1 or 0 or -1.
223 my %chan = &getChanConfList($param);
225 if (!defined $msgType) {
228 $nomatch++ if ($msgType eq "");
229 $nomatch++ unless ($msgType =~ /^(public|private)$/i);
232 ### debug purposes only.
233 # &DEBUG("param => $param, msgType => $msgType.");
234 # foreach (keys %chan) {
235 # &DEBUG(" $_ => $chan{$_}");
240 &DEBUG("ICC: other: $chan{$chan} (_default/$param)") if ($debug);
241 } elsif ($chan{_default}) {
242 &DEBUG("ICC: other: $chan{_default} (_default/$param)") if ($debug);
244 &DEBUG("ICC: other: 0 ($param)") if ($debug);
247 return $chan{$chan} || $chan{_default} || 0;
250 if ($msgType eq "public") {
252 &DEBUG("ICC: public: $chan{$chan} ($chan/$param)") if ($debug);
253 } elsif ($chan{_default}) {
254 &DEBUG("ICC: public: $chan{_default} (_default/$param)") if ($debug);
256 &DEBUG("ICC: public: 0 ($param)") if ($debug);
259 return $chan{$chan} || $chan{_default} || 0;
262 if ($msgType eq "private") {
263 if ($chan{_default}) {
264 &DEBUG("ICC: private: $chan{_default} (_default/$param)") if ($debug);
265 } elsif ($chan{$chan}) {
266 &DEBUG("ICC: private: $chan{$chan} ($chan/$param) (hack)") if ($debug);
268 &DEBUG("ICC: private: 0 ($param)") if ($debug);
271 return $chan{$chan} || $chan{_default} || 0;
274 &DEBUG("ICC: no-match: 0/$param (msgType = $msgType)");
280 # Usage: &getChanConf($param);
281 # About: Retrieve value for 'param' value in current/default chan.
282 # Return: scalar for success, undef for failure.
286 if (!defined $param) {
287 &WARN("gCC: param == NULL.");
292 if (0 and !defined $chan) {
293 &DEBUG("gCC: ok !chan... doing _default instead.");
298 $c = "_default" if ($c eq "*"); # fix!
299 my @c = grep /^$c$/i, keys %chanconf;
302 if (0 and $c[0] ne $c) {
303 &WARN("c ne chan ($c[0] ne $chan)");
305 return $chanconf{$c[0]}{$param};
308 # &DEBUG("gCC: returning _default... ");
309 return $chanconf{"_default"}{$param};
312 sub getChanConfDefault {
313 my($what, $default, $chan) = @_;
315 if (exists $param{$what}) {
316 if (!exists $cache{config}{$what}) {
317 &status("conf: backward-compat: found param{$what} ($param{$what}) instead.");
318 $cache{config}{$what} = 1;
321 return $param{$what};
323 my $val = &getChanConf($what, $chan);
324 return $val if (defined $val);
326 $param{$what} = $default;
327 &status("conf: auto-setting param{$what} = $default");
328 $cache{config}{$what} = 1;
334 # Usage: &findChanConf($param);
335 # About: Retrieve value for 'param' value from any chan.
336 # Return: scalar for success, undef for failure.
340 if (!defined $param) {
341 &WARN("param == NULL.");
346 foreach $c (keys %chanconf) {
347 foreach (keys %{ $chanconf{$c} }) {
348 next unless (/^$param$/);
350 &DEBUG("chanconf{$c}{$_} ...");
351 return $chanconf{$c}{$_};
359 my ($prefix) = $_[0] || "";
361 if ($^O eq "linux") {
362 if (!open(IN, "/proc/$$/status")) {
363 &ERROR("cannot open '/proc/$$/status'.");
368 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
372 } elsif ($^O eq "netbsd") {
373 $memusage = (stat "/proc/$$/mem")[7]/1024;
375 } elsif ($^O =~ /^(free|open)bsd$/) {
376 my @info = split /\s+/, `/bin/ps -l -p $$`;
377 $memusage = $info[20];
380 $memusage = "UNKNOWN";
384 if (defined $memusageOld and &IsParam("DEBUG")) {
385 # it's always going to be increase.
386 my $delta = $memusage - $memusageOld;
390 } elsif ($delta > 500) {
391 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
392 } elsif ($delta > 0) {
393 $str = "MEM:$prefix increased by $delta kB";
394 } else { # delta < 0.
396 # never knew RSS could decrease, probably Size can't?
397 $str = "MEM:$prefix decreased by $delta kB.";
402 $memusageOld = $memusage;
410 &showProc(" (\&openLog before)");
411 &openLog(); # write, append.
412 &status("--- Started logging.");
415 &loadLang($bot_data_dir. "/blootbot.lang");
419 &loadMyModulesNow(); # must be after chan file.
422 &openSQLDebug() if (&IsParam("SQLDebug"));
423 &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
426 &status("Setup: ". &countKeys("factoids") ." factoids.");
427 &News::readNews() if (&ChanConfList("news"));
428 &getChanConfDefault("sendPrivateLimitLines", 3);
429 &getChanConfDefault("sendPrivateLimitBytes", 1000);
430 &getChanConfDefault("sendPublicLimitLines", 3);
431 &getChanConfDefault("sendPublicLimitBytes", 1000);
432 &getChanConfDefault("sendNoticeLimitLines", 3);
433 &getChanConfDefault("sendNoticeLimitBytes", 1000);
435 $param{tempDir} =~ s#\~/#$ENV{HOME}/#;
437 &status("Initial memory usage: $memusage kB");
438 &status("-------------------------------------------------------");
442 $param{'VERBOSITY'} = 1;
443 &loadConfig($bot_config_dir."/blootbot.config");
445 foreach ( qw(ircNick ircUser ircName DBType tempDir) ) {
446 next if &IsParam($_);
447 &ERROR("Parameter $_ has not been defined.");
451 if ($param{tempDir} =~ s#\~/#$ENV{HOME}/#) {
452 &VERB("Fixing up tempDir.",2);
455 if ($param{tempDir} =~ /~/) {
456 &ERROR("parameter tempDir still contains tilde.");
460 if (! -d $param{tempDir}) {
461 &status("making $param{tempDir}...");
462 mkdir $param{tempDir}, 0755;
465 # static scalar variables.
466 $file{utm} = "$bot_state_dir/$param{'ircUser'}.uptime";
467 $file{PID} = "$bot_run_dir/$param{'ircUser'}.pid";
471 if (&IsParam("DEBUG")) {
472 &status("enabling debug diagnostics.");
473 ### I thought disabling this reduced memory usage by 1000 kB.
477 $count{'Question'} = 0;
478 $count{'Update'} = 0;
484 # reverse order of &setup().
485 &status("--- shutdown called.");
487 $ident ||= "blootbot"; # hack.
489 # opened files must be written to on shutdown/hup/whatever
490 # unless they're write-only, like uptime.
493 &News::writeNews() if (&ChanConfList("news"));
496 &closeSHM($shm); # aswell. TODO: use this in &doExit?
503 if ($$ == $bot_pid) {
504 &status("--- $sig called.");
506 ### crappy bug in Net::IRC?
507 if (!$conn->connected and time - $msgtime > 900) {
508 &status("reconnecting because of uncaught disconnect \@ ".scalar(localtime) );
515 &ircCheck(); # heh, evil!
517 &DCCBroadcast("-HUP called.","m");
519 &loadConfig($bot_config_dir."/blootbot.config");
520 &reloadAllModules() if (&IsParam("DEBUG"));
523 &status("--- End of $sig.");
525 &status("$sig called; ignoring restart.");
529 # File: Configuration.
533 if (!open(FILE, $file)) {
534 &ERROR("Failed to read configuration file ($file): $!");
535 &status("Please read the INSTALL file on how to install and setup this file.");
544 my ($set,$key,$val) = split(/\s+/, $_, 3);
547 &status("loadConfig: invalid line '$_'.");
551 # perform variable interpolation
552 $val =~ s/(\$(\w+))/$param{$2}/g;
561 &status("Loaded config $file ($count items)");