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 &status("Loading DB modules...");
82 my $f = "$bot_src_dir/modules.pl";
83 $moduleAge{$f} = (stat $f)[9];
85 if ($param{'DBType'} =~ /^mysql$/i) {
88 &ERROR("libdbd-mysql-perl is not installed!");
91 &showProc(" (DBI // mysql)");
93 &status(" using MySQL support.");
94 $f = "$bot_src_dir/db_mysql.pl";
96 $moduleAge{$f} = (stat $f)[9];
98 } elsif ($param{'DBType'} =~ /^pgsql$/i) {
102 &ERROR("libpgperl is not installed!");
105 &showProc(" (pgsql)");
107 &status(" using pgsql support.");
108 require "$bot_src_dir/db_pgsql.pl";
110 } elsif ($param{'DBType'} =~ /^dbm$/i) {
112 &status(" using Berkeley DBM support.");
113 require "$bot_src_dir/db_dbm.pl";
116 &status("DB support DISABLED.");
121 sub loadFactoidsModules {
122 if (!&IsParam("factoids")) {
123 &status("Factoid support DISABLED.");
127 &status("Loading Factoids modules...");
129 foreach ( &getPerlFiles("$bot_src_dir/Factoids") ) {
130 my $mod = "$bot_src_dir/Factoids/$_";
132 eval "require \"$mod\"";
138 $moduleAge{$mod} = (stat $mod)[9];
139 &showProc(" ($_)") if (&IsParam("DEBUG"));
144 my ($interface) = &whatInterface();
145 if ($interface =~ /IRC/) {
146 &status("Loading IRC modules...");
150 &ERROR("libnet-irc-perl is not installed!");
153 &showProc(" (Net::IRC)");
155 &status("IRC support DISABLED.");
161 foreach ( &getPerlFiles("$bot_src_dir/$interface") ) {
162 my $mod = "$bot_src_dir/$interface/$_";
164 &status("Loading Modules \"$mod\"");
165 eval "require \"$mod\"";
167 &ERROR("require \"$mod\" => $@");
172 $moduleAge{$mod} = (stat $mod)[9];
173 &showProc(" ($_)") if (&IsParam("DEBUG"));
177 sub loadMyModulesNow {
181 &status("Loading MyModules...");
182 foreach (@myModulesLoadNow) {
185 &WARN("mMLN: null element.");
189 if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
190 if (exists $myModules{$_}) {
191 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
193 &DEBUG("myModule: $_ (2) not loaded.");
199 # weird hack to get rootwarn to work.
200 # it may break on other cases though, any ideas?
201 &loadMyModule( $myModules{$_} || $myModules{lc $_} );
205 &status("Module: Runtime: Loaded/Total [$loaded/$total]");
208 ### rename to moduleReloadAll?
209 sub reloadAllModules {
210 &VERB("Module: reloading all.",2);
212 # obscure usage of map and regex :)
213 foreach (map { s/.*?\/?src/src/; $_ } keys %moduleAge) {
217 &VERB("Module: reloading done.",2);
220 ### rename to modulesReload?
223 my $file = (grep /\/$mod/, keys %INC)[0];
225 # don't reload if it's not our module.
226 if ($mod =~ /::/ or $mod !~ /pl$/) {
227 &VERB("Not reloading $mod.",3);
231 if (!defined $file) {
232 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
237 &ERROR("rM: file '$file' does not exist?");
241 if (grep /$mod/, @myModulesReloadNot) {
242 &DEBUG("rM: should not reload $mod");
246 my $age = (stat $file)[9];
248 if (!exists $moduleAge{$file}) {
249 &DEBUG("Looks like $file was not loaded; fixing.");
251 return if ($age == $moduleAge{$file});
253 if ($age < $moduleAge{$file}) {
254 &WARN("rM: we're not gonna downgrade '$file'; use touch.");
255 &DEBUG("age => $age");
256 &DEBUG("mA{$file} => $moduleAge{$file}");
260 my $dc = &Time2String($age - $moduleAge{$file});
261 my $ago = &Time2String(time() - $moduleAge{$file});
263 &VERB("Module: delta change: $dc",2);
264 &VERB("Module: ago: $ago",2);
267 &status("Module: Loading $mod...");
270 eval "require \"$file\""; # require or use?
272 &DEBUG("rM: failure: @$");
274 my $basename = $file;
275 $basename =~ s/^.*\///;
276 &status("Module: reloaded $basename");
277 $moduleAge{$file} = $age;
282 ### OPTIONAL MODULES.
285 my %perlModulesLoaded = ();
286 my %perlModulesMissing = ();
289 return 0 if (exists $perlModulesMissing{$_[0]});
290 &reloadModule($_[0]);
291 return 1 if (exists $perlModulesLoaded{$_[0]});
295 &WARN("Module: $_[0] is not installed!");
296 $perlModulesMissing{$_[0]} = 1;
299 $perlModulesLoaded{$_[0]} = 1;
300 &status("Module: Loaded $_[0] ...");
301 &showProc(" ($_[0])");
309 &WARN("loadMyModule: module is NULL.");
313 my ($modulename, $modulebase);
314 if (exists $myModules{$tmp}) {
315 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
318 if ($tmp = grep /^$modulebase$/, keys %myModules) {
319 &DEBUG("lMM: lame hack, file => name => $tmp.");
323 my $modulefile = "$bot_src_dir/Modules/$modulebase";
325 # call reloadModule() which checks age of file and reload.
326 if (grep /\/$modulebase$/, keys %INC) {
327 &reloadModule($modulebase);
328 return 1; # depend on reloadModule?
331 if (! -f $modulefile) {
332 &ERROR("lMM: module ($modulebase) does not exist.");
333 if ($$ == $bot_pid) { # parent.
334 &shutdown() if (defined $shm and defined $dbh);
336 &DEBUG("b4 delfork 1");
337 &delForked($modulebase);
343 eval "require \"$modulefile\"";
345 &ERROR("cannot load my module: $modulebase");
346 if ($bot_pid != $$) { # child.
347 &DEBUG("b4 delfork 2");
348 &delForked($modulebase);
354 $moduleAge{$modulefile} = (stat $modulefile)[9];
356 &status("myModule: Loaded $modulebase ...");
357 &showProc(" ($modulebase)");
363 eval "use Time::HiRes qw(gettimeofday tv_interval)";
365 &WARN("No Time::HiRes?");
368 &showProc(" (Time::HiRes)");
371 if (!defined $AUTOLOAD and defined $::AUTOLOAD) {
372 &DEBUG("AUTOLOAD: hrm.. ::AUTOLOAD defined!");
374 return unless (defined $AUTOLOAD);
375 return if ($AUTOLOAD =~ /__/); # internal.
377 my $str = join(', ', @_);
378 my ($package, $filename, $line) = caller;
379 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD ($str) $filename line $line");
381 $AUTOLOAD =~ s/^(\S+):://g;
383 if (exists $myModules{lc $AUTOLOAD}) {
384 # hopefully this will work.
385 &DEBUG("Trying to load module $AUTOLOAD...");
386 &loadMyModule(lc $AUTOLOAD);
393 if (!opendir(DIR, $dir)) {
394 &ERROR("Cannot open source directory ($dir): $!");
399 while (defined(my $file = readdir DIR)) {
400 next unless $file =~ /\.pl$/;
401 next unless $file =~ /^[A-Z]/;
406 return reverse sort @mods;