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 "allowDNS" => "DNS.pl",
26 "debian" => "Debian.pl",
27 "debianExtra" => "DebianExtra.pl",
29 "dumpvars" => "DumpVars.pl",
30 "factoids" => "Factoids.pl",
31 "freshmeat" => "Freshmeat.pl",
32 "kernel" => "Kernel.pl",
33 "ircdcc" => "UserDCC.pl",
34 "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",
49 @myModulesLoadNow = ('topic', 'uptime',);
50 @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
53 if (!opendir(DIR, $bot_src_dir)) {
54 &ERROR("can't open source directory $bot_src_dir: $!");
58 &status("Loading CORE modules...");
60 while (defined(my $file = readdir DIR)) {
61 next unless $file =~ /\.pl$/;
62 next unless $file =~ /^[A-Z]/;
63 my $mod = "$bot_src_dir/$file";
64 ### TODO: use eval and exit gracefully?
66 $moduleAge{$mod} = (stat $mod)[9];
67 &showProc(" ($file)") if (&IsParam("DEBUG"));
73 &status("Loading DB modules...");
75 if ($param{'DBType'} =~ /^mysql$/i) {
78 &ERROR("libdbd-mysql-perl is not installed!");
81 &showProc(" (DBI // mysql)");
83 &status(" using MySQL support.");
84 require "$bot_src_dir/db_mysql.pl";
86 } elsif ($param{'DBType'} =~ /^pgsql$/i) {
89 &ERROR("libpgperl is not installed!");
92 &showProc(" (Pg // postgreSQLl)");
94 &status(" using PostgreSQL support.");
95 require "$bot_src_dir/db_pgsql.pl";
96 } elsif ($param{'DBType'} =~ /^dbm$/i) {
98 &status(" using Berkeley DBM 1.85/2.0 support.");
99 require "$bot_src_dir/db_dbm.pl";
102 &status("DB support DISABLED.");
107 sub loadFactoidsModules {
108 &status("Loading Factoids modules...");
110 if (!&IsParam("factoids")) {
111 &status("Factoid support DISABLED.");
115 if (!opendir(DIR, "$bot_src_dir/Factoids")) {
116 &ERROR("can't open source directory Factoids: $!");
120 while (defined(my $file = readdir DIR)) {
121 next unless $file =~ /\.pl$/;
122 next unless $file =~ /^[A-Z]/;
123 my $mod = "$bot_src_dir/Factoids/$file";
124 ### TODO: use eval and exit gracefully?
126 $moduleAge{$mod} = (stat $mod)[9];
127 &showProc(" ($file)") if (&IsParam("DEBUG"));
133 &status("Loading IRC modules...");
134 if (&whatInterface() =~ /IRC/) {
137 &ERROR("libnet-irc-perl is not installed!");
140 &showProc(" (Net::IRC)");
142 &status("IRC support DISABLED.");
146 if (!opendir(DIR, "$bot_src_dir/IRC")) {
147 &ERROR("can't open source directory Factoids: $!");
151 while (defined(my $file = readdir DIR)) {
152 next unless $file =~ /\.pl$/;
153 next unless $file =~ /^[A-Z]/;
154 my $mod = "$bot_src_dir/IRC/$file";
155 ### TODO: use eval and exit gracefully?
157 $moduleAge{$mod} = (stat $mod)[9];
158 &showProc(" ($file)") if (&IsParam("DEBUG"));
163 sub loadMyModulesNow {
167 &status("Loading MyModules...");
168 foreach (@myModulesLoadNow) {
171 if (!exists $param{$_}) {
172 &DEBUG("myModule: $myModules{$_} not loaded.");
175 &loadMyModule($myModules{$_});
179 &status("Module: Loaded/Total [$loaded/$total]");
182 ### rename to moduleReloadAll?
183 sub reloadAllModules {
184 ### &status("Module: reloading all.");
185 foreach (map { substr($_,2) } keys %moduleAge) {
188 ### &status("Module: reloading done.");
191 ### rename to modulesReload?
194 my $file = (grep /\/$mod/, keys %INC)[0];
196 # don't reload if it's not our module.
197 if ($mod =~ /::/ or $mod !~ /pl$/) {
198 &VERB("Not reloading $mod.",3);
202 if (!defined $file) {
203 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
208 &ERROR("rM: file '$file' does not exist?");
212 my $age = (stat $file)[9];
213 return if ($age == $moduleAge{$file});
215 if ($age < $moduleAge{$file}) {
216 &WARN("rM: we're not gonna downgrade the file. use 'touch'.");
220 if (grep /$mod/, @myModulesReloadNot) {
221 &DEBUG("rM: SHOULD NOT RELOAD $mod!!!");
225 my $dc = &Time2String($age - $moduleAge{$file});
226 my $ago = &Time2String(time() - $moduleAge{$file});
228 &status("Module: Loading $mod...");
229 &VERB("Module: delta change: $dc",2);
230 &VERB("Module: ago: $ago",2);
233 eval "require \"$file\""; # require or use?
235 &DEBUG("rM: failure: @$");
237 my $basename = $file;
238 $basename =~ s/^.*\///;
239 &status("Module: reloaded $basename");
240 $moduleAge{$file} = $age;
245 ### OPTIONAL MODULES.
248 local %perlModulesLoaded = ();
249 local %perlModulesMissing = ();
252 return 0 if (exists $perlModulesMissing{$_[0]});
253 &reloadModule($_[0]);
254 return 1 if (exists $perlModulesLoaded{$_[0]});
258 &WARN("Module: $_[0] is not installed!");
259 $perlModulesMissing{$_[0]} = 1;
262 $perlModulesLoaded{$_[0]} = 1;
263 &status("Module: Loaded $_[0] ...");
264 &showProc(" ($_[0])");
272 &WARN("loadMyModule: module is NULL.");
276 my ($modulebase, $modulefile);
277 if (exists $myModules{$tmp}) {
278 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
281 if ($tmp = grep /^$modulebase$/, keys %myModules) {
282 &DEBUG("lMM: lame hack, file => name => $tmp.");
286 my $modulefile = "$bot_src_dir/Modules/$modulebase";
288 # call reloadModule() which checks age of file and reload.
289 if (grep /\/$modulebase$/, keys %INC) {
290 &reloadModule($modulebase);
291 return 1; # depend on reloadModule?
294 if (! -f $modulefile) {
295 &ERROR("lMM: module ($modulebase) does not exist.");
296 if ($$ == $bot_pid) { # parent.
297 &shutdown() if (defined $shm and defined $dbh);
299 &DEBUG("b4 delfork 1");
300 &delForked($modulebase);
306 eval "require \"$modulefile\"";
308 &ERROR("cannot load my module: $modulebase");
309 if ($bot_pid != $$) { # child.
310 &DEBUG("b4 delfork 2");
311 &delForked($modulebase);
317 $moduleAge{$modulefile} = (stat $modulefile)[9];
319 &status("myModule: Loaded $modulebase ...");
320 &showProc(" ($modulebase)");
325 ### this chews 3megs on potato, 300 kB on slink.
327 ###eval "require 'sys/syscall.ph'";
329 # &WARN("sys/syscall.ph has not been installed//generated. gettimeofday
330 #will use time() instead");
333 #&showProc(" (syscall)");
336 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD");