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.");
96 &quit($param{'quitMsg'}) if (&whatInterface() =~ /IRC/);
99 &uptimeWriteFile() if (&ChanConfList("uptime"));
100 &News::writeNews() if (&ChanConfList("news"));
103 &dumpallvars() if (&IsParam("dumpvarsAtExit"));
105 &closeSQLDebug() if (&IsParam("SQLDebug"));
107 &status("--- QUIT.");
109 &status("child caught SIG$sig (pid $$).");
116 $SIG{__WARN__} = sub { warn $_[0]; };
122 $SIG{__WARN__} = 'doWarn'; # ???
125 # Usage: &IsParam($param);
126 # blootbot.config specific.
130 return 0 unless (defined $param);
131 return 0 unless (exists $param{$param});
132 return 0 unless ($param{$param});
133 return 0 if $param{$param} =~ /^false$/i;
138 # Usage: &ChanConfList($param)
139 # About: gets channels with 'param' enabled. (!!!)
140 # Return: array of channels
143 return unless (defined $param);
144 my %chan = &getChanConfList($param);
146 if (exists $chan{_default}) {
147 return keys %chanconf;
154 # Usage: &getChanConfList($param)
155 # About: gets channels with 'param' enabled, internal use only.
156 # Return: hash of channels
157 sub getChanConfList {
161 return unless (defined $param);
163 foreach (keys %chanconf) {
165 # &DEBUG("chan => $chan");
166 my @array = grep /^$param$/, keys %{ $chanconf{$chan} };
168 next unless (scalar @array);
170 if (scalar @array > 1) {
171 &WARN("multiple items found?");
174 if ($array[0] eq "0") {
185 # Usage: &IsChanConf($param);
186 # About: Check for 'param' on the basis of channel config.
187 # Return: 1 for enabled, 0 for passive disable, -1 for active disable.
190 my $debug = 0; # knocked tons of bugs with this! :)
192 if (!defined $param) {
193 &WARN("IsChanConf: param == NULL.");
198 if ($chan =~ tr/A-Z/a-z/) {
199 &WARN("IsChanConf: lowercased chan. ($old)");
202 ### TODO: VERBOSITY on how chanconf returned 1 or 0 or -1.
203 my %chan = &getChanConfList($param);
205 if (!defined $msgType) {
208 $nomatch++ if ($msgType eq "");
209 $nomatch++ unless ($msgType =~ /^(public|private)$/i);
212 ### debug purposes only.
213 # &DEBUG("param => $param, msgType => $msgType.");
214 # foreach (keys %chan) {
215 # &DEBUG(" $_ => $chan{$_}");
220 &DEBUG("ICC: other: $chan{$chan} (_default/$param)") if ($debug);
221 } elsif ($chan{_default}) {
222 &DEBUG("ICC: other: $chan{_default} (_default/$param)") if ($debug);
224 &DEBUG("ICC: other: 0 ($param)") if ($debug);
227 return $chan{$chan} || $chan{_default} || 0;
230 if ($msgType eq "public") {
232 &DEBUG("ICC: public: $chan{$chan} ($chan/$param)") if ($debug);
233 } elsif ($chan{_default}) {
234 &DEBUG("ICC: public: $chan{_default} (_default/$param)") if ($debug);
236 &DEBUG("ICC: public: 0 ($param)") if ($debug);
239 return $chan{$chan} || $chan{_default} || 0;
242 if ($msgType eq "private") {
243 if ($chan{_default}) {
244 &DEBUG("ICC: private: $chan{_default} (_default/$param)") if ($debug);
245 } elsif ($chan{$chan}) {
246 &DEBUG("ICC: private: $chan{$chan} ($chan/$param) (hack)") if ($debug);
248 &DEBUG("ICC: private: 0 ($param)") if ($debug);
251 return $chan{$chan} || $chan{_default} || 0;
254 &DEBUG("ICC: no-match: 0/$param (msgType = $msgType)");
260 # Usage: &getChanConf($param);
261 # About: Retrieve value for 'param' value in current/default chan.
262 # Return: scalar for success, undef for failure.
264 my($param,$chan) = @_;
266 if (!defined $param) {
267 &WARN("param == NULL.");
271 $chan ||= "_default";
272 my @c = grep /^$chan$/i, keys %chanconf;
275 if ($c[0] ne $chan) {
276 &WARN("c ne chan ($c[0] ne $chan)");
278 return $chanconf{$c[0]}{$param};
281 return $chanconf{"_default"}{$param};
285 my ($prefix) = $_[0] || "";
287 if (!open(IN, "/proc/$$/status")) {
288 &ERROR("cannot open '/proc/$$/status'.");
292 if ($^O eq "linux") {
294 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
298 } elsif ($^O eq "netbsd") {
299 $memusage = (stat "/proc/$$/mem")[7]/1024;
301 } elsif ($^O =~ /^(free|open)bsd$/) {
302 my @info = split /\s+/, `/bin/ps -l -p $$`;
303 $memusage = $info[20];
306 $memusage = "UNKNOWN";
310 if (defined $memusageOld and &IsParam("DEBUG")) {
311 # it's always going to be increase.
312 my $delta = $memusage - $memusageOld;
316 } elsif ($delta > 500) {
317 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
318 } elsif ($delta > 0) {
319 $str = "MEM:$prefix increased by $delta kB";
320 } else { # delta < 0.
322 # never knew RSS could decrease, probably Size can't?
323 $str = "MEM:$prefix decreased by $delta kB. YES YES YES";
328 $memusageOld = $memusage;
336 &showProc(" (\&openLog before)");
337 &openLog(); # write, append.
338 &status("--- Started logging.");
341 my $dir = "$bot_base_dir/$_/";
343 &status("Making dir $_");
348 &loadLang($bot_misc_dir. "/blootbot.lang");
352 &loadMyModulesNow(); # must be after chan file.
355 &openSQLDebug() if (&IsParam("SQLDebug"));
356 &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
358 &status("Setup: ". &countKeys("factoids") ." factoids.");
359 &News::readNews() if (&ChanConfList("news"));
360 &getChanConfDefault("sendPrivateLimitLines", 3);
361 &getChanConfDefault("sendPrivateLimitBytes", 1000);
362 &getChanConfDefault("sendPublicLimitLines", 3);
363 &getChanConfDefault("sendPublicLimitBytes", 1000);
364 &getChanConfDefault("sendNoticeLimitLines", 3);
365 &getChanConfDefault("sendNoticeLimitBytes", 1000);
367 $param{tempDir} =~ s#\~/#$ENV{HOME}/#;
369 &status("Initial memory usage: $memusage kB");
373 $param{'VERBOSITY'} = 1;
374 &loadConfig($bot_misc_dir."/blootbot.config");
376 foreach ("ircNick", "ircUser", "ircName", "DBType", "tempDir") {
377 next if &IsParam($_);
378 &ERROR("Parameter $_ has not been defined.");
382 if ($param{tempDir} =~ s#\~/#$ENV{HOME}/#) {
383 &VERB("Fixing up tempDir.",2);
386 if ($param{tempDir} =~ /~/) {
387 &ERROR("parameter tempDir still contains tilde.");
391 if (! -d $param{tempDir}) {
392 &status("making $param{tempDir}...");
393 system("mkdir $param{tempDir}");
396 # static scalar variables.
397 $file{utm} = "$bot_base_dir/$param{'ircUser'}.uptime";
398 $file{PID} = "$bot_base_dir/$param{'ircUser'}.pid";
402 if (&IsParam("DEBUG")) {
403 &status("enabling debug diagnostics.");
404 ### I thought disabling this reduced memory usage by 1000 kB.
408 $count{'Question'} = 0;
409 $count{'Update'} = 0;
415 # reverse order of &setup().
416 &DEBUG("shutdown called.");
418 # opened files must be written to on shutdown/hup/whatever
419 # unless they're write-only, like uptime.
422 &News::writeNews() if (&ChanConfList("news"));
425 &closeSHM($shm); # aswell. TODO: use this in &doExit?
432 if ($$ == $bot_pid) {
433 &status("--- $sig called.");
435 ### crappy bug in Net::IRC?
436 if (!$conn->connected and time - $msgtime > 900) {
437 &status("reconnecting because of uncaught disconnect.");
443 &ircCheck(); # heh, evil!
445 &DCCBroadcast("-HUP called.","m");
447 &loadConfig($bot_misc_dir."/blootbot.config");
448 &reloadAllModules() if (&IsParam("DEBUG"));
451 &status("--- End of $sig.");
453 &status("$sig called; ignoring restart.");
457 # File: Configuration.
461 if (!open(FILE, $file)) {
462 &ERROR("FAILED loadConfig ($file): $!");
463 &status("Please copy files/sample.config to files/blootbot.config");
464 &status(" and edit files/blootbot.config, modify to tastes.");
473 my ($set,$key,$val) = split(/\s+/, $_, 3);
476 &status("loadConfig: invalid line '$_'.");
480 # perform variable interpolation
481 $val =~ s/(\$(\w+))/$param{$2}/g;
490 &status("Loaded config $file ($count items)");