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.");
205 if ($chan =~ tr/A-Z/a-z/) {
206 &WARN("IsChanConf: lowercased chan. ($old)");
209 ### TODO: VERBOSITY on how chanconf returned 1 or 0 or -1.
210 my %chan = &getChanConfList($param);
212 if (!defined $msgType) {
215 $nomatch++ if ($msgType eq "");
216 $nomatch++ unless ($msgType =~ /^(public|private)$/i);
219 ### debug purposes only.
220 # &DEBUG("param => $param, msgType => $msgType.");
221 # foreach (keys %chan) {
222 # &DEBUG(" $_ => $chan{$_}");
227 &DEBUG("ICC: other: $chan{$chan} (_default/$param)") if ($debug);
228 } elsif ($chan{_default}) {
229 &DEBUG("ICC: other: $chan{_default} (_default/$param)") if ($debug);
231 &DEBUG("ICC: other: 0 ($param)") if ($debug);
234 return $chan{$chan} || $chan{_default} || 0;
237 if ($msgType eq "public") {
239 &DEBUG("ICC: public: $chan{$chan} ($chan/$param)") if ($debug);
240 } elsif ($chan{_default}) {
241 &DEBUG("ICC: public: $chan{_default} (_default/$param)") if ($debug);
243 &DEBUG("ICC: public: 0 ($param)") if ($debug);
246 return $chan{$chan} || $chan{_default} || 0;
249 if ($msgType eq "private") {
250 if ($chan{_default}) {
251 &DEBUG("ICC: private: $chan{_default} (_default/$param)") if ($debug);
252 } elsif ($chan{$chan}) {
253 &DEBUG("ICC: private: $chan{$chan} ($chan/$param) (hack)") if ($debug);
255 &DEBUG("ICC: private: 0 ($param)") if ($debug);
258 return $chan{$chan} || $chan{_default} || 0;
261 &DEBUG("ICC: no-match: 0/$param (msgType = $msgType)");
267 # Usage: &getChanConf($param);
268 # About: Retrieve value for 'param' value in current/default chan.
269 # Return: scalar for success, undef for failure.
271 my($param,$chan) = @_;
273 if (!defined $param) {
274 &WARN("param == NULL.");
278 $chan ||= "_default";
279 my @c = grep /^$chan$/i, keys %chanconf;
282 if ($c[0] ne $chan) {
283 &WARN("c ne chan ($c[0] ne $chan)");
285 return $chanconf{$c[0]}{$param};
288 return $chanconf{"_default"}{$param};
292 my ($prefix) = $_[0] || "";
294 if (!open(IN, "/proc/$$/status")) {
295 &ERROR("cannot open '/proc/$$/status'.");
299 if ($^O eq "linux") {
301 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
305 } elsif ($^O eq "netbsd") {
306 $memusage = (stat "/proc/$$/mem")[7]/1024;
308 } elsif ($^O =~ /^(free|open)bsd$/) {
309 my @info = split /\s+/, `/bin/ps -l -p $$`;
310 $memusage = $info[20];
313 $memusage = "UNKNOWN";
317 if (defined $memusageOld and &IsParam("DEBUG")) {
318 # it's always going to be increase.
319 my $delta = $memusage - $memusageOld;
323 } elsif ($delta > 500) {
324 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
325 } elsif ($delta > 0) {
326 $str = "MEM:$prefix increased by $delta kB";
327 } else { # delta < 0.
329 # never knew RSS could decrease, probably Size can't?
330 $str = "MEM:$prefix decreased by $delta kB. YES YES YES";
335 $memusageOld = $memusage;
343 &showProc(" (\&openLog before)");
344 &openLog(); # write, append.
345 &status("--- Started logging.");
348 my $dir = "$bot_base_dir/$_/";
350 &status("Making dir $_");
355 &loadLang($bot_misc_dir. "/blootbot.lang");
359 &loadMyModulesNow(); # must be after chan file.
362 &openSQLDebug() if (&IsParam("SQLDebug"));
363 &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
366 &status("Setup: ". &countKeys("factoids") ." factoids.");
367 &News::readNews() if (&ChanConfList("news"));
368 &getChanConfDefault("sendPrivateLimitLines", 3);
369 &getChanConfDefault("sendPrivateLimitBytes", 1000);
370 &getChanConfDefault("sendPublicLimitLines", 3);
371 &getChanConfDefault("sendPublicLimitBytes", 1000);
372 &getChanConfDefault("sendNoticeLimitLines", 3);
373 &getChanConfDefault("sendNoticeLimitBytes", 1000);
375 $param{tempDir} =~ s#\~/#$ENV{HOME}/#;
377 &status("Initial memory usage: $memusage kB");
378 &status("-------------------------------------------------------");
382 $param{'VERBOSITY'} = 1;
383 &loadConfig($bot_misc_dir."/blootbot.config");
385 foreach ("ircNick", "ircUser", "ircName", "DBType", "tempDir") {
386 next if &IsParam($_);
387 &ERROR("Parameter $_ has not been defined.");
391 if ($param{tempDir} =~ s#\~/#$ENV{HOME}/#) {
392 &VERB("Fixing up tempDir.",2);
395 if ($param{tempDir} =~ /~/) {
396 &ERROR("parameter tempDir still contains tilde.");
400 if (! -d $param{tempDir}) {
401 &status("making $param{tempDir}...");
402 system("mkdir $param{tempDir}");
405 # static scalar variables.
406 $file{utm} = "$bot_base_dir/$param{'ircUser'}.uptime";
407 $file{PID} = "$bot_base_dir/$param{'ircUser'}.pid";
411 if (&IsParam("DEBUG")) {
412 &status("enabling debug diagnostics.");
413 ### I thought disabling this reduced memory usage by 1000 kB.
417 $count{'Question'} = 0;
418 $count{'Update'} = 0;
424 # reverse order of &setup().
425 &status("--- shutdown called.");
427 $ident ||= "blootbot"; # hack.
429 # opened files must be written to on shutdown/hup/whatever
430 # unless they're write-only, like uptime.
433 &News::writeNews() if (&ChanConfList("news"));
436 &closeSHM($shm); # aswell. TODO: use this in &doExit?
443 if ($$ == $bot_pid) {
444 &status("--- $sig called.");
446 ### crappy bug in Net::IRC?
447 if (!$conn->connected and time - $msgtime > 900) {
448 &status("reconnecting because of uncaught disconnect.");
454 &ircCheck(); # heh, evil!
456 &DCCBroadcast("-HUP called.","m");
458 &loadConfig($bot_misc_dir."/blootbot.config");
459 &reloadAllModules() if (&IsParam("DEBUG"));
462 &status("--- End of $sig.");
464 &status("$sig called; ignoring restart.");
468 # File: Configuration.
472 if (!open(FILE, $file)) {
473 &ERROR("FAILED loadConfig ($file): $!");
474 &status("Please copy files/sample.config to files/blootbot.config");
475 &status(" and edit files/blootbot.config, modify to tastes.");
484 my ($set,$key,$val) = split(/\s+/, $_, 3);
487 &status("loadConfig: invalid line '$_'.");
491 # perform variable interpolation
492 $val =~ s/(\$(\w+))/$param{$2}/g;
501 &status("Loaded config $file ($count items)");