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\"";
72 $moduleAge{$mod} = (stat $mod)[9];
73 &showProc(" ($file)") if (&IsParam("DEBUG"));
79 &status("Loading DB modules...");
81 if ($param{'DBType'} =~ /^mysql$/i) {
84 &ERROR("libdbd-mysql-perl is not installed!");
87 &showProc(" (DBI // mysql)");
89 &status(" using MySQL support.");
90 require "$bot_src_dir/db_mysql.pl";
92 } elsif ($param{'DBType'} =~ /^pgsql$/i) {
95 &ERROR("libpgperl is not installed!");
98 &showProc(" (Pg // postgreSQLl)");
100 &status(" using PostgreSQL support.");
101 require "$bot_src_dir/db_pgsql.pl";
102 } elsif ($param{'DBType'} =~ /^dbm$/i) {
104 &status(" using Berkeley DBM 1.85/2.0 support.");
105 require "$bot_src_dir/db_dbm.pl";
108 &status("DB support DISABLED.");
113 sub loadFactoidsModules {
114 &status("Loading Factoids modules...");
116 if (!&IsParam("factoids")) {
117 &status("Factoid support DISABLED.");
121 if (!opendir(DIR, "$bot_src_dir/Factoids")) {
122 &ERROR("can't open source directory Factoids: $!");
126 while (defined(my $file = readdir DIR)) {
127 next unless $file =~ /\.pl$/;
128 next unless $file =~ /^[A-Z]/;
129 my $mod = "$bot_src_dir/Factoids/$file";
130 ### TODO: use eval and exit gracefully?
131 eval "require \"$mod\"";
137 $moduleAge{$mod} = (stat $mod)[9];
138 &showProc(" ($file)") if (&IsParam("DEBUG"));
144 &status("Loading IRC modules...");
145 if (&whatInterface() =~ /IRC/) {
148 &ERROR("libnet-irc-perl is not installed!");
151 &showProc(" (Net::IRC)");
154 &status("IRC support DISABLED.");
158 if (!opendir(DIR, "$bot_src_dir/IRC")) {
159 &ERROR("can't open source directory Factoids: $!");
163 while (defined(my $file = readdir DIR)) {
164 next unless $file =~ /\.pl$/;
165 next unless $file =~ /^[A-Z]/;
166 my $mod = "$bot_src_dir/IRC/$file";
167 ### TODO: use eval and exit gracefully?
169 $moduleAge{$mod} = (stat $mod)[9];
170 &showProc(" ($file)") if (&IsParam("DEBUG"));
175 sub loadMyModulesNow {
179 &status("Loading MyModules...");
180 foreach (@myModulesLoadNow) {
183 &WARN("mMLN: null element.");
189 if (!&IsParam($_) and !&IsChanConf($_)) {
190 if (exists $myModules{$_}) {
191 &DEBUG("myModule: $myModules{$_} (1) not loaded.");
193 &DEBUG("myModule: $_ (2) not loaded.");
199 &loadMyModule($myModules{$_});
203 &status("Module: Loaded/Total [$loaded/$total]");
206 ### rename to moduleReloadAll?
207 sub reloadAllModules {
208 ### &status("Module: reloading all.");
209 foreach (map { substr($_,2) } keys %moduleAge) {
212 ### &status("Module: reloading done.");
215 ### rename to modulesReload?
218 my $file = (grep /\/$mod/, keys %INC)[0];
220 # don't reload if it's not our module.
221 if ($mod =~ /::/ or $mod !~ /pl$/) {
222 &VERB("Not reloading $mod.",3);
226 if (!defined $file) {
227 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
232 &ERROR("rM: file '$file' does not exist?");
236 my $age = (stat $file)[9];
237 return if ($age == $moduleAge{$file});
239 if ($age < $moduleAge{$file}) {
240 &WARN("rM: we're not gonna downgrade the file. use 'touch'.");
244 if (grep /$mod/, @myModulesReloadNot) {
245 &DEBUG("rM: SHOULD NOT RELOAD $mod!!!");
249 my $dc = &Time2String($age - $moduleAge{$file});
250 my $ago = &Time2String(time() - $moduleAge{$file});
252 &status("Module: Loading $mod...");
253 &VERB("Module: delta change: $dc",2);
254 &VERB("Module: ago: $ago",2);
257 eval "require \"$file\""; # require or use?
259 &DEBUG("rM: failure: @$");
261 my $basename = $file;
262 $basename =~ s/^.*\///;
263 &status("Module: reloaded $basename");
264 $moduleAge{$file} = $age;
269 ### OPTIONAL MODULES.
272 local %perlModulesLoaded = ();
273 local %perlModulesMissing = ();
276 return 0 if (exists $perlModulesMissing{$_[0]});
277 &reloadModule($_[0]);
278 return 1 if (exists $perlModulesLoaded{$_[0]});
282 &WARN("Module: $_[0] is not installed!");
283 $perlModulesMissing{$_[0]} = 1;
286 $perlModulesLoaded{$_[0]} = 1;
287 &status("Module: Loaded $_[0] ...");
288 &showProc(" ($_[0])");
296 &WARN("loadMyModule: module is NULL.");
300 my ($modulebase, $modulefile);
301 if (exists $myModules{$tmp}) {
302 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
305 if ($tmp = grep /^$modulebase$/, keys %myModules) {
306 &DEBUG("lMM: lame hack, file => name => $tmp.");
310 my $modulefile = "$bot_src_dir/Modules/$modulebase";
312 # call reloadModule() which checks age of file and reload.
313 if (grep /\/$modulebase$/, keys %INC) {
314 &reloadModule($modulebase);
315 return 1; # depend on reloadModule?
318 if (! -f $modulefile) {
319 &ERROR("lMM: module ($modulebase) does not exist.");
320 if ($$ == $bot_pid) { # parent.
321 &shutdown() if (defined $shm and defined $dbh);
323 &DEBUG("b4 delfork 1");
324 &delForked($modulebase);
330 eval "require \"$modulefile\"";
332 &ERROR("cannot load my module: $modulebase");
333 if ($bot_pid != $$) { # child.
334 &DEBUG("b4 delfork 2");
335 &delForked($modulebase);
341 $moduleAge{$modulefile} = (stat $modulefile)[9];
343 &status("myModule: Loaded $modulebase ...");
344 &showProc(" ($modulebase)");
350 eval "use Time::HiRes qw(gettimeofday tv_interval)";
352 &WARN("No Time::HiRes?");
355 &showProc(" (Time::HiRes)");
358 return if ($AUTOLOAD =~ /__/); # internal.
360 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD");
362 next unless (defined $_);