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)", 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 &status("Memory Usage: $memusage kB");
107 # shutdown IRC and related components.
108 if (&whatInterface() =~ /IRC/) {
111 &quit($param{'quitMsg'});
115 &uptimeWriteFile() if (&IsChanConf("uptime"));
116 &News::writeNews() if (&ChanConfList("news"));
119 &dumpallvars() if (&IsParam("dumpvarsAtExit"));
120 &symdumpAll() if (&IsParam("symdumpAtExit"));
122 &closeSQLDebug() if (&IsParam("SQLDebug"));
124 &status("--- QUIT.");
126 &status("child caught SIG$sig (pid $$).");
133 $SIG{__WARN__} = sub { warn $_[0]; };
139 $SIG{__WARN__} = 'doWarn'; # ???
142 # Usage: &IsParam($param);
143 # blootbot.config specific.
147 return 0 unless (defined $param);
148 return 0 unless (exists $param{$param});
149 return 0 unless ($param{$param});
150 return 0 if $param{$param} =~ /^false$/i;
155 # Usage: &ChanConfList($param)
156 # About: gets channels with 'param' enabled. (!!!)
157 # Return: array of channels
160 return unless (defined $param);
161 my %chan = &getChanConfList($param);
163 if (exists $chan{_default}) {
164 return keys %chanconf;
171 # Usage: &getChanConfList($param)
172 # About: gets channels with 'param' enabled, internal use only.
173 # Return: hash of channels
174 sub getChanConfList {
178 return unless (defined $param);
180 foreach (keys %chanconf) {
182 # &DEBUG("chan => $chan");
183 my @array = grep /^$param$/, keys %{ $chanconf{$chan} };
185 next unless (scalar @array);
187 if (scalar @array > 1) {
188 &WARN("multiple items found?");
191 if ($array[0] eq "0") {
202 # Usage: &IsChanConf($param);
203 # About: Check for 'param' on the basis of channel config.
204 # Return: 1 for enabled, 0 for passive disable, -1 for active disable.
207 my $debug = 0; # knocked tons of bugs with this! :)
209 if (!defined $param) {
210 &WARN("IsChanConf: param == NULL.");
214 # should we use IsParam() externally where needed or hack it in
215 # here just in case? fix it later.
216 if (&IsParam($param)) {
217 &DEBUG("ICC: found '$param' option in main config file.");
221 $chan ||= "_default";
224 if ($chan =~ tr/A-Z/a-z/) {
225 &WARN("IsChanConf: lowercased chan. ($old)");
228 ### TODO: VERBOSITY on how chanconf returned 1 or 0 or -1.
229 my %chan = &getChanConfList($param);
231 if (!defined $msgType) {
234 $nomatch++ if ($msgType eq "");
235 $nomatch++ unless ($msgType =~ /^(public|private)$/i);
238 ### debug purposes only.
239 # &DEBUG("param => $param, msgType => $msgType.");
240 # foreach (keys %chan) {
241 # &DEBUG(" $_ => $chan{$_}");
246 &DEBUG("ICC: other: $chan{$chan} (_default/$param)") if ($debug);
247 } elsif ($chan{_default}) {
248 &DEBUG("ICC: other: $chan{_default} (_default/$param)") if ($debug);
250 &DEBUG("ICC: other: 0 ($param)") if ($debug);
253 return $chan{$chan} || $chan{_default} || 0;
256 if ($msgType eq "public") {
258 &DEBUG("ICC: public: $chan{$chan} ($chan/$param)") if ($debug);
259 } elsif ($chan{_default}) {
260 &DEBUG("ICC: public: $chan{_default} (_default/$param)") if ($debug);
262 &DEBUG("ICC: public: 0 ($param)") if ($debug);
265 return $chan{$chan} || $chan{_default} || 0;
268 if ($msgType eq "private") {
269 if ($chan{_default}) {
270 &DEBUG("ICC: private: $chan{_default} (_default/$param)") if ($debug);
271 } elsif ($chan{$chan}) {
272 &DEBUG("ICC: private: $chan{$chan} ($chan/$param) (hack)") if ($debug);
274 &DEBUG("ICC: private: 0 ($param)") if ($debug);
277 return $chan{$chan} || $chan{_default} || 0;
280 &DEBUG("ICC: no-match: 0/$param (msgType = $msgType)");
286 # Usage: &getChanConf($param);
287 # About: Retrieve value for 'param' value in current/default chan.
288 # Return: scalar for success, undef for failure.
292 if (!defined $param) {
293 &WARN("gCC: param == NULL.");
298 if (0 and !defined $chan) {
299 &DEBUG("gCC: ok !chan... doing _default instead.");
304 $c = "_default" if ($c eq "*"); # fix!
305 my @c = grep /^$c$/i, keys %chanconf;
308 if (0 and $c[0] ne $c) {
309 &WARN("c ne chan ($c[0] ne $chan)");
311 return $chanconf{$c[0]}{$param};
314 # &DEBUG("gCC: returning _default... ");
315 return $chanconf{"_default"}{$param};
318 sub getChanConfDefault {
319 my($what, $default, $chan) = @_;
321 $chan ||= "_default";
323 if (exists $param{$what}) {
324 if (!exists $cache{config}{$what}) {
325 &status("conf: backward-compat: found param{$what} ($param{$what}) for $chan instead.");
326 $cache{config}{$what} = 1;
329 return $param{$what};
331 my $val = &getChanConf($what, $chan);
332 return $val if (defined $val);
334 $param{$what} = $default;
335 &status("conf: $chan auto-setting param{$what} = $default");
336 $cache{config}{$what} = 1;
342 # Usage: &findChanConf($param);
343 # About: Retrieve value for 'param' value from any chan.
344 # Return: scalar for success, undef for failure.
348 if (!defined $param) {
349 &WARN("param == NULL.");
354 foreach $c (keys %chanconf) {
355 foreach (keys %{ $chanconf{$c} }) {
356 next unless (/^$param$/);
358 return $chanconf{$c}{$_};
366 my ($prefix) = $_[0] || "";
368 if ($^O eq "linux") {
369 if (!open(IN, "/proc/$$/status")) {
370 &ERROR("cannot open '/proc/$$/status'.");
375 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
379 } elsif ($^O eq "netbsd") {
380 $memusage = int( (stat "/proc/$$/mem")[7]/1024 );
382 } elsif ($^O =~ /^(free|open)bsd$/) {
383 my @info = split /\s+/, `/bin/ps -l -p $$`;
384 $memusage = $info[20];
387 $memusage = "UNKNOWN";
391 if (defined $memusageOld and &IsParam("DEBUG")) {
392 # it's always going to be increase.
393 my $delta = $memusage - $memusageOld;
397 } elsif ($delta > 500) {
398 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
399 } elsif ($delta > 0) {
400 $str = "MEM:$prefix increased by $delta kB";
401 } else { # delta < 0.
403 # never knew RSS could decrease, probably Size can't?
404 $str = "MEM:$prefix decreased by $delta kB.";
409 $memusageOld = $memusage;
417 &showProc(" (\&openLog before)");
418 &openLog(); # write, append.
419 &status("--- Started logging.");
422 &loadLang($bot_data_dir. "/blootbot.lang");
426 &loadMyModulesNow(); # must be after chan file.
429 &openSQLDebug() if (&IsParam("SQLDebug"));
430 &openDB($param{'DBName'}, $param{'DBType'}, $param{'SQLUser'},
434 &status("Setup: ". &countKeys("factoids") ." factoids.");
435 &News::readNews() if (&ChanConfList("news"));
436 &getChanConfDefault("sendPrivateLimitLines", 3);
437 &getChanConfDefault("sendPrivateLimitBytes", 1000);
438 &getChanConfDefault("sendPublicLimitLines", 3);
439 &getChanConfDefault("sendPublicLimitBytes", 1000);
440 &getChanConfDefault("sendNoticeLimitLines", 3);
441 &getChanConfDefault("sendNoticeLimitBytes", 1000);
443 $param{tempDir} =~ s#\~/#$ENV{HOME}/#;
445 &status("Initial memory usage: $memusage kB");
446 &status("-------------------------------------------------------");
450 $param{'VERBOSITY'} = 1;
451 &loadConfig($bot_config_dir."/blootbot.config");
453 foreach ( qw(ircNick ircUser ircName DBType tempDir) ) {
454 next if &IsParam($_);
455 &ERROR("Parameter $_ has not been defined.");
459 if ($param{tempDir} =~ s#\~/#$ENV{HOME}/#) {
460 &VERB("Fixing up tempDir.",2);
463 if ($param{tempDir} =~ /~/) {
464 &ERROR("parameter tempDir still contains tilde.");
468 if (! -d $param{tempDir}) {
469 &status("making $param{tempDir}...");
470 mkdir $param{tempDir}, 0755;
473 # static scalar variables.
474 $file{utm} = "$bot_state_dir/$param{'ircUser'}.uptime";
475 $file{PID} = "$bot_run_dir/$param{'ircUser'}.pid";
479 if (&IsParam("DEBUG")) {
480 &status("enabling debug diagnostics.");
481 ### I thought disabling this reduced memory usage by 1000 kB.
485 $count{'Question'} = 0;
486 $count{'Update'} = 0;
492 # reverse order of &setup().
493 &status("--- shutdown called.");
495 $ident ||= "blootbot"; # hack.
497 # opened files must be written to on shutdown/hup/whatever
498 # unless they're write-only, like uptime.
501 &News::writeNews() if (&ChanConfList("news"));
504 &closeSHM($shm); # aswell. TODO: use this in &doExit?
511 if ($$ == $bot_pid) {
512 &status("--- $sig called.");
514 ### crappy bug in Net::IRC?
515 my $delta = time() - $msgtime;
516 &DEBUG("restart: dtime = $delta");
517 if (!$conn->connected or time() - $msgtime > 900) {
518 &status("reconnecting because of uncaught disconnect \@ ".scalar(localtime) );
525 &ircCheck(); # heh, evil!
527 &DCCBroadcast("-HUP called.","m");
529 &loadConfig($bot_config_dir."/blootbot.config");
530 &reloadAllModules() if (&IsParam("DEBUG"));
533 &status("--- End of $sig.");
535 &status("$sig called; ignoring restart.");
539 # File: Configuration.
543 if (!open(FILE, $file)) {
544 &ERROR("Failed to read configuration file ($file): $!");
545 &status("Please read the INSTALL file on how to install and setup this file.");
554 my ($set,$key,$val) = split(/\s+/, $_, 3);
557 &status("loadConfig: invalid line '$_'.");
561 # perform variable interpolation
562 $val =~ s/(\$(\w+))/$param{$2}/g;
571 &status("Loaded config $file ($count items)");