]> git.donarmstrong.com Git - infobot.git/blob - src/modules.pl
- now support verstats for those who don't reply to "CTCP VERSION
[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_mysql.pl";
91         $moduleAge{"$bot_src_dir/db_mysql.pl"} = time();
92
93     } elsif ($param{'DBType'} =~ /^pgsql$/i) {
94 #       eval "use Pg";
95         eval "use DBI";
96         if ($@) {
97             &ERROR("libpgperl is not installed!");
98             exit 1;
99         }
100         &showProc(" (pgsql)");
101
102         &status("  using pgsql support.");
103         require "$bot_src_dir/db_pgsql.pl";
104
105     } elsif ($param{'DBType'} =~ /^dbm$/i) {
106
107         &status("  using Berkeley DBM 1.85/2.0 support.");
108         &ERROR("dbm support is broken... if you want it, fix it yourself!");
109         &shutdown();
110         exit 1;
111
112 #       require "$bot_src_dir/db_dbm.pl";
113     } else {
114
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     if (&whatInterface() =~ /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
153     } else {
154         &status("IRC support DISABLED.");
155         return;
156     }
157
158     foreach ( &getPerlFiles("$bot_src_dir/IRC") ) {
159         my $mod = "$bot_src_dir/IRC/$_";
160
161         eval "require \"$mod\"";
162         if ($@) {
163             &ERROR("lIRCM => $@");
164             &shutdown();
165             exit 1;
166         }
167
168         $moduleAge{$mod} = (stat $mod)[9];
169         &showProc(" ($_)") if (&IsParam("DEBUG"));
170     }
171 }
172
173 sub loadMyModulesNow {
174     my $loaded = 0;
175     my $total  = 0;
176
177     &status("Loading MyModules...");
178     foreach (@myModulesLoadNow) {
179         $total++;
180         if (!defined $_) {
181             &WARN("mMLN: null element.");
182             next;
183         }
184
185         if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
186             if (exists $myModules{$_}) {
187                 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
188             } else {
189                 &DEBUG("myModule: $_ (2) not loaded.");
190             }
191
192             next;
193         }
194
195         # weird hack to get rootwarn to work.
196         # it may break on other cases though, any ideas?
197         &loadMyModule( $myModules{$_} || $myModules{lc $_} );
198         $loaded++;
199     }
200
201     &status("Module: Runtime: Loaded/Total [$loaded/$total]");
202 }
203
204 ### rename to moduleReloadAll?
205 sub reloadAllModules {
206     &VERB("Module: reloading all.",2);
207
208     # obscure usage of map and regex :)
209     foreach (map { s/.*?\/?src/src/; $_ } keys %moduleAge) {
210         &reloadModule($_);
211     }
212
213     &VERB("Module: reloading done.",2);
214 }
215
216 ### rename to modulesReload?
217 sub reloadModule {
218     my ($mod)   = @_;
219     my $file    = (grep /\/$mod/, keys %INC)[0];
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;
225     }
226
227     if (!defined $file) {
228         &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
229         return;
230     }
231
232     if (! -f $file) {
233         &ERROR("rM: file '$file' does not exist?");
234         return;
235     }
236
237     if (grep /$mod/, @myModulesReloadNot) {
238         &DEBUG("rM: SHOULD NOT RELOAD $mod!!!");
239         return;
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 if ($age == $moduleAge{$file});
248
249         if ($age < $moduleAge{$file}) {
250             &WARN("rM: we're not gonna downgrade '$file'; use touch.");
251             return;
252         }
253
254         my $dc  = &Time2String($age   - $moduleAge{$file});
255         my $ago = &Time2String(time() - $moduleAge{$file});
256
257         &VERB("Module:  delta change: $dc",2);
258         &VERB("Module:           ago: $ago",2);
259     }
260
261     &status("Module: Loading $mod...");
262
263     delete $INC{$file};
264     eval "require \"$file\"";   # require or use?
265     if (@$) {
266         &DEBUG("rM: failure: @$");
267     } else {
268         my $basename = $file;
269         $basename =~ s/^.*\///;
270         &status("Module: reloaded $basename");
271         $moduleAge{$file} = $age;
272     }
273 }
274
275 ###
276 ### OPTIONAL MODULES.
277 ###
278
279 local %perlModulesLoaded  = ();
280 local %perlModulesMissing = ();
281
282 sub loadPerlModule {
283     return 0 if (exists $perlModulesMissing{$_[0]});
284     &reloadModule($_[0]);
285     return 1 if (exists $perlModulesLoaded{$_[0]});
286
287     eval "use $_[0]";
288     if ($@) {
289         &WARN("Module: $_[0] is not installed!");
290         $perlModulesMissing{$_[0]} = 1;
291         return 0;
292     } else {
293         $perlModulesLoaded{$_[0]} = 1;
294         &status("Module: Loaded $_[0] ...");
295         &showProc(" ($_[0])");
296         return 1;
297     }
298 }
299
300 sub loadMyModule {
301     my ($tmp) = @_;
302     if (!defined $tmp) {
303         &WARN("loadMyModule: module is NULL.");
304         return 0; 
305     }
306
307     my ($modulebase, $modulefile);
308     if (exists $myModules{$tmp}) {
309         ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
310     } else {
311         $modulebase = $tmp;
312         if ($tmp = grep /^$modulebase$/, keys %myModules) {
313             &DEBUG("lMM: lame hack, file => name => $tmp.");
314             $modulename = $tmp;
315         }
316     }
317     $modulefile = "$bot_src_dir/Modules/$modulebase";
318
319     # call reloadModule() which checks age of file and reload.
320     if (grep /\/$modulebase$/, keys %INC) {
321         &reloadModule($modulebase);
322         return 1;       # depend on reloadModule?
323     }
324
325     if (! -f $modulefile) {
326         &ERROR("lMM: module ($modulebase) does not exist.");
327         if ($$ == $bot_pid) {   # parent.
328             &shutdown() if (defined $shm and defined $dbh);
329         } else {                        # child.
330             &DEBUG("b4 delfork 1");
331             &delForked($modulebase);
332         }
333
334         exit 1;
335     }
336
337     eval "require \"$modulefile\"";
338     if ($@) {
339         &ERROR("cannot load my module: $modulebase");
340         if ($bot_pid != $$) {   # child.
341             &DEBUG("b4 delfork 2");
342             &delForked($modulebase);
343             exit 1;
344         }
345
346         return 0;
347     } else {
348         $moduleAge{$modulefile} = (stat $modulefile)[9];
349
350         &status("myModule: Loaded $modulebase ...");
351         &showProc(" ($modulebase)");
352         return 1;
353     }
354 }
355
356 $no_timehires = 0;
357 eval "use Time::HiRes qw(gettimeofday tv_interval)";
358 if ($@) {
359     &WARN("No Time::HiRes?");
360     $no_timehires = 1;
361 }
362 &showProc(" (Time::HiRes)");
363
364 sub AUTOLOAD {
365     if (!defined $AUTOLOAD and defined $::AUTOLOAD) {
366         &DEBUG("AUTOLOAD: hrm.. ::AUTOLOAD defined!");
367     }
368     return unless (defined $AUTOLOAD);
369     return if ($AUTOLOAD =~ /__/);      # internal.
370
371     my $str = join(', ', @_);
372     &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD ($str)");
373
374     $AUTOLOAD =~ s/^(\S+):://g;
375
376     if (exists $myModules{lc $AUTOLOAD}) {
377         # hopefully this will work.
378         &DEBUG("Trying to load module $AUTOLOAD...");
379         &loadMyModule(lc $AUTOLOAD);
380     }
381 }
382
383 sub getPerlFiles {
384     my($dir) = @_;
385
386     if (!opendir(DIR, $dir)) {
387         &ERROR("cannot open source directory $dir: $!");
388         exit 1;
389     }
390
391     my @mods;
392     while (defined(my $file = readdir DIR)) {
393         next unless $file =~ /\.pl$/;
394         next unless $file =~ /^[A-Z]/;
395         push(@mods, $file);
396     }
397     closedir DIR;
398
399     return reverse sort @mods;
400 }
401
402 1;