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');
53 @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
57 if (!opendir(DIR, $bot_src_dir)) {
58 &ERROR("can't open source directory $bot_src_dir: $!");
63 while (defined(my $file = readdir DIR)) {
64 next unless $file =~ /\.pl$/;
65 next unless $file =~ /^[A-Z]/;
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 &status("Loading DB modules...");
89 $moduleAge{"$bot_src_dir/modules.pl"} = time();
91 if ($param{'DBType'} =~ /^mysql$/i) {
94 &ERROR("libdbd-mysql-perl is not installed!");
97 &showProc(" (DBI // mysql)");
99 &status(" using MySQL support.");
100 require "$bot_src_dir/db_mysql.pl";
101 $moduleAge{"$bot_src_dir/db_mysql.pl"} = time();
103 } elsif ($param{'DBType'} =~ /^pgsql$/i) {
106 &ERROR("libpgperl is not installed!");
109 &showProc(" (Pg // postgreSQLl)");
111 &status(" using PostgreSQL support.");
112 require "$bot_src_dir/db_pgsql.pl";
113 } elsif ($param{'DBType'} =~ /^dbm$/i) {
115 &status(" using Berkeley DBM 1.85/2.0 support.");
116 &ERROR("dbm support is broken... you want it, you fix it!");
120 # require "$bot_src_dir/db_dbm.pl";
123 &status("DB support DISABLED.");
128 sub loadFactoidsModules {
129 &status("Loading Factoids modules...");
131 if (!&IsParam("factoids")) {
132 &status("Factoid support DISABLED.");
136 if (!opendir(DIR, "$bot_src_dir/Factoids")) {
137 &ERROR("can't open source directory Factoids: $!");
141 while (defined(my $file = readdir DIR)) {
142 next unless $file =~ /\.pl$/;
143 next unless $file =~ /^[A-Z]/;
144 my $mod = "$bot_src_dir/Factoids/$file";
145 ### TODO: use eval and exit gracefully?
146 eval "require \"$mod\"";
152 $moduleAge{$mod} = (stat $mod)[9];
153 &showProc(" ($file)") if (&IsParam("DEBUG"));
159 &status("Loading IRC modules...");
160 if (&whatInterface() =~ /IRC/) {
163 &ERROR("libnet-irc-perl is not installed!");
166 &showProc(" (Net::IRC)");
169 &status("IRC support DISABLED.");
173 if (!opendir(DIR, "$bot_src_dir/IRC")) {
174 &ERROR("can't open source directory Factoids: $!");
178 while (defined(my $file = readdir DIR)) {
179 next unless $file =~ /\.pl$/;
180 next unless $file =~ /^[A-Z]/;
181 my $mod = "$bot_src_dir/IRC/$file";
182 ### TODO: use eval and exit gracefully?
184 $moduleAge{$mod} = (stat $mod)[9];
185 &showProc(" ($file)") if (&IsParam("DEBUG"));
190 sub loadMyModulesNow {
194 &status("Loading MyModules...");
195 foreach (@myModulesLoadNow) {
198 &WARN("mMLN: null element.");
202 if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
203 if (exists $myModules{$_}) {
204 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
206 &DEBUG("myModule: $_ (2) not loaded.");
212 &loadMyModule($myModules{$_});
216 &status("Module: Runtime: Loaded/Total [$loaded/$total]");
219 ### rename to moduleReloadAll?
220 sub reloadAllModules {
221 ### &status("Module: reloading all.");
222 foreach (map { substr($_,2) } keys %moduleAge) {
225 ### &status("Module: reloading done.");
228 ### rename to modulesReload?
231 my $file = (grep /\/$mod/, keys %INC)[0];
233 # don't reload if it's not our module.
234 if ($mod =~ /::/ or $mod !~ /pl$/) {
235 &VERB("Not reloading $mod.",3);
239 if (!defined $file) {
240 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
245 &ERROR("rM: file '$file' does not exist?");
249 if (grep /$mod/, @myModulesReloadNot) {
250 &DEBUG("rM: SHOULD NOT RELOAD $mod!!!");
254 my $age = (stat $file)[9];
256 if (!exists $moduleAge{$file}) {
257 &DEBUG("Looks like $file was not loaded; fixing.");
259 return if ($age == $moduleAge{$file});
261 if ($age < $moduleAge{$file}) {
262 &WARN("rM: we're not gonna downgrade the file. use 'touch'.");
266 my $dc = &Time2String($age - $moduleAge{$file});
267 my $ago = &Time2String(time() - $moduleAge{$file});
269 &VERB("Module: delta change: $dc",2);
270 &VERB("Module: ago: $ago",2);
273 &status("Module: Loading $mod...");
276 eval "require \"$file\""; # require or use?
278 &DEBUG("rM: failure: @$");
280 my $basename = $file;
281 $basename =~ s/^.*\///;
282 &status("Module: reloaded $basename");
283 $moduleAge{$file} = $age;
288 ### OPTIONAL MODULES.
291 local %perlModulesLoaded = ();
292 local %perlModulesMissing = ();
295 return 0 if (exists $perlModulesMissing{$_[0]});
296 &reloadModule($_[0]);
297 return 1 if (exists $perlModulesLoaded{$_[0]});
301 &WARN("Module: $_[0] is not installed!");
302 $perlModulesMissing{$_[0]} = 1;
305 $perlModulesLoaded{$_[0]} = 1;
306 &status("Module: Loaded $_[0] ...");
307 &showProc(" ($_[0])");
315 &WARN("loadMyModule: module is NULL.");
319 my ($modulebase, $modulefile);
320 if (exists $myModules{$tmp}) {
321 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
324 if ($tmp = grep /^$modulebase$/, keys %myModules) {
325 &DEBUG("lMM: lame hack, file => name => $tmp.");
329 my $modulefile = "$bot_src_dir/Modules/$modulebase";
331 # call reloadModule() which checks age of file and reload.
332 if (grep /\/$modulebase$/, keys %INC) {
333 &reloadModule($modulebase);
334 return 1; # depend on reloadModule?
337 if (! -f $modulefile) {
338 &ERROR("lMM: module ($modulebase) does not exist.");
339 if ($$ == $bot_pid) { # parent.
340 &shutdown() if (defined $shm and defined $dbh);
342 &DEBUG("b4 delfork 1");
343 &delForked($modulebase);
349 eval "require \"$modulefile\"";
351 &ERROR("cannot load my module: $modulebase");
352 if ($bot_pid != $$) { # child.
353 &DEBUG("b4 delfork 2");
354 &delForked($modulebase);
360 $moduleAge{$modulefile} = (stat $modulefile)[9];
362 &status("myModule: Loaded $modulebase ...");
363 &showProc(" ($modulebase)");
369 eval "use Time::HiRes qw(gettimeofday tv_interval)";
371 &WARN("No Time::HiRes?");
374 &showProc(" (Time::HiRes)");
377 return if ($AUTOLOAD =~ /__/); # internal.
379 my $str = join(', ', @_);
380 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD ($str)");
382 $AUTOLOAD =~ s/^(\S+):://g;
384 if (exists $myModules{lc $AUTOLOAD}) {
385 # hopefully this will work.
386 &DEBUG("Trying to load module $AUTOLOAD...");
387 &loadMyModule(lc $AUTOLOAD);