2 # modules.pl: pseudo-Module handler
4 # Version: v0.2 (20000629)
10 use vars qw($AUTOLOAD);
16 eval "use IO::Socket";
18 &ERROR("no IO::Socket?");
21 &showProc(" (IO::Socket)");
25 "bzflag" => "BZFlag.pl",
26 "countdown" => "Countdown.pl",
27 "debian" => "Debian.pl",
28 "debianExtra" => "DebianExtra.pl",
30 "dumpvars" => "DumpVars.pl",
31 "symdump" => "DumpVars2.pl",
32 "exchange" => "Exchange.pl",
33 "factoids" => "Factoids.pl",
34 "freshmeat" => "Freshmeat.pl",
35 "kernel" => "Kernel.pl",
36 "perlMath" => "Math.pl",
39 "quote" => "Quote.pl",
40 "rootwarn" => "RootWarn.pl",
41 "search" => "Search.pl",
42 "slashdot" => "Slashdot3.pl",
43 "topic" => "Topic.pl",
44 "units" => "Units.pl",
45 "uptime" => "Uptime.pl",
46 "ircdcc" => "UserDCC.pl",
47 "userinfo" => "UserInfo.pl",
48 "weather" => "Weather.pl",
49 "wwwsearch" => "W3Search.pl",
50 "whatis" => "WhatIs.pl",
51 "wingate" => "Wingate.pl",
52 "babelfish" => "babel.pl",
53 "insult" => "insult.pl",
54 "nickometer" => "nickometer.pl",
56 "zippy" => "Zippy.pl",
58 "botmail" => "botmail.pl",
60 ### THIS IS NOT LOADED ON RELOAD :(
62 my @myModulesReloadNot;
64 @myModulesLoadNow = ('topic', 'uptime', 'news', 'rootWarn', 'symdump', 'botmail');
65 @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
69 my @mods = &getPerlFiles($bot_src_dir);
71 &status("Loading CORE modules...");
73 foreach (sort @mods) {
74 my $mod = "$bot_src_dir/$_";
76 eval "require \"$mod\"";
83 $moduleAge{$mod} = (stat $mod)[9];
84 &showProc(" ($_)") if (&IsParam("DEBUG"));
90 # todo: use function to load module.
92 if ($param{'DBType'} =~ /^(mysql|SQLite|pgsql)$/i) {
95 &ERROR("No support for DBI::" . $param{'DBType'} . ", exiting!");
98 &status("Loading " . $param{'DBType'} . " support.");
99 $f = "$bot_src_dir/dbi.pl";
101 $moduleAge{$f} = (stat $f)[9];
103 &showProc(" (DBI::" . $param{'DBType'} . ")");
105 &WARN("DB support DISABLED.");
110 sub loadFactoidsModules {
111 if (!&IsParam("factoids")) {
112 &status("Factoid support DISABLED.");
116 &status("Loading Factoids modules...");
118 foreach ( &getPerlFiles("$bot_src_dir/Factoids") ) {
119 my $mod = "$bot_src_dir/Factoids/$_";
121 eval "require \"$mod\"";
127 $moduleAge{$mod} = (stat $mod)[9];
128 &showProc(" ($_)") if (&IsParam("DEBUG"));
133 my ($interface) = &whatInterface();
134 if ($interface =~ /IRC/) {
135 &status("Loading IRC modules...");
139 &ERROR("libnet-irc-perl is not installed!");
142 &showProc(" (Net::IRC)");
144 &status("IRC support DISABLED.");
145 # disabling forking. Why?
146 #$param{forking} = 0;
150 foreach ( &getPerlFiles("$bot_src_dir/$interface") ) {
151 my $mod = "$bot_src_dir/$interface/$_";
153 # hrm... use another config option besides DEBUG to display
154 # change in memory usage.
155 &status("Loading Modules \"$mod\"") if (!&IsParam("DEBUG"));
156 eval "require \"$mod\"";
158 &ERROR("require \"$mod\" => $@");
163 $moduleAge{$mod} = (stat $mod)[9];
164 &showProc(" ($_)") if (&IsParam("DEBUG"));
168 sub loadMyModulesNow {
172 &status("Loading MyModules...");
173 foreach (@myModulesLoadNow) {
176 &WARN("mMLN: null element.");
180 if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
181 if (exists $myModules{$_}) {
182 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
184 &DEBUG("myModule: $_ (2) not loaded.");
190 # weird hack to get rootwarn to work.
191 # it may break on other cases though, any ideas?
192 &loadMyModule( $myModules{$_} || $myModules{lc $_} );
196 &status("Module: Runtime: Loaded/Total [$loaded/$total]");
199 ### rename to moduleReloadAll?
200 sub reloadAllModules {
201 &VERB("Module: reloading all.",2);
203 # obscure usage of map and regex :)
204 foreach (map { s/.*?\/?src/src/; $_ } keys %moduleAge) {
208 &VERB("Module: reloading done.",2);
211 ### rename to modulesReload?
214 my $file = (grep /\/$mod/, keys %INC)[0];
216 # don't reload if it's not our module.
217 if ($mod =~ /::/ or $mod !~ /pl$/) {
218 &VERB("Not reloading $mod.",3);
222 if (!defined $file) {
223 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
228 &ERROR("rM: file '$file' does not exist?");
232 if (grep /$mod/, @myModulesReloadNot) {
233 &DEBUG("rM: should not reload $mod");
237 my $age = (stat $file)[9];
239 if (!exists $moduleAge{$file}) {
240 &DEBUG("Looks like $file was not loaded; fixing.");
242 return if ($age == $moduleAge{$file});
244 if ($age < $moduleAge{$file}) {
245 &WARN("rM: we're not gonna downgrade '$file'; use touch.");
246 &DEBUG("age => $age");
247 &DEBUG("mA{$file} => $moduleAge{$file}");
251 my $dc = &Time2String($age - $moduleAge{$file});
252 my $ago = &Time2String(time() - $moduleAge{$file});
254 &VERB("Module: delta change: $dc",2);
255 &VERB("Module: ago: $ago",2);
258 &status("Module: Loading $mod...");
261 eval "require \"$file\""; # require or use?
263 &DEBUG("rM: failure: @$");
265 my $basename = $file;
266 $basename =~ s/^.*\///;
267 &status("Module: reloaded $basename");
268 $moduleAge{$file} = $age;
273 ### OPTIONAL MODULES.
276 my %perlModulesLoaded = ();
277 my %perlModulesMissing = ();
280 return 0 if (exists $perlModulesMissing{$_[0]});
281 &reloadModule($_[0]);
282 return 1 if (exists $perlModulesLoaded{$_[0]});
286 &WARN("Module: $_[0] is not installed!");
287 $perlModulesMissing{$_[0]} = 1;
290 $perlModulesLoaded{$_[0]} = 1;
291 &status("Module: Loaded $_[0] ...");
292 &showProc(" ($_[0])");
300 &WARN("loadMyModule: module is NULL.");
304 my ($modulename, $modulebase);
305 if (exists $myModules{$tmp}) {
306 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
309 if ($tmp = grep /^$modulebase$/, keys %myModules) {
310 &DEBUG("lMM: lame hack, file => name => $tmp.");
314 my $modulefile = "$bot_src_dir/Modules/$modulebase";
316 # call reloadModule() which checks age of file and reload.
317 if (grep /\/$modulebase$/, keys %INC) {
318 &reloadModule($modulebase);
319 return 1; # depend on reloadModule?
322 if (! -f $modulefile) {
323 &ERROR("lMM: module ($modulebase) does not exist.");
324 if ($$ == $bot_pid) { # parent.
325 &shutdown() if (defined $shm and defined $dbh);
327 &DEBUG("b4 delfork 1");
328 &delForked($modulename);
334 eval "require \"$modulefile\"";
336 &ERROR("cannot load my module: $modulebase");
337 if ($bot_pid != $$) { # child.
338 &DEBUG("b4 delfork 2");
339 &delForked($modulename);
345 $moduleAge{$modulefile} = (stat $modulefile)[9];
347 &status("myModule: Loaded $modulebase ...");
348 &showProc(" ($modulebase)");
354 eval "use Time::HiRes qw(gettimeofday tv_interval)";
356 &WARN("No Time::HiRes?");
359 &showProc(" (Time::HiRes)");
362 if (!defined $AUTOLOAD and defined $::AUTOLOAD) {
363 &DEBUG("AUTOLOAD: hrm.. ::AUTOLOAD defined!");
365 return unless (defined $AUTOLOAD);
366 return if ($AUTOLOAD =~ /__/); # internal.
368 my $str = join(', ', @_);
369 my ($package, $filename, $line) = caller;
370 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD ($str) $filename line $line");
372 $AUTOLOAD =~ s/^(\S+):://g;
374 if (exists $myModules{lc $AUTOLOAD}) {
375 # hopefully this will work.
376 &DEBUG("Trying to load module $AUTOLOAD...");
377 &loadMyModule(lc $AUTOLOAD);
384 if (!opendir(DIR, $dir)) {
385 &ERROR("Cannot open source directory ($dir): $!");
390 while (defined(my $file = readdir DIR)) {
391 next unless $file =~ /\.pl$/;
392 next unless $file =~ /^[A-Z]/;
397 return reverse sort @mods;