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.
103 &quit($param{'quitMsg'}) if (&whatInterface() =~ /IRC/);
106 &uptimeWriteFile() if (&ChanConfList("uptime"));
107 &News::writeNews() if (&ChanConfList("news"));
110 &dumpallvars() if (&IsParam("dumpvarsAtExit"));
112 &closeSQLDebug() if (&IsParam("SQLDebug"));
114 &status("--- QUIT.");
116 &status("child caught SIG$sig (pid $$).");
123 $SIG{__WARN__} = sub { warn $_[0]; };
129 $SIG{__WARN__} = 'doWarn'; # ???
132 # Usage: &IsParam($param);
133 # blootbot.config specific.
137 return 0 unless (defined $param);
138 return 0 unless (exists $param{$param});
139 return 0 unless ($param{$param});
140 return 0 if $param{$param} =~ /^false$/i;
145 # Usage: &ChanConfList($param)
146 # About: gets channels with 'param' enabled. (!!!)
147 # Return: array of channels
150 return unless (defined $param);
151 my %chan = &getChanConfList($param);
153 if (exists $chan{_default}) {
154 return keys %chanconf;
161 # Usage: &getChanConfList($param)
162 # About: gets channels with 'param' enabled, internal use only.
163 # Return: hash of channels
164 sub getChanConfList {
168 return unless (defined $param);
170 foreach (keys %chanconf) {
172 # &DEBUG("chan => $chan");
173 my @array = grep /^$param$/, keys %{ $chanconf{$chan} };
175 next unless (scalar @array);
177 if (scalar @array > 1) {
178 &WARN("multiple items found?");
181 if ($array[0] eq "0") {
192 # Usage: &IsChanConf($param);
193 # About: Check for 'param' on the basis of channel config.
194 # Return: 1 for enabled, 0 for passive disable, -1 for active disable.
197 my $debug = 0; # knocked tons of bugs with this! :)
199 if (!defined $param) {
200 &WARN("IsChanConf: param == NULL.");
204 $chan ||= "_default";
207 if ($chan =~ tr/A-Z/a-z/) {
208 &WARN("IsChanConf: lowercased chan. ($old)");
211 ### TODO: VERBOSITY on how chanconf returned 1 or 0 or -1.
212 my %chan = &getChanConfList($param);
214 if (!defined $msgType) {
217 $nomatch++ if ($msgType eq "");
218 $nomatch++ unless ($msgType =~ /^(public|private)$/i);
221 ### debug purposes only.
222 # &DEBUG("param => $param, msgType => $msgType.");
223 # foreach (keys %chan) {
224 # &DEBUG(" $_ => $chan{$_}");
229 &DEBUG("ICC: other: $chan{$chan} (_default/$param)") if ($debug);
230 } elsif ($chan{_default}) {
231 &DEBUG("ICC: other: $chan{_default} (_default/$param)") if ($debug);
233 &DEBUG("ICC: other: 0 ($param)") if ($debug);
236 return $chan{$chan} || $chan{_default} || 0;
239 if ($msgType eq "public") {
241 &DEBUG("ICC: public: $chan{$chan} ($chan/$param)") if ($debug);
242 } elsif ($chan{_default}) {
243 &DEBUG("ICC: public: $chan{_default} (_default/$param)") if ($debug);
245 &DEBUG("ICC: public: 0 ($param)") if ($debug);
248 return $chan{$chan} || $chan{_default} || 0;
251 if ($msgType eq "private") {
252 if ($chan{_default}) {
253 &DEBUG("ICC: private: $chan{_default} (_default/$param)") if ($debug);
254 } elsif ($chan{$chan}) {
255 &DEBUG("ICC: private: $chan{$chan} ($chan/$param) (hack)") if ($debug);
257 &DEBUG("ICC: private: 0 ($param)") if ($debug);
260 return $chan{$chan} || $chan{_default} || 0;
263 &DEBUG("ICC: no-match: 0/$param (msgType = $msgType)");
269 # Usage: &getChanConf($param);
270 # About: Retrieve value for 'param' value in current/default chan.
271 # Return: scalar for success, undef for failure.
275 if (!defined $param) {
276 &WARN("param == NULL.");
281 if (0 and !defined $chan) {
282 &DEBUG("gCC: ok !chan... doing _default instead.");
287 $c = "_default" if ($c eq "*"); # fix!
288 my @c = grep /^$c$/i, keys %chanconf;
291 if (0 and $c[0] ne $c) {
292 &WARN("c ne chan ($c[0] ne $chan)");
294 return $chanconf{$c[0]}{$param};
297 # &DEBUG("gCC: returning _default... ");
298 return $chanconf{"_default"}{$param};
302 my ($prefix) = $_[0] || "";
304 if (!open(IN, "/proc/$$/status")) {
305 &ERROR("cannot open '/proc/$$/status'.");
309 if ($^O eq "linux") {
311 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
315 } elsif ($^O eq "netbsd") {
316 $memusage = (stat "/proc/$$/mem")[7]/1024;
318 } elsif ($^O =~ /^(free|open)bsd$/) {
319 my @info = split /\s+/, `/bin/ps -l -p $$`;
320 $memusage = $info[20];
323 $memusage = "UNKNOWN";
327 if (defined $memusageOld and &IsParam("DEBUG")) {
328 # it's always going to be increase.
329 my $delta = $memusage - $memusageOld;
333 } elsif ($delta > 500) {
334 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
335 } elsif ($delta > 0) {
336 $str = "MEM:$prefix increased by $delta kB";
337 } else { # delta < 0.
339 # never knew RSS could decrease, probably Size can't?
340 $str = "MEM:$prefix decreased by $delta kB. YES YES YES";
345 $memusageOld = $memusage;
353 &showProc(" (\&openLog before)");
354 &openLog(); # write, append.
355 &status("--- Started logging.");
358 my $dir = "$bot_base_dir/$_/";
360 &status("Making dir $_");
365 &loadLang($bot_misc_dir. "/blootbot.lang");
369 &loadMyModulesNow(); # must be after chan file.
372 &openSQLDebug() if (&IsParam("SQLDebug"));
373 &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
376 &status("Setup: ". &countKeys("factoids") ." factoids.");
377 &News::readNews() if (&ChanConfList("news"));
378 &getChanConfDefault("sendPrivateLimitLines", 3);
379 &getChanConfDefault("sendPrivateLimitBytes", 1000);
380 &getChanConfDefault("sendPublicLimitLines", 3);
381 &getChanConfDefault("sendPublicLimitBytes", 1000);
382 &getChanConfDefault("sendNoticeLimitLines", 3);
383 &getChanConfDefault("sendNoticeLimitBytes", 1000);
385 $param{tempDir} =~ s#\~/#$ENV{HOME}/#;
387 &status("Initial memory usage: $memusage kB");
388 &status("-------------------------------------------------------");
392 $param{'VERBOSITY'} = 1;
393 &loadConfig($bot_misc_dir."/blootbot.config");
395 foreach ("ircNick", "ircUser", "ircName", "DBType", "tempDir") {
396 next if &IsParam($_);
397 &ERROR("Parameter $_ has not been defined.");
401 if ($param{tempDir} =~ s#\~/#$ENV{HOME}/#) {
402 &VERB("Fixing up tempDir.",2);
405 if ($param{tempDir} =~ /~/) {
406 &ERROR("parameter tempDir still contains tilde.");
410 if (! -d $param{tempDir}) {
411 &status("making $param{tempDir}...");
412 system("mkdir $param{tempDir}");
415 # static scalar variables.
416 $file{utm} = "$bot_base_dir/$param{'ircUser'}.uptime";
417 $file{PID} = "$bot_base_dir/$param{'ircUser'}.pid";
421 if (&IsParam("DEBUG")) {
422 &status("enabling debug diagnostics.");
423 ### I thought disabling this reduced memory usage by 1000 kB.
427 $count{'Question'} = 0;
428 $count{'Update'} = 0;
434 # reverse order of &setup().
435 &status("--- shutdown called.");
437 $ident ||= "blootbot"; # hack.
439 # opened files must be written to on shutdown/hup/whatever
440 # unless they're write-only, like uptime.
443 &News::writeNews() if (&ChanConfList("news"));
446 &closeSHM($shm); # aswell. TODO: use this in &doExit?
453 if ($$ == $bot_pid) {
454 &status("--- $sig called.");
456 ### crappy bug in Net::IRC?
457 if (!$conn->connected and time - $msgtime > 900) {
458 &status("reconnecting because of uncaught disconnect.");
464 &ircCheck(); # heh, evil!
466 &DCCBroadcast("-HUP called.","m");
468 &loadConfig($bot_misc_dir."/blootbot.config");
469 &reloadAllModules() if (&IsParam("DEBUG"));
472 &status("--- End of $sig.");
474 &status("$sig called; ignoring restart.");
478 # File: Configuration.
482 if (!open(FILE, $file)) {
483 &ERROR("FAILED loadConfig ($file): $!");
484 &status("Please copy files/sample.config to files/blootbot.config");
485 &status(" and edit files/blootbot.config, modify to tastes.");
494 my ($set,$key,$val) = split(/\s+/, $_, 3);
497 &status("loadConfig: invalid line '$_'.");
501 # perform variable interpolation
502 $val =~ s/(\$(\w+))/$param{$2}/g;
511 &status("Loaded config $file ($count items)");