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 "babelfish" => "babel.pl",
26 "botmail" => "botmail.pl",
27 "bzflag" => "BZFlag.pl",
28 "countdown" => "Countdown.pl",
29 "debian" => "Debian.pl",
30 "debianExtra" => "DebianExtra.pl",
32 "dumpvars" => "DumpVars.pl",
33 "exchange" => "Exchange.pl",
34 "factoids" => "Factoids.pl",
35 "httpdtype" => "HTTPDtype.pl",
36 "insult" => "insult.pl",
37 "ircdcc" => "UserDCC.pl",
38 "kernel" => "Kernel.pl",
40 "nickometer" => "nickometer.pl",
41 "perlMath" => "Math.pl",
43 "quote" => "Quote.pl",
44 "rootwarn" => "RootWarn.pl",
46 "search" => "Search.pl",
47 "slashdot" => "Slashdot3.pl",
48 "symdump" => "DumpVars2.pl",
49 "topic" => "Topic.pl",
50 "units" => "Units.pl",
51 "uptime" => "Uptime.pl",
52 "userinfo" => "UserInfo.pl",
53 "weather" => "Weather.pl",
54 "whatis" => "WhatIs.pl",
55 "wikipedia" => "wikipedia.pl",
56 "wingate" => "Wingate.pl",
57 "wwwsearch" => "W3Search.pl",
59 "zippy" => "Zippy.pl",
62 ### THIS IS NOT LOADED ON RELOAD :(
64 my @myModulesReloadNot;
66 @myModulesLoadNow = ('topic', 'uptime', 'news', 'rootWarn', 'symdump', 'botmail');
67 @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
71 my @mods = &getPerlFiles($bot_src_dir);
73 &status("Loading CORE modules...");
75 foreach (sort @mods) {
76 my $mod = "$bot_src_dir/$_";
78 eval "require \"$mod\"";
85 $moduleAge{$mod} = (stat $mod)[9];
86 &showProc(" ($_)") if (&IsParam("DEBUG"));
92 # TODO: use function to load module.
94 if ($param{'DBType'} =~ /^(mysql|SQLite|pgsql)$/i) {
97 &ERROR("No support for DBI::" . $param{'DBType'} . ", exiting!");
100 &status("Loading " . $param{'DBType'} . " support.");
101 $f = "$bot_src_dir/dbi.pl";
103 $moduleAge{$f} = (stat $f)[9];
105 &showProc(" (DBI::" . $param{'DBType'} . ")");
107 &WARN("DB support DISABLED.");
112 sub loadFactoidsModules {
113 if (!&IsParam("factoids")) {
114 &status("Factoid support DISABLED.");
118 &status("Loading Factoids modules...");
120 foreach ( &getPerlFiles("$bot_src_dir/Factoids") ) {
121 my $mod = "$bot_src_dir/Factoids/$_";
123 eval "require \"$mod\"";
129 $moduleAge{$mod} = (stat $mod)[9];
130 &showProc(" ($_)") if (&IsParam("DEBUG"));
135 my ($interface) = &whatInterface();
136 if ($interface =~ /IRC/) {
137 &status("Loading IRC modules...");
141 &ERROR("libnet-irc-perl is not installed!");
144 &showProc(" (Net::IRC)");
146 &status("IRC support DISABLED.");
147 # disabling forking. Why?
148 #$param{forking} = 0;
152 foreach ( &getPerlFiles("$bot_src_dir/$interface") ) {
153 my $mod = "$bot_src_dir/$interface/$_";
155 # hrm... use another config option besides DEBUG to display
156 # change in memory usage.
157 &status("Loading Modules \"$mod\"") if (!&IsParam("DEBUG"));
158 eval "require \"$mod\"";
160 &ERROR("require \"$mod\" => $@");
165 $moduleAge{$mod} = (stat $mod)[9];
166 &showProc(" ($_)") if (&IsParam("DEBUG"));
170 sub loadMyModulesNow {
174 &status("Loading MyModules...");
175 foreach (@myModulesLoadNow) {
178 &WARN("mMLN: null element.");
182 if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
183 if (exists $myModules{$_}) {
184 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
186 &DEBUG("myModule: $_ (2) not loaded.");
192 # weird hack to get rootwarn to work.
193 # it may break on other cases though, any ideas?
194 &loadMyModule( $myModules{$_} || $myModules{lc $_} );
198 &status("Module: Runtime: Loaded/Total [$loaded/$total]");
201 ### rename to moduleReloadAll?
202 sub reloadAllModules {
205 &VERB("Module: reloading all.",2);
207 # obscure usage of map and regex :)
208 foreach (map { s/.*?\/?src/src/; $_ } keys %moduleAge) {
209 $retval .= &reloadModule($_);
212 &VERB("Module: reloading done.",2);
216 ### rename to modulesReload?
219 my $file = (grep /\/$mod/, keys %INC)[0];
222 # don't reload if it's not our module.
223 if ($mod =~ /::/ or $mod !~ /pl$/) {
224 &VERB("Not reloading $mod.",3);
228 if (!defined $file) {
229 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
234 &ERROR("rM: file '$file' does not exist?");
238 if (grep /$mod/, @myModulesReloadNot) {
239 &DEBUG("rM: should not reload $mod");
243 my $age = (stat $file)[9];
245 if (!exists $moduleAge{$file}) {
246 &DEBUG("Looks like $file was not loaded; fixing.");
248 return $retval if ($age == $moduleAge{$file});
250 if ($age < $moduleAge{$file}) {
251 &WARN("rM: we're not gonna downgrade '$file'; use touch.");
252 &DEBUG("age => $age");
253 &DEBUG("mA{$file} => $moduleAge{$file}");
257 my $dc = &Time2String($age - $moduleAge{$file});
258 my $ago = &Time2String(time() - $moduleAge{$file});
260 &VERB("Module: delta change: $dc",2);
261 &VERB("Module: ago: $ago",2);
264 &status("Module: Loading $mod...");
267 eval "require \"$file\""; # require or use?
269 &DEBUG("rM: failure: @$ ");
271 my $basename = $file;
272 $basename =~ s/^.*\///;
273 &status("Module: reloaded $basename");
274 $retval = " $basename";
275 $moduleAge{$file} = $age;
281 ### OPTIONAL MODULES.
284 my %perlModulesLoaded = ();
285 my %perlModulesMissing = ();
288 return 0 if (exists $perlModulesMissing{$_[0]});
289 &reloadModule($_[0]);
290 return 1 if (exists $perlModulesLoaded{$_[0]});
294 &WARN("Module: $_[0] is not installed!");
295 $perlModulesMissing{$_[0]} = 1;
298 $perlModulesLoaded{$_[0]} = 1;
299 &status("Loaded $_[0]");
300 &showProc(" ($_[0])");
308 &WARN("loadMyModule: module is NULL.");
312 my ($modulename, $modulebase);
313 if (exists $myModules{$tmp}) {
314 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
317 if ($tmp = grep /^$modulebase$/, keys %myModules) {
318 &DEBUG("lMM: lame hack, file => name => $tmp.");
322 my $modulefile = "$bot_src_dir/Modules/$modulebase";
324 # call reloadModule() which checks age of file and reload.
325 if (grep /\/$modulebase$/, keys %INC) {
326 &reloadModule($modulebase);
327 return 1; # depend on reloadModule?
330 if (! -f $modulefile) {
331 &ERROR("lMM: module ($modulebase) does not exist.");
332 if ($$ == $bot_pid) { # parent.
333 &shutdown() if (defined $shm and defined $dbh);
335 &DEBUG("b4 delfork 1");
336 &delForked($modulename);
342 eval "require \"$modulefile\"";
344 &ERROR("cannot load my module: $modulebase");
345 if ($bot_pid != $$) { # child.
346 &DEBUG("b4 delfork 2");
347 &delForked($modulename);
353 $moduleAge{$modulefile} = (stat $modulefile)[9];
355 &status("Loaded $modulebase");
356 &showProc(" ($modulebase)");
362 eval "use Time::HiRes qw(gettimeofday tv_interval)";
364 &WARN("No Time::HiRes?");
367 &showProc(" (Time::HiRes)");
370 if (!defined $AUTOLOAD and defined $::AUTOLOAD) {
371 &DEBUG("AUTOLOAD: hrm.. ::AUTOLOAD defined!");
373 return unless (defined $AUTOLOAD);
374 return if ($AUTOLOAD =~ /__/); # internal.
376 my $str = join(', ', @_);
377 my ($package, $filename, $line) = caller;
378 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD ($str) $filename line $line");
380 $AUTOLOAD =~ s/^(\S+):://g;
382 if (exists $myModules{lc $AUTOLOAD}) {
383 # hopefully this will work.
384 &DEBUG("Trying to load module $AUTOLOAD...");
385 &loadMyModule(lc $AUTOLOAD);
392 if (!opendir(DIR, $dir)) {
393 &ERROR("Cannot open source directory ($dir): $!");
398 while (defined(my $file = readdir DIR)) {
399 next unless $file =~ /\.pl$/;
400 next unless $file =~ /^[A-Z]/;
405 return reverse sort @mods;