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.
107 &quit($param{'quitMsg'}) if (&whatInterface() =~ /IRC/);
110 &uptimeWriteFile() if (&ChanConfList("uptime"));
111 &News::writeNews() if (&ChanConfList("news"));
114 &dumpallvars() if (&IsParam("dumpvarsAtExit"));
116 &closeSQLDebug() if (&IsParam("SQLDebug"));
118 &status("--- QUIT.");
120 &status("child caught SIG$sig (pid $$).");
127 $SIG{__WARN__} = sub { warn $_[0]; };
133 $SIG{__WARN__} = 'doWarn'; # ???
136 # Usage: &IsParam($param);
137 # blootbot.config specific.
141 return 0 unless (defined $param);
142 return 0 unless (exists $param{$param});
143 return 0 unless ($param{$param});
144 return 0 if $param{$param} =~ /^false$/i;
149 # Usage: &ChanConfList($param)
150 # About: gets channels with 'param' enabled. (!!!)
151 # Return: array of channels
154 return unless (defined $param);
155 my %chan = &getChanConfList($param);
157 if (exists $chan{_default}) {
158 return keys %chanconf;
165 # Usage: &getChanConfList($param)
166 # About: gets channels with 'param' enabled, internal use only.
167 # Return: hash of channels
168 sub getChanConfList {
172 return unless (defined $param);
174 foreach (keys %chanconf) {
176 # &DEBUG("chan => $chan");
177 my @array = grep /^$param$/, keys %{ $chanconf{$chan} };
179 next unless (scalar @array);
181 if (scalar @array > 1) {
182 &WARN("multiple items found?");
185 if ($array[0] eq "0") {
196 # Usage: &IsChanConf($param);
197 # About: Check for 'param' on the basis of channel config.
198 # Return: 1 for enabled, 0 for passive disable, -1 for active disable.
201 my $debug = 0; # knocked tons of bugs with this! :)
203 if (!defined $param) {
204 &WARN("IsChanConf: param == NULL.");
208 $chan ||= "_default";
211 if ($chan =~ tr/A-Z/a-z/) {
212 &WARN("IsChanConf: lowercased chan. ($old)");
215 ### TODO: VERBOSITY on how chanconf returned 1 or 0 or -1.
216 my %chan = &getChanConfList($param);
218 if (!defined $msgType) {
221 $nomatch++ if ($msgType eq "");
222 $nomatch++ unless ($msgType =~ /^(public|private)$/i);
225 ### debug purposes only.
226 # &DEBUG("param => $param, msgType => $msgType.");
227 # foreach (keys %chan) {
228 # &DEBUG(" $_ => $chan{$_}");
233 &DEBUG("ICC: other: $chan{$chan} (_default/$param)") if ($debug);
234 } elsif ($chan{_default}) {
235 &DEBUG("ICC: other: $chan{_default} (_default/$param)") if ($debug);
237 &DEBUG("ICC: other: 0 ($param)") if ($debug);
240 return $chan{$chan} || $chan{_default} || 0;
243 if ($msgType eq "public") {
245 &DEBUG("ICC: public: $chan{$chan} ($chan/$param)") if ($debug);
246 } elsif ($chan{_default}) {
247 &DEBUG("ICC: public: $chan{_default} (_default/$param)") if ($debug);
249 &DEBUG("ICC: public: 0 ($param)") if ($debug);
252 return $chan{$chan} || $chan{_default} || 0;
255 if ($msgType eq "private") {
256 if ($chan{_default}) {
257 &DEBUG("ICC: private: $chan{_default} (_default/$param)") if ($debug);
258 } elsif ($chan{$chan}) {
259 &DEBUG("ICC: private: $chan{$chan} ($chan/$param) (hack)") if ($debug);
261 &DEBUG("ICC: private: 0 ($param)") if ($debug);
264 return $chan{$chan} || $chan{_default} || 0;
267 &DEBUG("ICC: no-match: 0/$param (msgType = $msgType)");
273 # Usage: &getChanConf($param);
274 # About: Retrieve value for 'param' value in current/default chan.
275 # Return: scalar for success, undef for failure.
279 if (!defined $param) {
280 &WARN("param == NULL.");
285 if (0 and !defined $chan) {
286 &DEBUG("gCC: ok !chan... doing _default instead.");
291 $c = "_default" if ($c eq "*"); # fix!
292 my @c = grep /^$c$/i, keys %chanconf;
295 if (0 and $c[0] ne $c) {
296 &WARN("c ne chan ($c[0] ne $chan)");
298 return $chanconf{$c[0]}{$param};
301 # &DEBUG("gCC: returning _default... ");
302 return $chanconf{"_default"}{$param};
306 # Usage: &findChanConf($param);
307 # About: Retrieve value for 'param' value from any chan.
308 # Return: scalar for success, undef for failure.
312 if (!defined $param) {
313 &WARN("param == NULL.");
318 foreach $c (keys %chanconf) {
319 foreach (keys %{ $chanconf{$c} }) {
320 next unless (/^$param$/);
322 &DEBUG("chanconf{$c}{$_} ...");
323 return $chanconf{$c}{$_};
331 my ($prefix) = $_[0] || "";
333 if (!open(IN, "/proc/$$/status")) {
334 &ERROR("cannot open '/proc/$$/status'.");
338 if ($^O eq "linux") {
340 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
344 } elsif ($^O eq "netbsd") {
345 $memusage = (stat "/proc/$$/mem")[7]/1024;
347 } elsif ($^O =~ /^(free|open)bsd$/) {
348 my @info = split /\s+/, `/bin/ps -l -p $$`;
349 $memusage = $info[20];
352 $memusage = "UNKNOWN";
356 if (defined $memusageOld and &IsParam("DEBUG")) {
357 # it's always going to be increase.
358 my $delta = $memusage - $memusageOld;
362 } elsif ($delta > 500) {
363 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
364 } elsif ($delta > 0) {
365 $str = "MEM:$prefix increased by $delta kB";
366 } else { # delta < 0.
368 # never knew RSS could decrease, probably Size can't?
369 $str = "MEM:$prefix decreased by $delta kB. YES YES YES";
374 $memusageOld = $memusage;
382 &showProc(" (\&openLog before)");
383 &openLog(); # write, append.
384 &status("--- Started logging.");
387 my $dir = "$bot_base_dir/$_/";
389 &status("Making dir $_");
394 &loadLang($bot_data_dir. "/blootbot.lang");
398 &loadMyModulesNow(); # must be after chan file.
401 &openSQLDebug() if (&IsParam("SQLDebug"));
402 &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
405 &status("Setup: ". &countKeys("factoids") ." factoids.");
406 &News::readNews() if (&ChanConfList("news"));
407 &getChanConfDefault("sendPrivateLimitLines", 3);
408 &getChanConfDefault("sendPrivateLimitBytes", 1000);
409 &getChanConfDefault("sendPublicLimitLines", 3);
410 &getChanConfDefault("sendPublicLimitBytes", 1000);
411 &getChanConfDefault("sendNoticeLimitLines", 3);
412 &getChanConfDefault("sendNoticeLimitBytes", 1000);
414 $param{tempDir} =~ s#\~/#$ENV{HOME}/#;
416 &status("Initial memory usage: $memusage kB");
417 &status("-------------------------------------------------------");
421 $param{'VERBOSITY'} = 1;
422 &loadConfig($bot_config_dir."/blootbot.config");
424 foreach ("ircNick", "ircUser", "ircName", "DBType", "tempDir") {
425 next if &IsParam($_);
426 &ERROR("Parameter $_ has not been defined.");
430 if ($param{tempDir} =~ s#\~/#$ENV{HOME}/#) {
431 &VERB("Fixing up tempDir.",2);
434 if ($param{tempDir} =~ /~/) {
435 &ERROR("parameter tempDir still contains tilde.");
439 if (! -d $param{tempDir}) {
440 &status("making $param{tempDir}...");
441 system("mkdir $param{tempDir}");
444 # static scalar variables.
445 $file{utm} = "$bot_state_dir/$param{'ircUser'}.uptime";
446 $file{PID} = "$bot_run_dir/$param{'ircUser'}.pid";
450 if (&IsParam("DEBUG")) {
451 &status("enabling debug diagnostics.");
452 ### I thought disabling this reduced memory usage by 1000 kB.
456 $count{'Question'} = 0;
457 $count{'Update'} = 0;
463 # reverse order of &setup().
464 &status("--- shutdown called.");
466 $ident ||= "blootbot"; # hack.
468 # opened files must be written to on shutdown/hup/whatever
469 # unless they're write-only, like uptime.
472 &News::writeNews() if (&ChanConfList("news"));
475 &closeSHM($shm); # aswell. TODO: use this in &doExit?
482 if ($$ == $bot_pid) {
483 &status("--- $sig called.");
485 ### crappy bug in Net::IRC?
486 if (!$conn->connected and time - $msgtime > 900) {
487 &status("reconnecting because of uncaught disconnect \@ ".scalar(localtime) );
494 &ircCheck(); # heh, evil!
496 &DCCBroadcast("-HUP called.","m");
498 &loadConfig($bot_config_dir."/blootbot.config");
499 &reloadAllModules() if (&IsParam("DEBUG"));
502 &status("--- End of $sig.");
504 &status("$sig called; ignoring restart.");
508 # File: Configuration.
512 if (!open(FILE, $file)) {
513 &ERROR("Failed to read configuration file ($file): $!");
514 &status("Please read the INSTALL file on how to install and setup this file.");
523 my ($set,$key,$val) = split(/\s+/, $_, 3);
526 &status("loadConfig: invalid line '$_'.");
530 # perform variable interpolation
531 $val =~ s/(\$(\w+))/$param{$2}/g;
540 &status("Loaded config $file ($count items)");