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",
47 "babelfish" => "babel.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";
65 ### TODO: use eval and exit gracefully?
66 eval "require \"$mod\"";
73 $moduleAge{$mod} = (stat $mod)[9];
74 &showProc(" ($file)") if (&IsParam("DEBUG"));
80 &status("Loading DB modules...");
82 if ($param{'DBType'} =~ /^mysql$/i) {
85 &ERROR("libdbd-mysql-perl is not installed!");
88 &showProc(" (DBI // mysql)");
90 &status(" using MySQL support.");
91 require "$bot_src_dir/db_mysql.pl";
93 } elsif ($param{'DBType'} =~ /^pgsql$/i) {
96 &ERROR("libpgperl is not installed!");
99 &showProc(" (Pg // postgreSQLl)");
101 &status(" using PostgreSQL support.");
102 require "$bot_src_dir/db_pgsql.pl";
103 } elsif ($param{'DBType'} =~ /^dbm$/i) {
105 &status(" using Berkeley DBM 1.85/2.0 support.");
106 require "$bot_src_dir/db_dbm.pl";
109 &status("DB support DISABLED.");
114 sub loadFactoidsModules {
115 &status("Loading Factoids modules...");
117 if (!&IsParam("factoids")) {
118 &status("Factoid support DISABLED.");
122 if (!opendir(DIR, "$bot_src_dir/Factoids")) {
123 &ERROR("can't open source directory Factoids: $!");
127 while (defined(my $file = readdir DIR)) {
128 next unless $file =~ /\.pl$/;
129 next unless $file =~ /^[A-Z]/;
130 my $mod = "$bot_src_dir/Factoids/$file";
131 ### TODO: use eval and exit gracefully?
132 eval "require \"$mod\"";
138 $moduleAge{$mod} = (stat $mod)[9];
139 &showProc(" ($file)") if (&IsParam("DEBUG"));
145 &status("Loading IRC modules...");
146 if (&whatInterface() =~ /IRC/) {
149 &ERROR("libnet-irc-perl is not installed!");
152 &showProc(" (Net::IRC)");
155 &status("IRC support DISABLED.");
159 if (!opendir(DIR, "$bot_src_dir/IRC")) {
160 &ERROR("can't open source directory Factoids: $!");
164 while (defined(my $file = readdir DIR)) {
165 next unless $file =~ /\.pl$/;
166 next unless $file =~ /^[A-Z]/;
167 my $mod = "$bot_src_dir/IRC/$file";
168 ### TODO: use eval and exit gracefully?
170 $moduleAge{$mod} = (stat $mod)[9];
171 &showProc(" ($file)") if (&IsParam("DEBUG"));
176 sub loadMyModulesNow {
180 &status("Loading MyModules...");
181 foreach (@myModulesLoadNow) {
184 &WARN("mMLN: null element.");
188 if (!&IsParam($_) and !&IsChanConf($_)) {
189 if (exists $myModules{$_}) {
190 &DEBUG("myModule: $myModules{$_} (1) not loaded.");
192 &DEBUG("myModule: $_ (2) not loaded.");
198 &loadMyModule($myModules{$_});
202 &status("Module: Loaded/Total [$loaded/$total]");
205 ### rename to moduleReloadAll?
206 sub reloadAllModules {
207 ### &status("Module: reloading all.");
208 foreach (map { substr($_,2) } keys %moduleAge) {
211 ### &status("Module: reloading done.");
214 ### rename to modulesReload?
217 my $file = (grep /\/$mod/, keys %INC)[0];
219 # don't reload if it's not our module.
220 if ($mod =~ /::/ or $mod !~ /pl$/) {
221 &VERB("Not reloading $mod.",3);
225 if (!defined $file) {
226 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
231 &ERROR("rM: file '$file' does not exist?");
235 my $age = (stat $file)[9];
236 return if ($age == $moduleAge{$file});
238 if ($age < $moduleAge{$file}) {
239 &WARN("rM: we're not gonna downgrade the file. use 'touch'.");
243 if (grep /$mod/, @myModulesReloadNot) {
244 &DEBUG("rM: SHOULD NOT RELOAD $mod!!!");
248 my $dc = &Time2String($age - $moduleAge{$file});
249 my $ago = &Time2String(time() - $moduleAge{$file});
251 &status("Module: Loading $mod...");
252 &VERB("Module: delta change: $dc",2);
253 &VERB("Module: ago: $ago",2);
256 eval "require \"$file\""; # require or use?
258 &DEBUG("rM: failure: @$");
260 my $basename = $file;
261 $basename =~ s/^.*\///;
262 &status("Module: reloaded $basename");
263 $moduleAge{$file} = $age;
268 ### OPTIONAL MODULES.
271 local %perlModulesLoaded = ();
272 local %perlModulesMissing = ();
275 return 0 if (exists $perlModulesMissing{$_[0]});
276 &reloadModule($_[0]);
277 return 1 if (exists $perlModulesLoaded{$_[0]});
281 &WARN("Module: $_[0] is not installed!");
282 $perlModulesMissing{$_[0]} = 1;
285 $perlModulesLoaded{$_[0]} = 1;
286 &status("Module: Loaded $_[0] ...");
287 &showProc(" ($_[0])");
295 &WARN("loadMyModule: module is NULL.");
299 my ($modulebase, $modulefile);
300 if (exists $myModules{$tmp}) {
301 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
304 if ($tmp = grep /^$modulebase$/, keys %myModules) {
305 &DEBUG("lMM: lame hack, file => name => $tmp.");
309 my $modulefile = "$bot_src_dir/Modules/$modulebase";
311 # call reloadModule() which checks age of file and reload.
312 if (grep /\/$modulebase$/, keys %INC) {
313 &reloadModule($modulebase);
314 return 1; # depend on reloadModule?
317 if (! -f $modulefile) {
318 &ERROR("lMM: module ($modulebase) does not exist.");
319 if ($$ == $bot_pid) { # parent.
320 &shutdown() if (defined $shm and defined $dbh);
322 &DEBUG("b4 delfork 1");
323 &delForked($modulebase);
329 eval "require \"$modulefile\"";
331 &ERROR("cannot load my module: $modulebase");
332 if ($bot_pid != $$) { # child.
333 &DEBUG("b4 delfork 2");
334 &delForked($modulebase);
340 $moduleAge{$modulefile} = (stat $modulefile)[9];
342 &status("myModule: Loaded $modulebase ...");
343 &showProc(" ($modulebase)");
349 eval "use Time::HiRes qw(gettimeofday tv_interval)";
351 &WARN("No Time::HiRes?");
354 &showProc(" (Time::HiRes)");
357 return if ($AUTOLOAD =~ /__/); # internal.
359 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD");
361 next unless (defined $_);