2 # core.pl: Important functions stuff...
3 # Author: xk <xk@leguin.openprojects.net>
4 # Version: v0.4 (20000718)
10 # dynamic scalar. MUST BE REDUCED IN SIZE!!!
13 $answer $correction_plausible $loggingstatus $talkchannel
14 $statcount $memusage $user $memusageOld $infobot_version $dbh
15 $shm $host $msg $infobot_misc_dir $infobot_pid $infobot_base_dir
16 $infobot_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';
41 $userHandle = "default";
45 $infobot_version = "blootbot 1.0.0 (20000725) -- $^O";
54 if (!defined $infobot_pid) { # independent.
56 } elsif ($infobot_pid == $$) { # parent.
57 &status("parent caught SIG$sig (pid $$).") if (defined $sig);
63 &quit($param{'quitMsg'}) if (&whatInterface() =~ /IRC/);
67 &dumpallvars() if (&IsParam("dumpvarsAtExit"));
70 &status("child caught SIG$sig (pid $$).");
77 $SIG{__WARN__} = sub { warn $_[0]; };
83 $SIG{__WARN__} = 'doWarn';
86 # Usage: &IsParam($param);
90 return 0 unless (defined $param);
91 return 0 unless (exists $param{$param});
92 return 0 unless ($param{$param});
93 return 0 if $param{$param} =~ /^false$/i;
98 my ($prefix) = $_[0] || "";
100 if (!open(IN, "/proc/$$/status")) {
101 &ERROR("cannot open '/proc/$$/status'.");
105 if ($^O eq "linux") {
107 $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
111 if (defined $memusageOld and &IsParam("DEBUG")) {
112 # it's always going to be increase.
113 my $delta = $memusage - $memusageOld;
115 &status("MEM:$prefix increased by $delta kB. (total: $memusage kB)");
116 } elsif ($delta > 0) {
117 &status("MEM:$prefix increased by $delta kB.");
118 } elsif ($delta < 0) {
120 # never knew RSS could decrease, probably Size can't?
121 &status("MEM:$prefix decreased by $delta kB. YES YES YES");
124 $memusageOld = $memusage;
126 $memusage = "UNKNOWN";
128 ### TODO: FreeBSD/*BSD support.
136 &showProc(" (\&openLog before)");
137 &openLog(); # write, append.
140 &loadIgnore($infobot_misc_dir."/infobot.ignore");
141 &loadLang($infobot_misc_dir."/infobot.lang");
142 &loadIRCServers($infobot_misc_dir."/ircII.servers");
143 &loadUsers($infobot_misc_dir."/infobot.users");
148 &status("Setup: ". &countKeys("factoids") ." factoids.");
150 &status("Initial memory usage: $memusage kB");
154 &loadConfig($infobot_misc_dir."/infobot.config");
156 foreach ("ircNick", "ircUser", "ircName", "DBType") {
157 next if &IsParam($_);
158 &ERROR("Parameter $_ has not been defined.");
162 # static scalar variables.
163 $file{utm} = "$infobot_base_dir/$param{'ircUser'}.uptime";
164 $file{PID} = "$infobot_base_dir/$param{'ircUser'}.pid";
168 if (&IsParam("DEBUG")) {
169 &status("enabling debug diagnostics.");
170 ### I thought disabling this reduced memory usage by 1000 kB.
174 $count{'Question'} = 0;
175 $count{'Update'} = 0;
182 # reverse order of &setup().
184 &closeSHM($shm); # aswell. TODO: use this in &doExit?
191 if ($$ == $infobot_pid) {
192 &status("$sig called.");
194 ### crappy bug in Net::IRC?
195 if (!$conn->connected and time - $msgtime > 900) {
196 &status("reconnecting because of uncaught disconnect.");
203 &loadConfig($infobot_misc_dir."/infobot.config");
204 &reloadModules() if (&IsParam("DEBUG"));
207 &status("End of $sig.");
209 &status("$sig called; ignoring restart.");
213 # File: Configuration.
217 if (!open(FILE, $file)) {
218 &ERROR("FAILED loadConfig ($file): $!");
219 &status("Please make sure the configuration file exists.");
228 my ($set,$key,$val) = split(/\s+/, $_, 3);
231 &status("loadConfig: invalid line '$_'.");
235 # perform variable interpolation
236 $val =~ s/(\$(\w+))/$param{$2}/g;
245 &status("Loaded config $file ($count items)");