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
25 use vars qw(@joinchan @ircServers @wingateBad @wingateNow @wingateCache
28 ### dynamic hash. MUST BE REDUCED IN SIZE!!!
30 use vars qw(%count %netsplit %netsplitservers %flood %dcc %orig
31 %nuh %talkWho %seen %floodwarn %param %dbh %ircPort
32 %topic %moduleAge %last %time %mask %file
33 %forked %chanconf %channels
37 $SIG{'HUP'} = 'restart'; # 1.
38 $SIG{'INT'} = 'doExit'; # 2.
39 $SIG{'KILL'} = 'doExit'; # 9. DOES NOT WORK. 'man perlipc' for details.
40 $SIG{'TERM'} = 'doExit'; # 15.
41 $SIG{'__WARN__'} = 'doWarn';
43 # initialize variables.
47 $userHandle = "default";
58 $bot_version = "blootbot cvs (20010214) -- $^O";
68 if (defined $flag_quit) {
69 &WARN("doExit: quit already called.");
74 if (!defined $bot_pid) { # independent.
76 } elsif ($bot_pid == $$) { # parent.
77 &status("parent caught SIG$sig (pid $$).") if (defined $sig);
79 &status("--- Start of quit.");
84 &quit($param{'quitMsg'}) if (&whatInterface() =~ /IRC/);
87 &uptimeWriteFile() if (&ChanConfList("uptime"));
88 &News::writeNews() if (&ChanConfList("news"));
91 &dumpallvars() if (&IsParam("dumpvarsAtExit"));
93 &closeSQLDebug() if (&IsParam("SQLDebug"));
97 &status("child caught SIG$sig (pid $$).");
104 $SIG{__WARN__} = sub { warn $_[0]; };
110 $SIG{__WARN__} = 'doWarn'; # ???
113 # Usage: &IsParam($param);
114 # blootbot.config specific.
118 return 0 unless (defined $param);
119 return 0 unless (exists $param{$param});
120 return 0 unless ($param{$param});
121 return 0 if $param{$param} =~ /^false$/i;
126 # Usage: &ChanConfList($param)
127 # About: gets channels with 'param' enabled. (!!!)
128 # Return: array of channels
131 return unless (defined $param);
132 my %chan = &getChanConfList($param);
134 if (exists $chan{_default}) {
135 return keys %chanconf;
142 # Usage: &getChanConfList($param)
143 # About: gets channels with 'param' enabled, internal use only.
144 # Return: hash of channels
145 sub getChanConfList {
149 return unless (defined $param);
151 foreach (keys %chanconf) {
153 # &DEBUG("chan => $chan");
154 my @array = grep /^$param$/, keys %{ $chanconf{$chan} };
156 next unless (scalar @array);
158 if (scalar @array > 1) {
159 &WARN("multiple items found?");
162 if ($array[0] eq "0") {
173 # Usage: &IsChanConf($param);
174 # About: Check for 'param' on the basis of channel config.
175 # Return: 1 for enabled, 0 for passive disable, -1 for active disable.
178 my $debug = 0; # knocked tons of bugs with this! :)
180 if (!defined $param) {
181 &WARN("IsChanConf: param == NULL.");
186 if ($chan =~ tr/A-Z/a-z/) {
187 &WARN("IsChanConf: lowercased chan. ($old)");
190 ### TODO: VERBOSITY on how chanconf returned 1 or 0 or -1.
191 my %chan = &getChanConfList($param);
193 if (!defined $msgType) {
196 $nomatch++ if ($msgType eq "");
197 $nomatch++ unless ($msgType =~ /^(public|private)$/i);
200 ### debug purposes only.
201 # &DEBUG("param => $param, msgType => $msgType.");
202 # foreach (keys %chan) {
203 # &DEBUG(" $_ => $chan{$_}");
208 &DEBUG("ICC: other: $chan{$chan} (_default/$param)") if ($debug);
209 } elsif ($chan{_default}) {
210 &DEBUG("ICC: other: $chan{_default} (_default/$param)") if ($debug);
212 &DEBUG("ICC: other: 0 ($param)") if ($debug);
215 return $chan{$chan} || $chan{_default} || 0;
218 if ($msgType eq "public") {
220 &DEBUG("ICC: public: $chan{$chan} ($chan/$param)") if ($debug);
221 } elsif ($chan{_default}) {
222 &DEBUG("ICC: public: $chan{_default} (_default/$param)") if ($debug);
224 &DEBUG("ICC: public: 0 ($param)") if ($debug);
227 return $chan{$chan} || $chan{_default} || 0;
230 if ($msgType eq "private") {
231 if ($chan{_default}) {
232 &DEBUG("ICC: private: $chan{_default} (_default/$param)") if ($debug);
233 } elsif ($chan{$chan}) {
234 &DEBUG("ICC: private: $chan{$chan} ($chan/$param) (hack)") if ($debug);
236 &DEBUG("ICC: private: 0 ($param)") if ($debug);
239 return $chan{$chan} || $chan{_default} || 0;
242 &DEBUG("ICC: no-match: 0/$param (msgType = $msgType)");
248 # Usage: &getChanConf($param);
249 # About: Retrieve value for 'param' value in current/default chan.
250 # Return: scalar for success, undef for failure.
252 my($param,$chan) = @_;
254 if (!defined $param) {
255 &WARN("param == NULL.");
259 $chan ||= "_default";
260 my @c = grep /^$chan$/i, keys %chanconf;
262 if (@c and $c[0] ne $chan) {
263 &WARN("c ne chan ($c[0] ne $chan)");
266 return $chanconf{$c[0]}{$param} || $chanconf{"_default"}{$param};
270 my ($prefix) = $_[0] || "";
272 if (!open(IN, "/proc/$$/status")) {
273 &ERROR("cannot open '/proc/$$/status'.");
277 if ($^O eq "linux") {
279 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
283 } elsif ($^O eq "netbsd") {
284 $memusage = (stat "/proc/$$/mem")[7]/1024;
286 } elsif ($^O =~ /^(free|open)bsd$/) {
287 my @info = split /\s+/, `/bin/ps -l -p $$`;
288 $memusage = $info[20];
291 $memusage = "UNKNOWN";
295 if (defined $memusageOld and &IsParam("DEBUG")) {
296 # it's always going to be increase.
297 my $delta = $memusage - $memusageOld;
301 } elsif ($delta > 500) {
302 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
303 } elsif ($delta > 0) {
304 $str = "MEM:$prefix increased by $delta kB";
305 } else { # delta < 0.
307 # never knew RSS could decrease, probably Size can't?
308 $str = "MEM:$prefix decreased by $delta kB. YES YES YES";
313 $memusageOld = $memusage;
321 &showProc(" (\&openLog before)");
322 &openLog(); # write, append.
323 &status("--- Started logging.");
326 my $dir = "$bot_base_dir/$_/";
328 &status("Making dir $_");
333 &loadLang($bot_misc_dir. "/blootbot.lang");
337 &loadMyModulesNow(); # must be after chan file.
340 &openSQLDebug() if (&IsParam("SQLDebug"));
341 &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
343 &status("Setup: ". &countKeys("factoids") ." factoids.");
344 &News::readNews() if (&ChanConfList("news"));
346 $param{tempDir} =~ s#\~/#$ENV{HOME}/#;
348 &status("Initial memory usage: $memusage kB");
352 $param{'VERBOSITY'} = 1;
353 &loadConfig($bot_misc_dir."/blootbot.config");
355 foreach ("ircNick", "ircUser", "ircName", "DBType", "tempDir") {
356 next if &IsParam($_);
357 &ERROR("Parameter $_ has not been defined.");
361 if ($param{tempDir} =~ s#\~/#$ENV{HOME}/#) {
362 &VERB("Fixing up tempDir.",2);
365 if ($param{tempDir} =~ /~/) {
366 &ERROR("parameter tempDir still contains tilde.");
370 if (! -d $param{tempDir}) {
371 &status("making $param{tempDir}...");
372 system("mkdir $param{tempDir}");
375 # static scalar variables.
376 $file{utm} = "$bot_base_dir/$param{'ircUser'}.uptime";
377 $file{PID} = "$bot_base_dir/$param{'ircUser'}.pid";
381 if (&IsParam("DEBUG")) {
382 &status("enabling debug diagnostics.");
383 ### I thought disabling this reduced memory usage by 1000 kB.
387 $count{'Question'} = 0;
388 $count{'Update'} = 0;
394 # reverse order of &setup().
395 &DEBUG("shutdown called.");
397 # opened files must be written to on shutdown/hup/whatever
398 # unless they're write-only, like uptime.
401 &News::writeNews() if (&ChanConfList("news"));
404 &closeSHM($shm); # aswell. TODO: use this in &doExit?
411 if ($$ == $bot_pid) {
412 &status("--- $sig called.");
414 ### crappy bug in Net::IRC?
415 if (!$conn->connected and time - $msgtime > 900) {
416 &status("reconnecting because of uncaught disconnect.");
422 &DCCBroadcast("-HUP called.","m");
424 &loadConfig($bot_misc_dir."/blootbot.config");
425 &reloadAllModules() if (&IsParam("DEBUG"));
428 &status("--- End of $sig.");
430 &status("$sig called; ignoring restart.");
434 # File: Configuration.
438 if (!open(FILE, $file)) {
439 &ERROR("FAILED loadConfig ($file): $!");
440 &status("Please copy files/sample.config to files/blootbot.config");
441 &status(" and edit files/blootbot.config, modify to tastes.");
450 my ($set,$key,$val) = split(/\s+/, $_, 3);
453 &status("loadConfig: invalid line '$_'.");
457 # perform variable interpolation
458 $val =~ s/(\$(\w+))/$param{$2}/g;
467 &status("Loaded config $file ($count items)");