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