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', 'rootWarn');
53 @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
57 my @mods = &getPerlFiles($bot_src_dir);
59 &status("Loading ".scalar(@mods)." CORE modules...");
61 foreach (sort @mods) {
62 my $mod = "$bot_src_dir/$_";
64 eval "require \"$mod\"";
71 $moduleAge{$mod} = (stat $mod)[9];
72 &showProc(" ($_)") if (&IsParam("DEBUG"));
77 &status("Loading DB modules...");
79 $moduleAge{"$bot_src_dir/modules.pl"} = time();
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_sql.pl";
91 require "$bot_src_dir/db_mysql.pl";
92 $moduleAge{"$bot_src_dir/db_mysql.pl"} = time();
94 } elsif ($param{'DBType'} =~ /^pgsql$/i) {
97 &ERROR("libpgperl is not installed!");
100 &showProc(" (Pg // postgreSQLl)");
102 &status(" using PostgreSQL support.");
103 require "$bot_src_dir/db_sql.pl";
104 require "$bot_src_dir/db_pgsql.pl";
106 } elsif ($param{'DBType'} =~ /^dbm$/i) {
108 &status(" using Berkeley DBM 1.85/2.0 support.");
109 &ERROR("dbm support is broken... if you want it, fix it yourself!");
113 # require "$bot_src_dir/db_dbm.pl";
116 &status("DB support DISABLED.");
121 sub loadFactoidsModules {
122 if (!&IsParam("factoids")) {
123 &status("Factoid support DISABLED.");
127 &status("Loading Factoids modules...");
129 foreach ( &getPerlFiles("$bot_src_dir/Factoids") ) {
130 my $mod = "$bot_src_dir/Factoids/$_";
132 eval "require \"$mod\"";
138 $moduleAge{$mod} = (stat $mod)[9];
139 &showProc(" ($_)") if (&IsParam("DEBUG"));
144 if (&whatInterface() =~ /IRC/) {
145 &status("Loading IRC modules...");
149 &ERROR("libnet-irc-perl is not installed!");
152 &showProc(" (Net::IRC)");
155 &status("IRC support DISABLED.");
159 foreach ( &getPerlFiles("$bot_src_dir/IRC") ) {
160 my $mod = "$bot_src_dir/IRC/$_";
162 eval "require \"$mod\"";
164 &ERROR("lIRCM => $@");
169 $moduleAge{$mod} = (stat $mod)[9];
170 &showProc(" ($_)") if (&IsParam("DEBUG"));
174 sub loadMyModulesNow {
178 &status("Loading MyModules...");
179 foreach (@myModulesLoadNow) {
182 &WARN("mMLN: null element.");
186 if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
187 if (exists $myModules{$_}) {
188 &status("myModule: $myModules{$_} or $_ (1) not loaded.");
190 &DEBUG("myModule: $_ (2) not loaded.");
196 # weird hack to get rootwarn to work.
197 # it may break on other cases though, any ideas?
198 &loadMyModule( $myModules{$_} || $myModules{lc $_} );
202 &status("Module: Runtime: Loaded/Total [$loaded/$total]");
205 ### rename to moduleReloadAll?
206 sub reloadAllModules {
207 &VERB("Module: reloading all.",2);
209 # obscure usage of map and regex :)
210 foreach (map { s/.*?\/?src/src/; $_ } keys %moduleAge) {
214 &VERB("Module: reloading done.",2);
217 ### rename to modulesReload?
220 my $file = (grep /\/$mod/, keys %INC)[0];
222 # don't reload if it's not our module.
223 if ($mod =~ /::/ or $mod !~ /pl$/) {
224 &VERB("Not reloading $mod.",3);
228 if (!defined $file) {
229 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
234 &ERROR("rM: file '$file' does not exist?");
238 if (grep /$mod/, @myModulesReloadNot) {
239 &DEBUG("rM: SHOULD NOT RELOAD $mod!!!");
243 my $age = (stat $file)[9];
245 if (!exists $moduleAge{$file}) {
246 &DEBUG("Looks like $file was not loaded; fixing.");
248 return if ($age == $moduleAge{$file});
250 if ($age < $moduleAge{$file}) {
251 &WARN("rM: we're not gonna downgrade the file. use 'touch'.");
255 my $dc = &Time2String($age - $moduleAge{$file});
256 my $ago = &Time2String(time() - $moduleAge{$file});
258 &VERB("Module: delta change: $dc",2);
259 &VERB("Module: ago: $ago",2);
262 &status("Module: Loading $mod...");
265 eval "require \"$file\""; # require or use?
267 &DEBUG("rM: failure: @$");
269 my $basename = $file;
270 $basename =~ s/^.*\///;
271 &status("Module: reloaded $basename");
272 $moduleAge{$file} = $age;
277 ### OPTIONAL MODULES.
280 local %perlModulesLoaded = ();
281 local %perlModulesMissing = ();
284 return 0 if (exists $perlModulesMissing{$_[0]});
285 &reloadModule($_[0]);
286 return 1 if (exists $perlModulesLoaded{$_[0]});
290 &WARN("Module: $_[0] is not installed!");
291 $perlModulesMissing{$_[0]} = 1;
294 $perlModulesLoaded{$_[0]} = 1;
295 &status("Module: Loaded $_[0] ...");
296 &showProc(" ($_[0])");
304 &WARN("loadMyModule: module is NULL.");
308 my ($modulebase, $modulefile);
309 if (exists $myModules{$tmp}) {
310 ($modulename, $modulebase) = ($tmp, $myModules{$tmp});
313 if ($tmp = grep /^$modulebase$/, keys %myModules) {
314 &DEBUG("lMM: lame hack, file => name => $tmp.");
318 $modulefile = "$bot_src_dir/Modules/$modulebase";
320 # call reloadModule() which checks age of file and reload.
321 if (grep /\/$modulebase$/, keys %INC) {
322 &reloadModule($modulebase);
323 return 1; # depend on reloadModule?
326 if (! -f $modulefile) {
327 &ERROR("lMM: module ($modulebase) does not exist.");
328 if ($$ == $bot_pid) { # parent.
329 &shutdown() if (defined $shm and defined $dbh);
331 &DEBUG("b4 delfork 1");
332 &delForked($modulebase);
338 eval "require \"$modulefile\"";
340 &ERROR("cannot load my module: $modulebase");
341 if ($bot_pid != $$) { # child.
342 &DEBUG("b4 delfork 2");
343 &delForked($modulebase);
349 $moduleAge{$modulefile} = (stat $modulefile)[9];
351 &status("myModule: Loaded $modulebase ...");
352 &showProc(" ($modulebase)");
358 eval "use Time::HiRes qw(gettimeofday tv_interval)";
360 &WARN("No Time::HiRes?");
363 &showProc(" (Time::HiRes)");
366 return if ($AUTOLOAD =~ /__/); # internal.
368 my $str = join(', ', @_);
369 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD ($str)");
371 $AUTOLOAD =~ s/^(\S+):://g;
373 if (exists $myModules{lc $AUTOLOAD}) {
374 # hopefully this will work.
375 &DEBUG("Trying to load module $AUTOLOAD...");
376 &loadMyModule(lc $AUTOLOAD);
383 if (!opendir(DIR, $dir)) {
384 &ERROR("cannot open source directory $dir: $!");
389 while (defined(my $file = readdir DIR)) {
390 next unless $file =~ /\.pl$/;
391 next unless $file =~ /^[A-Z]/;
396 return reverse sort @mods;