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 "ircdcc" => "UserDCC.pl",
35 "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 "userinfo" => "UserInfo.pl",
45 "wwwsearch" => "W3Search.pl",
46 "whatis" => "WhatIs.pl",
47 "wingate" => "Wingate.pl",
50 "insult" => "insult.pl",
51 "nickometer" => "nickometer.pl",
52 "babelfish" => "babel.pl",
54 ### THIS IS NOT LOADED ON RELOAD :(
56 my @myModulesReloadNot;
58 @myModulesLoadNow = ('topic', 'uptime', 'news', 'rootWarn', 'symdump');
59 @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
63 my @mods = &getPerlFiles($bot_src_dir);
65 &status("Loading ".scalar(@mods)." CORE modules...");
67 foreach (sort @mods) {
68 my $mod = "$bot_src_dir/$_";
70 eval "require \"$mod\"";
77 $moduleAge{$mod} = (stat $mod)[9];
78 &showProc(" ($_)") if (&IsParam("DEBUG"));
83 my $f = "$bot_src_dir/modules.pl";
84 $moduleAge{$f} = (stat $f)[9];
86 if ($param{'DBType'} =~ /^mysql$/i) {
89 &ERROR("libdbd-mysql-perl is not installed!");
92 &status("Loading MySQL support.");
93 $f = "$bot_src_dir/db_mysql.pl";
95 $moduleAge{$f} = (stat $f)[9];
96 &showProc(" (DBI // mysql)");
97 } elsif ($param{'DBType'} =~ /^pgsql$/i) {
101 &ERROR("libpgperl is not installed!");
104 &status("Loading pgsql support.");
105 require "$bot_src_dir/db_pgsql.pl";
106 &showProc(" (pgsql)");
107 } elsif ($param{'DBType'} =~ /^dbm$/i) {
108 &status("Loading Berkeley DBM support.");
109 $f = "$bot_src_dir/db_dbm.pl";
111 $moduleAge{$f} = (stat $f)[9];
112 &showProc(" $bot_src_dir/db_dbm.pl");
114 &status("DB support DISABLED.");
119 sub loadFactoidsModules {
120 if (!&IsParam("factoids")) {
121 &status("Factoid support DISABLED.");
125 &status("Loading Factoids modules...");
127 foreach ( &getPerlFiles("$bot_src_dir/Factoids") ) {
128 my $mod = "$bot_src_dir/Factoids/$_";
130 eval "require \"$mod\"";
136 $moduleAge{$mod} = (stat $mod)[9];
137 &showProc(" ($_)") if (&IsParam("DEBUG"));
142 my ($interface) = &whatInterface();
143 if ($interface =~ /IRC/) {
144 &status("Loading IRC modules...");
148 &ERROR("libnet-irc-perl is not installed!");
151 &showProc(" (Net::IRC)");
153 &status("IRC support DISABLED.");
159 foreach ( &getPerlFiles("$bot_src_dir/$interface") ) {
160 my $mod = "$bot_src_dir/$interface/$_";
162 &status("Loading Modules \"$mod\"");
163 eval "require \"$mod\"";
165 &ERROR("require \"$mod\" => $@");
170 $moduleAge{$mod} = (stat $mod)[9];
171 &showProc(" ($_)") if (&IsParam("DEBUG"));
175 sub loadMyModulesNow {
179 &status("Loading MyModules...");
180 foreach (@myModulesLoadNow) {
183 &WARN("mMLN: null element.");
187 if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
188 if (exists $myModules{$_}) {
189 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
191 &DEBUG("myModule: $_ (2) not loaded.");
197 # weird hack to get rootwarn to work.
198 # it may break on other cases though, any ideas?
199 &loadMyModule( $myModules{$_} || $myModules{lc $_} );
203 &status("Module: Runtime: Loaded/Total [$loaded/$total]");
206 ### rename to moduleReloadAll?
207 sub reloadAllModules {
208 &VERB("Module: reloading all.",2);
210 # obscure usage of map and regex :)
211 foreach (map { s/.*?\/?src/src/; $_ } keys %moduleAge) {
215 &VERB("Module: reloading done.",2);
218 ### rename to modulesReload?
221 my $file = (grep /\/$mod/, keys %INC)[0];
223 # don't reload if it's not our module.
224 if ($mod =~ /::/ or $mod !~ /pl$/) {
225 &VERB("Not reloading $mod.",3);
229 if (!defined $file) {
230 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
235 &ERROR("rM: file '$file' does not exist?");
239 if (grep /$mod/, @myModulesReloadNot) {
240 &DEBUG("rM: should not reload $mod");
244 my $age = (stat $file)[9];
246 if (!exists $moduleAge{$file}) {
247 &DEBUG("Looks like $file was not loaded; fixing.");
249 return if ($age == $moduleAge{$file});
251 if ($age < $moduleAge{$file}) {
252 &WARN("rM: we're not gonna downgrade '$file'; use touch.");
253 &DEBUG("age => $age");
254 &DEBUG("mA{$file} => $moduleAge{$file}");
258 my $dc = &Time2String($age - $moduleAge{$file});
259 my $ago = &Time2String(time() - $moduleAge{$file});
261 &VERB("Module: delta change: $dc",2);
262 &VERB("Module: ago: $ago",2);
265 &status("Module: Loading $mod...");
268 eval "require \"$file\""; # require or use?
270 &DEBUG("rM: failure: @$");
272 my $basename = $file;
273 $basename =~ s/^.*\///;
274 &status("Module: reloaded $basename");
275 $moduleAge{$file} = $age;
280 ### OPTIONAL MODULES.
283 my %perlModulesLoaded = ();
284 my %perlModulesMissing = ();
287 return 0 if (exists $perlModulesMissing{$_[0]});
288 &reloadModule($_[0]);
289 return 1 if (exists $perlModulesLoaded{$_[0]});
293 &WARN("Module: $_[0] is not installed!");
294 $perlModulesMissing{$_[0]} = 1;
297 $perlModulesLoaded{$_[0]} = 1;
298 &status("Module: Loaded $_[0] ...");
299 &showProc(" ($_[0])");
307 &WARN("loadMyModule: module is NULL.");
311 my ($modulename, $modulebase);
312 if (exists $myModules{$tmp}) {
313 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
316 if ($tmp = grep /^$modulebase$/, keys %myModules) {
317 &DEBUG("lMM: lame hack, file => name => $tmp.");
321 my $modulefile = "$bot_src_dir/Modules/$modulebase";
323 # call reloadModule() which checks age of file and reload.
324 if (grep /\/$modulebase$/, keys %INC) {
325 &reloadModule($modulebase);
326 return 1; # depend on reloadModule?
329 if (! -f $modulefile) {
330 &ERROR("lMM: module ($modulebase) does not exist.");
331 if ($$ == $bot_pid) { # parent.
332 &shutdown() if (defined $shm and defined $dbh);
334 &DEBUG("b4 delfork 1");
335 &delForked($modulebase);
341 eval "require \"$modulefile\"";
343 &ERROR("cannot load my module: $modulebase");
344 if ($bot_pid != $$) { # child.
345 &DEBUG("b4 delfork 2");
346 &delForked($modulebase);
352 $moduleAge{$modulefile} = (stat $modulefile)[9];
354 &status("myModule: Loaded $modulebase ...");
355 &showProc(" ($modulebase)");
361 eval "use Time::HiRes qw(gettimeofday tv_interval)";
363 &WARN("No Time::HiRes?");
366 &showProc(" (Time::HiRes)");
369 if (!defined $AUTOLOAD and defined $::AUTOLOAD) {
370 &DEBUG("AUTOLOAD: hrm.. ::AUTOLOAD defined!");
372 return unless (defined $AUTOLOAD);
373 return if ($AUTOLOAD =~ /__/); # internal.
375 my $str = join(', ', @_);
376 my ($package, $filename, $line) = caller;
377 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD ($str) $filename line $line");
379 $AUTOLOAD =~ s/^(\S+):://g;
381 if (exists $myModules{lc $AUTOLOAD}) {
382 # hopefully this will work.
383 &DEBUG("Trying to load module $AUTOLOAD...");
384 &loadMyModule(lc $AUTOLOAD);
391 if (!opendir(DIR, $dir)) {
392 &ERROR("Cannot open source directory ($dir): $!");
397 while (defined(my $file = readdir DIR)) {
398 next unless $file =~ /\.pl$/;
399 next unless $file =~ /^[A-Z]/;
404 return reverse sort @mods;