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 %jointime %topic %joinverb %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";
59 $bot_version = "blootbot cvs (20001212) -- $^O";
69 if (defined $flag_quit) {
70 &WARN("doExit: quit already called.");
75 if (!defined $bot_pid) { # independent.
77 } elsif ($bot_pid == $$) { # parent.
78 &status("parent caught SIG$sig (pid $$).") if (defined $sig);
80 &status("--- Start of quit.");
85 &quit($param{'quitMsg'}) if (&whatInterface() =~ /IRC/);
88 &uptimeWriteFile() if (&IsParam("uptime"));
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 ### TODO: -option is included aswell though.
139 # Usage: &getChanConfList($param)
140 # About: gets channels with 'param' enabled, internal use only.
141 # Return: hash of channels
142 sub getChanConfList {
146 return unless (defined $param);
148 foreach (keys %chanconf) {
150 # &DEBUG("chan => $chan");
151 my @array = grep /^$param$/, keys %{ $chanconf{$chan} };
153 next unless (scalar @array);
155 if (scalar @array > 1) {
156 &WARN("multiple items found?");
159 if ($array[0] eq "0") {
170 # Usage: &IsChanConf($param);
171 # About: Check for 'param' on the basis of channel config.
172 # Return: 1 for enabled, 0 for passive disable, -1 for active disable.
176 if (!defined $param) {
177 &WARN("param == NULL.");
181 ### TODO: VERBOSITY on how chanconf returned 1 or 0 or -1.
182 my %chan = &getChanConfList($param);
183 if (!defined $msgType) {
184 return $chan{_default} || 0;
187 if ($msgType eq "public") {
188 return $chan{lc $chan} || $chan{_default} || 0;
191 if ($msgType eq "private") {
192 return $chan{_default} || 0;
195 &DEBUG("param => $param, msgType => $msgType.");
196 foreach (keys %chan) {
197 &DEBUG(" $_ => $chan{$_}");
204 # Usage: &getChanConf($param);
205 # About: Retrieve value for 'param' value in current/default chan.
206 # Return: scalar for success, undef for failure.
208 my($param,$chan) = @_;
210 if (!defined $param) {
211 &WARN("param == NULL.");
215 $chan ||= "_default";
216 my @c = grep /^$chan$/i, keys %chanconf;
218 if ($c[0] ne $chan) {
219 &WARN("c ne chan ($c[0] ne $chan)");
222 return $chanconf{$c[0] || "_default"}{$param};
226 my ($prefix) = $_[0] || "";
228 if (!open(IN, "/proc/$$/status")) {
229 &ERROR("cannot open '/proc/$$/status'.");
233 if ($^O eq "linux") {
235 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
239 if (defined $memusageOld and &IsParam("DEBUG")) {
240 # it's always going to be increase.
241 my $delta = $memusage - $memusageOld;
245 } elsif ($delta > 500) {
246 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
247 } elsif ($delta > 0) {
248 $str = "MEM:$prefix increased by $delta kB";
249 } else { # delta < 0.
251 # never knew RSS could decrease, probably Size can't?
252 $str = "MEM:$prefix decreased by $delta kB. YES YES YES";
256 &DCCBroadcast($str) if (&whatInterface() =~ /IRC/ &&
257 grep(/Irc.pl/, keys %moduleAge));
259 $memusageOld = $memusage;
261 $memusage = "UNKNOWN";
263 ### TODO: FreeBSD/*BSD support.
271 &showProc(" (\&openLog before)");
272 &openLog(); # write, append.
273 &status("--- Started logging.");
276 my $dir = "$bot_base_dir/$_/";
278 &status("Making dir $_");
283 &loadLang($bot_misc_dir. "/blootbot.lang");
284 &loadIRCServers($bot_misc_dir. "/ircII.servers");
287 &loadMyModulesNow(); # must be after chan file.
290 &openSQLDebug() if (&IsParam("SQLDebug"));
291 &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
293 &status("Setup: ". &countKeys("factoids") ." factoids.");
295 $param{tempDir} =~ s#\~/#$ENV{HOME}/#;
297 &status("Initial memory usage: $memusage kB");
301 $param{'VERBOSITY'} = 1;
302 &loadConfig($bot_misc_dir."/blootbot.config");
304 foreach ("ircNick", "ircUser", "ircName", "DBType", "tempDir") {
305 next if &IsParam($_);
306 &ERROR("Parameter $_ has not been defined.");
310 if ($param{tempDir} =~ s#\~/#$ENV{HOME}/#) {
311 &VERB("Fixing up tempDir.",2);
314 if ($param{tempDir} =~ /~/) {
315 &ERROR("parameter tempDir still contains tilde.");
319 if (! -d $param{tempDir}) {
320 &status("making $param{tempDir}...");
321 system("mkdir $param{tempDir}");
324 # static scalar variables.
325 $file{utm} = "$bot_base_dir/$param{'ircUser'}.uptime";
326 $file{PID} = "$bot_base_dir/$param{'ircUser'}.pid";
330 if (&IsParam("DEBUG")) {
331 &status("enabling debug diagnostics.");
332 ### I thought disabling this reduced memory usage by 1000 kB.
336 $count{'Question'} = 0;
337 $count{'Update'} = 0;
342 # reverse order of &setup().
343 &DEBUG("shutdown called.");
346 &closeSHM($shm); # aswell. TODO: use this in &doExit?
353 if ($$ == $bot_pid) {
354 &status("--- $sig called.");
356 ### crappy bug in Net::IRC?
357 if (!$conn->connected and time - $msgtime > 900) {
358 &status("reconnecting because of uncaught disconnect.");
365 &loadConfig($bot_misc_dir."/blootbot.config");
366 &reloadAllModules() if (&IsParam("DEBUG"));
369 &status("--- End of $sig.");
371 &status("$sig called; ignoring restart.");
375 # File: Configuration.
379 if (!open(FILE, $file)) {
380 &ERROR("FAILED loadConfig ($file): $!");
381 &status("Please copy files/sample.config to files/blootbot.config");
382 &status(" and edit files/blootbot.config, modify to tastes.");
391 my ($set,$key,$val) = split(/\s+/, $_, 3);
394 &status("loadConfig: invalid line '$_'.");
398 # perform variable interpolation
399 $val =~ s/(\$(\w+))/$param{$2}/g;
408 &status("Loaded config $file ($count items)");