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");
150 &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
152 &status("Setup: ". &countKeys("factoids") ." factoids.");
154 &status("Initial memory usage: $memusage kB");
158 $param{'VERBOSITY'} = 1;
159 &loadConfig($bot_misc_dir."/blootbot.config");
161 foreach ("ircNick", "ircUser", "ircName", "DBType") {
162 next if &IsParam($_);
163 &ERROR("Parameter $_ has not been defined.");
167 # static scalar variables.
168 $file{utm} = "$bot_base_dir/$param{'ircUser'}.uptime";
169 $file{PID} = "$bot_base_dir/$param{'ircUser'}.pid";
173 if (&IsParam("DEBUG")) {
174 &status("enabling debug diagnostics.");
175 ### I thought disabling this reduced memory usage by 1000 kB.
179 $count{'Question'} = 0;
180 $count{'Update'} = 0;
187 # reverse order of &setup().
189 &closeSHM($shm); # aswell. TODO: use this in &doExit?
196 if ($$ == $bot_pid) {
197 &status("$sig called.");
199 ### crappy bug in Net::IRC?
200 if (!$conn->connected and time - $msgtime > 900) {
201 &status("reconnecting because of uncaught disconnect.");
208 &loadConfig($bot_misc_dir."/blootbot.config");
209 &reloadModules() if (&IsParam("DEBUG"));
212 &status("End of $sig.");
214 &status("$sig called; ignoring restart.");
218 # File: Configuration.
222 if (!open(FILE, $file)) {
223 &ERROR("FAILED loadConfig ($file): $!");
224 &status("Please copy files/sample.config to files/blootbot.config");
225 &status(" and edit files/blootbot.config, modify to tastes.");
234 my ($set,$key,$val) = split(/\s+/, $_, 3);
237 &status("loadConfig: invalid line '$_'.");
241 # perform variable interpolation
242 $val =~ s/(\$(\w+))/$param{$2}/g;
251 &status("Loaded config $file ($count items)");