2 # modules.pl: pseudo-Module handler
4 # Version: v0.2 (20000629)
8 if (&IsParam("useStrict")) { use strict; }
9 use vars qw($AUTOLOAD);
15 eval "use IO::Socket";
17 &ERROR("no IO::Socket?");
20 &showProc(" (IO::Socket)");
24 "countdown" => "Countdown.pl",
25 "debian" => "Debian.pl",
26 "debianExtra" => "DebianExtra.pl",
28 "dumpvars" => "DumpVars.pl",
29 "factoids" => "Factoids.pl",
30 "freshmeat" => "Freshmeat.pl",
31 "kernel" => "Kernel.pl",
32 "ircdcc" => "UserDCC.pl",
33 "perlMath" => "Math.pl",
35 "quote" => "Quote.pl",
36 "rootwarn" => "RootWarn.pl",
37 "search" => "Search.pl",
38 "slashdot" => "Slashdot3.pl",
39 "topic" => "Topic.pl",
40 "units" => "Units.pl",
41 "uptime" => "Uptime.pl",
42 "userinfo" => "UserInfo.pl",
43 "wwwsearch" => "W3Search.pl",
44 "whatis" => "WhatIs.pl",
45 "wingate" => "Wingate.pl",
46 "insult" => "insult.pl",
47 "nickometer" => "nickometer.pl",
48 "babelfish" => "babel.pl",
50 ### THIS IS NOT LOADED ON RELOAD :(
52 @myModulesLoadNow = ('topic', 'uptime', 'news', 'rootWarn');
53 @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
57 my @mods = &getPerlFiles($bot_src_dir);
59 &status("Loading ".scalar(@mods)." CORE modules...");
61 foreach (sort @mods) {
62 my $mod = "$bot_src_dir/$_";
64 eval "require \"$mod\"";
71 $moduleAge{$mod} = (stat $mod)[9];
72 &showProc(" ($_)") if (&IsParam("DEBUG"));
77 &status("Loading DB modules...");
79 my $f = "$bot_src_dir/modules.pl";
80 $moduleAge{$f} = (stat $f)[9];
82 if ($param{'DBType'} =~ /^mysql$/i) {
85 &ERROR("libdbd-mysql-perl is not installed!");
88 &showProc(" (DBI // mysql)");
90 &status(" using MySQL support.");
91 $f = "$bot_src_dir/db_mysql.pl";
93 $moduleAge{$f} = (stat $f)[9];
95 } elsif ($param{'DBType'} =~ /^pgsql$/i) {
99 &ERROR("libpgperl is not installed!");
102 &showProc(" (pgsql)");
104 &status(" using pgsql support.");
105 require "$bot_src_dir/db_pgsql.pl";
107 } elsif ($param{'DBType'} =~ /^dbm$/i) {
109 &status(" using Berkeley DBM 1.85/2.0 support.");
110 &ERROR("dbm support is broken... if you want it, fix it yourself!");
114 # require "$bot_src_dir/db_dbm.pl";
117 &status("DB support DISABLED.");
122 sub loadFactoidsModules {
123 if (!&IsParam("factoids")) {
124 &status("Factoid support DISABLED.");
128 &status("Loading Factoids modules...");
130 foreach ( &getPerlFiles("$bot_src_dir/Factoids") ) {
131 my $mod = "$bot_src_dir/Factoids/$_";
133 eval "require \"$mod\"";
139 $moduleAge{$mod} = (stat $mod)[9];
140 &showProc(" ($_)") if (&IsParam("DEBUG"));
145 if (&whatInterface() =~ /IRC/) {
146 &status("Loading IRC modules...");
150 &ERROR("libnet-irc-perl is not installed!");
153 &showProc(" (Net::IRC)");
156 &status("IRC support DISABLED.");
160 foreach ( &getPerlFiles("$bot_src_dir/IRC") ) {
161 my $mod = "$bot_src_dir/IRC/$_";
163 eval "require \"$mod\"";
165 &ERROR("lIRCM => $@");
170 $moduleAge{$mod} = (stat $mod)[9];
171 &showProc(" ($_)") if (&IsParam("DEBUG"));
175 sub loadMyModulesNow {
179 &status("Loading MyModules...");
180 foreach (@myModulesLoadNow) {
183 &WARN("mMLN: null element.");
187 if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
188 if (exists $myModules{$_}) {
189 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
191 &DEBUG("myModule: $_ (2) not loaded.");
197 # weird hack to get rootwarn to work.
198 # it may break on other cases though, any ideas?
199 &loadMyModule( $myModules{$_} || $myModules{lc $_} );
203 &status("Module: Runtime: Loaded/Total [$loaded/$total]");
206 ### rename to moduleReloadAll?
207 sub reloadAllModules {
208 &VERB("Module: reloading all.",2);
210 # obscure usage of map and regex :)
211 foreach (map { s/.*?\/?src/src/; $_ } keys %moduleAge) {
215 &VERB("Module: reloading done.",2);
218 ### rename to modulesReload?
221 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 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 $moduleAge{$file} = $age;
280 ### OPTIONAL MODULES.
283 local %perlModulesLoaded = ();
284 local %perlModulesMissing = ();
287 return 0 if (exists $perlModulesMissing{$_[0]});
288 &reloadModule($_[0]);
289 return 1 if (exists $perlModulesLoaded{$_[0]});
293 &WARN("Module: $_[0] is not installed!");
294 $perlModulesMissing{$_[0]} = 1;
297 $perlModulesLoaded{$_[0]} = 1;
298 &status("Module: Loaded $_[0] ...");
299 &showProc(" ($_[0])");
307 &WARN("loadMyModule: module is NULL.");
311 my ($modulebase, $modulefile);
312 if (exists $myModules{$tmp}) {
313 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
316 if ($tmp = grep /^$modulebase$/, keys %myModules) {
317 &DEBUG("lMM: lame hack, file => name => $tmp.");
321 $modulefile = "$bot_src_dir/Modules/$modulebase";
323 # call reloadModule() which checks age of file and reload.
324 if (grep /\/$modulebase$/, keys %INC) {
325 &reloadModule($modulebase);
326 return 1; # depend on reloadModule?
329 if (! -f $modulefile) {
330 &ERROR("lMM: module ($modulebase) does not exist.");
331 if ($$ == $bot_pid) { # parent.
332 &shutdown() if (defined $shm and defined $dbh);
334 &DEBUG("b4 delfork 1");
335 &delForked($modulebase);
341 eval "require \"$modulefile\"";
343 &ERROR("cannot load my module: $modulebase");
344 if ($bot_pid != $$) { # child.
345 &DEBUG("b4 delfork 2");
346 &delForked($modulebase);
352 $moduleAge{$modulefile} = (stat $modulefile)[9];
354 &status("myModule: Loaded $modulebase ...");
355 &showProc(" ($modulebase)");
361 eval "use Time::HiRes qw(gettimeofday tv_interval)";
363 &WARN("No Time::HiRes?");
366 &showProc(" (Time::HiRes)");
369 if (!defined $AUTOLOAD and defined $::AUTOLOAD) {
370 &DEBUG("AUTOLOAD: hrm.. ::AUTOLOAD defined!");
372 return unless (defined $AUTOLOAD);
373 return if ($AUTOLOAD =~ /__/); # internal.
375 my $str = join(', ', @_);
376 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD ($str)");
378 $AUTOLOAD =~ s/^(\S+):://g;
380 if (exists $myModules{lc $AUTOLOAD}) {
381 # hopefully this will work.
382 &DEBUG("Trying to load module $AUTOLOAD...");
383 &loadMyModule(lc $AUTOLOAD);
390 if (!opendir(DIR, $dir)) {
391 &ERROR("cannot open source directory $dir: $!");
396 while (defined(my $file = readdir DIR)) {
397 next unless $file =~ /\.pl$/;
398 next unless $file =~ /^[A-Z]/;
403 return reverse sort @mods;