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: $!");
62 &status("Loading CORE modules...");
65 while (defined(my $file = readdir DIR)) {
66 next unless $file =~ /\.pl$/;
67 next unless $file =~ /^[A-Z]/;
71 &DEBUG("mods: ".scalar(@mods)." to be loaded...");
73 foreach (sort @mods) {
74 my $mod = "$bot_src_dir/$_";
76 ### TODO: use eval and exit gracefully?
77 eval "require \"$mod\"";
84 $moduleAge{$mod} = (stat $mod)[9];
85 &showProc(" ($_)") if (&IsParam("DEBUG"));
90 &status("Loading DB modules...");
92 if ($param{'DBType'} =~ /^mysql$/i) {
95 &ERROR("libdbd-mysql-perl is not installed!");
98 &showProc(" (DBI // mysql)");
100 &status(" using MySQL support.");
101 require "$bot_src_dir/db_mysql.pl";
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 require "$bot_src_dir/db_dbm.pl";
119 &status("DB support DISABLED.");
124 sub loadFactoidsModules {
125 &status("Loading Factoids modules...");
127 if (!&IsParam("factoids")) {
128 &status("Factoid support DISABLED.");
132 if (!opendir(DIR, "$bot_src_dir/Factoids")) {
133 &ERROR("can't open source directory Factoids: $!");
137 while (defined(my $file = readdir DIR)) {
138 next unless $file =~ /\.pl$/;
139 next unless $file =~ /^[A-Z]/;
140 my $mod = "$bot_src_dir/Factoids/$file";
141 ### TODO: use eval and exit gracefully?
142 eval "require \"$mod\"";
148 $moduleAge{$mod} = (stat $mod)[9];
149 &showProc(" ($file)") if (&IsParam("DEBUG"));
155 &status("Loading IRC modules...");
156 if (&whatInterface() =~ /IRC/) {
159 &ERROR("libnet-irc-perl is not installed!");
162 &showProc(" (Net::IRC)");
165 &status("IRC support DISABLED.");
169 if (!opendir(DIR, "$bot_src_dir/IRC")) {
170 &ERROR("can't open source directory Factoids: $!");
174 while (defined(my $file = readdir DIR)) {
175 next unless $file =~ /\.pl$/;
176 next unless $file =~ /^[A-Z]/;
177 my $mod = "$bot_src_dir/IRC/$file";
178 ### TODO: use eval and exit gracefully?
180 $moduleAge{$mod} = (stat $mod)[9];
181 &showProc(" ($file)") if (&IsParam("DEBUG"));
186 sub loadMyModulesNow {
190 &status("Loading MyModules...");
191 foreach (@myModulesLoadNow) {
194 &WARN("mMLN: null element.");
198 if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
200 if (exists $myModules{$_}) {
201 &status("myModule: $myModules{$_} (1) not loaded.");
203 &DEBUG("myModule: $_ (2) not loaded.");
209 &loadMyModule($myModules{$_});
213 &status("Module: Runtime: Loaded/Total [$loaded/$total]");
216 ### rename to moduleReloadAll?
217 sub reloadAllModules {
218 ### &status("Module: reloading all.");
219 foreach (map { substr($_,2) } keys %moduleAge) {
222 ### &status("Module: reloading done.");
225 ### rename to modulesReload?
228 my $file = (grep /\/$mod/, keys %INC)[0];
230 # don't reload if it's not our module.
231 if ($mod =~ /::/ or $mod !~ /pl$/) {
232 &VERB("Not reloading $mod.",3);
236 if (!defined $file) {
237 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
242 &ERROR("rM: file '$file' does not exist?");
246 my $age = (stat $file)[9];
247 return if ($age == $moduleAge{$file});
249 if ($age < $moduleAge{$file}) {
250 &WARN("rM: we're not gonna downgrade the file. use 'touch'.");
254 if (grep /$mod/, @myModulesReloadNot) {
255 &DEBUG("rM: SHOULD NOT RELOAD $mod!!!");
259 my $dc = &Time2String($age - $moduleAge{$file});
260 my $ago = &Time2String(time() - $moduleAge{$file});
262 &status("Module: Loading $mod...");
263 &VERB("Module: delta change: $dc",2);
264 &VERB("Module: ago: $ago",2);
267 eval "require \"$file\""; # require or use?
269 &DEBUG("rM: failure: @$");
271 my $basename = $file;
272 $basename =~ s/^.*\///;
273 &status("Module: reloaded $basename");
274 $moduleAge{$file} = $age;
279 ### OPTIONAL MODULES.
282 local %perlModulesLoaded = ();
283 local %perlModulesMissing = ();
286 return 0 if (exists $perlModulesMissing{$_[0]});
287 &reloadModule($_[0]);
288 return 1 if (exists $perlModulesLoaded{$_[0]});
292 &WARN("Module: $_[0] is not installed!");
293 $perlModulesMissing{$_[0]} = 1;
296 $perlModulesLoaded{$_[0]} = 1;
297 &status("Module: Loaded $_[0] ...");
298 &showProc(" ($_[0])");
306 &WARN("loadMyModule: module is NULL.");
310 my ($modulebase, $modulefile);
311 if (exists $myModules{$tmp}) {
312 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
315 if ($tmp = grep /^$modulebase$/, keys %myModules) {
316 &DEBUG("lMM: lame hack, file => name => $tmp.");
320 my $modulefile = "$bot_src_dir/Modules/$modulebase";
322 # call reloadModule() which checks age of file and reload.
323 if (grep /\/$modulebase$/, keys %INC) {
324 &reloadModule($modulebase);
325 return 1; # depend on reloadModule?
328 if (! -f $modulefile) {
329 &ERROR("lMM: module ($modulebase) does not exist.");
330 if ($$ == $bot_pid) { # parent.
331 &shutdown() if (defined $shm and defined $dbh);
333 &DEBUG("b4 delfork 1");
334 &delForked($modulebase);
340 eval "require \"$modulefile\"";
342 &ERROR("cannot load my module: $modulebase");
343 if ($bot_pid != $$) { # child.
344 &DEBUG("b4 delfork 2");
345 &delForked($modulebase);
351 $moduleAge{$modulefile} = (stat $modulefile)[9];
353 &status("myModule: Loaded $modulebase ...");
354 &showProc(" ($modulebase)");
360 eval "use Time::HiRes qw(gettimeofday tv_interval)";
362 &WARN("No Time::HiRes?");
365 &showProc(" (Time::HiRes)");
368 return if ($AUTOLOAD =~ /__/); # internal.
370 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD");
372 next unless (defined $_);