2 # modules.pl: pseudo-Module handler
4 # Version: v0.2 (20000629)
9 use vars qw($AUTOLOAD);
15 eval "use IO::Socket";
17 &ERROR("no IO::Socket?");
20 &showProc(" (IO::Socket)");
24 "bzflag" => "BZFlag.pl",
25 "countdown" => "Countdown.pl",
26 "debian" => "Debian.pl",
27 "debianExtra" => "DebianExtra.pl",
29 "dumpvars" => "DumpVars.pl",
30 "symdump" => "DumpVars2.pl",
31 "factoids" => "Factoids.pl",
32 "freshmeat" => "Freshmeat.pl",
33 "kernel" => "Kernel.pl",
34 "perlMath" => "Math.pl",
37 "quote" => "Quote.pl",
38 "rootwarn" => "RootWarn.pl",
39 "search" => "Search.pl",
40 "slashdot" => "Slashdot3.pl",
41 "topic" => "Topic.pl",
42 "units" => "Units.pl",
43 "uptime" => "Uptime.pl",
44 "ircdcc" => "UserDCC.pl",
45 "userinfo" => "UserInfo.pl",
46 "weather" => "Weather.pl",
47 "wwwsearch" => "W3Search.pl",
48 "whatis" => "WhatIs.pl",
49 "wingate" => "Wingate.pl",
50 "babelfish" => "babel.pl",
51 "insult" => "insult.pl",
52 "nickometer" => "nickometer.pl",
54 "zippy" => "Zippy.pl",
57 ### THIS IS NOT LOADED ON RELOAD :(
59 my @myModulesReloadNot;
61 @myModulesLoadNow = ('topic', 'uptime', 'news', 'rootWarn', 'symdump');
62 @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
66 my @mods = &getPerlFiles($bot_src_dir);
68 &status("Loading ".scalar(@mods)." CORE modules...");
70 foreach (sort @mods) {
71 my $mod = "$bot_src_dir/$_";
73 eval "require \"$mod\"";
80 $moduleAge{$mod} = (stat $mod)[9];
81 &showProc(" ($_)") if (&IsParam("DEBUG"));
86 my $f = "$bot_src_dir/modules.pl";
87 $moduleAge{$f} = (stat $f)[9];
89 if ($param{'DBType'} =~ /^mysql$/i) {
92 &ERROR("libdbd-mysql-perl is not installed!");
95 &status("Loading MySQL support.");
96 $f = "$bot_src_dir/db_mysql.pl";
98 $moduleAge{$f} = (stat $f)[9];
99 &showProc(" (DBI // mysql)");
100 } elsif ($param{'DBType'} =~ /^pgsql$/i) {
104 &ERROR("libpgperl is not installed!");
107 &status("Loading pgsql support.");
108 require "$bot_src_dir/db_pgsql.pl";
109 &showProc(" (pgsql)");
110 } elsif ($param{'DBType'} =~ /^sqlite$|^dbm$/i) {
111 &status("Loading " . $param{'DBType'} . " support.");
112 $f="$bot_src_dir/db_" . $param{'DBType'} . ".pl";
113 $moduleAge{$f} = (stat $f)[9];
115 &showProc(" $bot_src_dir/db_" . $param{'DBType'} . ".pl");
117 &status("DB support DISABLED.");
122 sub loadFactoidsModules {
123 if (!&IsParam("factoids")) {
124 &status("Factoid support DISABLED.");
128 &status("Loading Factoids modules...");
130 foreach ( &getPerlFiles("$bot_src_dir/Factoids") ) {
131 my $mod = "$bot_src_dir/Factoids/$_";
133 eval "require \"$mod\"";
139 $moduleAge{$mod} = (stat $mod)[9];
140 &showProc(" ($_)") if (&IsParam("DEBUG"));
145 my ($interface) = &whatInterface();
146 if ($interface =~ /IRC/) {
147 &status("Loading IRC modules...");
151 &ERROR("libnet-irc-perl is not installed!");
154 &showProc(" (Net::IRC)");
156 &status("IRC support DISABLED.");
162 foreach ( &getPerlFiles("$bot_src_dir/$interface") ) {
163 my $mod = "$bot_src_dir/$interface/$_";
165 # hrm... use another config option besides DEBUG to display
166 # change in memory usage.
167 &status("Loading Modules \"$mod\"") if (!&IsParam("DEBUG"));
168 eval "require \"$mod\"";
170 &ERROR("require \"$mod\" => $@");
175 $moduleAge{$mod} = (stat $mod)[9];
176 &showProc(" ($_)") if (&IsParam("DEBUG"));
180 sub loadMyModulesNow {
184 &status("Loading MyModules...");
185 foreach (@myModulesLoadNow) {
188 &WARN("mMLN: null element.");
192 if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
193 if (exists $myModules{$_}) {
194 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
196 &DEBUG("myModule: $_ (2) not loaded.");
202 # weird hack to get rootwarn to work.
203 # it may break on other cases though, any ideas?
204 &loadMyModule( $myModules{$_} || $myModules{lc $_} );
208 &status("Module: Runtime: Loaded/Total [$loaded/$total]");
211 ### rename to moduleReloadAll?
212 sub reloadAllModules {
213 &VERB("Module: reloading all.",2);
215 # obscure usage of map and regex :)
216 foreach (map { s/.*?\/?src/src/; $_ } keys %moduleAge) {
220 &VERB("Module: reloading done.",2);
223 ### rename to modulesReload?
226 my $file = (grep /\/$mod/, keys %INC)[0];
228 # don't reload if it's not our module.
229 if ($mod =~ /::/ or $mod !~ /pl$/) {
230 &VERB("Not reloading $mod.",3);
234 if (!defined $file) {
235 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
240 &ERROR("rM: file '$file' does not exist?");
244 if (grep /$mod/, @myModulesReloadNot) {
245 &DEBUG("rM: should not reload $mod");
249 my $age = (stat $file)[9];
251 if (!exists $moduleAge{$file}) {
252 &DEBUG("Looks like $file was not loaded; fixing.");
254 return if ($age == $moduleAge{$file});
256 if ($age < $moduleAge{$file}) {
257 &WARN("rM: we're not gonna downgrade '$file'; use touch.");
258 &DEBUG("age => $age");
259 &DEBUG("mA{$file} => $moduleAge{$file}");
263 my $dc = &Time2String($age - $moduleAge{$file});
264 my $ago = &Time2String(time() - $moduleAge{$file});
266 &VERB("Module: delta change: $dc",2);
267 &VERB("Module: ago: $ago",2);
270 &status("Module: Loading $mod...");
273 eval "require \"$file\""; # require or use?
275 &DEBUG("rM: failure: @$");
277 my $basename = $file;
278 $basename =~ s/^.*\///;
279 &status("Module: reloaded $basename");
280 $moduleAge{$file} = $age;
285 ### OPTIONAL MODULES.
288 my %perlModulesLoaded = ();
289 my %perlModulesMissing = ();
292 return 0 if (exists $perlModulesMissing{$_[0]});
293 &reloadModule($_[0]);
294 return 1 if (exists $perlModulesLoaded{$_[0]});
298 &WARN("Module: $_[0] is not installed!");
299 $perlModulesMissing{$_[0]} = 1;
302 $perlModulesLoaded{$_[0]} = 1;
303 &status("Module: Loaded $_[0] ...");
304 &showProc(" ($_[0])");
312 &WARN("loadMyModule: module is NULL.");
316 my ($modulename, $modulebase);
317 if (exists $myModules{$tmp}) {
318 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
321 if ($tmp = grep /^$modulebase$/, keys %myModules) {
322 &DEBUG("lMM: lame hack, file => name => $tmp.");
326 my $modulefile = "$bot_src_dir/Modules/$modulebase";
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?
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);
339 &DEBUG("b4 delfork 1");
340 &delForked($modulebase);
346 eval "require \"$modulefile\"";
348 &ERROR("cannot load my module: $modulebase");
349 if ($bot_pid != $$) { # child.
350 &DEBUG("b4 delfork 2");
351 &delForked($modulebase);
357 $moduleAge{$modulefile} = (stat $modulefile)[9];
359 &status("myModule: Loaded $modulebase ...");
360 &showProc(" ($modulebase)");
366 eval "use Time::HiRes qw(gettimeofday tv_interval)";
368 &WARN("No Time::HiRes?");
371 &showProc(" (Time::HiRes)");
374 if (!defined $AUTOLOAD and defined $::AUTOLOAD) {
375 &DEBUG("AUTOLOAD: hrm.. ::AUTOLOAD defined!");
377 return unless (defined $AUTOLOAD);
378 return if ($AUTOLOAD =~ /__/); # internal.
380 my $str = join(', ', @_);
381 my ($package, $filename, $line) = caller;
382 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD ($str) $filename line $line");
384 $AUTOLOAD =~ s/^(\S+):://g;
386 if (exists $myModules{lc $AUTOLOAD}) {
387 # hopefully this will work.
388 &DEBUG("Trying to load module $AUTOLOAD...");
389 &loadMyModule(lc $AUTOLOAD);
396 if (!opendir(DIR, $dir)) {
397 &ERROR("Cannot open source directory ($dir): $!");
402 while (defined(my $file = readdir DIR)) {
403 next unless $file =~ /\.pl$/;
404 next unless $file =~ /^[A-Z]/;
409 return reverse sort @mods;