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",
48 "insult" => "insult.pl",
49 "nickometer" => "nickometer.pl",
50 "babelfish" => "babel.pl",
52 ### THIS IS NOT LOADED ON RELOAD :(
54 my @myModulesReloadNot;
56 @myModulesLoadNow = ('topic', 'uptime', 'news', 'rootWarn', 'symdump');
57 @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
61 my @mods = &getPerlFiles($bot_src_dir);
63 &status("Loading ".scalar(@mods)." CORE modules...");
65 foreach (sort @mods) {
66 my $mod = "$bot_src_dir/$_";
68 eval "require \"$mod\"";
75 $moduleAge{$mod} = (stat $mod)[9];
76 &showProc(" ($_)") if (&IsParam("DEBUG"));
81 my $f = "$bot_src_dir/modules.pl";
82 $moduleAge{$f} = (stat $f)[9];
84 if ($param{'DBType'} =~ /^mysql$/i) {
87 &ERROR("libdbd-mysql-perl is not installed!");
90 &status("Loading MySQL support.");
91 $f = "$bot_src_dir/db_mysql.pl";
93 $moduleAge{$f} = (stat $f)[9];
94 &showProc(" (DBI // mysql)");
95 } elsif ($param{'DBType'} =~ /^pgsql$/i) {
99 &ERROR("libpgperl is not installed!");
102 &status("Loading pgsql support.");
103 require "$bot_src_dir/db_pgsql.pl";
104 &showProc(" (pgsql)");
105 } elsif ($param{'DBType'} =~ /^dbm$/i) {
106 &status("Loading Berkeley DBM support.");
107 $f = "$bot_src_dir/db_dbm.pl";
109 $moduleAge{$f} = (stat $f)[9];
110 &showProc(" $bot_src_dir/db_dbm.pl");
112 &status("DB support DISABLED.");
117 sub loadFactoidsModules {
118 if (!&IsParam("factoids")) {
119 &status("Factoid support DISABLED.");
123 &status("Loading Factoids modules...");
125 foreach ( &getPerlFiles("$bot_src_dir/Factoids") ) {
126 my $mod = "$bot_src_dir/Factoids/$_";
128 eval "require \"$mod\"";
134 $moduleAge{$mod} = (stat $mod)[9];
135 &showProc(" ($_)") if (&IsParam("DEBUG"));
140 my ($interface) = &whatInterface();
141 if ($interface =~ /IRC/) {
142 &status("Loading IRC modules...");
146 &ERROR("libnet-irc-perl is not installed!");
149 &showProc(" (Net::IRC)");
151 &status("IRC support DISABLED.");
157 foreach ( &getPerlFiles("$bot_src_dir/$interface") ) {
158 my $mod = "$bot_src_dir/$interface/$_";
160 &status("Loading Modules \"$mod\"");
161 eval "require \"$mod\"";
163 &ERROR("require \"$mod\" => $@");
168 $moduleAge{$mod} = (stat $mod)[9];
169 &showProc(" ($_)") if (&IsParam("DEBUG"));
173 sub loadMyModulesNow {
177 &status("Loading MyModules...");
178 foreach (@myModulesLoadNow) {
181 &WARN("mMLN: null element.");
185 if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
186 if (exists $myModules{$_}) {
187 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
189 &DEBUG("myModule: $_ (2) not loaded.");
195 # weird hack to get rootwarn to work.
196 # it may break on other cases though, any ideas?
197 &loadMyModule( $myModules{$_} || $myModules{lc $_} );
201 &status("Module: Runtime: Loaded/Total [$loaded/$total]");
204 ### rename to moduleReloadAll?
205 sub reloadAllModules {
206 &VERB("Module: reloading all.",2);
208 # obscure usage of map and regex :)
209 foreach (map { s/.*?\/?src/src/; $_ } keys %moduleAge) {
213 &VERB("Module: reloading done.",2);
216 ### rename to modulesReload?
219 my $file = (grep /\/$mod/, keys %INC)[0];
221 # don't reload if it's not our module.
222 if ($mod =~ /::/ or $mod !~ /pl$/) {
223 &VERB("Not reloading $mod.",3);
227 if (!defined $file) {
228 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
233 &ERROR("rM: file '$file' does not exist?");
237 if (grep /$mod/, @myModulesReloadNot) {
238 &DEBUG("rM: should not reload $mod");
242 my $age = (stat $file)[9];
244 if (!exists $moduleAge{$file}) {
245 &DEBUG("Looks like $file was not loaded; fixing.");
247 return if ($age == $moduleAge{$file});
249 if ($age < $moduleAge{$file}) {
250 &WARN("rM: we're not gonna downgrade '$file'; use touch.");
251 &DEBUG("age => $age");
252 &DEBUG("mA{$file} => $moduleAge{$file}");
256 my $dc = &Time2String($age - $moduleAge{$file});
257 my $ago = &Time2String(time() - $moduleAge{$file});
259 &VERB("Module: delta change: $dc",2);
260 &VERB("Module: ago: $ago",2);
263 &status("Module: Loading $mod...");
266 eval "require \"$file\""; # require or use?
268 &DEBUG("rM: failure: @$");
270 my $basename = $file;
271 $basename =~ s/^.*\///;
272 &status("Module: reloaded $basename");
273 $moduleAge{$file} = $age;
278 ### OPTIONAL MODULES.
281 my %perlModulesLoaded = ();
282 my %perlModulesMissing = ();
285 return 0 if (exists $perlModulesMissing{$_[0]});
286 &reloadModule($_[0]);
287 return 1 if (exists $perlModulesLoaded{$_[0]});
291 &WARN("Module: $_[0] is not installed!");
292 $perlModulesMissing{$_[0]} = 1;
295 $perlModulesLoaded{$_[0]} = 1;
296 &status("Module: Loaded $_[0] ...");
297 &showProc(" ($_[0])");
305 &WARN("loadMyModule: module is NULL.");
309 my ($modulename, $modulebase);
310 if (exists $myModules{$tmp}) {
311 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
314 if ($tmp = grep /^$modulebase$/, keys %myModules) {
315 &DEBUG("lMM: lame hack, file => name => $tmp.");
319 my $modulefile = "$bot_src_dir/Modules/$modulebase";
321 # call reloadModule() which checks age of file and reload.
322 if (grep /\/$modulebase$/, keys %INC) {
323 &reloadModule($modulebase);
324 return 1; # depend on reloadModule?
327 if (! -f $modulefile) {
328 &ERROR("lMM: module ($modulebase) does not exist.");
329 if ($$ == $bot_pid) { # parent.
330 &shutdown() if (defined $shm and defined $dbh);
332 &DEBUG("b4 delfork 1");
333 &delForked($modulebase);
339 eval "require \"$modulefile\"";
341 &ERROR("cannot load my module: $modulebase");
342 if ($bot_pid != $$) { # child.
343 &DEBUG("b4 delfork 2");
344 &delForked($modulebase);
350 $moduleAge{$modulefile} = (stat $modulefile)[9];
352 &status("myModule: Loaded $modulebase ...");
353 &showProc(" ($modulebase)");
359 eval "use Time::HiRes qw(gettimeofday tv_interval)";
361 &WARN("No Time::HiRes?");
364 &showProc(" (Time::HiRes)");
367 if (!defined $AUTOLOAD and defined $::AUTOLOAD) {
368 &DEBUG("AUTOLOAD: hrm.. ::AUTOLOAD defined!");
370 return unless (defined $AUTOLOAD);
371 return if ($AUTOLOAD =~ /__/); # internal.
373 my $str = join(', ', @_);
374 my ($package, $filename, $line) = caller;
375 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD ($str) $filename line $line");
377 $AUTOLOAD =~ s/^(\S+):://g;
379 if (exists $myModules{lc $AUTOLOAD}) {
380 # hopefully this will work.
381 &DEBUG("Trying to load module $AUTOLOAD...");
382 &loadMyModule(lc $AUTOLOAD);
389 if (!opendir(DIR, $dir)) {
390 &ERROR("Cannot open source directory ($dir): $!");
395 while (defined(my $file = readdir DIR)) {
396 next unless $file =~ /\.pl$/;
397 next unless $file =~ /^[A-Z]/;
402 return reverse sort @mods;