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",
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",
48 "babelfish" => "babel.pl",
50 ### THIS IS NOT LOADED ON RELOAD :(
52 @myModulesLoadNow = ('topic', 'uptime', 'news');
53 @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
57 if (!opendir(DIR, $bot_src_dir)) {
58 &ERROR("can't open source directory $bot_src_dir: $!");
63 while (defined(my $file = readdir DIR)) {
64 next unless $file =~ /\.pl$/;
65 next unless $file =~ /^[A-Z]/;
69 &status("Loading ".scalar(@mods)." CORE modules...");
71 foreach (sort @mods) {
72 my $mod = "$bot_src_dir/$_";
74 eval "require \"$mod\"";
81 $moduleAge{$mod} = (stat $mod)[9];
82 &showProc(" ($_)") if (&IsParam("DEBUG"));
87 &status("Loading DB modules...");
89 if ($param{'DBType'} =~ /^mysql$/i) {
92 &ERROR("libdbd-mysql-perl is not installed!");
95 &showProc(" (DBI // mysql)");
97 &status(" using MySQL support.");
98 require "$bot_src_dir/db_mysql.pl";
100 } elsif ($param{'DBType'} =~ /^pgsql$/i) {
103 &ERROR("libpgperl is not installed!");
106 &showProc(" (Pg // postgreSQLl)");
108 &status(" using PostgreSQL support.");
109 require "$bot_src_dir/db_pgsql.pl";
110 } elsif ($param{'DBType'} =~ /^dbm$/i) {
112 &status(" using Berkeley DBM 1.85/2.0 support.");
113 require "$bot_src_dir/db_dbm.pl";
116 &status("DB support DISABLED.");
121 sub loadFactoidsModules {
122 &status("Loading Factoids modules...");
124 if (!&IsParam("factoids")) {
125 &status("Factoid support DISABLED.");
129 if (!opendir(DIR, "$bot_src_dir/Factoids")) {
130 &ERROR("can't open source directory Factoids: $!");
134 while (defined(my $file = readdir DIR)) {
135 next unless $file =~ /\.pl$/;
136 next unless $file =~ /^[A-Z]/;
137 my $mod = "$bot_src_dir/Factoids/$file";
138 ### TODO: use eval and exit gracefully?
139 eval "require \"$mod\"";
145 $moduleAge{$mod} = (stat $mod)[9];
146 &showProc(" ($file)") if (&IsParam("DEBUG"));
152 &status("Loading IRC modules...");
153 if (&whatInterface() =~ /IRC/) {
156 &ERROR("libnet-irc-perl is not installed!");
159 &showProc(" (Net::IRC)");
162 &status("IRC support DISABLED.");
166 if (!opendir(DIR, "$bot_src_dir/IRC")) {
167 &ERROR("can't open source directory Factoids: $!");
171 while (defined(my $file = readdir DIR)) {
172 next unless $file =~ /\.pl$/;
173 next unless $file =~ /^[A-Z]/;
174 my $mod = "$bot_src_dir/IRC/$file";
175 ### TODO: use eval and exit gracefully?
177 $moduleAge{$mod} = (stat $mod)[9];
178 &showProc(" ($file)") if (&IsParam("DEBUG"));
183 sub loadMyModulesNow {
187 &status("Loading MyModules...");
188 foreach (@myModulesLoadNow) {
191 &WARN("mMLN: null element.");
195 if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
196 if (exists $myModules{$_}) {
197 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
199 &DEBUG("myModule: $_ (2) not loaded.");
205 &loadMyModule($myModules{$_});
209 &status("Module: Runtime: Loaded/Total [$loaded/$total]");
212 ### rename to moduleReloadAll?
213 sub reloadAllModules {
214 ### &status("Module: reloading all.");
215 foreach (map { substr($_,2) } keys %moduleAge) {
218 ### &status("Module: reloading done.");
221 ### rename to modulesReload?
224 my $file = (grep /\/$mod/, keys %INC)[0];
226 # don't reload if it's not our module.
227 if ($mod =~ /::/ or $mod !~ /pl$/) {
228 &VERB("Not reloading $mod.",3);
232 if (!defined $file) {
233 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
238 &ERROR("rM: file '$file' does not exist?");
242 my $age = (stat $file)[9];
243 return if ($age == $moduleAge{$file});
245 if ($age < $moduleAge{$file}) {
246 &WARN("rM: we're not gonna downgrade the file. use 'touch'.");
250 if (grep /$mod/, @myModulesReloadNot) {
251 &DEBUG("rM: SHOULD NOT RELOAD $mod!!!");
255 my $dc = &Time2String($age - $moduleAge{$file});
256 my $ago = &Time2String(time() - $moduleAge{$file});
258 &status("Module: Loading $mod...");
259 &VERB("Module: delta change: $dc",2);
260 &VERB("Module: ago: $ago",2);
263 eval "require \"$file\""; # require or use?
265 &DEBUG("rM: failure: @$");
267 my $basename = $file;
268 $basename =~ s/^.*\///;
269 &status("Module: reloaded $basename");
270 $moduleAge{$file} = $age;
275 ### OPTIONAL MODULES.
278 local %perlModulesLoaded = ();
279 local %perlModulesMissing = ();
282 return 0 if (exists $perlModulesMissing{$_[0]});
283 &reloadModule($_[0]);
284 return 1 if (exists $perlModulesLoaded{$_[0]});
288 &WARN("Module: $_[0] is not installed!");
289 $perlModulesMissing{$_[0]} = 1;
292 $perlModulesLoaded{$_[0]} = 1;
293 &status("Module: Loaded $_[0] ...");
294 &showProc(" ($_[0])");
302 &WARN("loadMyModule: module is NULL.");
306 my ($modulebase, $modulefile);
307 if (exists $myModules{$tmp}) {
308 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
311 if ($tmp = grep /^$modulebase$/, keys %myModules) {
312 &DEBUG("lMM: lame hack, file => name => $tmp.");
316 my $modulefile = "$bot_src_dir/Modules/$modulebase";
318 # call reloadModule() which checks age of file and reload.
319 if (grep /\/$modulebase$/, keys %INC) {
320 &reloadModule($modulebase);
321 return 1; # depend on reloadModule?
324 if (! -f $modulefile) {
325 &ERROR("lMM: module ($modulebase) does not exist.");
326 if ($$ == $bot_pid) { # parent.
327 &shutdown() if (defined $shm and defined $dbh);
329 &DEBUG("b4 delfork 1");
330 &delForked($modulebase);
336 eval "require \"$modulefile\"";
338 &ERROR("cannot load my module: $modulebase");
339 if ($bot_pid != $$) { # child.
340 &DEBUG("b4 delfork 2");
341 &delForked($modulebase);
347 $moduleAge{$modulefile} = (stat $modulefile)[9];
349 &status("myModule: Loaded $modulebase ...");
350 &showProc(" ($modulebase)");
356 eval "use Time::HiRes qw(gettimeofday tv_interval)";
358 &WARN("No Time::HiRes?");
361 &showProc(" (Time::HiRes)");
364 return if ($AUTOLOAD =~ /__/); # internal.
366 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD");
368 next unless (defined $_);