]> git.donarmstrong.com Git - infobot.git/blob - src/modules.pl
333c43e145950a3d9f5e78d8658e94261cc62a52
[infobot.git] / src / modules.pl
1 #
2 #  modules.pl: pseudo-Module handler
3 #      Author: dms
4 #     Version: v0.2 (20000629)
5 #     Created: 20000624
6 #
7
8 if (&IsParam("useStrict")) { use strict; }
9 use vars qw($AUTOLOAD);
10
11 ###
12 ### REQUIRED MODULES.
13 ###
14
15 eval "use IO::Socket";
16 if ($@) {
17     &ERROR("no IO::Socket?");
18     exit 1;
19 }
20 &showProc(" (IO::Socket)");
21
22 ### MODULES.
23 %myModules = (
24         "countdown"     => "Countdown.pl",
25         "debian"        => "Debian.pl",
26         "debianExtra"   => "DebianExtra.pl",
27         "dict"          => "Dict.pl",
28         "dumpvars"      => "DumpVars.pl",
29         "factoids"      => "Factoids.pl",
30         "freshmeat"     => "Freshmeat.pl",
31         "kernel"        => "Kernel.pl",
32         "ircdcc"        => "UserDCC.pl",
33         "perlMath"      => "Math.pl",
34         "quote"         => "Quote.pl",
35         "rootwarn"      => "RootWarn.pl",
36         "search"        => "Search.pl",
37         "slashdot"      => "Slashdot3.pl",
38         "topic"         => "Topic.pl",
39         "units"         => "Units.pl",
40         "uptime"        => "Uptime.pl",
41         "userinfo"      => "UserInfo.pl",
42         "wwwsearch"     => "W3Search.pl",
43         "whatis"        => "WhatIs.pl",
44         "wingate"       => "Wingate.pl",
45         "insult"        => "insult.pl",
46         "nickometer"    => "nickometer.pl",
47 );
48 @myModulesLoadNow       = ('topic', 'uptime',);
49 @myModulesReloadNot     = ('IRC/Irc.pl','IRC/Schedulers.pl');
50
51 sub loadCoreModules {
52     if (!opendir(DIR, $bot_src_dir)) {
53         &ERROR("can't open source directory $bot_src_dir: $!");
54         exit 1;
55     }
56
57     &status("Loading CORE modules...");
58
59     while (defined(my $file = readdir DIR)) {
60         next unless $file =~ /\.pl$/;
61         next unless $file =~ /^[A-Z]/;
62         my $mod = "$bot_src_dir/$file";
63
64         ### TODO: use eval and exit gracefully?
65         eval "require \"$mod\"";
66         if ($@) {
67             &ERROR("lCM => $@");
68             &shutdown();
69             exit 1;
70         }
71
72         $moduleAge{$mod} = (stat $mod)[9];
73         &showProc(" ($file)") if (&IsParam("DEBUG"));
74     }
75     closedir DIR;
76 }
77
78 sub loadDBModules {
79     &status("Loading DB modules...");
80
81     if ($param{'DBType'} =~ /^mysql$/i) {
82         eval "use DBI";
83         if ($@) {
84             &ERROR("libdbd-mysql-perl is not installed!");
85             exit 1;
86         }
87         &showProc(" (DBI // mysql)");
88
89         &status("  using MySQL support.");
90         require "$bot_src_dir/db_mysql.pl";
91
92     } elsif ($param{'DBType'} =~ /^pgsql$/i) {
93         eval "use Pg";
94         if ($@) {
95             &ERROR("libpgperl is not installed!");
96             exit 1;
97         }
98         &showProc(" (Pg // postgreSQLl)");
99
100         &status("  using PostgreSQL support.");
101         require "$bot_src_dir/db_pgsql.pl";
102     } elsif ($param{'DBType'} =~ /^dbm$/i) {
103
104         &status("  using Berkeley DBM 1.85/2.0 support.");
105         require "$bot_src_dir/db_dbm.pl";
106     } else {
107
108         &status("DB support DISABLED.");
109         return;
110     }
111 }
112
113 sub loadFactoidsModules {
114     &status("Loading Factoids modules...");
115
116     if (!&IsParam("factoids")) {
117         &status("Factoid support DISABLED.");
118         return;
119     }
120
121     if (!opendir(DIR, "$bot_src_dir/Factoids")) {
122         &ERROR("can't open source directory Factoids: $!");
123         exit 1;
124     }
125
126     while (defined(my $file = readdir DIR)) {
127         next unless $file =~ /\.pl$/;
128         next unless $file =~ /^[A-Z]/;
129         my $mod = "$bot_src_dir/Factoids/$file";
130         ### TODO: use eval and exit gracefully?
131         eval "require \"$mod\"";
132         if ($@) {
133             &WARN("lFM: $@");
134             exit 1;
135         }
136
137         $moduleAge{$mod} = (stat $mod)[9];
138         &showProc(" ($file)") if (&IsParam("DEBUG"));
139     }
140     closedir DIR;
141 }
142
143 sub loadIRCModules {
144     &status("Loading IRC modules...");
145     if (&whatInterface() =~ /IRC/) {
146         eval "use Net::IRC";
147         if ($@) {
148             &ERROR("libnet-irc-perl is not installed!");
149             exit 1;
150         }
151         &showProc(" (Net::IRC)");
152
153     } else {
154         &status("IRC support DISABLED.");
155         return;
156     }
157
158     if (!opendir(DIR, "$bot_src_dir/IRC")) {
159         &ERROR("can't open source directory Factoids: $!");
160         exit 1;
161     }
162
163     while (defined(my $file = readdir DIR)) {
164         next unless $file =~ /\.pl$/;
165         next unless $file =~ /^[A-Z]/;
166         my $mod = "$bot_src_dir/IRC/$file";
167         ### TODO: use eval and exit gracefully?
168         require $mod;
169         $moduleAge{$mod} = (stat $mod)[9];
170         &showProc(" ($file)") if (&IsParam("DEBUG"));
171     }
172     closedir DIR;
173 }
174
175 sub loadMyModulesNow {
176     my $loaded = 0;
177     my $total  = 0;
178
179     &status("Loading MyModules...");
180     foreach (@myModulesLoadNow) {
181         $total++;
182         if (!defined $_) {
183             &WARN("mMLN: null element.");
184             next;
185         }
186
187         if (!&IsParam($_) and !&IsChanConf($_)) {
188             if (exists $myModules{$_}) {
189                 &DEBUG("myModule: $myModules{$_} (1) not loaded.");
190             } else {
191                 &DEBUG("myModule: $_ (2) not loaded.");
192             }
193
194             next;
195         }
196
197         &loadMyModule($myModules{$_});
198         $loaded++;
199     }
200
201     &status("Module: Loaded/Total [$loaded/$total]");
202 }
203
204 ### rename to moduleReloadAll?
205 sub reloadAllModules {
206 ###    &status("Module: reloading all.");
207     foreach (map { substr($_,2) } keys %moduleAge) {
208         &reloadModule($_);
209     }
210 ###    &status("Module: reloading done.");
211 }
212
213 ### rename to modulesReload?
214 sub reloadModule {
215     my ($mod)   = @_;
216     my $file    = (grep /\/$mod/, keys %INC)[0];
217
218     # don't reload if it's not our module.
219     if ($mod =~ /::/ or $mod !~ /pl$/) {
220         &VERB("Not reloading $mod.",3);
221         return;
222     }
223
224     if (!defined $file) {
225         &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
226         return;
227     }
228
229     if (! -f $file) {
230         &ERROR("rM: file '$file' does not exist?");
231         return;
232     }
233
234     my $age = (stat $file)[9];
235     return if ($age == $moduleAge{$file});
236
237     if ($age < $moduleAge{$file}) {
238         &WARN("rM: we're not gonna downgrade the file. use 'touch'.");
239         return;
240     }
241
242     if (grep /$mod/, @myModulesReloadNot) {
243         &DEBUG("rM: SHOULD NOT RELOAD $mod!!!");
244         return;
245     }
246
247     my $dc  = &Time2String($age   - $moduleAge{$file});
248     my $ago = &Time2String(time() - $moduleAge{$file});
249
250     &status("Module: Loading $mod...");
251     &VERB("Module:  delta change: $dc",2);
252     &VERB("Module:           ago: $ago",2);
253
254     delete $INC{$file};
255     eval "require \"$file\"";   # require or use?
256     if (@$) {
257         &DEBUG("rM: failure: @$");
258     } else {
259         my $basename = $file;
260         $basename =~ s/^.*\///;
261         &status("Module: reloaded $basename");
262         $moduleAge{$file} = $age;
263     }
264 }
265
266 ###
267 ### OPTIONAL MODULES.
268 ###
269
270 local %perlModulesLoaded  = ();
271 local %perlModulesMissing = ();
272
273 sub loadPerlModule {
274     return 0 if (exists $perlModulesMissing{$_[0]});
275     &reloadModule($_[0]);
276     return 1 if (exists $perlModulesLoaded{$_[0]});
277
278     eval "use $_[0]";
279     if ($@) {
280         &WARN("Module: $_[0] is not installed!");
281         $perlModulesMissing{$_[0]} = 1;
282         return 0;
283     } else {
284         $perlModulesLoaded{$_[0]} = 1;
285         &status("Module: Loaded $_[0] ...");
286         &showProc(" ($_[0])");
287         return 1;
288     }
289 }
290
291 sub loadMyModule {
292     my ($tmp) = @_;
293     if (!defined $tmp) {
294         &WARN("loadMyModule: module is NULL.");
295         return 0; 
296     }
297
298     my ($modulebase, $modulefile);
299     if (exists $myModules{$tmp}) {
300         ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
301     } else {
302         $modulebase = $tmp;
303         if ($tmp = grep /^$modulebase$/, keys %myModules) {
304             &DEBUG("lMM: lame hack, file => name => $tmp.");
305             $modulename = $tmp;
306         }
307     }
308     my $modulefile = "$bot_src_dir/Modules/$modulebase";
309
310     # call reloadModule() which checks age of file and reload.
311     if (grep /\/$modulebase$/, keys %INC) {
312         &reloadModule($modulebase);
313         return 1;       # depend on reloadModule?
314     }
315
316     if (! -f $modulefile) {
317         &ERROR("lMM: module ($modulebase) does not exist.");
318         if ($$ == $bot_pid) {   # parent.
319             &shutdown() if (defined $shm and defined $dbh);
320         } else {                        # child.
321             &DEBUG("b4 delfork 1");
322             &delForked($modulebase);
323         }
324
325         exit 1;
326     }
327
328     eval "require \"$modulefile\"";
329     if ($@) {
330         &ERROR("cannot load my module: $modulebase");
331         if ($bot_pid != $$) {   # child.
332             &DEBUG("b4 delfork 2");
333             &delForked($modulebase);
334             exit 1;
335         }
336
337         return 0;
338     } else {
339         $moduleAge{$modulefile} = (stat $modulefile)[9];
340
341         &status("myModule: Loaded $modulebase ...");
342         &showProc(" ($modulebase)");
343         return 1;
344     }
345 }
346
347 $no_timehires = 0;
348 eval "use Time::HiRes qw(gettimeofday tv_interval)";
349 if ($@) {
350     &WARN("No Time::HiRes?");
351     $no_timehires = 1;
352 }
353 &showProc(" (Time::HiRes)");
354
355 sub AUTOLOAD {
356     return if ($AUTOLOAD =~ /__/);      # internal.
357
358     &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD");
359     foreach (@_) {
360         next unless (defined $_);
361         &status("  => $_");
362     }
363 }
364
365 1;