2 # core.pl: Important functions stuff...
4 # Version: v0.4 (20000718)
10 # dynamic scalar. MUST BE REDUCED IN SIZE!!!
13 $answer $correction_plausible $talkchannel
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 $pubsleep
23 $msgsize $msgcount $msgtime $msgsleep
24 $notsize $notcount $nottime $notsleep
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";
62 $msgcount = $msgsleep = 0;
65 $pubcount = $pubsleep = 0;
68 $notcount = $notsleep = 0;
70 $bot_version = "blootbot cvs (20010214) -- $^O";
80 if (defined $flag_quit) {
81 &WARN("doExit: quit already called.");
86 if (!defined $bot_pid) { # independent.
88 } elsif ($bot_pid == $$) { # parent.
89 &status("parent caught SIG$sig (pid $$).") if (defined $sig);
91 &status("--- Start of quit.");
92 $ident ||= "blootbot"; # lame hack.
97 &quit($param{'quitMsg'}) if (&whatInterface() =~ /IRC/);
100 &uptimeWriteFile() if (&ChanConfList("uptime"));
101 &News::writeNews() if (&ChanConfList("news"));
104 &dumpallvars() if (&IsParam("dumpvarsAtExit"));
106 &closeSQLDebug() if (&IsParam("SQLDebug"));
108 &status("--- QUIT.");
110 &status("child caught SIG$sig (pid $$).");
117 $SIG{__WARN__} = sub { warn $_[0]; };
123 $SIG{__WARN__} = 'doWarn'; # ???
126 # Usage: &IsParam($param);
127 # blootbot.config specific.
131 return 0 unless (defined $param);
132 return 0 unless (exists $param{$param});
133 return 0 unless ($param{$param});
134 return 0 if $param{$param} =~ /^false$/i;
139 # Usage: &ChanConfList($param)
140 # About: gets channels with 'param' enabled. (!!!)
141 # Return: array of channels
144 return unless (defined $param);
145 my %chan = &getChanConfList($param);
147 if (exists $chan{_default}) {
148 return keys %chanconf;
155 # Usage: &getChanConfList($param)
156 # About: gets channels with 'param' enabled, internal use only.
157 # Return: hash of channels
158 sub getChanConfList {
162 return unless (defined $param);
164 foreach (keys %chanconf) {
166 # &DEBUG("chan => $chan");
167 my @array = grep /^$param$/, keys %{ $chanconf{$chan} };
169 next unless (scalar @array);
171 if (scalar @array > 1) {
172 &WARN("multiple items found?");
175 if ($array[0] eq "0") {
186 # Usage: &IsChanConf($param);
187 # About: Check for 'param' on the basis of channel config.
188 # Return: 1 for enabled, 0 for passive disable, -1 for active disable.
191 my $debug = 0; # knocked tons of bugs with this! :)
193 if (!defined $param) {
194 &WARN("IsChanConf: param == NULL.");
199 if ($chan =~ tr/A-Z/a-z/) {
200 &WARN("IsChanConf: lowercased chan. ($old)");
203 ### TODO: VERBOSITY on how chanconf returned 1 or 0 or -1.
204 my %chan = &getChanConfList($param);
206 if (!defined $msgType) {
209 $nomatch++ if ($msgType eq "");
210 $nomatch++ unless ($msgType =~ /^(public|private)$/i);
213 ### debug purposes only.
214 # &DEBUG("param => $param, msgType => $msgType.");
215 # foreach (keys %chan) {
216 # &DEBUG(" $_ => $chan{$_}");
221 &DEBUG("ICC: other: $chan{$chan} (_default/$param)") if ($debug);
222 } elsif ($chan{_default}) {
223 &DEBUG("ICC: other: $chan{_default} (_default/$param)") if ($debug);
225 &DEBUG("ICC: other: 0 ($param)") if ($debug);
228 return $chan{$chan} || $chan{_default} || 0;
231 if ($msgType eq "public") {
233 &DEBUG("ICC: public: $chan{$chan} ($chan/$param)") if ($debug);
234 } elsif ($chan{_default}) {
235 &DEBUG("ICC: public: $chan{_default} (_default/$param)") if ($debug);
237 &DEBUG("ICC: public: 0 ($param)") if ($debug);
240 return $chan{$chan} || $chan{_default} || 0;
243 if ($msgType eq "private") {
244 if ($chan{_default}) {
245 &DEBUG("ICC: private: $chan{_default} (_default/$param)") if ($debug);
246 } elsif ($chan{$chan}) {
247 &DEBUG("ICC: private: $chan{$chan} ($chan/$param) (hack)") if ($debug);
249 &DEBUG("ICC: private: 0 ($param)") if ($debug);
252 return $chan{$chan} || $chan{_default} || 0;
255 &DEBUG("ICC: no-match: 0/$param (msgType = $msgType)");
261 # Usage: &getChanConf($param);
262 # About: Retrieve value for 'param' value in current/default chan.
263 # Return: scalar for success, undef for failure.
265 my($param,$chan) = @_;
267 if (!defined $param) {
268 &WARN("param == NULL.");
272 $chan ||= "_default";
273 my @c = grep /^$chan$/i, keys %chanconf;
276 if ($c[0] ne $chan) {
277 &WARN("c ne chan ($c[0] ne $chan)");
279 return $chanconf{$c[0]}{$param};
282 return $chanconf{"_default"}{$param};
286 my ($prefix) = $_[0] || "";
288 if (!open(IN, "/proc/$$/status")) {
289 &ERROR("cannot open '/proc/$$/status'.");
293 if ($^O eq "linux") {
295 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
299 } elsif ($^O eq "netbsd") {
300 $memusage = (stat "/proc/$$/mem")[7]/1024;
302 } elsif ($^O =~ /^(free|open)bsd$/) {
303 my @info = split /\s+/, `/bin/ps -l -p $$`;
304 $memusage = $info[20];
307 $memusage = "UNKNOWN";
311 if (defined $memusageOld and &IsParam("DEBUG")) {
312 # it's always going to be increase.
313 my $delta = $memusage - $memusageOld;
317 } elsif ($delta > 500) {
318 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
319 } elsif ($delta > 0) {
320 $str = "MEM:$prefix increased by $delta kB";
321 } else { # delta < 0.
323 # never knew RSS could decrease, probably Size can't?
324 $str = "MEM:$prefix decreased by $delta kB. YES YES YES";
329 $memusageOld = $memusage;
337 &showProc(" (\&openLog before)");
338 &openLog(); # write, append.
339 &status("--- Started logging.");
342 my $dir = "$bot_base_dir/$_/";
344 &status("Making dir $_");
349 &loadLang($bot_misc_dir. "/blootbot.lang");
353 &loadMyModulesNow(); # must be after chan file.
356 &openSQLDebug() if (&IsParam("SQLDebug"));
357 &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
360 &status("Setup: ". &countKeys("factoids") ." factoids.");
361 &News::readNews() if (&ChanConfList("news"));
362 &getChanConfDefault("sendPrivateLimitLines", 3);
363 &getChanConfDefault("sendPrivateLimitBytes", 1000);
364 &getChanConfDefault("sendPublicLimitLines", 3);
365 &getChanConfDefault("sendPublicLimitBytes", 1000);
366 &getChanConfDefault("sendNoticeLimitLines", 3);
367 &getChanConfDefault("sendNoticeLimitBytes", 1000);
369 $param{tempDir} =~ s#\~/#$ENV{HOME}/#;
371 &status("Initial memory usage: $memusage kB");
375 $param{'VERBOSITY'} = 1;
376 &loadConfig($bot_misc_dir."/blootbot.config");
378 foreach ("ircNick", "ircUser", "ircName", "DBType", "tempDir") {
379 next if &IsParam($_);
380 &ERROR("Parameter $_ has not been defined.");
384 if ($param{tempDir} =~ s#\~/#$ENV{HOME}/#) {
385 &VERB("Fixing up tempDir.",2);
388 if ($param{tempDir} =~ /~/) {
389 &ERROR("parameter tempDir still contains tilde.");
393 if (! -d $param{tempDir}) {
394 &status("making $param{tempDir}...");
395 system("mkdir $param{tempDir}");
398 # static scalar variables.
399 $file{utm} = "$bot_base_dir/$param{'ircUser'}.uptime";
400 $file{PID} = "$bot_base_dir/$param{'ircUser'}.pid";
404 if (&IsParam("DEBUG")) {
405 &status("enabling debug diagnostics.");
406 ### I thought disabling this reduced memory usage by 1000 kB.
410 $count{'Question'} = 0;
411 $count{'Update'} = 0;
417 # reverse order of &setup().
418 &DEBUG("shutdown called.");
420 $ident ||= "blootbot"; # hack.
422 # opened files must be written to on shutdown/hup/whatever
423 # unless they're write-only, like uptime.
426 &News::writeNews() if (&ChanConfList("news"));
429 &closeSHM($shm); # aswell. TODO: use this in &doExit?
436 if ($$ == $bot_pid) {
437 &status("--- $sig called.");
439 ### crappy bug in Net::IRC?
440 if (!$conn->connected and time - $msgtime > 900) {
441 &status("reconnecting because of uncaught disconnect.");
447 &ircCheck(); # heh, evil!
449 &DCCBroadcast("-HUP called.","m");
451 &loadConfig($bot_misc_dir."/blootbot.config");
452 &reloadAllModules() if (&IsParam("DEBUG"));
455 &status("--- End of $sig.");
457 &status("$sig called; ignoring restart.");
461 # File: Configuration.
465 if (!open(FILE, $file)) {
466 &ERROR("FAILED loadConfig ($file): $!");
467 &status("Please copy files/sample.config to files/blootbot.config");
468 &status(" and edit files/blootbot.config, modify to tastes.");
477 my ($set,$key,$val) = split(/\s+/, $_, 3);
480 &status("loadConfig: invalid line '$_'.");
484 # perform variable interpolation
485 $val =~ s/(\$(\w+))/$param{$2}/g;
494 &status("Loaded config $file ($count items)");