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
32 $SIG{'HUP'} = 'restart'; # 1.
33 $SIG{'INT'} = 'doExit'; # 2.
34 $SIG{'KILL'} = 'doExit'; # 9. DOES NOT WORK. 'man perlipc' for details.
35 $SIG{'TERM'} = 'doExit'; # 15.
36 $SIG{'__WARN__'} = 'doWarn';
38 # initialize variables.
42 $userHandle = "default";
48 $bot_version = "blootbot 1.0.0 (20000725) -- $^O";
58 if (!defined $bot_pid) { # independent.
60 } elsif ($bot_pid == $$) { # parent.
61 &status("parent caught SIG$sig (pid $$).") if (defined $sig);
67 &quit($param{'quitMsg'}) if (&whatInterface() =~ /IRC/);
71 &dumpallvars() if (&IsParam("dumpvarsAtExit"));
73 &closeSQLDebug() if (&IsParam("SQLDebug"));
75 &status("child caught SIG$sig (pid $$).");
82 $SIG{__WARN__} = sub { warn $_[0]; };
88 $SIG{__WARN__} = 'doWarn';
91 # Usage: &IsParam($param);
95 return 0 unless (defined $param);
96 return 0 unless (exists $param{$param});
97 return 0 unless ($param{$param});
98 return 0 if $param{$param} =~ /^false$/i;
103 my ($prefix) = $_[0] || "";
105 if (!open(IN, "/proc/$$/status")) {
106 &ERROR("cannot open '/proc/$$/status'.");
110 if ($^O eq "linux") {
112 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
116 if (defined $memusageOld and &IsParam("DEBUG")) {
117 # it's always going to be increase.
118 my $delta = $memusage - $memusageOld;
122 } elsif ($delta > 500) {
123 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
124 } elsif ($delta > 0) {
125 $str = "MEM:$prefix increased by $delta kB";
126 } else { # delta < 0.
128 # never knew RSS could decrease, probably Size can't?
129 $str = "MEM:$prefix decreased by $delta kB. YES YES YES";
133 &DCCBroadcast($str) if (&whatInterface() =~ /IRC/ &&
134 grep(/Irc.pl/, keys %moduleAge));
136 $memusageOld = $memusage;
138 $memusage = "UNKNOWN";
140 ### TODO: FreeBSD/*BSD support.
148 &showProc(" (\&openLog before)");
149 &openLog(); # write, append.
151 foreach ("debian","Temp") {
152 &status("Making dir $_");
153 mkdir "$bot_base_dir/$_/", 0755;
157 &loadIgnore($bot_misc_dir. "/blootbot.ignore");
158 &loadLang($bot_misc_dir. "/blootbot.lang");
159 &loadIRCServers($bot_misc_dir. "/ircII.servers");
160 &loadUsers($bot_misc_dir. "/blootbot.users");
163 &openSQLDebug() if (&IsParam("SQLDebug"));
164 &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
166 &status("Setup: ". &countKeys("factoids") ." factoids.");
168 &status("Initial memory usage: $memusage kB");
172 $param{'VERBOSITY'} = 1;
173 &loadConfig($bot_misc_dir."/blootbot.config");
175 foreach ("ircNick", "ircUser", "ircName", "DBType") {
176 next if &IsParam($_);
177 &ERROR("Parameter $_ has not been defined.");
181 # static scalar variables.
182 $file{utm} = "$bot_base_dir/$param{'ircUser'}.uptime";
183 $file{PID} = "$bot_base_dir/$param{'ircUser'}.pid";
187 if (&IsParam("DEBUG")) {
188 &status("enabling debug diagnostics.");
189 ### I thought disabling this reduced memory usage by 1000 kB.
193 $count{'Question'} = 0;
194 $count{'Update'} = 0;
201 # reverse order of &setup().
203 &closeSHM($shm); # aswell. TODO: use this in &doExit?
210 if ($$ == $bot_pid) {
211 &status("$sig called.");
213 ### crappy bug in Net::IRC?
214 if (!$conn->connected and time - $msgtime > 900) {
215 &status("reconnecting because of uncaught disconnect.");
222 &loadConfig($bot_misc_dir."/blootbot.config");
223 &reloadAllModules() if (&IsParam("DEBUG"));
226 &status("End of $sig.");
228 &status("$sig called; ignoring restart.");
232 # File: Configuration.
236 if (!open(FILE, $file)) {
237 &ERROR("FAILED loadConfig ($file): $!");
238 &status("Please copy files/sample.config to files/blootbot.config");
239 &status(" and edit files/blootbot.config, modify to tastes.");
248 my ($set,$key,$val) = split(/\s+/, $_, 3);
251 &status("loadConfig: invalid line '$_'.");
255 # perform variable interpolation
256 $val =~ s/(\$(\w+))/$param{$2}/g;
265 &status("Loaded config $file ($count items)");