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";
63 ### TODO: use eval and exit gracefully?
65 $moduleAge{$mod} = (stat $mod)[9];
66 &showProc(" ($file)") if (&IsParam("DEBUG"));
72 &status("Loading DB modules...");
74 if ($param{'DBType'} =~ /^mysql$/i) {
77 &ERROR("libdbd-mysql-perl is not installed!");
80 &showProc(" (DBI // mysql)");
82 &status(" using MySQL support.");
83 require "$bot_src_dir/db_mysql.pl";
85 } elsif ($param{'DBType'} =~ /^pgsql$/i) {
88 &ERROR("libpgperl is not installed!");
91 &showProc(" (Pg // postgreSQLl)");
93 &status(" using PostgreSQL support.");
94 require "$bot_src_dir/db_pgsql.pl";
95 } elsif ($param{'DBType'} =~ /^dbm$/i) {
97 &status(" using Berkeley DBM 1.85/2.0 support.");
98 require "$bot_src_dir/db_dbm.pl";
101 &status("DB support DISABLED.");
106 sub loadFactoidsModules {
107 &status("Loading Factoids modules...");
109 if (!&IsParam("factoids")) {
110 &status("Factoid support DISABLED.");
114 if (!opendir(DIR, "$bot_src_dir/Factoids")) {
115 &ERROR("can't open source directory Factoids: $!");
119 while (defined(my $file = readdir DIR)) {
120 next unless $file =~ /\.pl$/;
121 next unless $file =~ /^[A-Z]/;
122 my $mod = "$bot_src_dir/Factoids/$file";
123 ### TODO: use eval and exit gracefully?
125 $moduleAge{$mod} = (stat $mod)[9];
126 &showProc(" ($file)") if (&IsParam("DEBUG"));
132 &status("Loading IRC modules...");
133 if (&whatInterface() =~ /IRC/) {
136 &ERROR("libnet-irc-perl is not installed!");
139 &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)");
326 eval "use Time::HiRes qw(gettimeofday tv_interval)";
328 &WARN("No Time::HiRes?");
331 &showProc(" (Time::HiRes)");
334 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD");
336 next unless (defined $_);