]> git.donarmstrong.com Git - infobot.git/blob - src/modules.pl
162279d65c6124a9c5e677edfeefe2dab5eb34f5
[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         &ERROR("dbm support is broken... you want it, you fix it!");
114         &shutdown();
115         exit 1;
116
117 #       require "$bot_src_dir/db_dbm.pl";
118     } else {
119
120         &status("DB support DISABLED.");
121         return;
122     }
123 }
124
125 sub loadFactoidsModules {
126     &status("Loading Factoids modules...");
127
128     if (!&IsParam("factoids")) {
129         &status("Factoid support DISABLED.");
130         return;
131     }
132
133     if (!opendir(DIR, "$bot_src_dir/Factoids")) {
134         &ERROR("can't open source directory Factoids: $!");
135         exit 1;
136     }
137
138     while (defined(my $file = readdir DIR)) {
139         next unless $file =~ /\.pl$/;
140         next unless $file =~ /^[A-Z]/;
141         my $mod = "$bot_src_dir/Factoids/$file";
142         ### TODO: use eval and exit gracefully?
143         eval "require \"$mod\"";
144         if ($@) {
145             &WARN("lFM: $@");
146             exit 1;
147         }
148
149         $moduleAge{$mod} = (stat $mod)[9];
150         &showProc(" ($file)") if (&IsParam("DEBUG"));
151     }
152     closedir DIR;
153 }
154
155 sub loadIRCModules {
156     &status("Loading IRC modules...");
157     if (&whatInterface() =~ /IRC/) {
158         eval "use Net::IRC";
159         if ($@) {
160             &ERROR("libnet-irc-perl is not installed!");
161             exit 1;
162         }
163         &showProc(" (Net::IRC)");
164
165     } else {
166         &status("IRC support DISABLED.");
167         return;
168     }
169
170     if (!opendir(DIR, "$bot_src_dir/IRC")) {
171         &ERROR("can't open source directory Factoids: $!");
172         exit 1;
173     }
174
175     while (defined(my $file = readdir DIR)) {
176         next unless $file =~ /\.pl$/;
177         next unless $file =~ /^[A-Z]/;
178         my $mod = "$bot_src_dir/IRC/$file";
179         ### TODO: use eval and exit gracefully?
180         require $mod;
181         $moduleAge{$mod} = (stat $mod)[9];
182         &showProc(" ($file)") if (&IsParam("DEBUG"));
183     }
184     closedir DIR;
185 }
186
187 sub loadMyModulesNow {
188     my $loaded = 0;
189     my $total  = 0;
190
191     &status("Loading MyModules...");
192     foreach (@myModulesLoadNow) {
193         $total++;
194         if (!defined $_) {
195             &WARN("mMLN: null element.");
196             next;
197         }
198
199         if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
200             if (exists $myModules{$_}) {
201                 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
202             } else {
203                 &DEBUG("myModule: $_ (2) not loaded.");
204             }
205
206             next;
207         }
208
209         &loadMyModule($myModules{$_});
210         $loaded++;
211     }
212
213     &status("Module: Runtime: Loaded/Total [$loaded/$total]");
214 }
215
216 ### rename to moduleReloadAll?
217 sub reloadAllModules {
218 ###    &status("Module: reloading all.");
219     foreach (map { substr($_,2) } keys %moduleAge) {
220         &reloadModule($_);
221     }
222 ###    &status("Module: reloading done.");
223 }
224
225 ### rename to modulesReload?
226 sub reloadModule {
227     my ($mod)   = @_;
228     my $file    = (grep /\/$mod/, keys %INC)[0];
229
230     # don't reload if it's not our module.
231     if ($mod =~ /::/ or $mod !~ /pl$/) {
232         &VERB("Not reloading $mod.",3);
233         return;
234     }
235
236     if (!defined $file) {
237         &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
238         return;
239     }
240
241     if (! -f $file) {
242         &ERROR("rM: file '$file' does not exist?");
243         return;
244     }
245
246     if (grep /$mod/, @myModulesReloadNot) {
247         &DEBUG("rM: SHOULD NOT RELOAD $mod!!!");
248         return;
249     }
250
251     my $age = (stat $file)[9];
252
253     if (!exists $moduleAge{$file}) {
254         &DEBUG("Looks like $file was not loaded; fixing.");
255     } else {
256         return if ($age == $moduleAge{$file});
257
258         if ($age < $moduleAge{$file}) {
259             &WARN("rM: we're not gonna downgrade the file. use 'touch'.");
260             return;
261         }
262
263         my $dc  = &Time2String($age   - $moduleAge{$file});
264         my $ago = &Time2String(time() - $moduleAge{$file});
265
266         &VERB("Module:  delta change: $dc",2);
267         &VERB("Module:           ago: $ago",2);
268     }
269
270     &status("Module: Loading $mod...");
271
272     delete $INC{$file};
273     eval "require \"$file\"";   # require or use?
274     if (@$) {
275         &DEBUG("rM: failure: @$");
276     } else {
277         my $basename = $file;
278         $basename =~ s/^.*\///;
279         &status("Module: reloaded $basename");
280         $moduleAge{$file} = $age;
281     }
282 }
283
284 ###
285 ### OPTIONAL MODULES.
286 ###
287
288 local %perlModulesLoaded  = ();
289 local %perlModulesMissing = ();
290
291 sub loadPerlModule {
292     return 0 if (exists $perlModulesMissing{$_[0]});
293     &reloadModule($_[0]);
294     return 1 if (exists $perlModulesLoaded{$_[0]});
295
296     eval "use $_[0]";
297     if ($@) {
298         &WARN("Module: $_[0] is not installed!");
299         $perlModulesMissing{$_[0]} = 1;
300         return 0;
301     } else {
302         $perlModulesLoaded{$_[0]} = 1;
303         &status("Module: Loaded $_[0] ...");
304         &showProc(" ($_[0])");
305         return 1;
306     }
307 }
308
309 sub loadMyModule {
310     my ($tmp) = @_;
311     if (!defined $tmp) {
312         &WARN("loadMyModule: module is NULL.");
313         return 0; 
314     }
315
316     my ($modulebase, $modulefile);
317     if (exists $myModules{$tmp}) {
318         ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
319     } else {
320         $modulebase = $tmp;
321         if ($tmp = grep /^$modulebase$/, keys %myModules) {
322             &DEBUG("lMM: lame hack, file => name => $tmp.");
323             $modulename = $tmp;
324         }
325     }
326     my $modulefile = "$bot_src_dir/Modules/$modulebase";
327
328     # call reloadModule() which checks age of file and reload.
329     if (grep /\/$modulebase$/, keys %INC) {
330         &reloadModule($modulebase);
331         return 1;       # depend on reloadModule?
332     }
333
334     if (! -f $modulefile) {
335         &ERROR("lMM: module ($modulebase) does not exist.");
336         if ($$ == $bot_pid) {   # parent.
337             &shutdown() if (defined $shm and defined $dbh);
338         } else {                        # child.
339             &DEBUG("b4 delfork 1");
340             &delForked($modulebase);
341         }
342
343         exit 1;
344     }
345
346     eval "require \"$modulefile\"";
347     if ($@) {
348         &ERROR("cannot load my module: $modulebase");
349         if ($bot_pid != $$) {   # child.
350             &DEBUG("b4 delfork 2");
351             &delForked($modulebase);
352             exit 1;
353         }
354
355         return 0;
356     } else {
357         $moduleAge{$modulefile} = (stat $modulefile)[9];
358
359         &status("myModule: Loaded $modulebase ...");
360         &showProc(" ($modulebase)");
361         return 1;
362     }
363 }
364
365 $no_timehires = 0;
366 eval "use Time::HiRes qw(gettimeofday tv_interval)";
367 if ($@) {
368     &WARN("No Time::HiRes?");
369     $no_timehires = 1;
370 }
371 &showProc(" (Time::HiRes)");
372
373 sub AUTOLOAD {
374     return if ($AUTOLOAD =~ /__/);      # internal.
375
376     my $str = join(', ', @_);
377     &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD ($str)");
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 1;