2 # core.pl: Important functions stuff...
4 # Version: v0.4 (20000718)
10 # dynamic scalar. MUST BE REDUCED IN SIZE!!!
13 $answer $correction_plausible $talkchannel $bot_release
14 $statcount $memusage $user $memusageOld $bot_version $dbh
15 $shm $host $msg $bot_misc_dir $bot_pid $bot_base_dir $noreply
16 $bot_src_dir $conn $irc $learnok $nick $ident $no_syscall
17 $force_public_reply $addrchar $userHandle $addressedother
18 $floodwho $chan $msgtime $server $firsttime $wingaterun
20 $utime_userfile $wtime_userfile $ucount_userfile
21 $utime_chanfile $wtime_chanfile $ucount_chanfile
22 $pubsize $pubcount $pubtime
23 $msgsize $msgcount $msgtime
24 $notsize $notcount $nottime
28 use vars qw(@joinchan @ircServers @wingateBad @wingateNow @wingateCache
31 ### dynamic hash. MUST BE REDUCED IN SIZE!!!
33 use vars qw(%count %netsplit %netsplitservers %flood %dcc %orig
34 %nuh %talkWho %seen %floodwarn %param %dbh %ircPort
35 %topic %moduleAge %last %time %mask %file
36 %forked %chanconf %channels
40 $SIG{'HUP'} = 'restart'; # 1.
41 $SIG{'INT'} = 'doExit'; # 2.
42 $SIG{'KILL'} = 'doExit'; # 9. DOES NOT WORK. 'man perlipc' for details.
43 $SIG{'TERM'} = 'doExit'; # 15.
44 $SIG{'__WARN__'} = 'doWarn';
46 # initialize variables.
50 $userHandle = "default";
71 use POSIX qw(strftime);
72 $bot_release = strftime("cvs (%Y%m%d)", localtime( (stat("CVS"))[9] ) );
74 $bot_release = "1.0.10 (2001xxxx)";
76 $bot_version = "blootbot $bot_release -- $^O";
86 if (defined $flag_quit) {
87 &WARN("doExit: quit already called.");
92 if (!defined $bot_pid) { # independent.
94 } elsif ($bot_pid == $$) { # parent.
95 &status("parent caught SIG$sig (pid $$).") if (defined $sig);
97 &status("--- Start of quit.");
98 $ident ||= "blootbot"; # lame hack.
104 &quit($param{'quitMsg'}) if (&whatInterface() =~ /IRC/);
107 &uptimeWriteFile() if (&ChanConfList("uptime"));
108 &News::writeNews() if (&ChanConfList("news"));
111 &dumpallvars() if (&IsParam("dumpvarsAtExit"));
113 &closeSQLDebug() if (&IsParam("SQLDebug"));
115 &status("--- QUIT.");
117 &status("child caught SIG$sig (pid $$).");
124 $SIG{__WARN__} = sub { warn $_[0]; };
130 $SIG{__WARN__} = 'doWarn'; # ???
133 # Usage: &IsParam($param);
134 # blootbot.config specific.
138 return 0 unless (defined $param);
139 return 0 unless (exists $param{$param});
140 return 0 unless ($param{$param});
141 return 0 if $param{$param} =~ /^false$/i;
146 # Usage: &ChanConfList($param)
147 # About: gets channels with 'param' enabled. (!!!)
148 # Return: array of channels
151 return unless (defined $param);
152 my %chan = &getChanConfList($param);
154 if (exists $chan{_default}) {
155 return keys %chanconf;
162 # Usage: &getChanConfList($param)
163 # About: gets channels with 'param' enabled, internal use only.
164 # Return: hash of channels
165 sub getChanConfList {
169 return unless (defined $param);
171 foreach (keys %chanconf) {
173 # &DEBUG("chan => $chan");
174 my @array = grep /^$param$/, keys %{ $chanconf{$chan} };
176 next unless (scalar @array);
178 if (scalar @array > 1) {
179 &WARN("multiple items found?");
182 if ($array[0] eq "0") {
193 # Usage: &IsChanConf($param);
194 # About: Check for 'param' on the basis of channel config.
195 # Return: 1 for enabled, 0 for passive disable, -1 for active disable.
198 my $debug = 0; # knocked tons of bugs with this! :)
200 if (!defined $param) {
201 &WARN("IsChanConf: param == NULL.");
205 $chan ||= "_default";
208 if ($chan =~ tr/A-Z/a-z/) {
209 &WARN("IsChanConf: lowercased chan. ($old)");
212 ### TODO: VERBOSITY on how chanconf returned 1 or 0 or -1.
213 my %chan = &getChanConfList($param);
215 if (!defined $msgType) {
218 $nomatch++ if ($msgType eq "");
219 $nomatch++ unless ($msgType =~ /^(public|private)$/i);
222 ### debug purposes only.
223 # &DEBUG("param => $param, msgType => $msgType.");
224 # foreach (keys %chan) {
225 # &DEBUG(" $_ => $chan{$_}");
230 &DEBUG("ICC: other: $chan{$chan} (_default/$param)") if ($debug);
231 } elsif ($chan{_default}) {
232 &DEBUG("ICC: other: $chan{_default} (_default/$param)") if ($debug);
234 &DEBUG("ICC: other: 0 ($param)") if ($debug);
237 return $chan{$chan} || $chan{_default} || 0;
240 if ($msgType eq "public") {
242 &DEBUG("ICC: public: $chan{$chan} ($chan/$param)") if ($debug);
243 } elsif ($chan{_default}) {
244 &DEBUG("ICC: public: $chan{_default} (_default/$param)") if ($debug);
246 &DEBUG("ICC: public: 0 ($param)") if ($debug);
249 return $chan{$chan} || $chan{_default} || 0;
252 if ($msgType eq "private") {
253 if ($chan{_default}) {
254 &DEBUG("ICC: private: $chan{_default} (_default/$param)") if ($debug);
255 } elsif ($chan{$chan}) {
256 &DEBUG("ICC: private: $chan{$chan} ($chan/$param) (hack)") if ($debug);
258 &DEBUG("ICC: private: 0 ($param)") if ($debug);
261 return $chan{$chan} || $chan{_default} || 0;
264 &DEBUG("ICC: no-match: 0/$param (msgType = $msgType)");
270 # Usage: &getChanConf($param);
271 # About: Retrieve value for 'param' value in current/default chan.
272 # Return: scalar for success, undef for failure.
276 if (!defined $param) {
277 &WARN("param == NULL.");
282 if (0 and !defined $chan) {
283 &DEBUG("gCC: ok !chan... doing _default instead.");
288 $c = "_default" if ($c eq "*"); # fix!
289 my @c = grep /^$c$/i, keys %chanconf;
292 if (0 and $c[0] ne $c) {
293 &WARN("c ne chan ($c[0] ne $chan)");
295 return $chanconf{$c[0]}{$param};
298 # &DEBUG("gCC: returning _default... ");
299 return $chanconf{"_default"}{$param};
303 # Usage: &findChanConf($param);
304 # About: Retrieve value for 'param' value from any chan.
305 # Return: scalar for success, undef for failure.
309 if (!defined $param) {
310 &WARN("param == NULL.");
315 foreach $c (keys %chanconf) {
316 foreach (keys %{ $chanconf{$c} }) {
317 next unless (/^$param$/);
319 &DEBUG("chanconf{$c}{$_} ...");
320 return $chanconf{$c}{$_};
328 my ($prefix) = $_[0] || "";
330 if (!open(IN, "/proc/$$/status")) {
331 &ERROR("cannot open '/proc/$$/status'.");
335 if ($^O eq "linux") {
337 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
341 } elsif ($^O eq "netbsd") {
342 $memusage = (stat "/proc/$$/mem")[7]/1024;
344 } elsif ($^O =~ /^(free|open)bsd$/) {
345 my @info = split /\s+/, `/bin/ps -l -p $$`;
346 $memusage = $info[20];
349 $memusage = "UNKNOWN";
353 if (defined $memusageOld and &IsParam("DEBUG")) {
354 # it's always going to be increase.
355 my $delta = $memusage - $memusageOld;
359 } elsif ($delta > 500) {
360 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
361 } elsif ($delta > 0) {
362 $str = "MEM:$prefix increased by $delta kB";
363 } else { # delta < 0.
365 # never knew RSS could decrease, probably Size can't?
366 $str = "MEM:$prefix decreased by $delta kB. YES YES YES";
371 $memusageOld = $memusage;
379 &showProc(" (\&openLog before)");
380 &openLog(); # write, append.
381 &status("--- Started logging.");
384 my $dir = "$bot_base_dir/$_/";
386 &status("Making dir $_");
391 &loadLang($bot_misc_dir. "/blootbot.lang");
395 &loadMyModulesNow(); # must be after chan file.
398 &openSQLDebug() if (&IsParam("SQLDebug"));
399 &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
402 &status("Setup: ". &countKeys("factoids") ." factoids.");
403 &News::readNews() if (&ChanConfList("news"));
404 &getChanConfDefault("sendPrivateLimitLines", 3);
405 &getChanConfDefault("sendPrivateLimitBytes", 1000);
406 &getChanConfDefault("sendPublicLimitLines", 3);
407 &getChanConfDefault("sendPublicLimitBytes", 1000);
408 &getChanConfDefault("sendNoticeLimitLines", 3);
409 &getChanConfDefault("sendNoticeLimitBytes", 1000);
411 $param{tempDir} =~ s#\~/#$ENV{HOME}/#;
413 &status("Initial memory usage: $memusage kB");
414 &status("-------------------------------------------------------");
418 $param{'VERBOSITY'} = 1;
419 &loadConfig($bot_misc_dir."/blootbot.config");
421 foreach ("ircNick", "ircUser", "ircName", "DBType", "tempDir") {
422 next if &IsParam($_);
423 &ERROR("Parameter $_ has not been defined.");
427 if ($param{tempDir} =~ s#\~/#$ENV{HOME}/#) {
428 &VERB("Fixing up tempDir.",2);
431 if ($param{tempDir} =~ /~/) {
432 &ERROR("parameter tempDir still contains tilde.");
436 if (! -d $param{tempDir}) {
437 &status("making $param{tempDir}...");
438 system("mkdir $param{tempDir}");
441 # static scalar variables.
442 $file{utm} = "$bot_base_dir/$param{'ircUser'}.uptime";
443 $file{PID} = "$bot_base_dir/$param{'ircUser'}.pid";
447 if (&IsParam("DEBUG")) {
448 &status("enabling debug diagnostics.");
449 ### I thought disabling this reduced memory usage by 1000 kB.
453 $count{'Question'} = 0;
454 $count{'Update'} = 0;
460 # reverse order of &setup().
461 &status("--- shutdown called.");
463 $ident ||= "blootbot"; # hack.
465 # opened files must be written to on shutdown/hup/whatever
466 # unless they're write-only, like uptime.
469 &News::writeNews() if (&ChanConfList("news"));
472 &closeSHM($shm); # aswell. TODO: use this in &doExit?
479 if ($$ == $bot_pid) {
480 &status("--- $sig called.");
482 ### crappy bug in Net::IRC?
483 if (!$conn->connected and time - $msgtime > 900) {
484 &status("reconnecting because of uncaught disconnect \@ ".scalar(localtime) );
490 &ircCheck(); # heh, evil!
492 &DCCBroadcast("-HUP called.","m");
494 &loadConfig($bot_misc_dir."/blootbot.config");
495 &reloadAllModules() if (&IsParam("DEBUG"));
498 &status("--- End of $sig.");
500 &status("$sig called; ignoring restart.");
504 # File: Configuration.
508 if (!open(FILE, $file)) {
509 &ERROR("FAILED loadConfig ($file): $!");
510 &status("Please copy files/sample.config to files/blootbot.config");
511 &status(" and edit files/blootbot.config, modify to tastes.");
520 my ($set,$key,$val) = split(/\s+/, $_, 3);
523 &status("loadConfig: invalid line '$_'.");
527 # perform variable interpolation
528 $val =~ s/(\$(\w+))/$param{$2}/g;
537 &status("Loaded config $file ($count items)");