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