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" => "babelfish.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 "pager" => "pager.pl",
42 "perlMath" => "Math.pl",
44 "quote" => "Quote.pl",
45 "rootwarn" => "RootWarn.pl",
47 "search" => "Search.pl",
48 "slashdot" => "Slashdot3.pl",
49 "symdump" => "DumpVars2.pl",
50 "topic" => "Topic.pl",
51 "units" => "Units.pl",
52 "uptime" => "Uptime.pl",
53 "userinfo" => "UserInfo.pl",
54 "weather" => "Weather.pl",
55 "whatis" => "WhatIs.pl",
56 "wikipedia" => "wikipedia.pl",
57 "wingate" => "Wingate.pl",
58 "wwwsearch" => "W3Search.pl",
60 "zippy" => "Zippy.pl",
63 ### THIS IS NOT LOADED ON RELOAD :(
65 my @myModulesReloadNot;
67 @myModulesLoadNow = ('topic', 'uptime', 'news', 'rootWarn', 'symdump', 'botmail');
68 @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
72 my @mods = &getPerlFiles($bot_src_dir);
74 &status("Loading CORE modules...");
76 foreach (sort @mods) {
77 my $mod = "$bot_src_dir/$_";
79 eval "require \"$mod\"";
86 $moduleAge{$mod} = (stat $mod)[9];
87 &showProc(" ($_)") if (&IsParam("DEBUG"));
93 # TODO: use function to load module.
95 if ($param{'DBType'} =~ /^(mysql|SQLite|pgsql)$/i) {
98 &ERROR("No support for DBI::" . $param{'DBType'} . ", exiting!");
101 &status("Loading " . $param{'DBType'} . " support.");
102 $f = "$bot_src_dir/dbi.pl";
104 $moduleAge{$f} = (stat $f)[9];
106 &showProc(" (DBI::" . $param{'DBType'} . ")");
108 &WARN("DB support DISABLED.");
113 sub loadFactoidsModules {
114 if (!&IsParam("factoids")) {
115 &status("Factoid support DISABLED.");
119 &status("Loading Factoids modules...");
121 foreach ( &getPerlFiles("$bot_src_dir/Factoids") ) {
122 my $mod = "$bot_src_dir/Factoids/$_";
124 eval "require \"$mod\"";
130 $moduleAge{$mod} = (stat $mod)[9];
131 &showProc(" ($_)") if (&IsParam("DEBUG"));
136 my ($interface) = &whatInterface();
137 if ($interface =~ /IRC/) {
138 &status("Loading IRC modules...");
142 &ERROR("libnet-irc-perl is not installed!");
145 &showProc(" (Net::IRC)");
147 &status("IRC support DISABLED.");
148 # disabling forking. Why?
149 #$param{forking} = 0;
153 foreach ( &getPerlFiles("$bot_src_dir/$interface") ) {
154 my $mod = "$bot_src_dir/$interface/$_";
156 # hrm... use another config option besides DEBUG to display
157 # change in memory usage.
158 &status("Loading Modules \"$mod\"") if (!&IsParam("DEBUG"));
159 eval "require \"$mod\"";
161 &ERROR("require \"$mod\" => $@");
166 $moduleAge{$mod} = (stat $mod)[9];
167 &showProc(" ($_)") if (&IsParam("DEBUG"));
171 sub loadMyModulesNow {
175 &status("Loading MyModules...");
176 foreach (@myModulesLoadNow) {
179 &WARN("mMLN: null element.");
183 if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
184 if (exists $myModules{$_}) {
185 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
187 &DEBUG("myModule: $_ (2) not loaded.");
193 # weird hack to get rootwarn to work.
194 # it may break on other cases though, any ideas?
195 &loadMyModule( $myModules{$_} || $myModules{lc $_} );
199 &status("Module: Runtime: Loaded/Total [$loaded/$total]");
202 ### rename to moduleReloadAll?
203 sub reloadAllModules {
206 &VERB("Module: reloading all.",2);
208 # obscure usage of map and regex :)
209 foreach (map { s/.*?\/?src/src/; $_ } keys %moduleAge) {
210 $retval .= &reloadModule($_);
213 &VERB("Module: reloading done.",2);
217 ### rename to modulesReload?
220 my $file = (grep /\/$mod/, keys %INC)[0];
223 # don't reload if it's not our module.
224 if ($mod =~ /::/ or $mod !~ /pl$/) {
225 &VERB("Not reloading $mod.",3);
229 if (!defined $file) {
230 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
235 &ERROR("rM: file '$file' does not exist?");
239 if (grep /$mod/, @myModulesReloadNot) {
240 &DEBUG("rM: should not reload $mod");
244 my $age = (stat $file)[9];
246 if (!exists $moduleAge{$file}) {
247 &DEBUG("Looks like $file was not loaded; fixing.");
249 return $retval if ($age == $moduleAge{$file});
251 if ($age < $moduleAge{$file}) {
252 &WARN("rM: we're not gonna downgrade '$file'; use touch.");
253 &DEBUG("age => $age");
254 &DEBUG("mA{$file} => $moduleAge{$file}");
258 my $dc = &Time2String($age - $moduleAge{$file});
259 my $ago = &Time2String(time() - $moduleAge{$file});
261 &VERB("Module: delta change: $dc",2);
262 &VERB("Module: ago: $ago",2);
265 &status("Module: Loading $mod...");
268 eval "require \"$file\""; # require or use?
270 &DEBUG("rM: failure: @$ ");
272 my $basename = $file;
273 $basename =~ s/^.*\///;
274 &status("Module: reloaded $basename");
275 $retval = " $basename";
276 $moduleAge{$file} = $age;
282 ### OPTIONAL MODULES.
285 my %perlModulesLoaded = ();
286 my %perlModulesMissing = ();
289 return 0 if (exists $perlModulesMissing{$_[0]});
290 &reloadModule($_[0]);
291 return 1 if (exists $perlModulesLoaded{$_[0]});
295 &WARN("Module: $_[0] is not installed!");
296 $perlModulesMissing{$_[0]} = 1;
299 $perlModulesLoaded{$_[0]} = 1;
300 &status("Loaded $_[0]");
301 &showProc(" ($_[0])");
309 &WARN("loadMyModule: module is NULL.");
313 my ($modulename, $modulebase);
314 if (exists $myModules{$tmp}) {
315 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
318 if ($tmp = grep /^$modulebase$/, keys %myModules) {
319 &DEBUG("lMM: lame hack, file => name => $tmp.");
323 my $modulefile = "$bot_src_dir/Modules/$modulebase";
325 # call reloadModule() which checks age of file and reload.
326 if (grep /\/$modulebase$/, keys %INC) {
327 &reloadModule($modulebase);
328 return 1; # depend on reloadModule?
331 if (! -f $modulefile) {
332 &ERROR("lMM: module ($modulebase) does not exist.");
333 if ($$ == $bot_pid) { # parent.
334 &shutdown() if (defined $shm and defined $dbh);
336 &DEBUG("b4 delfork 1");
337 &delForked($modulename);
343 eval "require \"$modulefile\"";
345 &ERROR("cannot load my module: $modulebase");
346 if ($bot_pid != $$) { # child.
347 &DEBUG("b4 delfork 2");
348 &delForked($modulename);
354 $moduleAge{$modulefile} = (stat $modulefile)[9];
356 &status("Loaded $modulebase");
357 &showProc(" ($modulebase)");
363 eval "use Time::HiRes qw(gettimeofday tv_interval)";
365 &WARN("No Time::HiRes?");
368 &showProc(" (Time::HiRes)");
371 if (!defined $AUTOLOAD and defined $::AUTOLOAD) {
372 &DEBUG("AUTOLOAD: hrm.. ::AUTOLOAD defined!");
374 return unless (defined $AUTOLOAD);
375 return if ($AUTOLOAD =~ /__/); # internal.
377 my $str = join(', ', @_);
378 my ($package, $filename, $line) = caller;
379 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD ($str) $filename line $line");
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;