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...");
64 while (defined(my $file = readdir DIR)) {
65 next unless $file =~ /\.pl$/;
66 next unless $file =~ /^[A-Z]/;
67 my $mod = "$bot_src_dir/$file";
69 ### TODO: use eval and exit gracefully?
70 eval "require \"$mod\"";
77 $moduleAge{$mod} = (stat $mod)[9];
78 &showProc(" ($file)") if (&IsParam("DEBUG"));
84 &status("Loading DB modules...");
86 if ($param{'DBType'} =~ /^mysql$/i) {
89 &ERROR("libdbd-mysql-perl is not installed!");
92 &showProc(" (DBI // mysql)");
94 &status(" using MySQL support.");
95 require "$bot_src_dir/db_mysql.pl";
97 } elsif ($param{'DBType'} =~ /^pgsql$/i) {
100 &ERROR("libpgperl is not installed!");
103 &showProc(" (Pg // postgreSQLl)");
105 &status(" using PostgreSQL support.");
106 require "$bot_src_dir/db_pgsql.pl";
107 } elsif ($param{'DBType'} =~ /^dbm$/i) {
109 &status(" using Berkeley DBM 1.85/2.0 support.");
110 require "$bot_src_dir/db_dbm.pl";
113 &status("DB support DISABLED.");
118 sub loadFactoidsModules {
119 &status("Loading Factoids modules...");
121 if (!&IsParam("factoids")) {
122 &status("Factoid support DISABLED.");
126 if (!opendir(DIR, "$bot_src_dir/Factoids")) {
127 &ERROR("can't open source directory Factoids: $!");
131 while (defined(my $file = readdir DIR)) {
132 next unless $file =~ /\.pl$/;
133 next unless $file =~ /^[A-Z]/;
134 my $mod = "$bot_src_dir/Factoids/$file";
135 ### TODO: use eval and exit gracefully?
136 eval "require \"$mod\"";
142 $moduleAge{$mod} = (stat $mod)[9];
143 &showProc(" ($file)") if (&IsParam("DEBUG"));
149 &status("Loading IRC modules...");
150 if (&whatInterface() =~ /IRC/) {
153 &ERROR("libnet-irc-perl is not installed!");
156 &showProc(" (Net::IRC)");
159 &status("IRC support DISABLED.");
163 if (!opendir(DIR, "$bot_src_dir/IRC")) {
164 &ERROR("can't open source directory Factoids: $!");
168 while (defined(my $file = readdir DIR)) {
169 next unless $file =~ /\.pl$/;
170 next unless $file =~ /^[A-Z]/;
171 my $mod = "$bot_src_dir/IRC/$file";
172 ### TODO: use eval and exit gracefully?
174 $moduleAge{$mod} = (stat $mod)[9];
175 &showProc(" ($file)") if (&IsParam("DEBUG"));
180 sub loadMyModulesNow {
184 &status("Loading MyModules...");
185 foreach (@myModulesLoadNow) {
188 &WARN("mMLN: null element.");
192 if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
194 if (exists $myModules{$_}) {
195 &status("myModule: $myModules{$_} (1) not loaded.");
197 &DEBUG("myModule: $_ (2) not loaded.");
203 &loadMyModule($myModules{$_});
207 &status("Module: Runtime: Loaded/Total [$loaded/$total]");
210 ### rename to moduleReloadAll?
211 sub reloadAllModules {
212 ### &status("Module: reloading all.");
213 foreach (map { substr($_,2) } keys %moduleAge) {
216 ### &status("Module: reloading done.");
219 ### rename to modulesReload?
222 my $file = (grep /\/$mod/, keys %INC)[0];
224 # don't reload if it's not our module.
225 if ($mod =~ /::/ or $mod !~ /pl$/) {
226 &VERB("Not reloading $mod.",3);
230 if (!defined $file) {
231 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
236 &ERROR("rM: file '$file' does not exist?");
240 my $age = (stat $file)[9];
241 return if ($age == $moduleAge{$file});
243 if ($age < $moduleAge{$file}) {
244 &WARN("rM: we're not gonna downgrade the file. use 'touch'.");
248 if (grep /$mod/, @myModulesReloadNot) {
249 &DEBUG("rM: SHOULD NOT RELOAD $mod!!!");
253 my $dc = &Time2String($age - $moduleAge{$file});
254 my $ago = &Time2String(time() - $moduleAge{$file});
256 &status("Module: Loading $mod...");
257 &VERB("Module: delta change: $dc",2);
258 &VERB("Module: ago: $ago",2);
261 eval "require \"$file\""; # require or use?
263 &DEBUG("rM: failure: @$");
265 my $basename = $file;
266 $basename =~ s/^.*\///;
267 &status("Module: reloaded $basename");
268 $moduleAge{$file} = $age;
273 ### OPTIONAL MODULES.
276 local %perlModulesLoaded = ();
277 local %perlModulesMissing = ();
280 return 0 if (exists $perlModulesMissing{$_[0]});
281 &reloadModule($_[0]);
282 return 1 if (exists $perlModulesLoaded{$_[0]});
286 &WARN("Module: $_[0] is not installed!");
287 $perlModulesMissing{$_[0]} = 1;
290 $perlModulesLoaded{$_[0]} = 1;
291 &status("Module: Loaded $_[0] ...");
292 &showProc(" ($_[0])");
300 &WARN("loadMyModule: module is NULL.");
304 my ($modulebase, $modulefile);
305 if (exists $myModules{$tmp}) {
306 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
309 if ($tmp = grep /^$modulebase$/, keys %myModules) {
310 &DEBUG("lMM: lame hack, file => name => $tmp.");
314 my $modulefile = "$bot_src_dir/Modules/$modulebase";
316 # call reloadModule() which checks age of file and reload.
317 if (grep /\/$modulebase$/, keys %INC) {
318 &reloadModule($modulebase);
319 return 1; # depend on reloadModule?
322 if (! -f $modulefile) {
323 &ERROR("lMM: module ($modulebase) does not exist.");
324 if ($$ == $bot_pid) { # parent.
325 &shutdown() if (defined $shm and defined $dbh);
327 &DEBUG("b4 delfork 1");
328 &delForked($modulebase);
334 eval "require \"$modulefile\"";
336 &ERROR("cannot load my module: $modulebase");
337 if ($bot_pid != $$) { # child.
338 &DEBUG("b4 delfork 2");
339 &delForked($modulebase);
345 $moduleAge{$modulefile} = (stat $modulefile)[9];
347 &status("myModule: Loaded $modulebase ...");
348 &showProc(" ($modulebase)");
354 eval "use Time::HiRes qw(gettimeofday tv_interval)";
356 &WARN("No Time::HiRes?");
359 &showProc(" (Time::HiRes)");
362 return if ($AUTOLOAD =~ /__/); # internal.
364 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD");
366 next unless (defined $_);