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
27 use vars qw(@joinchan @ircServers @wingateBad @wingateNow @wingateCache
30 ### dynamic hash. MUST BE REDUCED IN SIZE!!!
32 use vars qw(%count %netsplit %netsplitservers %flood %dcc %orig
33 %nuh %talkWho %seen %floodwarn %param %dbh %ircPort
34 %topic %moduleAge %last %time %mask %file
35 %forked %chanconf %channels
39 $SIG{'HUP'} = 'restart'; # 1.
40 $SIG{'INT'} = 'doExit'; # 2.
41 $SIG{'KILL'} = 'doExit'; # 9. DOES NOT WORK. 'man perlipc' for details.
42 $SIG{'TERM'} = 'doExit'; # 15.
43 $SIG{'__WARN__'} = 'doWarn';
45 # initialize variables.
49 $userHandle = "default";
61 $msgcount = $msgsleep = 0;
64 $pubcount = $pubsleep = 0;
66 $bot_version = "blootbot cvs (20010214) -- $^O";
76 if (defined $flag_quit) {
77 &WARN("doExit: quit already called.");
82 if (!defined $bot_pid) { # independent.
84 } elsif ($bot_pid == $$) { # parent.
85 &status("parent caught SIG$sig (pid $$).") if (defined $sig);
87 &status("--- Start of quit.");
92 &quit($param{'quitMsg'}) if (&whatInterface() =~ /IRC/);
95 &uptimeWriteFile() if (&ChanConfList("uptime"));
96 &News::writeNews() if (&ChanConfList("news"));
99 &dumpallvars() if (&IsParam("dumpvarsAtExit"));
101 &closeSQLDebug() if (&IsParam("SQLDebug"));
103 &status("--- QUIT.");
105 &status("child caught SIG$sig (pid $$).");
112 $SIG{__WARN__} = sub { warn $_[0]; };
118 $SIG{__WARN__} = 'doWarn'; # ???
121 # Usage: &IsParam($param);
122 # blootbot.config specific.
126 return 0 unless (defined $param);
127 return 0 unless (exists $param{$param});
128 return 0 unless ($param{$param});
129 return 0 if $param{$param} =~ /^false$/i;
134 # Usage: &ChanConfList($param)
135 # About: gets channels with 'param' enabled. (!!!)
136 # Return: array of channels
139 return unless (defined $param);
140 my %chan = &getChanConfList($param);
142 if (exists $chan{_default}) {
143 return keys %chanconf;
150 # Usage: &getChanConfList($param)
151 # About: gets channels with 'param' enabled, internal use only.
152 # Return: hash of channels
153 sub getChanConfList {
157 return unless (defined $param);
159 foreach (keys %chanconf) {
161 # &DEBUG("chan => $chan");
162 my @array = grep /^$param$/, keys %{ $chanconf{$chan} };
164 next unless (scalar @array);
166 if (scalar @array > 1) {
167 &WARN("multiple items found?");
170 if ($array[0] eq "0") {
181 # Usage: &IsChanConf($param);
182 # About: Check for 'param' on the basis of channel config.
183 # Return: 1 for enabled, 0 for passive disable, -1 for active disable.
186 my $debug = 0; # knocked tons of bugs with this! :)
188 if (!defined $param) {
189 &WARN("IsChanConf: param == NULL.");
194 if ($chan =~ tr/A-Z/a-z/) {
195 &WARN("IsChanConf: lowercased chan. ($old)");
198 ### TODO: VERBOSITY on how chanconf returned 1 or 0 or -1.
199 my %chan = &getChanConfList($param);
201 if (!defined $msgType) {
204 $nomatch++ if ($msgType eq "");
205 $nomatch++ unless ($msgType =~ /^(public|private)$/i);
208 ### debug purposes only.
209 # &DEBUG("param => $param, msgType => $msgType.");
210 # foreach (keys %chan) {
211 # &DEBUG(" $_ => $chan{$_}");
216 &DEBUG("ICC: other: $chan{$chan} (_default/$param)") if ($debug);
217 } elsif ($chan{_default}) {
218 &DEBUG("ICC: other: $chan{_default} (_default/$param)") if ($debug);
220 &DEBUG("ICC: other: 0 ($param)") if ($debug);
223 return $chan{$chan} || $chan{_default} || 0;
226 if ($msgType eq "public") {
228 &DEBUG("ICC: public: $chan{$chan} ($chan/$param)") if ($debug);
229 } elsif ($chan{_default}) {
230 &DEBUG("ICC: public: $chan{_default} (_default/$param)") if ($debug);
232 &DEBUG("ICC: public: 0 ($param)") if ($debug);
235 return $chan{$chan} || $chan{_default} || 0;
238 if ($msgType eq "private") {
239 if ($chan{_default}) {
240 &DEBUG("ICC: private: $chan{_default} (_default/$param)") if ($debug);
241 } elsif ($chan{$chan}) {
242 &DEBUG("ICC: private: $chan{$chan} ($chan/$param) (hack)") if ($debug);
244 &DEBUG("ICC: private: 0 ($param)") if ($debug);
247 return $chan{$chan} || $chan{_default} || 0;
250 &DEBUG("ICC: no-match: 0/$param (msgType = $msgType)");
256 # Usage: &getChanConf($param);
257 # About: Retrieve value for 'param' value in current/default chan.
258 # Return: scalar for success, undef for failure.
260 my($param,$chan) = @_;
262 if (!defined $param) {
263 &WARN("param == NULL.");
267 $chan ||= "_default";
268 my @c = grep /^$chan$/i, keys %chanconf;
270 if (@c and $c[0] ne $chan) {
271 &WARN("c ne chan ($c[0] ne $chan)");
274 return $chanconf{$c[0]}{$param} || $chanconf{"_default"}{$param};
278 my ($prefix) = $_[0] || "";
280 if (!open(IN, "/proc/$$/status")) {
281 &ERROR("cannot open '/proc/$$/status'.");
285 if ($^O eq "linux") {
287 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
291 } elsif ($^O eq "netbsd") {
292 $memusage = (stat "/proc/$$/mem")[7]/1024;
294 } elsif ($^O =~ /^(free|open)bsd$/) {
295 my @info = split /\s+/, `/bin/ps -l -p $$`;
296 $memusage = $info[20];
299 $memusage = "UNKNOWN";
303 if (defined $memusageOld and &IsParam("DEBUG")) {
304 # it's always going to be increase.
305 my $delta = $memusage - $memusageOld;
309 } elsif ($delta > 500) {
310 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
311 } elsif ($delta > 0) {
312 $str = "MEM:$prefix increased by $delta kB";
313 } else { # delta < 0.
315 # never knew RSS could decrease, probably Size can't?
316 $str = "MEM:$prefix decreased by $delta kB. YES YES YES";
321 $memusageOld = $memusage;
329 &showProc(" (\&openLog before)");
330 &openLog(); # write, append.
331 &status("--- Started logging.");
334 my $dir = "$bot_base_dir/$_/";
336 &status("Making dir $_");
341 &loadLang($bot_misc_dir. "/blootbot.lang");
345 &loadMyModulesNow(); # must be after chan file.
348 &openSQLDebug() if (&IsParam("SQLDebug"));
349 &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
351 &status("Setup: ". &countKeys("factoids") ." factoids.");
352 &News::readNews() if (&ChanConfList("news"));
354 $param{tempDir} =~ s#\~/#$ENV{HOME}/#;
356 &status("Initial memory usage: $memusage kB");
360 $param{'VERBOSITY'} = 1;
361 &loadConfig($bot_misc_dir."/blootbot.config");
363 foreach ("ircNick", "ircUser", "ircName", "DBType", "tempDir") {
364 next if &IsParam($_);
365 &ERROR("Parameter $_ has not been defined.");
369 if ($param{tempDir} =~ s#\~/#$ENV{HOME}/#) {
370 &VERB("Fixing up tempDir.",2);
373 if ($param{tempDir} =~ /~/) {
374 &ERROR("parameter tempDir still contains tilde.");
378 if (! -d $param{tempDir}) {
379 &status("making $param{tempDir}...");
380 system("mkdir $param{tempDir}");
383 # static scalar variables.
384 $file{utm} = "$bot_base_dir/$param{'ircUser'}.uptime";
385 $file{PID} = "$bot_base_dir/$param{'ircUser'}.pid";
389 if (&IsParam("DEBUG")) {
390 &status("enabling debug diagnostics.");
391 ### I thought disabling this reduced memory usage by 1000 kB.
395 $count{'Question'} = 0;
396 $count{'Update'} = 0;
402 # reverse order of &setup().
403 &DEBUG("shutdown called.");
405 # opened files must be written to on shutdown/hup/whatever
406 # unless they're write-only, like uptime.
409 &News::writeNews() if (&ChanConfList("news"));
412 &closeSHM($shm); # aswell. TODO: use this in &doExit?
419 if ($$ == $bot_pid) {
420 &status("--- $sig called.");
422 ### crappy bug in Net::IRC?
423 if (!$conn->connected and time - $msgtime > 900) {
424 &status("reconnecting because of uncaught disconnect.");
430 &ircCheck(); # heh, evil!
432 &DCCBroadcast("-HUP called.","m");
434 &loadConfig($bot_misc_dir."/blootbot.config");
435 &reloadAllModules() if (&IsParam("DEBUG"));
438 &status("--- End of $sig.");
440 &status("$sig called; ignoring restart.");
444 # File: Configuration.
448 if (!open(FILE, $file)) {
449 &ERROR("FAILED loadConfig ($file): $!");
450 &status("Please copy files/sample.config to files/blootbot.config");
451 &status(" and edit files/blootbot.config, modify to tastes.");
460 my ($set,$key,$val) = split(/\s+/, $_, 3);
463 &status("loadConfig: invalid line '$_'.");
467 # perform variable interpolation
468 $val =~ s/(\$(\w+))/$param{$2}/g;
477 &status("Loaded config $file ($count items)");