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.
103 &closeDCC() if (&whatInterface() =~ /IRC/);
106 &seenFlush() if (&whatInterface() =~ /IRC/);
107 &quit($param{'quitMsg'}) if (&whatInterface() =~ /IRC/);
110 &uptimeWriteFile() if (&ChanConfList("uptime"));
111 &News::writeNews() if (&ChanConfList("news"));
114 &dumpallvars() if (&IsParam("dumpvarsAtExit"));
115 &symdumpAll() if (&IsParam("symdumpAtExit"));
117 &closeSQLDebug() if (&IsParam("SQLDebug"));
119 &status("--- QUIT.");
121 &status("child caught SIG$sig (pid $$).");
128 $SIG{__WARN__} = sub { warn $_[0]; };
134 $SIG{__WARN__} = 'doWarn'; # ???
137 # Usage: &IsParam($param);
138 # blootbot.config specific.
142 return 0 unless (defined $param);
143 return 0 unless (exists $param{$param});
144 return 0 unless ($param{$param});
145 return 0 if $param{$param} =~ /^false$/i;
150 # Usage: &ChanConfList($param)
151 # About: gets channels with 'param' enabled. (!!!)
152 # Return: array of channels
155 return unless (defined $param);
156 my %chan = &getChanConfList($param);
158 if (exists $chan{_default}) {
159 return keys %chanconf;
166 # Usage: &getChanConfList($param)
167 # About: gets channels with 'param' enabled, internal use only.
168 # Return: hash of channels
169 sub getChanConfList {
173 return unless (defined $param);
175 foreach (keys %chanconf) {
177 # &DEBUG("chan => $chan");
178 my @array = grep /^$param$/, keys %{ $chanconf{$chan} };
180 next unless (scalar @array);
182 if (scalar @array > 1) {
183 &WARN("multiple items found?");
186 if ($array[0] eq "0") {
197 # Usage: &IsChanConf($param);
198 # About: Check for 'param' on the basis of channel config.
199 # Return: 1 for enabled, 0 for passive disable, -1 for active disable.
202 my $debug = 0; # knocked tons of bugs with this! :)
204 if (!defined $param) {
205 &WARN("IsChanConf: param == NULL.");
209 $chan ||= "_default";
212 if ($chan =~ tr/A-Z/a-z/) {
213 &WARN("IsChanConf: lowercased chan. ($old)");
216 ### TODO: VERBOSITY on how chanconf returned 1 or 0 or -1.
217 my %chan = &getChanConfList($param);
219 if (!defined $msgType) {
222 $nomatch++ if ($msgType eq "");
223 $nomatch++ unless ($msgType =~ /^(public|private)$/i);
226 ### debug purposes only.
227 # &DEBUG("param => $param, msgType => $msgType.");
228 # foreach (keys %chan) {
229 # &DEBUG(" $_ => $chan{$_}");
234 &DEBUG("ICC: other: $chan{$chan} (_default/$param)") if ($debug);
235 } elsif ($chan{_default}) {
236 &DEBUG("ICC: other: $chan{_default} (_default/$param)") if ($debug);
238 &DEBUG("ICC: other: 0 ($param)") if ($debug);
241 return $chan{$chan} || $chan{_default} || 0;
244 if ($msgType eq "public") {
246 &DEBUG("ICC: public: $chan{$chan} ($chan/$param)") if ($debug);
247 } elsif ($chan{_default}) {
248 &DEBUG("ICC: public: $chan{_default} (_default/$param)") if ($debug);
250 &DEBUG("ICC: public: 0 ($param)") if ($debug);
253 return $chan{$chan} || $chan{_default} || 0;
256 if ($msgType eq "private") {
257 if ($chan{_default}) {
258 &DEBUG("ICC: private: $chan{_default} (_default/$param)") if ($debug);
259 } elsif ($chan{$chan}) {
260 &DEBUG("ICC: private: $chan{$chan} ($chan/$param) (hack)") if ($debug);
262 &DEBUG("ICC: private: 0 ($param)") if ($debug);
265 return $chan{$chan} || $chan{_default} || 0;
268 &DEBUG("ICC: no-match: 0/$param (msgType = $msgType)");
274 # Usage: &getChanConf($param);
275 # About: Retrieve value for 'param' value in current/default chan.
276 # Return: scalar for success, undef for failure.
280 if (!defined $param) {
281 &WARN("param == NULL.");
286 if (0 and !defined $chan) {
287 &DEBUG("gCC: ok !chan... doing _default instead.");
292 $c = "_default" if ($c eq "*"); # fix!
293 my @c = grep /^$c$/i, keys %chanconf;
296 if (0 and $c[0] ne $c) {
297 &WARN("c ne chan ($c[0] ne $chan)");
299 return $chanconf{$c[0]}{$param};
302 # &DEBUG("gCC: returning _default... ");
303 return $chanconf{"_default"}{$param};
306 sub getChanConfDefault {
307 my($what, $default, $chan) = @_;
309 if (exists $param{$what}) {
310 if (!exists $cache{config}{$what}) {
311 &status("conf: backward-compat: found param{$what} ($param{$what}) instead.");
312 $cache{config}{$what} = 1;
315 return $param{$what};
317 my $val = &getChanConf($what, $chan);
318 return $val if (defined $val);
320 $param{$what} = $default;
321 &status("conf: auto-setting param{$what} = $default");
322 $cache{config}{$what} = 1;
328 # Usage: &findChanConf($param);
329 # About: Retrieve value for 'param' value from any chan.
330 # Return: scalar for success, undef for failure.
334 if (!defined $param) {
335 &WARN("param == NULL.");
340 foreach $c (keys %chanconf) {
341 foreach (keys %{ $chanconf{$c} }) {
342 next unless (/^$param$/);
344 &DEBUG("chanconf{$c}{$_} ...");
345 return $chanconf{$c}{$_};
353 my ($prefix) = $_[0] || "";
355 if ($^O eq "linux") {
356 if (!open(IN, "/proc/$$/status")) {
357 &ERROR("cannot open '/proc/$$/status'.");
362 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
366 } elsif ($^O eq "netbsd") {
367 $memusage = (stat "/proc/$$/mem")[7]/1024;
369 } elsif ($^O =~ /^(free|open)bsd$/) {
370 my @info = split /\s+/, `/bin/ps -l -p $$`;
371 $memusage = $info[20];
374 $memusage = "UNKNOWN";
378 if (defined $memusageOld and &IsParam("DEBUG")) {
379 # it's always going to be increase.
380 my $delta = $memusage - $memusageOld;
384 } elsif ($delta > 500) {
385 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
386 } elsif ($delta > 0) {
387 $str = "MEM:$prefix increased by $delta kB";
388 } else { # delta < 0.
390 # never knew RSS could decrease, probably Size can't?
391 $str = "MEM:$prefix decreased by $delta kB.";
396 $memusageOld = $memusage;
404 &showProc(" (\&openLog before)");
405 &openLog(); # write, append.
406 &status("--- Started logging.");
409 &loadLang($bot_data_dir. "/blootbot.lang");
413 &loadMyModulesNow(); # must be after chan file.
416 &openSQLDebug() if (&IsParam("SQLDebug"));
417 &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
420 &status("Setup: ". &countKeys("factoids") ." factoids.");
421 &News::readNews() if (&ChanConfList("news"));
422 &getChanConfDefault("sendPrivateLimitLines", 3);
423 &getChanConfDefault("sendPrivateLimitBytes", 1000);
424 &getChanConfDefault("sendPublicLimitLines", 3);
425 &getChanConfDefault("sendPublicLimitBytes", 1000);
426 &getChanConfDefault("sendNoticeLimitLines", 3);
427 &getChanConfDefault("sendNoticeLimitBytes", 1000);
429 $param{tempDir} =~ s#\~/#$ENV{HOME}/#;
431 &status("Initial memory usage: $memusage kB");
432 &status("-------------------------------------------------------");
436 $param{'VERBOSITY'} = 1;
437 &loadConfig($bot_config_dir."/blootbot.config");
439 foreach ("ircNick", "ircUser", "ircName", "DBType", "tempDir") {
440 next if &IsParam($_);
441 &ERROR("Parameter $_ has not been defined.");
445 if ($param{tempDir} =~ s#\~/#$ENV{HOME}/#) {
446 &VERB("Fixing up tempDir.",2);
449 if ($param{tempDir} =~ /~/) {
450 &ERROR("parameter tempDir still contains tilde.");
454 if (! -d $param{tempDir}) {
455 &status("making $param{tempDir}...");
456 mkdir $param{tempDir}, 0755;
459 # static scalar variables.
460 $file{utm} = "$bot_state_dir/$param{'ircUser'}.uptime";
461 $file{PID} = "$bot_run_dir/$param{'ircUser'}.pid";
465 if (&IsParam("DEBUG")) {
466 &status("enabling debug diagnostics.");
467 ### I thought disabling this reduced memory usage by 1000 kB.
471 $count{'Question'} = 0;
472 $count{'Update'} = 0;
478 # reverse order of &setup().
479 &status("--- shutdown called.");
481 $ident ||= "blootbot"; # hack.
483 # opened files must be written to on shutdown/hup/whatever
484 # unless they're write-only, like uptime.
487 &News::writeNews() if (&ChanConfList("news"));
490 &closeSHM($shm); # aswell. TODO: use this in &doExit?
497 if ($$ == $bot_pid) {
498 &status("--- $sig called.");
500 ### crappy bug in Net::IRC?
501 if (!$conn->connected and time - $msgtime > 900) {
502 &status("reconnecting because of uncaught disconnect \@ ".scalar(localtime) );
509 &ircCheck(); # heh, evil!
511 &DCCBroadcast("-HUP called.","m");
513 &loadConfig($bot_config_dir."/blootbot.config");
514 &reloadAllModules() if (&IsParam("DEBUG"));
517 &status("--- End of $sig.");
519 &status("$sig called; ignoring restart.");
523 # File: Configuration.
527 if (!open(FILE, $file)) {
528 &ERROR("Failed to read configuration file ($file): $!");
529 &status("Please read the INSTALL file on how to install and setup this file.");
538 my ($set,$key,$val) = split(/\s+/, $_, 3);
541 &status("loadConfig: invalid line '$_'.");
545 # perform variable interpolation
546 $val =~ s/(\$(\w+))/$param{$2}/g;
555 &status("Loaded config $file ($count items)");