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 &uptimeWriteFile() if (&IsChanConf("uptime"));
114 &News::writeNews() if (&ChanConfList("news"));
117 &dumpallvars() if (&IsParam("dumpvarsAtExit"));
118 &symdumpAll() if (&IsParam("symdumpAtExit"));
120 &closeSQLDebug() if (&IsParam("SQLDebug"));
122 &status("--- QUIT.");
124 &status("child caught SIG$sig (pid $$).");
131 $SIG{__WARN__} = sub { warn $_[0]; };
137 $SIG{__WARN__} = 'doWarn'; # ???
140 # Usage: &IsParam($param);
141 # blootbot.config specific.
145 return 0 unless (defined $param);
146 return 0 unless (exists $param{$param});
147 return 0 unless ($param{$param});
148 return 0 if $param{$param} =~ /^false$/i;
153 # Usage: &ChanConfList($param)
154 # About: gets channels with 'param' enabled. (!!!)
155 # Return: array of channels
158 return unless (defined $param);
159 my %chan = &getChanConfList($param);
161 if (exists $chan{_default}) {
162 return keys %chanconf;
169 # Usage: &getChanConfList($param)
170 # About: gets channels with 'param' enabled, internal use only.
171 # Return: hash of channels
172 sub getChanConfList {
176 return unless (defined $param);
178 foreach (keys %chanconf) {
180 # &DEBUG("chan => $chan");
181 my @array = grep /^$param$/, keys %{ $chanconf{$chan} };
183 next unless (scalar @array);
185 if (scalar @array > 1) {
186 &WARN("multiple items found?");
189 if ($array[0] eq "0") {
200 # Usage: &IsChanConf($param);
201 # About: Check for 'param' on the basis of channel config.
202 # Return: 1 for enabled, 0 for passive disable, -1 for active disable.
205 my $debug = 0; # knocked tons of bugs with this! :)
207 if (!defined $param) {
208 &WARN("IsChanConf: param == NULL.");
212 # should we use IsParam() externally where needed or hack it in
213 # here just in case? fix it later.
214 if (&IsParam($param)) {
215 &status("ICC: found '$param' option in main config file.");
219 $chan ||= "_default";
222 if ($chan =~ tr/A-Z/a-z/) {
223 &WARN("IsChanConf: lowercased chan. ($old)");
226 ### TODO: VERBOSITY on how chanconf returned 1 or 0 or -1.
227 my %chan = &getChanConfList($param);
229 if (!defined $msgType) {
232 $nomatch++ if ($msgType eq "");
233 $nomatch++ unless ($msgType =~ /^(public|private)$/i);
236 ### debug purposes only.
237 # &DEBUG("param => $param, msgType => $msgType.");
238 # foreach (keys %chan) {
239 # &DEBUG(" $_ => $chan{$_}");
244 &DEBUG("ICC: other: $chan{$chan} (_default/$param)") if ($debug);
245 } elsif ($chan{_default}) {
246 &DEBUG("ICC: other: $chan{_default} (_default/$param)") if ($debug);
248 &DEBUG("ICC: other: 0 ($param)") if ($debug);
251 return $chan{$chan} || $chan{_default} || 0;
254 if ($msgType eq "public") {
256 &DEBUG("ICC: public: $chan{$chan} ($chan/$param)") if ($debug);
257 } elsif ($chan{_default}) {
258 &DEBUG("ICC: public: $chan{_default} (_default/$param)") if ($debug);
260 &DEBUG("ICC: public: 0 ($param)") if ($debug);
263 return $chan{$chan} || $chan{_default} || 0;
266 if ($msgType eq "private") {
267 if ($chan{_default}) {
268 &DEBUG("ICC: private: $chan{_default} (_default/$param)") if ($debug);
269 } elsif ($chan{$chan}) {
270 &DEBUG("ICC: private: $chan{$chan} ($chan/$param) (hack)") if ($debug);
272 &DEBUG("ICC: private: 0 ($param)") if ($debug);
275 return $chan{$chan} || $chan{_default} || 0;
278 &DEBUG("ICC: no-match: 0/$param (msgType = $msgType)");
284 # Usage: &getChanConf($param);
285 # About: Retrieve value for 'param' value in current/default chan.
286 # Return: scalar for success, undef for failure.
290 if (!defined $param) {
291 &WARN("gCC: param == NULL.");
296 if (0 and !defined $chan) {
297 &DEBUG("gCC: ok !chan... doing _default instead.");
302 $c = "_default" if ($c eq "*"); # fix!
303 my @c = grep /^$c$/i, keys %chanconf;
306 if (0 and $c[0] ne $c) {
307 &WARN("c ne chan ($c[0] ne $chan)");
309 return $chanconf{$c[0]}{$param};
312 # &DEBUG("gCC: returning _default... ");
313 return $chanconf{"_default"}{$param};
316 sub getChanConfDefault {
317 my($what, $default, $chan) = @_;
319 if (exists $param{$what}) {
320 if (!exists $cache{config}{$what}) {
321 &status("conf: backward-compat: found param{$what} ($param{$what}) instead.");
322 $cache{config}{$what} = 1;
325 return $param{$what};
327 my $val = &getChanConf($what, $chan);
328 return $val if (defined $val);
330 $param{$what} = $default;
331 &status("conf: auto-setting param{$what} = $default");
332 $cache{config}{$what} = 1;
338 # Usage: &findChanConf($param);
339 # About: Retrieve value for 'param' value from any chan.
340 # Return: scalar for success, undef for failure.
344 if (!defined $param) {
345 &WARN("param == NULL.");
350 foreach $c (keys %chanconf) {
351 foreach (keys %{ $chanconf{$c} }) {
352 next unless (/^$param$/);
354 &DEBUG("chanconf{$c}{$_} ...");
355 return $chanconf{$c}{$_};
363 my ($prefix) = $_[0] || "";
365 if ($^O eq "linux") {
366 if (!open(IN, "/proc/$$/status")) {
367 &ERROR("cannot open '/proc/$$/status'.");
372 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
376 } elsif ($^O eq "netbsd") {
377 $memusage = (stat "/proc/$$/mem")[7]/1024;
379 } elsif ($^O =~ /^(free|open)bsd$/) {
380 my @info = split /\s+/, `/bin/ps -l -p $$`;
381 $memusage = $info[20];
384 $memusage = "UNKNOWN";
388 if (defined $memusageOld and &IsParam("DEBUG")) {
389 # it's always going to be increase.
390 my $delta = $memusage - $memusageOld;
394 } elsif ($delta > 500) {
395 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
396 } elsif ($delta > 0) {
397 $str = "MEM:$prefix increased by $delta kB";
398 } else { # delta < 0.
400 # never knew RSS could decrease, probably Size can't?
401 $str = "MEM:$prefix decreased by $delta kB.";
406 $memusageOld = $memusage;
414 &showProc(" (\&openLog before)");
415 &openLog(); # write, append.
416 &status("--- Started logging.");
419 &loadLang($bot_data_dir. "/blootbot.lang");
423 &loadMyModulesNow(); # must be after chan file.
426 &openSQLDebug() if (&IsParam("SQLDebug"));
427 &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
430 &status("Setup: ". &countKeys("factoids") ." factoids.");
431 &News::readNews() if (&ChanConfList("news"));
432 &getChanConfDefault("sendPrivateLimitLines", 3);
433 &getChanConfDefault("sendPrivateLimitBytes", 1000);
434 &getChanConfDefault("sendPublicLimitLines", 3);
435 &getChanConfDefault("sendPublicLimitBytes", 1000);
436 &getChanConfDefault("sendNoticeLimitLines", 3);
437 &getChanConfDefault("sendNoticeLimitBytes", 1000);
439 $param{tempDir} =~ s#\~/#$ENV{HOME}/#;
441 &status("Initial memory usage: $memusage kB");
442 &status("-------------------------------------------------------");
446 $param{'VERBOSITY'} = 1;
447 &loadConfig($bot_config_dir."/blootbot.config");
449 foreach ( qw(ircNick ircUser ircName DBType tempDir) ) {
450 next if &IsParam($_);
451 &ERROR("Parameter $_ has not been defined.");
455 if ($param{tempDir} =~ s#\~/#$ENV{HOME}/#) {
456 &VERB("Fixing up tempDir.",2);
459 if ($param{tempDir} =~ /~/) {
460 &ERROR("parameter tempDir still contains tilde.");
464 if (! -d $param{tempDir}) {
465 &status("making $param{tempDir}...");
466 mkdir $param{tempDir}, 0755;
469 # static scalar variables.
470 $file{utm} = "$bot_state_dir/$param{'ircUser'}.uptime";
471 $file{PID} = "$bot_run_dir/$param{'ircUser'}.pid";
475 if (&IsParam("DEBUG")) {
476 &status("enabling debug diagnostics.");
477 ### I thought disabling this reduced memory usage by 1000 kB.
481 $count{'Question'} = 0;
482 $count{'Update'} = 0;
488 # reverse order of &setup().
489 &status("--- shutdown called.");
491 $ident ||= "blootbot"; # hack.
493 # opened files must be written to on shutdown/hup/whatever
494 # unless they're write-only, like uptime.
497 &News::writeNews() if (&ChanConfList("news"));
500 &closeSHM($shm); # aswell. TODO: use this in &doExit?
507 if ($$ == $bot_pid) {
508 &status("--- $sig called.");
510 ### crappy bug in Net::IRC?
511 if (!$conn->connected and time - $msgtime > 900) {
512 &status("reconnecting because of uncaught disconnect \@ ".scalar(localtime) );
519 &ircCheck(); # heh, evil!
521 &DCCBroadcast("-HUP called.","m");
523 &loadConfig($bot_config_dir."/blootbot.config");
524 &reloadAllModules() if (&IsParam("DEBUG"));
527 &status("--- End of $sig.");
529 &status("$sig called; ignoring restart.");
533 # File: Configuration.
537 if (!open(FILE, $file)) {
538 &ERROR("Failed to read configuration file ($file): $!");
539 &status("Please read the INSTALL file on how to install and setup this file.");
548 my ($set,$key,$val) = split(/\s+/, $_, 3);
551 &status("loadConfig: invalid line '$_'.");
555 # perform variable interpolation
556 $val =~ s/(\$(\w+))/$param{$2}/g;
565 &status("Loaded config $file ($count items)");