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"));
88 &News::writeNews() if (&ChanConfList("news"));
91 &dumpallvars() if (&IsParam("dumpvarsAtExit"));
93 &closeSQLDebug() if (&IsParam("SQLDebug"));
97 &status("child caught SIG$sig (pid $$).");
104 $SIG{__WARN__} = sub { warn $_[0]; };
110 $SIG{__WARN__} = 'doWarn'; # ???
113 # Usage: &IsParam($param);
114 # blootbot.config specific.
118 return 0 unless (defined $param);
119 return 0 unless (exists $param{$param});
120 return 0 unless ($param{$param});
121 return 0 if $param{$param} =~ /^false$/i;
126 # Usage: &ChanConfList($param)
127 # About: gets channels with 'param' enabled. (!!!)
128 # Return: array of channels
131 return unless (defined $param);
132 my %chan = &getChanConfList($param);
134 if (exists $chan{_default}) {
135 return keys %chanconf;
142 # Usage: &getChanConfList($param)
143 # About: gets channels with 'param' enabled, internal use only.
144 # Return: hash of channels
145 sub getChanConfList {
149 return unless (defined $param);
151 foreach (keys %chanconf) {
153 # &DEBUG("chan => $chan");
154 my @array = grep /^$param$/, keys %{ $chanconf{$chan} };
156 next unless (scalar @array);
158 if (scalar @array > 1) {
159 &WARN("multiple items found?");
162 if ($array[0] eq "0") {
173 # Usage: &IsChanConf($param);
174 # About: Check for 'param' on the basis of channel config.
175 # Return: 1 for enabled, 0 for passive disable, -1 for active disable.
179 if (!defined $param) {
180 &WARN("param == NULL.");
184 ### TODO: VERBOSITY on how chanconf returned 1 or 0 or -1.
185 my %chan = &getChanConfList($param);
186 if (!defined $msgType) {
187 return $chan{_default} || 0;
190 if ($msgType eq "public") {
191 return $chan{lc $chan} || $chan{_default} || 0;
194 if ($msgType eq "private") {
195 return $chan{_default} || 0;
198 ### debug purposes only.
199 # &DEBUG("param => $param, msgType => $msgType.");
200 # foreach (keys %chan) {
201 # &DEBUG(" $_ => $chan{$_}");
208 # Usage: &getChanConf($param);
209 # About: Retrieve value for 'param' value in current/default chan.
210 # Return: scalar for success, undef for failure.
212 my($param,$chan) = @_;
214 if (!defined $param) {
215 &WARN("param == NULL.");
219 $chan ||= "_default";
220 my @c = grep /^$chan$/i, keys %chanconf;
222 if (@c and $c[0] ne $chan) {
223 &WARN("c ne chan ($c[0] ne $chan)");
226 return $chanconf{$c[0] || "_default"}{$param};
230 my ($prefix) = $_[0] || "";
232 if (!open(IN, "/proc/$$/status")) {
233 &ERROR("cannot open '/proc/$$/status'.");
237 if ($^O eq "linux") {
239 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
243 } elsif ($^O eq "netbsd") {
244 $memusage = (stat "/proc/$$/mem")[7]/1024;
246 } elsif ($^O =~ /^(free|open)bsd$/) {
247 my @info = split /\s+/, `/bin/ps -l -p $$`;
248 $memusage = $info[20];
251 $memusage = "UNKNOWN";
255 if (defined $memusageOld and &IsParam("DEBUG")) {
256 # it's always going to be increase.
257 my $delta = $memusage - $memusageOld;
261 } elsif ($delta > 500) {
262 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
263 } elsif ($delta > 0) {
264 $str = "MEM:$prefix increased by $delta kB";
265 } else { # delta < 0.
267 # never knew RSS could decrease, probably Size can't?
268 $str = "MEM:$prefix decreased by $delta kB. YES YES YES";
273 $memusageOld = $memusage;
281 &showProc(" (\&openLog before)");
282 &openLog(); # write, append.
283 &status("--- Started logging.");
286 my $dir = "$bot_base_dir/$_/";
288 &status("Making dir $_");
293 &loadLang($bot_misc_dir. "/blootbot.lang");
297 &loadMyModulesNow(); # must be after chan file.
300 &openSQLDebug() if (&IsParam("SQLDebug"));
301 &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
303 &status("Setup: ". &countKeys("factoids") ." factoids.");
304 &News::readNews() if (&ChanConfList("news"));
306 $param{tempDir} =~ s#\~/#$ENV{HOME}/#;
308 &status("Initial memory usage: $memusage kB");
312 $param{'VERBOSITY'} = 1;
313 &loadConfig($bot_misc_dir."/blootbot.config");
315 foreach ("ircNick", "ircUser", "ircName", "DBType", "tempDir") {
316 next if &IsParam($_);
317 &ERROR("Parameter $_ has not been defined.");
321 if ($param{tempDir} =~ s#\~/#$ENV{HOME}/#) {
322 &VERB("Fixing up tempDir.",2);
325 if ($param{tempDir} =~ /~/) {
326 &ERROR("parameter tempDir still contains tilde.");
330 if (! -d $param{tempDir}) {
331 &status("making $param{tempDir}...");
332 system("mkdir $param{tempDir}");
335 # static scalar variables.
336 $file{utm} = "$bot_base_dir/$param{'ircUser'}.uptime";
337 $file{PID} = "$bot_base_dir/$param{'ircUser'}.pid";
341 if (&IsParam("DEBUG")) {
342 &status("enabling debug diagnostics.");
343 ### I thought disabling this reduced memory usage by 1000 kB.
347 $count{'Question'} = 0;
348 $count{'Update'} = 0;
354 # reverse order of &setup().
355 &DEBUG("shutdown called.");
357 # opened files must be written to on shutdown/hup/whatever
358 # unless they're write-only, like uptime.
361 &News::writeNews() if (&ChanConfList("news"));
364 &closeSHM($shm); # aswell. TODO: use this in &doExit?
371 if ($$ == $bot_pid) {
372 &status("--- $sig called.");
374 ### crappy bug in Net::IRC?
375 if (!$conn->connected and time - $msgtime > 900) {
376 &status("reconnecting because of uncaught disconnect.");
382 &DCCBroadcast("-HUP called.","m");
384 &loadConfig($bot_misc_dir."/blootbot.config");
385 &reloadAllModules() if (&IsParam("DEBUG"));
388 &status("--- End of $sig.");
390 &status("$sig called; ignoring restart.");
394 # File: Configuration.
398 if (!open(FILE, $file)) {
399 &ERROR("FAILED loadConfig ($file): $!");
400 &status("Please copy files/sample.config to files/blootbot.config");
401 &status(" and edit files/blootbot.config, modify to tastes.");
410 my ($set,$key,$val) = split(/\s+/, $_, 3);
413 &status("loadConfig: invalid line '$_'.");
417 # perform variable interpolation
418 $val =~ s/(\$(\w+))/$param{$2}/g;
427 &status("Loaded config $file ($count items)");