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",
48 @myModulesLoadNow = ('topic', 'uptime',);
49 @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
52 if (!opendir(DIR, $bot_src_dir)) {
53 &ERROR("can't open source directory $bot_src_dir: $!");
57 &status("Loading CORE modules...");
59 while (defined(my $file = readdir DIR)) {
60 next unless $file =~ /\.pl$/;
61 next unless $file =~ /^[A-Z]/;
62 my $mod = "$bot_src_dir/$file";
64 ### TODO: use eval and exit gracefully?
65 eval "require \"$mod\"";
70 $moduleAge{$mod} = (stat $mod)[9];
71 &showProc(" ($file)") if (&IsParam("DEBUG"));
77 &status("Loading DB modules...");
79 if ($param{'DBType'} =~ /^mysql$/i) {
82 &ERROR("libdbd-mysql-perl is not installed!");
85 &showProc(" (DBI // mysql)");
87 &status(" using MySQL support.");
88 require "$bot_src_dir/db_mysql.pl";
90 } elsif ($param{'DBType'} =~ /^pgsql$/i) {
93 &ERROR("libpgperl is not installed!");
96 &showProc(" (Pg // postgreSQLl)");
98 &status(" using PostgreSQL support.");
99 require "$bot_src_dir/db_pgsql.pl";
100 } elsif ($param{'DBType'} =~ /^dbm$/i) {
102 &status(" using Berkeley DBM 1.85/2.0 support.");
103 require "$bot_src_dir/db_dbm.pl";
106 &status("DB support DISABLED.");
111 sub loadFactoidsModules {
112 &status("Loading Factoids modules...");
114 if (!&IsParam("factoids")) {
115 &status("Factoid support DISABLED.");
119 if (!opendir(DIR, "$bot_src_dir/Factoids")) {
120 &ERROR("can't open source directory Factoids: $!");
124 while (defined(my $file = readdir DIR)) {
125 next unless $file =~ /\.pl$/;
126 next unless $file =~ /^[A-Z]/;
127 my $mod = "$bot_src_dir/Factoids/$file";
128 ### TODO: use eval and exit gracefully?
129 eval "require \"$mod\"";
134 $moduleAge{$mod} = (stat $mod)[9];
135 &showProc(" ($file)") if (&IsParam("DEBUG"));
141 &status("Loading IRC modules...");
142 if (&whatInterface() =~ /IRC/) {
145 &ERROR("libnet-irc-perl is not installed!");
148 &showProc(" (Net::IRC)");
151 &status("IRC support DISABLED.");
155 if (!opendir(DIR, "$bot_src_dir/IRC")) {
156 &ERROR("can't open source directory Factoids: $!");
160 while (defined(my $file = readdir DIR)) {
161 next unless $file =~ /\.pl$/;
162 next unless $file =~ /^[A-Z]/;
163 my $mod = "$bot_src_dir/IRC/$file";
164 ### TODO: use eval and exit gracefully?
166 $moduleAge{$mod} = (stat $mod)[9];
167 &showProc(" ($file)") if (&IsParam("DEBUG"));
172 sub loadMyModulesNow {
176 &status("Loading MyModules...");
177 foreach (@myModulesLoadNow) {
180 if (!exists $param{$_}) {
181 &DEBUG("myModule: $myModules{$_} not loaded.");
184 &loadMyModule($myModules{$_});
188 &status("Module: Loaded/Total [$loaded/$total]");
191 ### rename to moduleReloadAll?
192 sub reloadAllModules {
193 ### &status("Module: reloading all.");
194 foreach (map { substr($_,2) } keys %moduleAge) {
197 ### &status("Module: reloading done.");
200 ### rename to modulesReload?
203 my $file = (grep /\/$mod/, keys %INC)[0];
205 # don't reload if it's not our module.
206 if ($mod =~ /::/ or $mod !~ /pl$/) {
207 &VERB("Not reloading $mod.",3);
211 if (!defined $file) {
212 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
217 &ERROR("rM: file '$file' does not exist?");
221 my $age = (stat $file)[9];
222 return if ($age == $moduleAge{$file});
224 if ($age < $moduleAge{$file}) {
225 &WARN("rM: we're not gonna downgrade the file. use 'touch'.");
229 if (grep /$mod/, @myModulesReloadNot) {
230 &DEBUG("rM: SHOULD NOT RELOAD $mod!!!");
234 my $dc = &Time2String($age - $moduleAge{$file});
235 my $ago = &Time2String(time() - $moduleAge{$file});
237 &status("Module: Loading $mod...");
238 &VERB("Module: delta change: $dc",2);
239 &VERB("Module: ago: $ago",2);
242 eval "require \"$file\""; # require or use?
244 &DEBUG("rM: failure: @$");
246 my $basename = $file;
247 $basename =~ s/^.*\///;
248 &status("Module: reloaded $basename");
249 $moduleAge{$file} = $age;
254 ### OPTIONAL MODULES.
257 local %perlModulesLoaded = ();
258 local %perlModulesMissing = ();
261 return 0 if (exists $perlModulesMissing{$_[0]});
262 &reloadModule($_[0]);
263 return 1 if (exists $perlModulesLoaded{$_[0]});
267 &WARN("Module: $_[0] is not installed!");
268 $perlModulesMissing{$_[0]} = 1;
271 $perlModulesLoaded{$_[0]} = 1;
272 &status("Module: Loaded $_[0] ...");
273 &showProc(" ($_[0])");
281 &WARN("loadMyModule: module is NULL.");
285 my ($modulebase, $modulefile);
286 if (exists $myModules{$tmp}) {
287 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
290 if ($tmp = grep /^$modulebase$/, keys %myModules) {
291 &DEBUG("lMM: lame hack, file => name => $tmp.");
295 my $modulefile = "$bot_src_dir/Modules/$modulebase";
297 # call reloadModule() which checks age of file and reload.
298 if (grep /\/$modulebase$/, keys %INC) {
299 &reloadModule($modulebase);
300 return 1; # depend on reloadModule?
303 if (! -f $modulefile) {
304 &ERROR("lMM: module ($modulebase) does not exist.");
305 if ($$ == $bot_pid) { # parent.
306 &shutdown() if (defined $shm and defined $dbh);
308 &DEBUG("b4 delfork 1");
309 &delForked($modulebase);
315 eval "require \"$modulefile\"";
317 &ERROR("cannot load my module: $modulebase");
318 if ($bot_pid != $$) { # child.
319 &DEBUG("b4 delfork 2");
320 &delForked($modulebase);
326 $moduleAge{$modulefile} = (stat $modulefile)[9];
328 &status("myModule: Loaded $modulebase ...");
329 &showProc(" ($modulebase)");
335 eval "use Time::HiRes qw(gettimeofday tv_interval)";
337 &WARN("No Time::HiRes?");
340 &showProc(" (Time::HiRes)");
343 return if ($AUTOLOAD =~ /__/); # internal.
345 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD");
347 next unless (defined $_);