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",
34 "quote" => "Quote.pl",
35 "rootwarn" => "RootWarn.pl",
36 "search" => "Search.pl",
37 "slashdot" => "Slashdot3.pl",
38 "topic" => "Topic.pl",
39 "units" => "Units.pl",
40 "uptime" => "Uptime.pl",
41 "userinfo" => "UserInfo.pl",
42 "wwwsearch" => "W3Search.pl",
43 "whatis" => "WhatIs.pl",
44 "wingate" => "Wingate.pl",
45 "insult" => "insult.pl",
46 "nickometer" => "nickometer.pl",
47 "babelfish" => "babel.pl",
50 @myModulesLoadNow = ('topic', 'uptime',);
51 @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
55 if (!opendir(DIR, $bot_src_dir)) {
56 &ERROR("can't open source directory $bot_src_dir: $!");
60 &status("Loading CORE modules...");
62 while (defined(my $file = readdir DIR)) {
63 next unless $file =~ /\.pl$/;
64 next unless $file =~ /^[A-Z]/;
65 my $mod = "$bot_src_dir/$file";
67 ### TODO: use eval and exit gracefully?
68 eval "require \"$mod\"";
75 $moduleAge{$mod} = (stat $mod)[9];
76 &showProc(" ($file)") if (&IsParam("DEBUG"));
82 &status("Loading DB modules...");
84 if ($param{'DBType'} =~ /^mysql$/i) {
87 &ERROR("libdbd-mysql-perl is not installed!");
90 &showProc(" (DBI // mysql)");
92 &status(" using MySQL support.");
93 require "$bot_src_dir/db_mysql.pl";
95 } elsif ($param{'DBType'} =~ /^pgsql$/i) {
98 &ERROR("libpgperl is not installed!");
101 &showProc(" (Pg // postgreSQLl)");
103 &status(" using PostgreSQL support.");
104 require "$bot_src_dir/db_pgsql.pl";
105 } elsif ($param{'DBType'} =~ /^dbm$/i) {
107 &status(" using Berkeley DBM 1.85/2.0 support.");
108 require "$bot_src_dir/db_dbm.pl";
111 &status("DB support DISABLED.");
116 sub loadFactoidsModules {
117 &status("Loading Factoids modules...");
119 if (!&IsParam("factoids")) {
120 &status("Factoid support DISABLED.");
124 if (!opendir(DIR, "$bot_src_dir/Factoids")) {
125 &ERROR("can't open source directory Factoids: $!");
129 while (defined(my $file = readdir DIR)) {
130 next unless $file =~ /\.pl$/;
131 next unless $file =~ /^[A-Z]/;
132 my $mod = "$bot_src_dir/Factoids/$file";
133 ### TODO: use eval and exit gracefully?
134 eval "require \"$mod\"";
140 $moduleAge{$mod} = (stat $mod)[9];
141 &showProc(" ($file)") if (&IsParam("DEBUG"));
147 &status("Loading IRC modules...");
148 if (&whatInterface() =~ /IRC/) {
151 &ERROR("libnet-irc-perl is not installed!");
154 &showProc(" (Net::IRC)");
157 &status("IRC support DISABLED.");
161 if (!opendir(DIR, "$bot_src_dir/IRC")) {
162 &ERROR("can't open source directory Factoids: $!");
166 while (defined(my $file = readdir DIR)) {
167 next unless $file =~ /\.pl$/;
168 next unless $file =~ /^[A-Z]/;
169 my $mod = "$bot_src_dir/IRC/$file";
170 ### TODO: use eval and exit gracefully?
172 $moduleAge{$mod} = (stat $mod)[9];
173 &showProc(" ($file)") if (&IsParam("DEBUG"));
178 sub loadMyModulesNow {
182 &status("Loading MyModules...");
183 foreach (@myModulesLoadNow) {
186 &WARN("mMLN: null element.");
190 if (!&IsParam($_) and !&IsChanConf($_)) {
191 if (exists $myModules{$_}) {
192 &DEBUG("myModule: $myModules{$_} (1) not loaded.");
194 &DEBUG("myModule: $_ (2) not loaded.");
200 &loadMyModule($myModules{$_});
204 &status("Module: Loaded/Total [$loaded/$total]");
207 ### rename to moduleReloadAll?
208 sub reloadAllModules {
209 ### &status("Module: reloading all.");
210 foreach (map { substr($_,2) } keys %moduleAge) {
213 ### &status("Module: reloading done.");
216 ### rename to modulesReload?
219 my $file = (grep /\/$mod/, keys %INC)[0];
221 # don't reload if it's not our module.
222 if ($mod =~ /::/ or $mod !~ /pl$/) {
223 &VERB("Not reloading $mod.",3);
227 if (!defined $file) {
228 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
233 &ERROR("rM: file '$file' does not exist?");
237 my $age = (stat $file)[9];
238 return if ($age == $moduleAge{$file});
240 if ($age < $moduleAge{$file}) {
241 &WARN("rM: we're not gonna downgrade the file. use 'touch'.");
245 if (grep /$mod/, @myModulesReloadNot) {
246 &DEBUG("rM: SHOULD NOT RELOAD $mod!!!");
250 my $dc = &Time2String($age - $moduleAge{$file});
251 my $ago = &Time2String(time() - $moduleAge{$file});
253 &status("Module: Loading $mod...");
254 &VERB("Module: delta change: $dc",2);
255 &VERB("Module: ago: $ago",2);
258 eval "require \"$file\""; # require or use?
260 &DEBUG("rM: failure: @$");
262 my $basename = $file;
263 $basename =~ s/^.*\///;
264 &status("Module: reloaded $basename");
265 $moduleAge{$file} = $age;
270 ### OPTIONAL MODULES.
273 local %perlModulesLoaded = ();
274 local %perlModulesMissing = ();
277 return 0 if (exists $perlModulesMissing{$_[0]});
278 &reloadModule($_[0]);
279 return 1 if (exists $perlModulesLoaded{$_[0]});
283 &WARN("Module: $_[0] is not installed!");
284 $perlModulesMissing{$_[0]} = 1;
287 $perlModulesLoaded{$_[0]} = 1;
288 &status("Module: Loaded $_[0] ...");
289 &showProc(" ($_[0])");
297 &WARN("loadMyModule: module is NULL.");
301 my ($modulebase, $modulefile);
302 if (exists $myModules{$tmp}) {
303 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
306 if ($tmp = grep /^$modulebase$/, keys %myModules) {
307 &DEBUG("lMM: lame hack, file => name => $tmp.");
311 my $modulefile = "$bot_src_dir/Modules/$modulebase";
313 # call reloadModule() which checks age of file and reload.
314 if (grep /\/$modulebase$/, keys %INC) {
315 &reloadModule($modulebase);
316 return 1; # depend on reloadModule?
319 if (! -f $modulefile) {
320 &ERROR("lMM: module ($modulebase) does not exist.");
321 if ($$ == $bot_pid) { # parent.
322 &shutdown() if (defined $shm and defined $dbh);
324 &DEBUG("b4 delfork 1");
325 &delForked($modulebase);
331 eval "require \"$modulefile\"";
333 &ERROR("cannot load my module: $modulebase");
334 if ($bot_pid != $$) { # child.
335 &DEBUG("b4 delfork 2");
336 &delForked($modulebase);
342 $moduleAge{$modulefile} = (stat $modulefile)[9];
344 &status("myModule: Loaded $modulebase ...");
345 &showProc(" ($modulebase)");
351 eval "use Time::HiRes qw(gettimeofday tv_interval)";
353 &WARN("No Time::HiRes?");
356 &showProc(" (Time::HiRes)");
359 return if ($AUTOLOAD =~ /__/); # internal.
361 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD");
363 next unless (defined $_);