]> git.donarmstrong.com Git - infobot.git/blob - src/core.pl
5f5d8138f1a75c0f8c323e5f829633667615504b
[infobot.git] / src / core.pl
1 #
2 #   core.pl: Important functions stuff...
3 #    Author: dms
4 #   Version: v0.4 (20000718)
5 #   Created: 20000322
6 #
7
8 use strict;
9
10 # dynamic scalar. MUST BE REDUCED IN SIZE!!!
11 ### TODO: reorder.
12 use vars qw(
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
19 );
20
21 # dynamic hash.
22 use vars qw(@joinchan @ircServers @wingateBad @wingateNow @wingateCache
23 );
24
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
29 );
30
31 # Signals.
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';
37
38 # initialize variables.
39 $last{buflen}   = 0;
40 $last{say}      = "";
41 $last{msg}      = "";
42 $userHandle     = "default";
43 $msgtime        = time();
44 $wingaterun     = time();
45 $firsttime      = 1;
46
47 ### CHANGE TO STATIC.
48 $bot_version = "blootbot 1.0.0 (20000725) -- $^O";
49 $noreply         = "NOREPLY";
50
51 ##########
52 ### misc commands.
53 ###
54
55 sub doExit {
56     my ($sig) = @_;
57
58     if (!defined $bot_pid) {    # independent.
59         exit 0;
60     } elsif ($bot_pid == $$) {  # parent.
61         &status("parent caught SIG$sig (pid $$).") if (defined $sig);
62
63         my $type;
64         &closeDCC();
65         &closePID();
66         &seenFlush();
67         &quit($param{'quitMsg'}) if (&whatInterface() =~ /IRC/);
68         &uptimeWriteFile();
69         &closeDB();
70         &closeSHM($shm);
71         &dumpallvars()  if (&IsParam("dumpvarsAtExit"));
72         &closeLog();
73     } else {                                    # child.
74         &status("child caught SIG$sig (pid $$).");
75     }
76
77     exit 0;
78 }
79
80 sub doWarn {
81     $SIG{__WARN__} = sub { warn $_[0]; };
82
83     foreach (@_) {
84         &WARN("PERL: $_");
85     }
86
87     $SIG{__WARN__} = 'doWarn';
88 }
89
90 # Usage: &IsParam($param);
91 sub IsParam {
92     my $param = $_[0];
93
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;
98     return 1;
99 }
100
101 sub showProc {
102     my ($prefix) = $_[0] || "";
103
104     if (!open(IN, "/proc/$$/status")) {
105         &ERROR("cannot open '/proc/$$/status'.");
106         return;
107     }
108
109     if ($^O eq "linux") {
110         while (<IN>) {
111             $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
112         }
113         close IN;
114
115         if (defined $memusageOld and &IsParam("DEBUG")) {
116             # it's always going to be increase.
117             my $delta = $memusage - $memusageOld;
118             if ($delta > 500) {
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) {
123                 $delta = -$delta;
124                 # never knew RSS could decrease, probably Size can't?
125                 &status("MEM:$prefix decreased by $delta kB. YES YES YES");
126             }
127         }
128         $memusageOld = $memusage;
129     } else {
130         $memusage = "UNKNOWN";
131     }
132     ### TODO: FreeBSD/*BSD support.
133 }
134
135 ######
136 ###### SETUP
137 ######
138
139 sub setup {
140     &showProc(" (\&openLog before)");
141     &openLog();         # write, append.
142
143     # read.
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");
148
149     $shm = &openSHM();
150     &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
151
152     &status("Setup: ". &countKeys("factoids") ." factoids.");
153
154     &status("Initial memory usage: $memusage kB");
155 }
156
157 sub setupConfig {
158     $param{'VERBOSITY'} = 1;
159     &loadConfig($bot_misc_dir."/blootbot.config");
160
161     foreach ("ircNick", "ircUser", "ircName", "DBType") {
162         next if &IsParam($_);
163         &ERROR("Parameter $_ has not been defined.");
164         exit 1;
165     }
166
167     # static scalar variables.
168     $file{utm}  = "$bot_base_dir/$param{'ircUser'}.uptime";
169     $file{PID}  = "$bot_base_dir/$param{'ircUser'}.pid";
170 }
171
172 sub startup {
173     if (&IsParam("DEBUG")) {
174         &status("enabling debug diagnostics.");
175         ### I thought disabling this reduced memory usage by 1000 kB.
176         use diagnostics;
177     }
178
179     $count{'Question'}  = 0;
180     $count{'Update'}    = 0;
181     $count{'Dunno'}     = 0;
182
183     &loadMyModulesNow();
184 }
185
186 sub shutdown {
187     # reverse order of &setup().
188     &closeDB();
189     &closeSHM($shm);    # aswell. TODO: use this in &doExit?
190     &closeLog();
191 }
192
193 sub restart {
194     my ($sig) = @_;
195
196     if ($$ == $bot_pid) {
197         &status("$sig called.");
198
199         ### crappy bug in Net::IRC?
200         if (!$conn->connected and time - $msgtime > 900) {
201             &status("reconnecting because of uncaught disconnect.");
202 ##          $irc->start;
203             $conn->connect();
204             return;
205         }
206
207         &shutdown();
208         &loadConfig($bot_misc_dir."/blootbot.config");
209         &reloadModules() if (&IsParam("DEBUG"));
210         &setup();
211
212         &status("End of $sig.");
213     } else {
214         &status("$sig called; ignoring restart.");
215     }
216 }
217
218 # File: Configuration.
219 sub loadConfig {
220     my ($file) = @_;
221
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.");
226         exit 0;
227     }
228
229     my $count = 0;
230     while (<FILE>) {
231         chomp;
232         next if /^\s*\#/;
233         next unless /\S/;
234         my ($set,$key,$val) = split(/\s+/, $_, 3);
235
236         if ($set ne "set") {
237             &status("loadConfig: invalid line '$_'.");
238             next;
239         }
240
241         # perform variable interpolation
242         $val =~ s/(\$(\w+))/$param{$2}/g;
243
244         $param{$key} = $val;
245
246         ++$count;
247     }
248     close FILE;
249
250     $file =~ s/^.*\///;
251     &status("Loaded config $file ($count items)");
252 }
253
254 1;