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