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 "countdown" => "Countdown.pl",
25 "debian" => "Debian.pl",
26 "debianExtra" => "DebianExtra.pl",
28 "dumpvars" => "DumpVars.pl",
29 "symdump" => "DumpVars2.pl",
30 "factoids" => "Factoids.pl",
31 "freshmeat" => "Freshmeat.pl",
32 "kernel" => "Kernel.pl",
33 "ircdcc" => "UserDCC.pl",
34 "perlMath" => "Math.pl",
36 "quote" => "Quote.pl",
37 "rootwarn" => "RootWarn.pl",
38 "search" => "Search.pl",
39 "slashdot" => "Slashdot3.pl",
40 "topic" => "Topic.pl",
41 "units" => "Units.pl",
42 "uptime" => "Uptime.pl",
43 "userinfo" => "UserInfo.pl",
44 "wwwsearch" => "W3Search.pl",
45 "whatis" => "WhatIs.pl",
46 "wingate" => "Wingate.pl",
47 "insult" => "insult.pl",
48 "nickometer" => "nickometer.pl",
49 "babelfish" => "babel.pl",
51 ### THIS IS NOT LOADED ON RELOAD :(
53 my @myModulesReloadNot;
55 @myModulesLoadNow = ('topic', 'uptime', 'news', 'rootWarn', 'symdump');
56 @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
60 my @mods = &getPerlFiles($bot_src_dir);
62 &status("Loading ".scalar(@mods)." CORE modules...");
64 foreach (sort @mods) {
65 my $mod = "$bot_src_dir/$_";
67 eval "require \"$mod\"";
74 $moduleAge{$mod} = (stat $mod)[9];
75 &showProc(" ($_)") if (&IsParam("DEBUG"));
80 my $f = "$bot_src_dir/modules.pl";
81 $moduleAge{$f} = (stat $f)[9];
83 if ($param{'DBType'} =~ /^mysql$/i) {
86 &ERROR("libdbd-mysql-perl is not installed!");
89 &status("Loading MySQL support.");
90 $f = "$bot_src_dir/db_mysql.pl";
92 $moduleAge{$f} = (stat $f)[9];
93 &showProc(" (DBI // mysql)");
94 } elsif ($param{'DBType'} =~ /^pgsql$/i) {
98 &ERROR("libpgperl is not installed!");
101 &status("Loading pgsql support.");
102 require "$bot_src_dir/db_pgsql.pl";
103 &showProc(" (pgsql)");
104 } elsif ($param{'DBType'} =~ /^dbm$/i) {
105 &status("Loading Berkeley DBM support.");
106 $f = "$bot_src_dir/db_dbm.pl";
108 $moduleAge{$f} = (stat $f)[9];
109 &showProc(" $bot_src_dir/db_dbm.pl");
111 &status("DB support DISABLED.");
116 sub loadFactoidsModules {
117 if (!&IsParam("factoids")) {
118 &status("Factoid support DISABLED.");
122 &status("Loading Factoids modules...");
124 foreach ( &getPerlFiles("$bot_src_dir/Factoids") ) {
125 my $mod = "$bot_src_dir/Factoids/$_";
127 eval "require \"$mod\"";
133 $moduleAge{$mod} = (stat $mod)[9];
134 &showProc(" ($_)") if (&IsParam("DEBUG"));
139 my ($interface) = &whatInterface();
140 if ($interface =~ /IRC/) {
141 &status("Loading IRC modules...");
145 &ERROR("libnet-irc-perl is not installed!");
148 &showProc(" (Net::IRC)");
150 &status("IRC support DISABLED.");
156 foreach ( &getPerlFiles("$bot_src_dir/$interface") ) {
157 my $mod = "$bot_src_dir/$interface/$_";
159 &status("Loading Modules \"$mod\"");
160 eval "require \"$mod\"";
162 &ERROR("require \"$mod\" => $@");
167 $moduleAge{$mod} = (stat $mod)[9];
168 &showProc(" ($_)") if (&IsParam("DEBUG"));
172 sub loadMyModulesNow {
176 &status("Loading MyModules...");
177 foreach (@myModulesLoadNow) {
180 &WARN("mMLN: null element.");
184 if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
185 if (exists $myModules{$_}) {
186 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
188 &DEBUG("myModule: $_ (2) not loaded.");
194 # weird hack to get rootwarn to work.
195 # it may break on other cases though, any ideas?
196 &loadMyModule( $myModules{$_} || $myModules{lc $_} );
200 &status("Module: Runtime: Loaded/Total [$loaded/$total]");
203 ### rename to moduleReloadAll?
204 sub reloadAllModules {
205 &VERB("Module: reloading all.",2);
207 # obscure usage of map and regex :)
208 foreach (map { s/.*?\/?src/src/; $_ } keys %moduleAge) {
212 &VERB("Module: reloading done.",2);
215 ### rename to modulesReload?
218 my $file = (grep /\/$mod/, keys %INC)[0];
220 # don't reload if it's not our module.
221 if ($mod =~ /::/ or $mod !~ /pl$/) {
222 &VERB("Not reloading $mod.",3);
226 if (!defined $file) {
227 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
232 &ERROR("rM: file '$file' does not exist?");
236 if (grep /$mod/, @myModulesReloadNot) {
237 &DEBUG("rM: should not reload $mod");
241 my $age = (stat $file)[9];
243 if (!exists $moduleAge{$file}) {
244 &DEBUG("Looks like $file was not loaded; fixing.");
246 return if ($age == $moduleAge{$file});
248 if ($age < $moduleAge{$file}) {
249 &WARN("rM: we're not gonna downgrade '$file'; use touch.");
250 &DEBUG("age => $age");
251 &DEBUG("mA{$file} => $moduleAge{$file}");
255 my $dc = &Time2String($age - $moduleAge{$file});
256 my $ago = &Time2String(time() - $moduleAge{$file});
258 &VERB("Module: delta change: $dc",2);
259 &VERB("Module: ago: $ago",2);
262 &status("Module: Loading $mod...");
265 eval "require \"$file\""; # require or use?
267 &DEBUG("rM: failure: @$");
269 my $basename = $file;
270 $basename =~ s/^.*\///;
271 &status("Module: reloaded $basename");
272 $moduleAge{$file} = $age;
277 ### OPTIONAL MODULES.
280 my %perlModulesLoaded = ();
281 my %perlModulesMissing = ();
284 return 0 if (exists $perlModulesMissing{$_[0]});
285 &reloadModule($_[0]);
286 return 1 if (exists $perlModulesLoaded{$_[0]});
290 &WARN("Module: $_[0] is not installed!");
291 $perlModulesMissing{$_[0]} = 1;
294 $perlModulesLoaded{$_[0]} = 1;
295 &status("Module: Loaded $_[0] ...");
296 &showProc(" ($_[0])");
304 &WARN("loadMyModule: module is NULL.");
308 my ($modulename, $modulebase);
309 if (exists $myModules{$tmp}) {
310 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
313 if ($tmp = grep /^$modulebase$/, keys %myModules) {
314 &DEBUG("lMM: lame hack, file => name => $tmp.");
318 my $modulefile = "$bot_src_dir/Modules/$modulebase";
320 # call reloadModule() which checks age of file and reload.
321 if (grep /\/$modulebase$/, keys %INC) {
322 &reloadModule($modulebase);
323 return 1; # depend on reloadModule?
326 if (! -f $modulefile) {
327 &ERROR("lMM: module ($modulebase) does not exist.");
328 if ($$ == $bot_pid) { # parent.
329 &shutdown() if (defined $shm and defined $dbh);
331 &DEBUG("b4 delfork 1");
332 &delForked($modulebase);
338 eval "require \"$modulefile\"";
340 &ERROR("cannot load my module: $modulebase");
341 if ($bot_pid != $$) { # child.
342 &DEBUG("b4 delfork 2");
343 &delForked($modulebase);
349 $moduleAge{$modulefile} = (stat $modulefile)[9];
351 &status("myModule: Loaded $modulebase ...");
352 &showProc(" ($modulebase)");
358 eval "use Time::HiRes qw(gettimeofday tv_interval)";
360 &WARN("No Time::HiRes?");
363 &showProc(" (Time::HiRes)");
366 if (!defined $AUTOLOAD and defined $::AUTOLOAD) {
367 &DEBUG("AUTOLOAD: hrm.. ::AUTOLOAD defined!");
369 return unless (defined $AUTOLOAD);
370 return if ($AUTOLOAD =~ /__/); # internal.
372 my $str = join(', ', @_);
373 my ($package, $filename, $line) = caller;
374 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD ($str) $filename line $line");
376 $AUTOLOAD =~ s/^(\S+):://g;
378 if (exists $myModules{lc $AUTOLOAD}) {
379 # hopefully this will work.
380 &DEBUG("Trying to load module $AUTOLOAD...");
381 &loadMyModule(lc $AUTOLOAD);
388 if (!opendir(DIR, $dir)) {
389 &ERROR("Cannot open source directory ($dir): $!");
394 while (defined(my $file = readdir DIR)) {
395 next unless $file =~ /\.pl$/;
396 next unless $file =~ /^[A-Z]/;
401 return reverse sort @mods;