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.");
187 if (!&IsParam($_) and !&IsChanConf($_)) {
188 if (exists $myModules{$_}) {
189 &DEBUG("myModule: $myModules{$_} (1) not loaded.");
191 &DEBUG("myModule: $_ (2) not loaded.");
197 &loadMyModule($myModules{$_});
201 &status("Module: Loaded/Total [$loaded/$total]");
204 ### rename to moduleReloadAll?
205 sub reloadAllModules {
206 ### &status("Module: reloading all.");
207 foreach (map { substr($_,2) } keys %moduleAge) {
210 ### &status("Module: reloading done.");
213 ### rename to modulesReload?
216 my $file = (grep /\/$mod/, keys %INC)[0];
218 # don't reload if it's not our module.
219 if ($mod =~ /::/ or $mod !~ /pl$/) {
220 &VERB("Not reloading $mod.",3);
224 if (!defined $file) {
225 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
230 &ERROR("rM: file '$file' does not exist?");
234 my $age = (stat $file)[9];
235 return if ($age == $moduleAge{$file});
237 if ($age < $moduleAge{$file}) {
238 &WARN("rM: we're not gonna downgrade the file. use 'touch'.");
242 if (grep /$mod/, @myModulesReloadNot) {
243 &DEBUG("rM: SHOULD NOT RELOAD $mod!!!");
247 my $dc = &Time2String($age - $moduleAge{$file});
248 my $ago = &Time2String(time() - $moduleAge{$file});
250 &status("Module: Loading $mod...");
251 &VERB("Module: delta change: $dc",2);
252 &VERB("Module: ago: $ago",2);
255 eval "require \"$file\""; # require or use?
257 &DEBUG("rM: failure: @$");
259 my $basename = $file;
260 $basename =~ s/^.*\///;
261 &status("Module: reloaded $basename");
262 $moduleAge{$file} = $age;
267 ### OPTIONAL MODULES.
270 local %perlModulesLoaded = ();
271 local %perlModulesMissing = ();
274 return 0 if (exists $perlModulesMissing{$_[0]});
275 &reloadModule($_[0]);
276 return 1 if (exists $perlModulesLoaded{$_[0]});
280 &WARN("Module: $_[0] is not installed!");
281 $perlModulesMissing{$_[0]} = 1;
284 $perlModulesLoaded{$_[0]} = 1;
285 &status("Module: Loaded $_[0] ...");
286 &showProc(" ($_[0])");
294 &WARN("loadMyModule: module is NULL.");
298 my ($modulebase, $modulefile);
299 if (exists $myModules{$tmp}) {
300 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
303 if ($tmp = grep /^$modulebase$/, keys %myModules) {
304 &DEBUG("lMM: lame hack, file => name => $tmp.");
308 my $modulefile = "$bot_src_dir/Modules/$modulebase";
310 # call reloadModule() which checks age of file and reload.
311 if (grep /\/$modulebase$/, keys %INC) {
312 &reloadModule($modulebase);
313 return 1; # depend on reloadModule?
316 if (! -f $modulefile) {
317 &ERROR("lMM: module ($modulebase) does not exist.");
318 if ($$ == $bot_pid) { # parent.
319 &shutdown() if (defined $shm and defined $dbh);
321 &DEBUG("b4 delfork 1");
322 &delForked($modulebase);
328 eval "require \"$modulefile\"";
330 &ERROR("cannot load my module: $modulebase");
331 if ($bot_pid != $$) { # child.
332 &DEBUG("b4 delfork 2");
333 &delForked($modulebase);
339 $moduleAge{$modulefile} = (stat $modulefile)[9];
341 &status("myModule: Loaded $modulebase ...");
342 &showProc(" ($modulebase)");
348 eval "use Time::HiRes qw(gettimeofday tv_interval)";
350 &WARN("No Time::HiRes?");
353 &showProc(" (Time::HiRes)");
356 return if ($AUTOLOAD =~ /__/); # internal.
358 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD");
360 next unless (defined $_);