]> git.donarmstrong.com Git - infobot.git/blob - src/modules.pl
topic -> Topic
[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         "whatis"        => "WhatIs.pl",
56         "wikipedia"     => "wikipedia.pl",
57         "wingate"       => "Wingate.pl",
58         "wwwsearch"     => "W3Search.pl",
59         "zfi"           => "zfi.pl",
60         "zippy"         => "Zippy.pl",
61         "zsi"           => "zsi.pl",
62 );
63 ### THIS IS NOT LOADED ON RELOAD :(
64 my @myModulesLoadNow;
65 my @myModulesReloadNot;
66 BEGIN {
67     @myModulesLoadNow   = ('Topic', 'uptime', 'News', 'RootWarn', 'DumpVars2', 'botmail');
68     @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
69 }
70
71 sub loadCoreModules {
72     my @mods = &getPerlFiles($bot_src_dir);
73
74     &status("Loading CORE modules...");
75
76     foreach (sort @mods) {
77         my $mod = "$bot_src_dir/$_";
78
79         eval "require \"$mod\"";
80         if ($@) {
81             &ERROR("lCM => $@");
82             &shutdown();
83             exit 1;
84         }
85
86         $moduleAge{$mod} = (stat $mod)[9];
87         &showProc(" ($_)") if (&IsParam("DEBUG"));
88     }
89 }
90
91 sub loadDBModules {
92     my $f;
93     # TODO: use function to load module.
94
95     if ($param{'DBType'} =~ /^(mysql|SQLite|pgsql)$/i) {
96         eval "use DBI";
97         if ($@) {
98             &ERROR("No support for DBI::" . $param{'DBType'} . ", exiting!");
99             exit 1;
100         }
101         &status("Loading " . $param{'DBType'} . " support.");
102         $f = "$bot_src_dir/dbi.pl";
103         require $f;
104         $moduleAge{$f} = (stat $f)[9];
105
106         &showProc(" (DBI::" . $param{'DBType'} . ")");
107     } else {
108         &WARN("DB support DISABLED.");
109         return;
110     }
111 }
112
113 sub loadFactoidsModules {
114     if (!&IsParam("factoids")) {
115         &status("Factoid support DISABLED.");
116         return;
117     }
118
119     &status("Loading Factoids modules...");
120
121     foreach ( &getPerlFiles("$bot_src_dir/Factoids") ) {
122         my $mod = "$bot_src_dir/Factoids/$_";
123
124         eval "require \"$mod\"";
125         if ($@) {
126             &ERROR("lFM: $@");
127             exit 1;
128         }
129
130         $moduleAge{$mod} = (stat $mod)[9];
131         &showProc(" ($_)") if (&IsParam("DEBUG"));
132     }
133 }
134
135 sub loadIRCModules {
136     my ($interface) = &whatInterface();
137     if ($interface =~ /IRC/) {
138         &status("Loading IRC modules...");
139
140         eval "use Net::IRC";
141         if ($@) {
142             &ERROR("libnet-irc-perl is not installed!");
143             exit 1;
144         }
145         &showProc(" (Net::IRC)");
146     } else {
147         &status("IRC support DISABLED.");
148         # disabling forking. Why?
149         #$param{forking}        = 0;
150         #$param{noSHM}  = 1;
151     }
152
153     foreach ( &getPerlFiles("$bot_src_dir/$interface") ) {
154         my $mod = "$bot_src_dir/$interface/$_";
155
156         # hrm... use another config option besides DEBUG to display
157         # change in memory usage.
158         &status("Loading Modules \"$mod\"") if (!&IsParam("DEBUG"));
159         eval "require \"$mod\"";
160         if ($@) {
161             &ERROR("require \"$mod\" => $@");
162             &shutdown();
163             exit 1;
164         }
165
166         $moduleAge{$mod} = (stat $mod)[9];
167         &showProc(" ($_)") if (&IsParam("DEBUG"));
168     }
169 }
170
171 sub loadMyModulesNow {
172     my $loaded = 0;
173     my $total  = 0;
174
175     &status("Loading MyModules...");
176     foreach (@myModulesLoadNow) {
177         $total++;
178         if (!defined $_) {
179             &WARN("mMLN: null element.");
180             next;
181         }
182
183         if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
184             if (exists $myModules{$_}) {
185                 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
186             } else {
187                 &DEBUG("myModule: $_ (2) not loaded.");
188             }
189
190             next;
191         }
192
193         &loadMyModule($myModules{$_});
194         $loaded++;
195     }
196
197     &status("Module: Runtime: Loaded/Total [$loaded/$total]");
198 }
199
200 ### rename to moduleReloadAll?
201 sub reloadAllModules {
202     my $retval = "";
203
204     &VERB("Module: reloading all.",2);
205
206     # obscure usage of map and regex :)
207     foreach (map { s/.*?\/?src/src/; $_ } keys %moduleAge) {
208         $retval .= &reloadModule($_);
209     }
210
211     &VERB("Module: reloading done.",2);
212     return $retval;
213 }
214
215 ### rename to modulesReload?
216 sub reloadModule {
217     my ($mod)   = @_;
218     my $file    = (grep /\/$mod/, keys %INC)[0];
219     my $retval = "";
220
221     # don't reload if it's not our module.
222     if ($mod =~ /::/ or $mod !~ /pl$/) {
223         &VERB("Not reloading $mod.",3);
224         return $retval;
225     }
226
227     if (!defined $file) {
228         &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
229         return $retval;
230     }
231
232     if (! -f $file) {
233         &ERROR("rM: file '$file' does not exist?");
234         return $retval;
235     }
236
237     if (grep /$mod/, @myModulesReloadNot) {
238         &DEBUG("rM: should not reload $mod");
239         return $retval;
240     }
241
242     my $age = (stat $file)[9];
243
244     if (!exists $moduleAge{$file}) {
245         &DEBUG("Looks like $file was not loaded; fixing.");
246     } else {
247         return $retval if ($age == $moduleAge{$file});
248
249         if ($age < $moduleAge{$file}) {
250             &WARN("rM: we're not gonna downgrade '$file'; use touch.");
251             &DEBUG("age => $age");
252             &DEBUG("mA{$file} => $moduleAge{$file}");
253             return $retval;
254         }
255
256         my $dc  = &Time2String($age   - $moduleAge{$file});
257         my $ago = &Time2String(time() - $moduleAge{$file});
258
259         &VERB("Module:  delta change: $dc",2);
260         &VERB("Module:           ago: $ago",2);
261     }
262
263     &status("Module: Loading $mod...");
264
265     delete $INC{$file};
266     eval "require \"$file\"";   # require or use?
267     if (@$) {
268         &DEBUG("rM: failure: @$ ");
269     } else {
270         my $basename = $file;
271         $basename =~ s/^.*\///;
272         &status("Module: reloaded $basename");
273         $retval = " $basename";
274         $moduleAge{$file} = $age;
275     }
276     return $retval;
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("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($modulename);
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($modulename);
347             exit 1;
348         }
349
350         return 0;
351     } else {
352         $moduleAge{$modulefile} = (stat $modulefile)[9];
353
354         &status("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;