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 "kernel" => "Kernel.pl",
35 "perlMath" => "Math.pl",
38 "quote" => "Quote.pl",
39 "rootwarn" => "RootWarn.pl",
40 "search" => "Search.pl",
41 "slashdot" => "Slashdot3.pl",
42 "topic" => "Topic.pl",
43 "units" => "Units.pl",
44 "uptime" => "Uptime.pl",
45 "ircdcc" => "UserDCC.pl",
46 "userinfo" => "UserInfo.pl",
47 "weather" => "Weather.pl",
48 "wwwsearch" => "W3Search.pl",
49 "whatis" => "WhatIs.pl",
50 "wingate" => "Wingate.pl",
51 "babelfish" => "babel.pl",
52 "insult" => "insult.pl",
53 "nickometer" => "nickometer.pl",
55 "zippy" => "Zippy.pl",
57 "botmail" => "botmail.pl",
59 ### THIS IS NOT LOADED ON RELOAD :(
61 my @myModulesReloadNot;
63 @myModulesLoadNow = ('topic', 'uptime', 'news', 'rootWarn', 'symdump', 'botmail');
64 @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
68 my @mods = &getPerlFiles($bot_src_dir);
70 &status("Loading CORE modules...");
72 foreach (sort @mods) {
73 my $mod = "$bot_src_dir/$_";
75 eval "require \"$mod\"";
82 $moduleAge{$mod} = (stat $mod)[9];
83 &showProc(" ($_)") if (&IsParam("DEBUG"));
89 # todo: use function to load module.
91 if ($param{'DBType'} =~ /^(mysql|SQLite|pgsql)$/i) {
94 &ERROR("No support for DBI::" . $param{'DBType'} . ", exiting!");
97 &status("Loading " . $param{'DBType'} . " support.");
98 $f = "$bot_src_dir/dbi.pl";
100 $moduleAge{$f} = (stat $f)[9];
102 &showProc(" (DBI::" . $param{'DBType'} . ")");
104 &WARN("DB support DISABLED.");
109 sub loadFactoidsModules {
110 if (!&IsParam("factoids")) {
111 &status("Factoid support DISABLED.");
115 &status("Loading Factoids modules...");
117 foreach ( &getPerlFiles("$bot_src_dir/Factoids") ) {
118 my $mod = "$bot_src_dir/Factoids/$_";
120 eval "require \"$mod\"";
126 $moduleAge{$mod} = (stat $mod)[9];
127 &showProc(" ($_)") if (&IsParam("DEBUG"));
132 my ($interface) = &whatInterface();
133 if ($interface =~ /IRC/) {
134 &status("Loading IRC modules...");
138 &ERROR("libnet-irc-perl is not installed!");
141 &showProc(" (Net::IRC)");
143 &status("IRC support DISABLED.");
144 # disabling forking. Why?
145 #$param{forking} = 0;
149 foreach ( &getPerlFiles("$bot_src_dir/$interface") ) {
150 my $mod = "$bot_src_dir/$interface/$_";
152 # hrm... use another config option besides DEBUG to display
153 # change in memory usage.
154 &status("Loading Modules \"$mod\"") if (!&IsParam("DEBUG"));
155 eval "require \"$mod\"";
157 &ERROR("require \"$mod\" => $@");
162 $moduleAge{$mod} = (stat $mod)[9];
163 &showProc(" ($_)") if (&IsParam("DEBUG"));
167 sub loadMyModulesNow {
171 &status("Loading MyModules...");
172 foreach (@myModulesLoadNow) {
175 &WARN("mMLN: null element.");
179 if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
180 if (exists $myModules{$_}) {
181 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
183 &DEBUG("myModule: $_ (2) not loaded.");
189 # weird hack to get rootwarn to work.
190 # it may break on other cases though, any ideas?
191 &loadMyModule( $myModules{$_} || $myModules{lc $_} );
195 &status("Module: Runtime: Loaded/Total [$loaded/$total]");
198 ### rename to moduleReloadAll?
199 sub reloadAllModules {
200 &VERB("Module: reloading all.",2);
202 # obscure usage of map and regex :)
203 foreach (map { s/.*?\/?src/src/; $_ } keys %moduleAge) {
207 &VERB("Module: reloading done.",2);
210 ### rename to modulesReload?
213 my $file = (grep /\/$mod/, keys %INC)[0];
215 # don't reload if it's not our module.
216 if ($mod =~ /::/ or $mod !~ /pl$/) {
217 &VERB("Not reloading $mod.",3);
221 if (!defined $file) {
222 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
227 &ERROR("rM: file '$file' does not exist?");
231 if (grep /$mod/, @myModulesReloadNot) {
232 &DEBUG("rM: should not reload $mod");
236 my $age = (stat $file)[9];
238 if (!exists $moduleAge{$file}) {
239 &DEBUG("Looks like $file was not loaded; fixing.");
241 return if ($age == $moduleAge{$file});
243 if ($age < $moduleAge{$file}) {
244 &WARN("rM: we're not gonna downgrade '$file'; use touch.");
245 &DEBUG("age => $age");
246 &DEBUG("mA{$file} => $moduleAge{$file}");
250 my $dc = &Time2String($age - $moduleAge{$file});
251 my $ago = &Time2String(time() - $moduleAge{$file});
253 &VERB("Module: delta change: $dc",2);
254 &VERB("Module: ago: $ago",2);
257 &status("Module: Loading $mod...");
260 eval "require \"$file\""; # require or use?
262 &DEBUG("rM: failure: @$");
264 my $basename = $file;
265 $basename =~ s/^.*\///;
266 &status("Module: reloaded $basename");
267 $moduleAge{$file} = $age;
272 ### OPTIONAL MODULES.
275 my %perlModulesLoaded = ();
276 my %perlModulesMissing = ();
279 return 0 if (exists $perlModulesMissing{$_[0]});
280 &reloadModule($_[0]);
281 return 1 if (exists $perlModulesLoaded{$_[0]});
285 &WARN("Module: $_[0] is not installed!");
286 $perlModulesMissing{$_[0]} = 1;
289 $perlModulesLoaded{$_[0]} = 1;
290 &status("Module: Loaded $_[0] ...");
291 &showProc(" ($_[0])");
299 &WARN("loadMyModule: module is NULL.");
303 my ($modulename, $modulebase);
304 if (exists $myModules{$tmp}) {
305 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
308 if ($tmp = grep /^$modulebase$/, keys %myModules) {
309 &DEBUG("lMM: lame hack, file => name => $tmp.");
313 my $modulefile = "$bot_src_dir/Modules/$modulebase";
315 # call reloadModule() which checks age of file and reload.
316 if (grep /\/$modulebase$/, keys %INC) {
317 &reloadModule($modulebase);
318 return 1; # depend on reloadModule?
321 if (! -f $modulefile) {
322 &ERROR("lMM: module ($modulebase) does not exist.");
323 if ($$ == $bot_pid) { # parent.
324 &shutdown() if (defined $shm and defined $dbh);
326 &DEBUG("b4 delfork 1");
327 &delForked($modulename);
333 eval "require \"$modulefile\"";
335 &ERROR("cannot load my module: $modulebase");
336 if ($bot_pid != $$) { # child.
337 &DEBUG("b4 delfork 2");
338 &delForked($modulename);
344 $moduleAge{$modulefile} = (stat $modulefile)[9];
346 &status("myModule: Loaded $modulebase ...");
347 &showProc(" ($modulebase)");
353 eval "use Time::HiRes qw(gettimeofday tv_interval)";
355 &WARN("No Time::HiRes?");
358 &showProc(" (Time::HiRes)");
361 if (!defined $AUTOLOAD and defined $::AUTOLOAD) {
362 &DEBUG("AUTOLOAD: hrm.. ::AUTOLOAD defined!");
364 return unless (defined $AUTOLOAD);
365 return if ($AUTOLOAD =~ /__/); # internal.
367 my $str = join(', ', @_);
368 my ($package, $filename, $line) = caller;
369 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD ($str) $filename line $line");
371 $AUTOLOAD =~ s/^(\S+):://g;
373 if (exists $myModules{lc $AUTOLOAD}) {
374 # hopefully this will work.
375 &DEBUG("Trying to load module $AUTOLOAD...");
376 &loadMyModule(lc $AUTOLOAD);
383 if (!opendir(DIR, $dir)) {
384 &ERROR("Cannot open source directory ($dir): $!");
389 while (defined(my $file = readdir DIR)) {
390 next unless $file =~ /\.pl$/;
391 next unless $file =~ /^[A-Z]/;
396 return reverse sort @mods;