]> git.donarmstrong.com Git - infobot.git/blob - src/modules.pl
30c5a40c8d9d2d2113ab89e7d0f159ec9bf4e5c4
[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');
53     @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
54 }
55
56 sub loadCoreModules {
57     if (!opendir(DIR, $bot_src_dir)) {
58         &ERROR("can't open source directory $bot_src_dir: $!");
59         exit 1;
60     }
61
62     my @mods;
63     while (defined(my $file = readdir DIR)) {
64         next unless $file =~ /\.pl$/;
65         next unless $file =~ /^[A-Z]/;
66         push(@mods, $file);
67     }
68     closedir DIR;
69     &status("Loading ".scalar(@mods)." CORE modules...");
70
71     foreach (sort @mods) {
72         my $mod = "$bot_src_dir/$_";
73
74         eval "require \"$mod\"";
75         if ($@) {
76             &ERROR("lCM => $@");
77             &shutdown();
78             exit 1;
79         }
80
81         $moduleAge{$mod} = (stat $mod)[9];
82         &showProc(" ($_)") if (&IsParam("DEBUG"));
83     }
84 }
85
86 sub loadDBModules {
87     &status("Loading DB modules...");
88
89     if ($param{'DBType'} =~ /^mysql$/i) {
90         eval "use DBI";
91         if ($@) {
92             &ERROR("libdbd-mysql-perl is not installed!");
93             exit 1;
94         }
95         &showProc(" (DBI // mysql)");
96
97         &status("  using MySQL support.");
98         require "$bot_src_dir/db_mysql.pl";
99
100     } elsif ($param{'DBType'} =~ /^pgsql$/i) {
101         eval "use Pg";
102         if ($@) {
103             &ERROR("libpgperl is not installed!");
104             exit 1;
105         }
106         &showProc(" (Pg // postgreSQLl)");
107
108         &status("  using PostgreSQL support.");
109         require "$bot_src_dir/db_pgsql.pl";
110     } elsif ($param{'DBType'} =~ /^dbm$/i) {
111
112         &status("  using Berkeley DBM 1.85/2.0 support.");
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     &status("Loading Factoids modules...");
123
124     if (!&IsParam("factoids")) {
125         &status("Factoid support DISABLED.");
126         return;
127     }
128
129     if (!opendir(DIR, "$bot_src_dir/Factoids")) {
130         &ERROR("can't open source directory Factoids: $!");
131         exit 1;
132     }
133
134     while (defined(my $file = readdir DIR)) {
135         next unless $file =~ /\.pl$/;
136         next unless $file =~ /^[A-Z]/;
137         my $mod = "$bot_src_dir/Factoids/$file";
138         ### TODO: use eval and exit gracefully?
139         eval "require \"$mod\"";
140         if ($@) {
141             &WARN("lFM: $@");
142             exit 1;
143         }
144
145         $moduleAge{$mod} = (stat $mod)[9];
146         &showProc(" ($file)") if (&IsParam("DEBUG"));
147     }
148     closedir DIR;
149 }
150
151 sub loadIRCModules {
152     &status("Loading IRC modules...");
153     if (&whatInterface() =~ /IRC/) {
154         eval "use Net::IRC";
155         if ($@) {
156             &ERROR("libnet-irc-perl is not installed!");
157             exit 1;
158         }
159         &showProc(" (Net::IRC)");
160
161     } else {
162         &status("IRC support DISABLED.");
163         return;
164     }
165
166     if (!opendir(DIR, "$bot_src_dir/IRC")) {
167         &ERROR("can't open source directory Factoids: $!");
168         exit 1;
169     }
170
171     while (defined(my $file = readdir DIR)) {
172         next unless $file =~ /\.pl$/;
173         next unless $file =~ /^[A-Z]/;
174         my $mod = "$bot_src_dir/IRC/$file";
175         ### TODO: use eval and exit gracefully?
176         require $mod;
177         $moduleAge{$mod} = (stat $mod)[9];
178         &showProc(" ($file)") if (&IsParam("DEBUG"));
179     }
180     closedir DIR;
181 }
182
183 sub loadMyModulesNow {
184     my $loaded = 0;
185     my $total  = 0;
186
187     &status("Loading MyModules...");
188     foreach (@myModulesLoadNow) {
189         $total++;
190         if (!defined $_) {
191             &WARN("mMLN: null element.");
192             next;
193         }
194
195         if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
196             if (exists $myModules{$_}) {
197                 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
198             } else {
199                 &DEBUG("myModule: $_ (2) not loaded.");
200             }
201
202             next;
203         }
204
205         &loadMyModule($myModules{$_});
206         $loaded++;
207     }
208
209     &status("Module: Runtime: Loaded/Total [$loaded/$total]");
210 }
211
212 ### rename to moduleReloadAll?
213 sub reloadAllModules {
214 ###    &status("Module: reloading all.");
215     foreach (map { substr($_,2) } keys %moduleAge) {
216         &reloadModule($_);
217     }
218 ###    &status("Module: reloading done.");
219 }
220
221 ### rename to modulesReload?
222 sub reloadModule {
223     my ($mod)   = @_;
224     my $file    = (grep /\/$mod/, keys %INC)[0];
225
226     # don't reload if it's not our module.
227     if ($mod =~ /::/ or $mod !~ /pl$/) {
228         &VERB("Not reloading $mod.",3);
229         return;
230     }
231
232     if (!defined $file) {
233         &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
234         return;
235     }
236
237     if (! -f $file) {
238         &ERROR("rM: file '$file' does not exist?");
239         return;
240     }
241
242     if (grep /$mod/, @myModulesReloadNot) {
243         &DEBUG("rM: SHOULD NOT RELOAD $mod!!!");
244         return;
245     }
246
247     my $age = (stat $file)[9];
248
249     if (!exists $moduleAge{$file}) {
250         &DEBUG("Looks like $file was not loaded; fixing.");
251     } else {
252         return if ($age == $moduleAge{$file});
253
254         if ($age < $moduleAge{$file}) {
255             &WARN("rM: we're not gonna downgrade the file. use 'touch'.");
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 local %perlModulesLoaded  = ();
285 local %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 ($modulebase, $modulefile);
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     return if ($AUTOLOAD =~ /__/);      # internal.
371
372     &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD");
373     foreach (@_) {
374         next unless (defined $_);
375         &status("  => $_");
376     }
377 }
378
379 1;