2 # core.pl: Important functions stuff...
4 # Version: v0.4 (20000718)
10 # dynamic scalar. MUST BE REDUCED IN SIZE!!!
13 $answer $correction_plausible $loggingstatus $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"));
74 &status("child caught SIG$sig (pid $$).");
81 $SIG{__WARN__} = sub { warn $_[0]; };
87 $SIG{__WARN__} = 'doWarn';
90 # Usage: &IsParam($param);
94 return 0 unless (defined $param);
95 return 0 unless (exists $param{$param});
96 return 0 unless ($param{$param});
97 return 0 if $param{$param} =~ /^false$/i;
102 my ($prefix) = $_[0] || "";
104 if (!open(IN, "/proc/$$/status")) {
105 &ERROR("cannot open '/proc/$$/status'.");
109 if ($^O eq "linux") {
111 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
115 if (defined $memusageOld and &IsParam("DEBUG")) {
116 # it's always going to be increase.
117 my $delta = $memusage - $memusageOld;
119 &status("MEM:$prefix increased by $delta kB. (total: $memusage kB)");
120 } elsif ($delta > 0) {
121 &status("MEM:$prefix increased by $delta kB.");
122 } elsif ($delta < 0) {
124 # never knew RSS could decrease, probably Size can't?
125 &status("MEM:$prefix decreased by $delta kB. YES YES YES");
128 $memusageOld = $memusage;
130 $memusage = "UNKNOWN";
132 ### TODO: FreeBSD/*BSD support.
140 &showProc(" (\&openLog before)");
141 &openLog(); # write, append.
144 &loadIgnore($bot_misc_dir. "/blootbot.ignore");
145 &loadLang($bot_misc_dir. "/blootbot.lang");
146 &loadIRCServers($bot_misc_dir. "/ircII.servers");
147 &loadUsers($bot_misc_dir. "/blootbot.users");
152 &status("Setup: ". &countKeys("factoids") ." factoids.");
154 &status("Initial memory usage: $memusage kB");
158 &loadConfig($bot_misc_dir."/blootbot.config");
160 foreach ("ircNick", "ircUser", "ircName", "DBType") {
161 next if &IsParam($_);
162 &ERROR("Parameter $_ has not been defined.");
166 # static scalar variables.
167 $file{utm} = "$bot_base_dir/$param{'ircUser'}.uptime";
168 $file{PID} = "$bot_base_dir/$param{'ircUser'}.pid";
172 if (&IsParam("DEBUG")) {
173 &status("enabling debug diagnostics.");
174 ### I thought disabling this reduced memory usage by 1000 kB.
178 $count{'Question'} = 0;
179 $count{'Update'} = 0;
186 # reverse order of &setup().
188 &closeSHM($shm); # aswell. TODO: use this in &doExit?
195 if ($$ == $bot_pid) {
196 &status("$sig called.");
198 ### crappy bug in Net::IRC?
199 if (!$conn->connected and time - $msgtime > 900) {
200 &status("reconnecting because of uncaught disconnect.");
207 &loadConfig($bot_misc_dir."/blootbot.config");
208 &reloadModules() if (&IsParam("DEBUG"));
211 &status("End of $sig.");
213 &status("$sig called; ignoring restart.");
217 # File: Configuration.
221 if (!open(FILE, $file)) {
222 &ERROR("FAILED loadConfig ($file): $!");
223 &status("Please copy files/sample.config to files/blootbot.config");
224 &status(" and edit files/blootbot.config, modify to tastes.");
233 my ($set,$key,$val) = split(/\s+/, $_, 3);
236 &status("loadConfig: invalid line '$_'.");
240 # perform variable interpolation
241 $val =~ s/(\$(\w+))/$param{$2}/g;
250 &status("Loaded config $file ($count items)");