]> git.donarmstrong.com Git - infobot.git/blob - src/core.pl
Changed $infobot_ to $bot_
[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();
151
152     &status("Setup: ". &countKeys("factoids") ." factoids.");
153
154     &status("Initial memory usage: $memusage kB");
155 }
156
157 sub setupConfig {
158     &loadConfig($bot_misc_dir."/blootbot.config");
159
160     foreach ("ircNick", "ircUser", "ircName", "DBType") {
161         next if &IsParam($_);
162         &ERROR("Parameter $_ has not been defined.");
163         exit 1;
164     }
165
166     # static scalar variables.
167     $file{utm}  = "$bot_base_dir/$param{'ircUser'}.uptime";
168     $file{PID}  = "$bot_base_dir/$param{'ircUser'}.pid";
169 }
170
171 sub startup {
172     if (&IsParam("DEBUG")) {
173         &status("enabling debug diagnostics.");
174         ### I thought disabling this reduced memory usage by 1000 kB.
175         use diagnostics;
176     }
177
178     $count{'Question'}  = 0;
179     $count{'Update'}    = 0;
180     $count{'Dunno'}     = 0;
181
182     &loadMyModulesNow();
183 }
184
185 sub shutdown {
186     # reverse order of &setup().
187     &closeDB();
188     &closeSHM($shm);    # aswell. TODO: use this in &doExit?
189     &closeLog();
190 }
191
192 sub restart {
193     my ($sig) = @_;
194
195     if ($$ == $bot_pid) {
196         &status("$sig called.");
197
198         ### crappy bug in Net::IRC?
199         if (!$conn->connected and time - $msgtime > 900) {
200             &status("reconnecting because of uncaught disconnect.");
201 ##          $irc->start;
202             $conn->connect();
203             return;
204         }
205
206         &shutdown();
207         &loadConfig($bot_misc_dir."/blootbot.config");
208         &reloadModules() if (&IsParam("DEBUG"));
209         &setup();
210
211         &status("End of $sig.");
212     } else {
213         &status("$sig called; ignoring restart.");
214     }
215 }
216
217 # File: Configuration.
218 sub loadConfig {
219     my ($file) = @_;
220
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.");
225         exit 0;
226     }
227
228     my $count = 0;
229     while (<FILE>) {
230         chomp;
231         next if /^\s*\#/;
232         next unless /\S/;
233         my ($set,$key,$val) = split(/\s+/, $_, 3);
234
235         if ($set ne "set") {
236             &status("loadConfig: invalid line '$_'.");
237             next;
238         }
239
240         # perform variable interpolation
241         $val =~ s/(\$(\w+))/$param{$2}/g;
242
243         $param{$key} = $val;
244
245         ++$count;
246     }
247     close FILE;
248
249     $file =~ s/^.*\///;
250     &status("Loaded config $file ($count items)");
251 }
252
253 1;