]> git.donarmstrong.com Git - infobot.git/blob - src/core.pl
bot: spit out memory change messages in DCC CHAT. TODO: DCCBroadcast should allow...
[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         &closeSQLDebug()        if (&IsParam("SQLDebug"));
74     } else {                                    # child.
75         &status("child caught SIG$sig (pid $$).");
76     }
77
78     exit 0;
79 }
80
81 sub doWarn {
82     $SIG{__WARN__} = sub { warn $_[0]; };
83
84     foreach (@_) {
85         &WARN("PERL: $_");
86     }
87
88     $SIG{__WARN__} = 'doWarn';
89 }
90
91 # Usage: &IsParam($param);
92 sub IsParam {
93     my $param = $_[0];
94
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;
99     return 1;
100 }
101
102 sub showProc {
103     my ($prefix) = $_[0] || "";
104
105     if (!open(IN, "/proc/$$/status")) {
106         &ERROR("cannot open '/proc/$$/status'.");
107         return;
108     }
109
110     if ($^O eq "linux") {
111         while (<IN>) {
112             $memusage = $1 if (/^VmSize:\s+(\d+) kB/);
113         }
114         close IN;
115
116         if (defined $memusageOld and &IsParam("DEBUG")) {
117             # it's always going to be increase.
118             my $delta = $memusage - $memusageOld;
119             my $str;
120             if ($delta > 500) {
121                 $str = "MEM:$prefix increased by $delta kB. (total: $memusage kB)";
122             } elsif ($delta > 0) {
123                 $str = "MEM:$prefix increased by $delta kB";
124             } elsif ($delta < 0) {
125                 $delta = -$delta;
126                 # never knew RSS could decrease, probably Size can't?
127                 $str = "MEM:$prefix decreased by $delta kB. YES YES YES";
128             }
129             &status($str);
130             &DCCBroadcast($str) if (&whatInterface() =~ /IRC/ &&
131                 grep(/Irc.pl/, keys %moduleAge));
132         }
133         $memusageOld = $memusage;
134     } else {
135         $memusage = "UNKNOWN";
136     }
137     ### TODO: FreeBSD/*BSD support.
138 }
139
140 ######
141 ###### SETUP
142 ######
143
144 sub setup {
145     &showProc(" (\&openLog before)");
146     &openLog();         # write, append.
147
148     # read.
149     &loadIgnore($bot_misc_dir.          "/blootbot.ignore");
150     &loadLang($bot_misc_dir.            "/blootbot.lang");
151     &loadIRCServers($bot_misc_dir.      "/ircII.servers");
152     &loadUsers($bot_misc_dir.           "/blootbot.users");
153
154     $shm = &openSHM();
155     &openSQLDebug()     if (&IsParam("SQLDebug"));
156     &openDB($param{'DBName'}, $param{'SQLUser'}, $param{'SQLPass'});
157
158     &status("Setup: ". &countKeys("factoids") ." factoids.");
159
160     &status("Initial memory usage: $memusage kB");
161 }
162
163 sub setupConfig {
164     $param{'VERBOSITY'} = 1;
165     &loadConfig($bot_misc_dir."/blootbot.config");
166
167     foreach ("ircNick", "ircUser", "ircName", "DBType") {
168         next if &IsParam($_);
169         &ERROR("Parameter $_ has not been defined.");
170         exit 1;
171     }
172
173     # static scalar variables.
174     $file{utm}  = "$bot_base_dir/$param{'ircUser'}.uptime";
175     $file{PID}  = "$bot_base_dir/$param{'ircUser'}.pid";
176 }
177
178 sub startup {
179     if (&IsParam("DEBUG")) {
180         &status("enabling debug diagnostics.");
181         ### I thought disabling this reduced memory usage by 1000 kB.
182         use diagnostics;
183     }
184
185     $count{'Question'}  = 0;
186     $count{'Update'}    = 0;
187     $count{'Dunno'}     = 0;
188
189     &loadMyModulesNow();
190 }
191
192 sub shutdown {
193     # reverse order of &setup().
194     &closeDB();
195     &closeSHM($shm);    # aswell. TODO: use this in &doExit?
196     &closeLog();
197 }
198
199 sub restart {
200     my ($sig) = @_;
201
202     if ($$ == $bot_pid) {
203         &status("$sig called.");
204
205         ### crappy bug in Net::IRC?
206         if (!$conn->connected and time - $msgtime > 900) {
207             &status("reconnecting because of uncaught disconnect.");
208 ##          $irc->start;
209             $conn->connect();
210             return;
211         }
212
213         &shutdown();
214         &loadConfig($bot_misc_dir."/blootbot.config");
215         &reloadAllModules() if (&IsParam("DEBUG"));
216         &setup();
217
218         &status("End of $sig.");
219     } else {
220         &status("$sig called; ignoring restart.");
221     }
222 }
223
224 # File: Configuration.
225 sub loadConfig {
226     my ($file) = @_;
227
228     if (!open(FILE, $file)) {
229         &ERROR("FAILED loadConfig ($file): $!");
230         &status("Please copy files/sample.config to files/blootbot.config");
231         &status("  and edit files/blootbot.config, modify to tastes.");
232         exit 0;
233     }
234
235     my $count = 0;
236     while (<FILE>) {
237         chomp;
238         next if /^\s*\#/;
239         next unless /\S/;
240         my ($set,$key,$val) = split(/\s+/, $_, 3);
241
242         if ($set ne "set") {
243             &status("loadConfig: invalid line '$_'.");
244             next;
245         }
246
247         # perform variable interpolation
248         $val =~ s/(\$(\w+))/$param{$2}/g;
249
250         $param{$key} = $val;
251
252         ++$count;
253     }
254     close FILE;
255
256     $file =~ s/^.*\///;
257     &status("Loaded config $file ($count items)");
258 }
259
260 1;