]> git.donarmstrong.com Git - infobot.git/blob - src/modules.pl
0cae3423342c6aae1ecdb2783eba3613b944e314
[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 #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         "bzflag"        => "BZFlag.pl",
25         "countdown"     => "Countdown.pl",
26         "debian"        => "Debian.pl",
27         "debianExtra"   => "DebianExtra.pl",
28         "dict"          => "Dict.pl",
29         "dumpvars"      => "DumpVars.pl",
30         "symdump"       => "DumpVars2.pl",
31         "factoids"      => "Factoids.pl",
32         "freshmeat"     => "Freshmeat.pl",
33         "kernel"        => "Kernel.pl",
34         "ircdcc"        => "UserDCC.pl",
35         "perlMath"      => "Math.pl",
36         "news"          => "News.pl",
37         "quote"         => "Quote.pl",
38         "rootwarn"      => "RootWarn.pl",
39         "search"        => "Search.pl",
40         "slashdot"      => "Slashdot3.pl",
41         "topic"         => "Topic.pl",
42         "units"         => "Units.pl",
43         "uptime"        => "Uptime.pl",
44         "userinfo"      => "UserInfo.pl",
45         "wwwsearch"     => "W3Search.pl",
46         "whatis"        => "WhatIs.pl",
47         "wingate"       => "Wingate.pl",
48         "zfi"           => "zfi.pl",
49         "zsi"           => "zsi.pl",
50         "insult"        => "insult.pl",
51         "nickometer"    => "nickometer.pl",
52         "babelfish"     => "babel.pl",
53 );
54 ### THIS IS NOT LOADED ON RELOAD :(
55 my @myModulesLoadNow;
56 my @myModulesReloadNot;
57 BEGIN {
58     @myModulesLoadNow   = ('topic', 'uptime', 'news', 'rootWarn', 'symdump');
59     @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
60 }
61
62 sub loadCoreModules {
63     my @mods = &getPerlFiles($bot_src_dir);
64
65     &status("Loading ".scalar(@mods)." CORE modules...");
66
67     foreach (sort @mods) {
68         my $mod = "$bot_src_dir/$_";
69
70         eval "require \"$mod\"";
71         if ($@) {
72             &ERROR("lCM => $@");
73             &shutdown();
74             exit 1;
75         }
76
77         $moduleAge{$mod} = (stat $mod)[9];
78         &showProc(" ($_)") if (&IsParam("DEBUG"));
79     }
80 }
81
82 sub loadDBModules {
83     my $f = "$bot_src_dir/modules.pl";
84     $moduleAge{$f} = (stat $f)[9];
85
86     if ($param{'DBType'} =~ /^mysql$/i) {
87         eval "use DBI";
88         if ($@) {
89             &ERROR("libdbd-mysql-perl is not installed!");
90             exit 1;
91         }
92         &status("Loading MySQL support.");
93         $f = "$bot_src_dir/db_mysql.pl";
94         require $f;
95         $moduleAge{$f} = (stat $f)[9];
96         &showProc(" (DBI // mysql)");
97     } elsif ($param{'DBType'} =~ /^pgsql$/i) {
98 #       eval "use Pg";
99         eval "use DBI";
100         if ($@) {
101             &ERROR("libpgperl is not installed!");
102             exit 1;
103         }
104         &status("Loading pgsql support.");
105         require "$bot_src_dir/db_pgsql.pl";
106         &showProc(" (pgsql)");
107     } elsif ($param{'DBType'} =~ /^dbm$/i) {
108         &status("Loading Berkeley DBM support.");
109         $f = "$bot_src_dir/db_dbm.pl";
110         require $f;
111         $moduleAge{$f} = (stat $f)[9];
112         &showProc(" $bot_src_dir/db_dbm.pl");
113     } else {
114         &status("DB support DISABLED.");
115         return;
116     }
117 }
118
119 sub loadFactoidsModules {
120     if (!&IsParam("factoids")) {
121         &status("Factoid support DISABLED.");
122         return;
123     }
124
125     &status("Loading Factoids modules...");
126
127     foreach ( &getPerlFiles("$bot_src_dir/Factoids") ) {
128         my $mod = "$bot_src_dir/Factoids/$_";
129
130         eval "require \"$mod\"";
131         if ($@) {
132             &ERROR("lFM: $@");
133             exit 1;
134         }
135
136         $moduleAge{$mod} = (stat $mod)[9];
137         &showProc(" ($_)") if (&IsParam("DEBUG"));
138     }
139 }
140
141 sub loadIRCModules {
142     my ($interface) = &whatInterface();
143     if ($interface =~ /IRC/) {
144         &status("Loading IRC modules...");
145
146         eval "use Net::IRC";
147         if ($@) {
148             &ERROR("libnet-irc-perl is not installed!");
149             exit 1;
150         }
151         &showProc(" (Net::IRC)");
152     } else {
153         &status("IRC support DISABLED.");
154         # disabling forking.
155         $param{forking} = 0;
156         $param{noSHM}   = 1;
157     }
158
159     foreach ( &getPerlFiles("$bot_src_dir/$interface") ) {
160         my $mod = "$bot_src_dir/$interface/$_";
161
162         &status("Loading Modules \"$mod\"");
163         eval "require \"$mod\"";
164         if ($@) {
165             &ERROR("require \"$mod\" => $@");
166             &shutdown();
167             exit 1;
168         }
169
170         $moduleAge{$mod} = (stat $mod)[9];
171         &showProc(" ($_)") if (&IsParam("DEBUG"));
172     }
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($_) and !&getChanConfList($_)) {
188             if (exists $myModules{$_}) {
189                 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
190             } else {
191                 &DEBUG("myModule: $_ (2) not loaded.");
192             }
193
194             next;
195         }
196
197         # weird hack to get rootwarn to work.
198         # it may break on other cases though, any ideas?
199         &loadMyModule( $myModules{$_} || $myModules{lc $_} );
200         $loaded++;
201     }
202
203     &status("Module: Runtime: Loaded/Total [$loaded/$total]");
204 }
205
206 ### rename to moduleReloadAll?
207 sub reloadAllModules {
208     &VERB("Module: reloading all.",2);
209
210     # obscure usage of map and regex :)
211     foreach (map { s/.*?\/?src/src/; $_ } keys %moduleAge) {
212         &reloadModule($_);
213     }
214
215     &VERB("Module: reloading done.",2);
216 }
217
218 ### rename to modulesReload?
219 sub reloadModule {
220     my ($mod)   = @_;
221     my $file    = (grep /\/$mod/, keys %INC)[0];
222
223     # don't reload if it's not our module.
224     if ($mod =~ /::/ or $mod !~ /pl$/) {
225         &VERB("Not reloading $mod.",3);
226         return;
227     }
228
229     if (!defined $file) {
230         &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
231         return;
232     }
233
234     if (! -f $file) {
235         &ERROR("rM: file '$file' does not exist?");
236         return;
237     }
238
239     if (grep /$mod/, @myModulesReloadNot) {
240         &DEBUG("rM: should not reload $mod");
241         return;
242     }
243
244     my $age = (stat $file)[9];
245
246     if (!exists $moduleAge{$file}) {
247         &DEBUG("Looks like $file was not loaded; fixing.");
248     } else {
249         return if ($age == $moduleAge{$file});
250
251         if ($age < $moduleAge{$file}) {
252             &WARN("rM: we're not gonna downgrade '$file'; use touch.");
253             &DEBUG("age => $age");
254             &DEBUG("mA{$file} => $moduleAge{$file}");
255             return;
256         }
257
258         my $dc  = &Time2String($age   - $moduleAge{$file});
259         my $ago = &Time2String(time() - $moduleAge{$file});
260
261         &VERB("Module:  delta change: $dc",2);
262         &VERB("Module:           ago: $ago",2);
263     }
264
265     &status("Module: Loading $mod...");
266
267     delete $INC{$file};
268     eval "require \"$file\"";   # require or use?
269     if (@$) {
270         &DEBUG("rM: failure: @$");
271     } else {
272         my $basename = $file;
273         $basename =~ s/^.*\///;
274         &status("Module: reloaded $basename");
275         $moduleAge{$file} = $age;
276     }
277 }
278
279 ###
280 ### OPTIONAL MODULES.
281 ###
282
283 my %perlModulesLoaded  = ();
284 my %perlModulesMissing = ();
285
286 sub loadPerlModule {
287     return 0 if (exists $perlModulesMissing{$_[0]});
288     &reloadModule($_[0]);
289     return 1 if (exists $perlModulesLoaded{$_[0]});
290
291     eval "use $_[0]";
292     if ($@) {
293         &WARN("Module: $_[0] is not installed!");
294         $perlModulesMissing{$_[0]} = 1;
295         return 0;
296     } else {
297         $perlModulesLoaded{$_[0]} = 1;
298         &status("Module: Loaded $_[0] ...");
299         &showProc(" ($_[0])");
300         return 1;
301     }
302 }
303
304 sub loadMyModule {
305     my ($tmp) = @_;
306     if (!defined $tmp) {
307         &WARN("loadMyModule: module is NULL.");
308         return 0; 
309     }
310
311     my ($modulename, $modulebase);
312     if (exists $myModules{$tmp}) {
313         ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
314     } else {
315         $modulebase = $tmp;
316         if ($tmp = grep /^$modulebase$/, keys %myModules) {
317             &DEBUG("lMM: lame hack, file => name => $tmp.");
318             $modulename = $tmp;
319         }
320     }
321     my $modulefile = "$bot_src_dir/Modules/$modulebase";
322
323     # call reloadModule() which checks age of file and reload.
324     if (grep /\/$modulebase$/, keys %INC) {
325         &reloadModule($modulebase);
326         return 1;       # depend on reloadModule?
327     }
328
329     if (! -f $modulefile) {
330         &ERROR("lMM: module ($modulebase) does not exist.");
331         if ($$ == $bot_pid) {   # parent.
332             &shutdown() if (defined $shm and defined $dbh);
333         } else {                        # child.
334             &DEBUG("b4 delfork 1");
335             &delForked($modulebase);
336         }
337
338         exit 1;
339     }
340
341     eval "require \"$modulefile\"";
342     if ($@) {
343         &ERROR("cannot load my module: $modulebase");
344         if ($bot_pid != $$) {   # child.
345             &DEBUG("b4 delfork 2");
346             &delForked($modulebase);
347             exit 1;
348         }
349
350         return 0;
351     } else {
352         $moduleAge{$modulefile} = (stat $modulefile)[9];
353
354         &status("myModule: Loaded $modulebase ...");
355         &showProc(" ($modulebase)");
356         return 1;
357     }
358 }
359
360 $no_timehires = 0;
361 eval "use Time::HiRes qw(gettimeofday tv_interval)";
362 if ($@) {
363     &WARN("No Time::HiRes?");
364     $no_timehires = 1;
365 }
366 &showProc(" (Time::HiRes)");
367
368 sub AUTOLOAD {
369     if (!defined $AUTOLOAD and defined $::AUTOLOAD) {
370         &DEBUG("AUTOLOAD: hrm.. ::AUTOLOAD defined!");
371     }
372     return unless (defined $AUTOLOAD);
373     return if ($AUTOLOAD =~ /__/);      # internal.
374
375     my $str = join(', ', @_);
376     my ($package, $filename, $line) = caller;
377     &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD ($str) $filename line $line");
378
379     $AUTOLOAD =~ s/^(\S+):://g;
380
381     if (exists $myModules{lc $AUTOLOAD}) {
382         # hopefully this will work.
383         &DEBUG("Trying to load module $AUTOLOAD...");
384         &loadMyModule(lc $AUTOLOAD);
385     }
386 }
387
388 sub getPerlFiles {
389     my($dir) = @_;
390
391     if (!opendir(DIR, $dir)) {
392         &ERROR("Cannot open source directory ($dir): $!");
393         exit 1;
394     }
395
396     my @mods;
397     while (defined(my $file = readdir DIR)) {
398         next unless $file =~ /\.pl$/;
399         next unless $file =~ /^[A-Z]/;
400         push(@mods, $file);
401     }
402     closedir DIR;
403
404     return reverse sort @mods;
405 }
406
407 1;