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 # what if we have it set on _default and a few channels with
134 # negative set? perhaps we should disable -blah settings to remove
137 if (exists $chan{_default}) {
138 return keys %chanconf;
140 ### TODO: -option is included aswell though.
146 # Usage: &getChanConfList($param)
147 # About: gets channels with 'param' enabled, internal use only.
148 # Return: hash of channels
149 sub getChanConfList {
153 return unless (defined $param);
155 foreach (keys %chanconf) {
157 # &DEBUG("chan => $chan");
158 my @array = grep /^$param$/, keys %{ $chanconf{$chan} };
160 next unless (scalar @array);
162 if (scalar @array > 1) {
163 &WARN("multiple items found?");
166 if ($array[0] eq "0") {
177 # Usage: &IsChanConf($param);
178 # About: Check for 'param' on the basis of channel config.
179 # Return: 1 for enabled, 0 for passive disable, -1 for active disable.
183 if (!defined $param) {
184 &WARN("param == NULL.");
188 ### TODO: VERBOSITY on how chanconf returned 1 or 0 or -1.
189 my %chan = &getChanConfList($param);
190 if (!defined $msgType) {
191 return $chan{_default} || 0;
194 if ($msgType eq "public") {
195 return $chan{lc $chan} || $chan{_default} || 0;
198 if ($msgType eq "private") {
199 return $chan{_default} || 0;
202 ### debug purposes only.
203 # &DEBUG("param => $param, msgType => $msgType.");
204 # foreach (keys %chan) {
205 # &DEBUG(" $_ => $chan{$_}");
212 # Usage: &getChanConf($param);
213 # About: Retrieve value for 'param' value in current/default chan.
214 # Return: scalar for success, undef for failure.
216 my($param,$chan) = @_;
218 if (!defined $param) {
219 &WARN("param == NULL.");
223 $chan ||= "_default";
224 my @c = grep /^$chan$/i, keys %chanconf;
226 if (@c and $c[0] ne $chan) {
227 &WARN("c ne chan ($c[0] ne $chan)");
230 return $chanconf{$c[0] || "_default"}{$param};
234 my ($prefix) = $_[0] || "";
236 if (!open(IN, "/proc/$$/status")) {
237 &ERROR("cannot open '/proc/$$/status'.");
241 if ($^O eq "linux") {
243 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
247 } elsif ($^O eq "netbsd") {
248 $memusage = (stat "/proc/$$/mem")[7]/1024;
250 } elsif ($^O =~ /^(free|open)bsd$/) {
251 my @info = split /\s+/, `/bin/ps -l -p $$`;
252 $memusage = $info[20];
255 $memusage = "UNKNOWN";
259 if (defined $memusageOld and &IsParam("DEBUG")) {
260 # it's always going to be increase.
261 my $delta = $memusage - $memusageOld;
265 } elsif ($delta > 500) {
266 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
267 } elsif ($delta > 0) {
268 $str = "MEM:$prefix increased by $delta kB";
269 } else { # delta < 0.
271 # never knew RSS could decrease, probably Size can't?
272 $str = "MEM:$prefix decreased by $delta kB. YES YES YES";
277 $memusageOld = $memusage;
285 &showProc(" (\&openLog before)");
286 &openLog(); # write, append.
287 &status("--- Started logging.");
290 my $dir = "$bot_base_dir/$_/";
292 &status("Making dir $_");
297 &loadLang($bot_misc_dir. "/blootbot.lang");
301 &loadMyModulesNow(); # must be after chan file.
304 &openSQLDebug() if (&IsParam("SQLDebug"));
305 &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
307 &status("Setup: ". &countKeys("factoids") ." factoids.");
309 $param{tempDir} =~ s#\~/#$ENV{HOME}/#;
311 &status("Initial memory usage: $memusage kB");
315 $param{'VERBOSITY'} = 1;
316 &loadConfig($bot_misc_dir."/blootbot.config");
318 foreach ("ircNick", "ircUser", "ircName", "DBType", "tempDir") {
319 next if &IsParam($_);
320 &ERROR("Parameter $_ has not been defined.");
324 if ($param{tempDir} =~ s#\~/#$ENV{HOME}/#) {
325 &VERB("Fixing up tempDir.",2);
328 if ($param{tempDir} =~ /~/) {
329 &ERROR("parameter tempDir still contains tilde.");
333 if (! -d $param{tempDir}) {
334 &status("making $param{tempDir}...");
335 system("mkdir $param{tempDir}");
338 # static scalar variables.
339 $file{utm} = "$bot_base_dir/$param{'ircUser'}.uptime";
340 $file{PID} = "$bot_base_dir/$param{'ircUser'}.pid";
344 if (&IsParam("DEBUG")) {
345 &status("enabling debug diagnostics.");
346 ### I thought disabling this reduced memory usage by 1000 kB.
350 $count{'Question'} = 0;
351 $count{'Update'} = 0;
357 # reverse order of &setup().
358 &DEBUG("shutdown called.");
361 &closeSHM($shm); # aswell. TODO: use this in &doExit?
368 if ($$ == $bot_pid) {
369 &status("--- $sig called.");
371 ### crappy bug in Net::IRC?
372 if (!$conn->connected and time - $msgtime > 900) {
373 &status("reconnecting because of uncaught disconnect.");
379 &DCCBroadcast("-HUP called.","m");
381 &loadConfig($bot_misc_dir."/blootbot.config");
382 &reloadAllModules() if (&IsParam("DEBUG"));
385 &status("--- End of $sig.");
387 &status("$sig called; ignoring restart.");
391 # File: Configuration.
395 if (!open(FILE, $file)) {
396 &ERROR("FAILED loadConfig ($file): $!");
397 &status("Please copy files/sample.config to files/blootbot.config");
398 &status(" and edit files/blootbot.config, modify to tastes.");
407 my ($set,$key,$val) = split(/\s+/, $_, 3);
410 &status("loadConfig: invalid line '$_'.");
414 # perform variable interpolation
415 $val =~ s/(\$(\w+))/$param{$2}/g;
424 &status("Loaded config $file ($count items)");