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
22 use vars qw(@joinchan @ircServers @wingateBad @wingateNow @wingateCache
25 # dynamic hash. MUST BE REDUCED IN SIZE!!!
26 use vars qw(%count %netsplit %netsplitservers %flood %dcc %orig
27 %nuh %talkWho %seen %floodwarn %param %dbh %ircPort %userList
28 %jointime %topic %joinverb %moduleAge %last %time %mask %file
33 $SIG{'HUP'} = 'restart'; # 1.
34 $SIG{'INT'} = 'doExit'; # 2.
35 $SIG{'KILL'} = 'doExit'; # 9. DOES NOT WORK. 'man perlipc' for details.
36 $SIG{'TERM'} = 'doExit'; # 15.
37 $SIG{'__WARN__'} = 'doWarn';
39 # initialize variables.
43 $userHandle = "default";
49 $bot_version = "blootbot cvs (20001212) -- $^O";
59 if (!defined $bot_pid) { # independent.
61 } elsif ($bot_pid == $$) { # parent.
62 &status("parent caught SIG$sig (pid $$).") if (defined $sig);
64 &status("--- Start of quit.");
70 &quit($param{'quitMsg'}) if (&whatInterface() =~ /IRC/);
74 &dumpallvars() if (&IsParam("dumpvarsAtExit"));
76 &closeSQLDebug() if (&IsParam("SQLDebug"));
79 &status("child caught SIG$sig (pid $$).");
86 $SIG{__WARN__} = sub { warn $_[0]; };
92 $SIG{__WARN__} = 'doWarn'; # ???
95 # Usage: &IsParam($param);
99 return 0 unless (defined $param);
100 return 0 unless (exists $param{$param});
101 return 0 unless ($param{$param});
102 return 0 if $param{$param} =~ /^false$/i;
107 my ($prefix) = $_[0] || "";
109 if (!open(IN, "/proc/$$/status")) {
110 &ERROR("cannot open '/proc/$$/status'.");
114 if ($^O eq "linux") {
116 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
120 if (defined $memusageOld and &IsParam("DEBUG")) {
121 # it's always going to be increase.
122 my $delta = $memusage - $memusageOld;
126 } elsif ($delta > 500) {
127 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
128 } elsif ($delta > 0) {
129 $str = "MEM:$prefix increased by $delta kB";
130 } else { # delta < 0.
132 # never knew RSS could decrease, probably Size can't?
133 $str = "MEM:$prefix decreased by $delta kB. YES YES YES";
137 &DCCBroadcast($str) if (&whatInterface() =~ /IRC/ &&
138 grep(/Irc.pl/, keys %moduleAge));
140 $memusageOld = $memusage;
142 $memusage = "UNKNOWN";
144 ### TODO: FreeBSD/*BSD support.
152 &showProc(" (\&openLog before)");
153 &openLog(); # write, append.
154 &status("--- Started logging.");
157 my $dir = "$bot_base_dir/$_/";
159 &status("Making dir $_");
164 &loadIgnore($bot_misc_dir. "/blootbot.ignore");
165 &loadLang($bot_misc_dir. "/blootbot.lang");
166 &loadIRCServers($bot_misc_dir. "/ircII.servers");
167 &loadUsers($bot_misc_dir. "/blootbot.users");
168 if (&IsParam("WIP")) {
169 require "src/UserFile.pl";
170 &NEWloadUsers($bot_misc_dir."/blootbot.users_NEW");
177 &openSQLDebug() if (&IsParam("SQLDebug"));
178 &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
180 &status("Setup: ". &countKeys("factoids") ." factoids.");
182 $param{tempDir} =~ s#\~/#$ENV{HOME}/#;
184 &status("Initial memory usage: $memusage kB");
188 $param{'VERBOSITY'} = 1;
189 &loadConfig($bot_misc_dir."/blootbot.config");
190 if (&IsParam("WIP")) {
191 require "src/Config.pl";
195 foreach ("ircNick", "ircUser", "ircName", "DBType", "tempDir") {
196 next if &IsParam($_);
197 &ERROR("Parameter $_ has not been defined.");
201 if ($param{tempDir} =~ s#\~/#$ENV{HOME}/#) {
202 &status("Fixing up tempDir.");
205 if ($param{tempDir} =~ /~/) {
206 &ERROR("parameter tempDir still contains tilde.");
210 if (! -d $param{tempDir}) {
211 &status("making $param{tempDir}...");
212 system("mkdir $param{tempDir}");
215 # static scalar variables.
216 $file{utm} = "$bot_base_dir/$param{'ircUser'}.uptime";
217 $file{PID} = "$bot_base_dir/$param{'ircUser'}.pid";
221 if (&IsParam("DEBUG")) {
222 &status("enabling debug diagnostics.");
223 ### I thought disabling this reduced memory usage by 1000 kB.
227 $count{'Question'} = 0;
228 $count{'Update'} = 0;
235 # reverse order of &setup().
236 &DEBUG("shutdown called.");
239 &closeSHM($shm); # aswell. TODO: use this in &doExit?
246 &DEBUG(" forked => ".scalar(keys %forked) );
247 &DEBUG(" fork => ".scalar(keys %fork) );
248 &DEBUG(" pid => ".scalar(keys %pid) );
250 if ($$ == $bot_pid) {
251 &status("--- $sig called.");
253 ### crappy bug in Net::IRC?
254 if (!$conn->connected and time - $msgtime > 900) {
255 &status("reconnecting because of uncaught disconnect.");
262 &loadConfig($bot_misc_dir."/blootbot.config");
263 &reloadAllModules() if (&IsParam("DEBUG"));
266 &status("--- End of $sig.");
268 &status("$sig called; ignoring restart.");
272 # File: Configuration.
276 if (!open(FILE, $file)) {
277 &ERROR("FAILED loadConfig ($file): $!");
278 &status("Please copy files/sample.config to files/blootbot.config");
279 &status(" and edit files/blootbot.config, modify to tastes.");
288 my ($set,$key,$val) = split(/\s+/, $_, 3);
291 &status("loadConfig: invalid line '$_'.");
295 # perform variable interpolation
296 $val =~ s/(\$(\w+))/$param{$2}/g;
305 &status("Loaded config $file ($count items)");