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 "wwwsearch" => "W3Search.pl",
47 "whatis" => "WhatIs.pl",
48 "wingate" => "Wingate.pl",
49 "babelfish" => "babel.pl",
50 "insult" => "insult.pl",
51 "nickometer" => "nickometer.pl",
55 ### THIS IS NOT LOADED ON RELOAD :(
57 my @myModulesReloadNot;
59 @myModulesLoadNow = ('topic', 'uptime', 'news', 'rootWarn', 'symdump');
60 @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
64 my @mods = &getPerlFiles($bot_src_dir);
66 &status("Loading ".scalar(@mods)." CORE modules...");
68 foreach (sort @mods) {
69 my $mod = "$bot_src_dir/$_";
71 eval "require \"$mod\"";
78 $moduleAge{$mod} = (stat $mod)[9];
79 &showProc(" ($_)") if (&IsParam("DEBUG"));
84 my $f = "$bot_src_dir/modules.pl";
85 $moduleAge{$f} = (stat $f)[9];
87 if ($param{'DBType'} =~ /^mysql$/i) {
90 &ERROR("libdbd-mysql-perl is not installed!");
93 &status("Loading MySQL support.");
94 $f = "$bot_src_dir/db_mysql.pl";
96 $moduleAge{$f} = (stat $f)[9];
97 &showProc(" (DBI // mysql)");
98 } elsif ($param{'DBType'} =~ /^pgsql$/i) {
102 &ERROR("libpgperl is not installed!");
105 &status("Loading pgsql support.");
106 require "$bot_src_dir/db_pgsql.pl";
107 &showProc(" (pgsql)");
108 } elsif ($param{'DBType'} =~ /^dbm$/i) {
109 &status("Loading Berkeley DBM support.");
110 $f = "$bot_src_dir/db_dbm.pl";
112 $moduleAge{$f} = (stat $f)[9];
113 &showProc(" $bot_src_dir/db_dbm.pl");
115 &status("DB support DISABLED.");
120 sub loadFactoidsModules {
121 if (!&IsParam("factoids")) {
122 &status("Factoid support DISABLED.");
126 &status("Loading Factoids modules...");
128 foreach ( &getPerlFiles("$bot_src_dir/Factoids") ) {
129 my $mod = "$bot_src_dir/Factoids/$_";
131 eval "require \"$mod\"";
137 $moduleAge{$mod} = (stat $mod)[9];
138 &showProc(" ($_)") if (&IsParam("DEBUG"));
143 my ($interface) = &whatInterface();
144 if ($interface =~ /IRC/) {
145 &status("Loading IRC modules...");
149 &ERROR("libnet-irc-perl is not installed!");
152 &showProc(" (Net::IRC)");
154 &status("IRC support DISABLED.");
160 foreach ( &getPerlFiles("$bot_src_dir/$interface") ) {
161 my $mod = "$bot_src_dir/$interface/$_";
163 &status("Loading Modules \"$mod\"");
164 eval "require \"$mod\"";
166 &ERROR("require \"$mod\" => $@");
171 $moduleAge{$mod} = (stat $mod)[9];
172 &showProc(" ($_)") if (&IsParam("DEBUG"));
176 sub loadMyModulesNow {
180 &status("Loading MyModules...");
181 foreach (@myModulesLoadNow) {
184 &WARN("mMLN: null element.");
188 if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
189 if (exists $myModules{$_}) {
190 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
192 &DEBUG("myModule: $_ (2) not loaded.");
198 # weird hack to get rootwarn to work.
199 # it may break on other cases though, any ideas?
200 &loadMyModule( $myModules{$_} || $myModules{lc $_} );
204 &status("Module: Runtime: Loaded/Total [$loaded/$total]");
207 ### rename to moduleReloadAll?
208 sub reloadAllModules {
209 &VERB("Module: reloading all.",2);
211 # obscure usage of map and regex :)
212 foreach (map { s/.*?\/?src/src/; $_ } keys %moduleAge) {
216 &VERB("Module: reloading done.",2);
219 ### rename to modulesReload?
222 my $file = (grep /\/$mod/, keys %INC)[0];
224 # don't reload if it's not our module.
225 if ($mod =~ /::/ or $mod !~ /pl$/) {
226 &VERB("Not reloading $mod.",3);
230 if (!defined $file) {
231 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
236 &ERROR("rM: file '$file' does not exist?");
240 if (grep /$mod/, @myModulesReloadNot) {
241 &DEBUG("rM: should not reload $mod");
245 my $age = (stat $file)[9];
247 if (!exists $moduleAge{$file}) {
248 &DEBUG("Looks like $file was not loaded; fixing.");
250 return if ($age == $moduleAge{$file});
252 if ($age < $moduleAge{$file}) {
253 &WARN("rM: we're not gonna downgrade '$file'; use touch.");
254 &DEBUG("age => $age");
255 &DEBUG("mA{$file} => $moduleAge{$file}");
259 my $dc = &Time2String($age - $moduleAge{$file});
260 my $ago = &Time2String(time() - $moduleAge{$file});
262 &VERB("Module: delta change: $dc",2);
263 &VERB("Module: ago: $ago",2);
266 &status("Module: Loading $mod...");
269 eval "require \"$file\""; # require or use?
271 &DEBUG("rM: failure: @$");
273 my $basename = $file;
274 $basename =~ s/^.*\///;
275 &status("Module: reloaded $basename");
276 $moduleAge{$file} = $age;
281 ### OPTIONAL MODULES.
284 my %perlModulesLoaded = ();
285 my %perlModulesMissing = ();
288 return 0 if (exists $perlModulesMissing{$_[0]});
289 &reloadModule($_[0]);
290 return 1 if (exists $perlModulesLoaded{$_[0]});
294 &WARN("Module: $_[0] is not installed!");
295 $perlModulesMissing{$_[0]} = 1;
298 $perlModulesLoaded{$_[0]} = 1;
299 &status("Module: Loaded $_[0] ...");
300 &showProc(" ($_[0])");
308 &WARN("loadMyModule: module is NULL.");
312 my ($modulename, $modulebase);
313 if (exists $myModules{$tmp}) {
314 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
317 if ($tmp = grep /^$modulebase$/, keys %myModules) {
318 &DEBUG("lMM: lame hack, file => name => $tmp.");
322 my $modulefile = "$bot_src_dir/Modules/$modulebase";
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?
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);
335 &DEBUG("b4 delfork 1");
336 &delForked($modulebase);
342 eval "require \"$modulefile\"";
344 &ERROR("cannot load my module: $modulebase");
345 if ($bot_pid != $$) { # child.
346 &DEBUG("b4 delfork 2");
347 &delForked($modulebase);
353 $moduleAge{$modulefile} = (stat $modulefile)[9];
355 &status("myModule: Loaded $modulebase ...");
356 &showProc(" ($modulebase)");
362 eval "use Time::HiRes qw(gettimeofday tv_interval)";
364 &WARN("No Time::HiRes?");
367 &showProc(" (Time::HiRes)");
370 if (!defined $AUTOLOAD and defined $::AUTOLOAD) {
371 &DEBUG("AUTOLOAD: hrm.. ::AUTOLOAD defined!");
373 return unless (defined $AUTOLOAD);
374 return if ($AUTOLOAD =~ /__/); # internal.
376 my $str = join(', ', @_);
377 my ($package, $filename, $line) = caller;
378 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD ($str) $filename line $line");
380 $AUTOLOAD =~ s/^(\S+):://g;
382 if (exists $myModules{lc $AUTOLOAD}) {
383 # hopefully this will work.
384 &DEBUG("Trying to load module $AUTOLOAD...");
385 &loadMyModule(lc $AUTOLOAD);
392 if (!opendir(DIR, $dir)) {
393 &ERROR("Cannot open source directory ($dir): $!");
398 while (defined(my $file = readdir DIR)) {
399 next unless $file =~ /\.pl$/;
400 next unless $file =~ /^[A-Z]/;
405 return reverse sort @mods;