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;
274 if (@c and $c[0] ne $chan) {
275 &WARN("c ne chan ($c[0] ne $chan)");
278 return $chanconf{$c[0]}{$param} || $chanconf{"_default"}{$param};
282 my ($prefix) = $_[0] || "";
284 if (!open(IN, "/proc/$$/status")) {
285 &ERROR("cannot open '/proc/$$/status'.");
289 if ($^O eq "linux") {
291 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
295 } elsif ($^O eq "netbsd") {
296 $memusage = (stat "/proc/$$/mem")[7]/1024;
298 } elsif ($^O =~ /^(free|open)bsd$/) {
299 my @info = split /\s+/, `/bin/ps -l -p $$`;
300 $memusage = $info[20];
303 $memusage = "UNKNOWN";
307 if (defined $memusageOld and &IsParam("DEBUG")) {
308 # it's always going to be increase.
309 my $delta = $memusage - $memusageOld;
313 } elsif ($delta > 500) {
314 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
315 } elsif ($delta > 0) {
316 $str = "MEM:$prefix increased by $delta kB";
317 } else { # delta < 0.
319 # never knew RSS could decrease, probably Size can't?
320 $str = "MEM:$prefix decreased by $delta kB. YES YES YES";
325 $memusageOld = $memusage;
333 &showProc(" (\&openLog before)");
334 &openLog(); # write, append.
335 &status("--- Started logging.");
338 my $dir = "$bot_base_dir/$_/";
340 &status("Making dir $_");
345 &loadLang($bot_misc_dir. "/blootbot.lang");
349 &loadMyModulesNow(); # must be after chan file.
352 &openSQLDebug() if (&IsParam("SQLDebug"));
353 &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
355 &status("Setup: ". &countKeys("factoids") ." factoids.");
356 &News::readNews() if (&ChanConfList("news"));
358 $param{tempDir} =~ s#\~/#$ENV{HOME}/#;
360 &status("Initial memory usage: $memusage kB");
364 $param{'VERBOSITY'} = 1;
365 &loadConfig($bot_misc_dir."/blootbot.config");
367 foreach ("ircNick", "ircUser", "ircName", "DBType", "tempDir") {
368 next if &IsParam($_);
369 &ERROR("Parameter $_ has not been defined.");
373 if ($param{tempDir} =~ s#\~/#$ENV{HOME}/#) {
374 &VERB("Fixing up tempDir.",2);
377 if ($param{tempDir} =~ /~/) {
378 &ERROR("parameter tempDir still contains tilde.");
382 if (! -d $param{tempDir}) {
383 &status("making $param{tempDir}...");
384 system("mkdir $param{tempDir}");
387 # static scalar variables.
388 $file{utm} = "$bot_base_dir/$param{'ircUser'}.uptime";
389 $file{PID} = "$bot_base_dir/$param{'ircUser'}.pid";
393 if (&IsParam("DEBUG")) {
394 &status("enabling debug diagnostics.");
395 ### I thought disabling this reduced memory usage by 1000 kB.
399 $count{'Question'} = 0;
400 $count{'Update'} = 0;
406 # reverse order of &setup().
407 &DEBUG("shutdown called.");
409 # opened files must be written to on shutdown/hup/whatever
410 # unless they're write-only, like uptime.
413 &News::writeNews() if (&ChanConfList("news"));
416 &closeSHM($shm); # aswell. TODO: use this in &doExit?
423 if ($$ == $bot_pid) {
424 &status("--- $sig called.");
426 ### crappy bug in Net::IRC?
427 if (!$conn->connected and time - $msgtime > 900) {
428 &status("reconnecting because of uncaught disconnect.");
434 &ircCheck(); # heh, evil!
436 &DCCBroadcast("-HUP called.","m");
438 &loadConfig($bot_misc_dir."/blootbot.config");
439 &reloadAllModules() if (&IsParam("DEBUG"));
442 &status("--- End of $sig.");
444 &status("$sig called; ignoring restart.");
448 # File: Configuration.
452 if (!open(FILE, $file)) {
453 &ERROR("FAILED loadConfig ($file): $!");
454 &status("Please copy files/sample.config to files/blootbot.config");
455 &status(" and edit files/blootbot.config, modify to tastes.");
464 my ($set,$key,$val) = split(/\s+/, $_, 3);
467 &status("loadConfig: invalid line '$_'.");
471 # perform variable interpolation
472 $val =~ s/(\$(\w+))/$param{$2}/g;
481 &status("Loaded config $file ($count items)");