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 1.85/2.0 support.");
113 &ERROR("dbm support is broken... if you want it, fix it yourself!");
117 # require "$bot_src_dir/db_dbm.pl";
120 &status("DB support DISABLED.");
125 sub loadFactoidsModules {
126 if (!&IsParam("factoids")) {
127 &status("Factoid support DISABLED.");
131 &status("Loading Factoids modules...");
133 foreach ( &getPerlFiles("$bot_src_dir/Factoids") ) {
134 my $mod = "$bot_src_dir/Factoids/$_";
136 eval "require \"$mod\"";
142 $moduleAge{$mod} = (stat $mod)[9];
143 &showProc(" ($_)") if (&IsParam("DEBUG"));
148 if (&whatInterface() =~ /IRC/) {
149 &status("Loading IRC modules...");
153 &ERROR("libnet-irc-perl is not installed!");
156 &showProc(" (Net::IRC)");
159 &status("IRC support DISABLED.");
163 foreach ( &getPerlFiles("$bot_src_dir/IRC") ) {
164 my $mod = "$bot_src_dir/IRC/$_";
166 eval "require \"$mod\"";
168 &ERROR("lIRCM => $@");
173 $moduleAge{$mod} = (stat $mod)[9];
174 &showProc(" ($_)") if (&IsParam("DEBUG"));
178 sub loadMyModulesNow {
182 &status("Loading MyModules...");
183 foreach (@myModulesLoadNow) {
186 &WARN("mMLN: null element.");
190 if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
191 if (exists $myModules{$_}) {
192 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
194 &DEBUG("myModule: $_ (2) not loaded.");
200 # weird hack to get rootwarn to work.
201 # it may break on other cases though, any ideas?
202 &loadMyModule( $myModules{$_} || $myModules{lc $_} );
206 &status("Module: Runtime: Loaded/Total [$loaded/$total]");
209 ### rename to moduleReloadAll?
210 sub reloadAllModules {
211 &VERB("Module: reloading all.",2);
213 # obscure usage of map and regex :)
214 foreach (map { s/.*?\/?src/src/; $_ } keys %moduleAge) {
218 &VERB("Module: reloading done.",2);
221 ### rename to modulesReload?
224 my $file = (grep /\/$mod/, keys %INC)[0];
226 # don't reload if it's not our module.
227 if ($mod =~ /::/ or $mod !~ /pl$/) {
228 &VERB("Not reloading $mod.",3);
232 if (!defined $file) {
233 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
238 &ERROR("rM: file '$file' does not exist?");
242 if (grep /$mod/, @myModulesReloadNot) {
243 &DEBUG("rM: should not reload $mod");
247 my $age = (stat $file)[9];
249 if (!exists $moduleAge{$file}) {
250 &DEBUG("Looks like $file was not loaded; fixing.");
252 return if ($age == $moduleAge{$file});
254 if ($age < $moduleAge{$file}) {
255 &WARN("rM: we're not gonna downgrade '$file'; use touch.");
256 &DEBUG("age => $age");
257 &DEBUG("mA{$file} => $moduleAge{$file}");
261 my $dc = &Time2String($age - $moduleAge{$file});
262 my $ago = &Time2String(time() - $moduleAge{$file});
264 &VERB("Module: delta change: $dc",2);
265 &VERB("Module: ago: $ago",2);
268 &status("Module: Loading $mod...");
271 eval "require \"$file\""; # require or use?
273 &DEBUG("rM: failure: @$");
275 my $basename = $file;
276 $basename =~ s/^.*\///;
277 &status("Module: reloaded $basename");
278 $moduleAge{$file} = $age;
283 ### OPTIONAL MODULES.
286 my %perlModulesLoaded = ();
287 my %perlModulesMissing = ();
290 return 0 if (exists $perlModulesMissing{$_[0]});
291 &reloadModule($_[0]);
292 return 1 if (exists $perlModulesLoaded{$_[0]});
296 &WARN("Module: $_[0] is not installed!");
297 $perlModulesMissing{$_[0]} = 1;
300 $perlModulesLoaded{$_[0]} = 1;
301 &status("Module: Loaded $_[0] ...");
302 &showProc(" ($_[0])");
310 &WARN("loadMyModule: module is NULL.");
314 my ($modulename, $modulebase);
315 if (exists $myModules{$tmp}) {
316 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
319 if ($tmp = grep /^$modulebase$/, keys %myModules) {
320 &DEBUG("lMM: lame hack, file => name => $tmp.");
324 my $modulefile = "$bot_src_dir/Modules/$modulebase";
326 # call reloadModule() which checks age of file and reload.
327 if (grep /\/$modulebase$/, keys %INC) {
328 &reloadModule($modulebase);
329 return 1; # depend on reloadModule?
332 if (! -f $modulefile) {
333 &ERROR("lMM: module ($modulebase) does not exist.");
334 if ($$ == $bot_pid) { # parent.
335 &shutdown() if (defined $shm and defined $dbh);
337 &DEBUG("b4 delfork 1");
338 &delForked($modulebase);
344 eval "require \"$modulefile\"";
346 &ERROR("cannot load my module: $modulebase");
347 if ($bot_pid != $$) { # child.
348 &DEBUG("b4 delfork 2");
349 &delForked($modulebase);
355 $moduleAge{$modulefile} = (stat $modulefile)[9];
357 &status("myModule: Loaded $modulebase ...");
358 &showProc(" ($modulebase)");
364 eval "use Time::HiRes qw(gettimeofday tv_interval)";
366 &WARN("No Time::HiRes?");
369 &showProc(" (Time::HiRes)");
372 if (!defined $AUTOLOAD and defined $::AUTOLOAD) {
373 &DEBUG("AUTOLOAD: hrm.. ::AUTOLOAD defined!");
375 return unless (defined $AUTOLOAD);
376 return if ($AUTOLOAD =~ /__/); # internal.
378 my $str = join(', ', @_);
379 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD ($str)");
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;