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"));
90 &dumpallvars() if (&IsParam("dumpvarsAtExit"));
92 &closeSQLDebug() if (&IsParam("SQLDebug"));
96 &status("child caught SIG$sig (pid $$).");
103 $SIG{__WARN__} = sub { warn $_[0]; };
109 $SIG{__WARN__} = 'doWarn'; # ???
112 # Usage: &IsParam($param);
113 # blootbot.config specific.
117 return 0 unless (defined $param);
118 return 0 unless (exists $param{$param});
119 return 0 unless ($param{$param});
120 return 0 if $param{$param} =~ /^false$/i;
125 # Usage: &ChanConfList($param)
126 # About: gets channels with 'param' enabled. (!!!)
127 # Return: array of channels
130 return unless (defined $param);
131 my %chan = &getChanConfList($param);
133 if (exists $chan{_default}) {
134 return keys %chanconf;
141 # Usage: &getChanConfList($param)
142 # About: gets channels with 'param' enabled, internal use only.
143 # Return: hash of channels
144 sub getChanConfList {
148 return unless (defined $param);
150 foreach (keys %chanconf) {
152 # &DEBUG("chan => $chan");
153 my @array = grep /^$param$/, keys %{ $chanconf{$chan} };
155 next unless (scalar @array);
157 if (scalar @array > 1) {
158 &WARN("multiple items found?");
161 if ($array[0] eq "0") {
172 # Usage: &IsChanConf($param);
173 # About: Check for 'param' on the basis of channel config.
174 # Return: 1 for enabled, 0 for passive disable, -1 for active disable.
178 if (!defined $param) {
179 &WARN("param == NULL.");
183 ### TODO: VERBOSITY on how chanconf returned 1 or 0 or -1.
184 my %chan = &getChanConfList($param);
185 if (!defined $msgType) {
186 return $chan{_default} || 0;
189 if ($msgType eq "public") {
190 return $chan{lc $chan} || $chan{_default} || 0;
193 if ($msgType eq "private") {
194 return $chan{_default} || 0;
197 ### debug purposes only.
198 # &DEBUG("param => $param, msgType => $msgType.");
199 # foreach (keys %chan) {
200 # &DEBUG(" $_ => $chan{$_}");
207 # Usage: &getChanConf($param);
208 # About: Retrieve value for 'param' value in current/default chan.
209 # Return: scalar for success, undef for failure.
211 my($param,$chan) = @_;
213 if (!defined $param) {
214 &WARN("param == NULL.");
218 $chan ||= "_default";
219 my @c = grep /^$chan$/i, keys %chanconf;
221 if (@c and $c[0] ne $chan) {
222 &WARN("c ne chan ($c[0] ne $chan)");
225 return $chanconf{$c[0] || "_default"}{$param};
229 my ($prefix) = $_[0] || "";
231 if (!open(IN, "/proc/$$/status")) {
232 &ERROR("cannot open '/proc/$$/status'.");
236 if ($^O eq "linux") {
238 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
242 } elsif ($^O eq "netbsd") {
243 $memusage = (stat "/proc/$$/mem")[7]/1024;
245 } elsif ($^O =~ /^(free|open)bsd$/) {
246 my @info = split /\s+/, `/bin/ps -l -p $$`;
247 $memusage = $info[20];
250 $memusage = "UNKNOWN";
254 if (defined $memusageOld and &IsParam("DEBUG")) {
255 # it's always going to be increase.
256 my $delta = $memusage - $memusageOld;
260 } elsif ($delta > 500) {
261 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
262 } elsif ($delta > 0) {
263 $str = "MEM:$prefix increased by $delta kB";
264 } else { # delta < 0.
266 # never knew RSS could decrease, probably Size can't?
267 $str = "MEM:$prefix decreased by $delta kB. YES YES YES";
272 $memusageOld = $memusage;
280 &showProc(" (\&openLog before)");
281 &openLog(); # write, append.
282 &status("--- Started logging.");
285 my $dir = "$bot_base_dir/$_/";
287 &status("Making dir $_");
292 &loadLang($bot_misc_dir. "/blootbot.lang");
296 &loadMyModulesNow(); # must be after chan file.
299 &openSQLDebug() if (&IsParam("SQLDebug"));
300 &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
302 &status("Setup: ". &countKeys("factoids") ." factoids.");
304 $param{tempDir} =~ s#\~/#$ENV{HOME}/#;
306 &status("Initial memory usage: $memusage kB");
310 $param{'VERBOSITY'} = 1;
311 &loadConfig($bot_misc_dir."/blootbot.config");
313 foreach ("ircNick", "ircUser", "ircName", "DBType", "tempDir") {
314 next if &IsParam($_);
315 &ERROR("Parameter $_ has not been defined.");
319 if ($param{tempDir} =~ s#\~/#$ENV{HOME}/#) {
320 &VERB("Fixing up tempDir.",2);
323 if ($param{tempDir} =~ /~/) {
324 &ERROR("parameter tempDir still contains tilde.");
328 if (! -d $param{tempDir}) {
329 &status("making $param{tempDir}...");
330 system("mkdir $param{tempDir}");
333 # static scalar variables.
334 $file{utm} = "$bot_base_dir/$param{'ircUser'}.uptime";
335 $file{PID} = "$bot_base_dir/$param{'ircUser'}.pid";
339 if (&IsParam("DEBUG")) {
340 &status("enabling debug diagnostics.");
341 ### I thought disabling this reduced memory usage by 1000 kB.
345 $count{'Question'} = 0;
346 $count{'Update'} = 0;
352 # reverse order of &setup().
353 &DEBUG("shutdown called.");
356 &closeSHM($shm); # aswell. TODO: use this in &doExit?
363 if ($$ == $bot_pid) {
364 &status("--- $sig called.");
366 ### crappy bug in Net::IRC?
367 if (!$conn->connected and time - $msgtime > 900) {
368 &status("reconnecting because of uncaught disconnect.");
374 &DCCBroadcast("-HUP called.","m");
376 &loadConfig($bot_misc_dir."/blootbot.config");
377 &reloadAllModules() if (&IsParam("DEBUG"));
380 &status("--- End of $sig.");
382 &status("$sig called; ignoring restart.");
386 # File: Configuration.
390 if (!open(FILE, $file)) {
391 &ERROR("FAILED loadConfig ($file): $!");
392 &status("Please copy files/sample.config to files/blootbot.config");
393 &status(" and edit files/blootbot.config, modify to tastes.");
402 my ($set,$key,$val) = split(/\s+/, $_, 3);
405 &status("loadConfig: invalid line '$_'.");
409 # perform variable interpolation
410 $val =~ s/(\$(\w+))/$param{$2}/g;
419 &status("Loaded config $file ($count items)");