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 "bzflag" => "BZFlag.pl",
25 "countdown" => "Countdown.pl",
26 "debian" => "Debian.pl",
27 "debianExtra" => "DebianExtra.pl",
29 "dumpvars" => "DumpVars.pl",
30 "symdump" => "DumpVars2.pl",
31 "exchange" => "Exchange.pl",
32 "factoids" => "Factoids.pl",
33 "freshmeat" => "Freshmeat.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",
58 ### THIS IS NOT LOADED ON RELOAD :(
60 my @myModulesReloadNot;
62 @myModulesLoadNow = ('topic', 'uptime', 'news', 'rootWarn', 'symdump');
63 @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
67 my @mods = &getPerlFiles($bot_src_dir);
69 &status("Loading ".scalar(@mods)." CORE modules...");
71 foreach (sort @mods) {
72 my $mod = "$bot_src_dir/$_";
74 eval "require \"$mod\"";
81 $moduleAge{$mod} = (stat $mod)[9];
82 &showProc(" ($_)") if (&IsParam("DEBUG"));
87 my $f = "$bot_src_dir/modules.pl";
88 $moduleAge{$f} = (stat $f)[9];
90 if ($param{'DBType'} =~ /^mysql$/i) {
93 &ERROR("libdbd-mysql-perl is not installed!");
96 &status("Loading MySQL support.");
97 $f = "$bot_src_dir/db_mysql.pl";
99 $moduleAge{$f} = (stat $f)[9];
100 &showProc(" (DBI // mysql)");
101 } elsif ($param{'DBType'} =~ /^pgsql$/i) {
105 &ERROR("libpgperl is not installed!");
108 &status("Loading pgsql support.");
109 require "$bot_src_dir/db_pgsql.pl";
110 &showProc(" (pgsql)");
111 } elsif ($param{'DBType'} =~ /^sqlite$|^dbm$/i) {
112 &status("Loading " . $param{'DBType'} . " support.");
113 $f="$bot_src_dir/db_" . $param{'DBType'} . ".pl";
114 $moduleAge{$f} = (stat $f)[9];
116 &showProc(" $bot_src_dir/db_" . $param{'DBType'} . ".pl");
118 &status("DB support DISABLED.");
123 sub loadFactoidsModules {
124 if (!&IsParam("factoids")) {
125 &status("Factoid support DISABLED.");
129 &status("Loading Factoids modules...");
131 foreach ( &getPerlFiles("$bot_src_dir/Factoids") ) {
132 my $mod = "$bot_src_dir/Factoids/$_";
134 eval "require \"$mod\"";
140 $moduleAge{$mod} = (stat $mod)[9];
141 &showProc(" ($_)") if (&IsParam("DEBUG"));
146 my ($interface) = &whatInterface();
147 if ($interface =~ /IRC/) {
148 &status("Loading IRC modules...");
152 &ERROR("libnet-irc-perl is not installed!");
155 &showProc(" (Net::IRC)");
157 &status("IRC support DISABLED.");
158 # disabling forking. Why?
159 #$param{forking} = 0;
163 foreach ( &getPerlFiles("$bot_src_dir/$interface") ) {
164 my $mod = "$bot_src_dir/$interface/$_";
166 # hrm... use another config option besides DEBUG to display
167 # change in memory usage.
168 &status("Loading Modules \"$mod\"") if (!&IsParam("DEBUG"));
169 eval "require \"$mod\"";
171 &ERROR("require \"$mod\" => $@");
176 $moduleAge{$mod} = (stat $mod)[9];
177 &showProc(" ($_)") if (&IsParam("DEBUG"));
181 sub loadMyModulesNow {
185 &status("Loading MyModules...");
186 foreach (@myModulesLoadNow) {
189 &WARN("mMLN: null element.");
193 if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
194 if (exists $myModules{$_}) {
195 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
197 &DEBUG("myModule: $_ (2) not loaded.");
203 # weird hack to get rootwarn to work.
204 # it may break on other cases though, any ideas?
205 &loadMyModule( $myModules{$_} || $myModules{lc $_} );
209 &status("Module: Runtime: Loaded/Total [$loaded/$total]");
212 ### rename to moduleReloadAll?
213 sub reloadAllModules {
214 &VERB("Module: reloading all.",2);
216 # obscure usage of map and regex :)
217 foreach (map { s/.*?\/?src/src/; $_ } keys %moduleAge) {
221 &VERB("Module: reloading done.",2);
224 ### rename to modulesReload?
227 my $file = (grep /\/$mod/, keys %INC)[0];
229 # don't reload if it's not our module.
230 if ($mod =~ /::/ or $mod !~ /pl$/) {
231 &VERB("Not reloading $mod.",3);
235 if (!defined $file) {
236 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
241 &ERROR("rM: file '$file' does not exist?");
245 if (grep /$mod/, @myModulesReloadNot) {
246 &DEBUG("rM: should not reload $mod");
250 my $age = (stat $file)[9];
252 if (!exists $moduleAge{$file}) {
253 &DEBUG("Looks like $file was not loaded; fixing.");
255 return if ($age == $moduleAge{$file});
257 if ($age < $moduleAge{$file}) {
258 &WARN("rM: we're not gonna downgrade '$file'; use touch.");
259 &DEBUG("age => $age");
260 &DEBUG("mA{$file} => $moduleAge{$file}");
264 my $dc = &Time2String($age - $moduleAge{$file});
265 my $ago = &Time2String(time() - $moduleAge{$file});
267 &VERB("Module: delta change: $dc",2);
268 &VERB("Module: ago: $ago",2);
271 &status("Module: Loading $mod...");
274 eval "require \"$file\""; # require or use?
276 &DEBUG("rM: failure: @$");
278 my $basename = $file;
279 $basename =~ s/^.*\///;
280 &status("Module: reloaded $basename");
281 $moduleAge{$file} = $age;
286 ### OPTIONAL MODULES.
289 my %perlModulesLoaded = ();
290 my %perlModulesMissing = ();
293 return 0 if (exists $perlModulesMissing{$_[0]});
294 &reloadModule($_[0]);
295 return 1 if (exists $perlModulesLoaded{$_[0]});
299 &WARN("Module: $_[0] is not installed!");
300 $perlModulesMissing{$_[0]} = 1;
303 $perlModulesLoaded{$_[0]} = 1;
304 &status("Module: Loaded $_[0] ...");
305 &showProc(" ($_[0])");
313 &WARN("loadMyModule: module is NULL.");
317 my ($modulename, $modulebase);
318 if (exists $myModules{$tmp}) {
319 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
322 if ($tmp = grep /^$modulebase$/, keys %myModules) {
323 &DEBUG("lMM: lame hack, file => name => $tmp.");
327 my $modulefile = "$bot_src_dir/Modules/$modulebase";
329 # call reloadModule() which checks age of file and reload.
330 if (grep /\/$modulebase$/, keys %INC) {
331 &reloadModule($modulebase);
332 return 1; # depend on reloadModule?
335 if (! -f $modulefile) {
336 &ERROR("lMM: module ($modulebase) does not exist.");
337 if ($$ == $bot_pid) { # parent.
338 &shutdown() if (defined $shm and defined $dbh);
340 &DEBUG("b4 delfork 1");
341 &delForked($modulename);
347 eval "require \"$modulefile\"";
349 &ERROR("cannot load my module: $modulebase");
350 if ($bot_pid != $$) { # child.
351 &DEBUG("b4 delfork 2");
352 &delForked($modulename);
358 $moduleAge{$modulefile} = (stat $modulefile)[9];
360 &status("myModule: Loaded $modulebase ...");
361 &showProc(" ($modulebase)");
367 eval "use Time::HiRes qw(gettimeofday tv_interval)";
369 &WARN("No Time::HiRes?");
372 &showProc(" (Time::HiRes)");
375 if (!defined $AUTOLOAD and defined $::AUTOLOAD) {
376 &DEBUG("AUTOLOAD: hrm.. ::AUTOLOAD defined!");
378 return unless (defined $AUTOLOAD);
379 return if ($AUTOLOAD =~ /__/); # internal.
381 my $str = join(', ', @_);
382 my ($package, $filename, $line) = caller;
383 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD ($str) $filename line $line");
385 $AUTOLOAD =~ s/^(\S+):://g;
387 if (exists $myModules{lc $AUTOLOAD}) {
388 # hopefully this will work.
389 &DEBUG("Trying to load module $AUTOLOAD...");
390 &loadMyModule(lc $AUTOLOAD);
397 if (!opendir(DIR, $dir)) {
398 &ERROR("Cannot open source directory ($dir): $!");
403 while (defined(my $file = readdir DIR)) {
404 next unless $file =~ /\.pl$/;
405 next unless $file =~ /^[A-Z]/;
410 return reverse sort @mods;