2 # modules.pl: pseudo-Module handler
4 # Version: v0.2 (20000629)
10 use vars qw($AUTOLOAD);
16 eval "use IO::Socket";
18 &ERROR("no IO::Socket?");
21 &showProc(" (IO::Socket)");
23 ### THIS IS NOT LOADED ON RELOAD :(
25 my @myModulesReloadNot;
27 @myModulesLoadNow = ('Topic', 'Uptime', 'News', 'RootWarn', 'DumpVars2', 'botmail');
28 @myModulesReloadNot = ('IRC/Irc.pl','IRC/Schedulers.pl');
32 my @mods = &getPerlFiles($bot_src_dir);
34 &status("Loading CORE modules...");
36 foreach (sort @mods) {
37 my $mod = "$bot_src_dir/$_";
39 eval "require \"$mod\"";
46 $moduleAge{$mod} = (stat $mod)[9];
47 &showProc(" ($_)") if (&IsParam("DEBUG"));
53 # TODO: use function to load module.
55 if ($param{'DBType'} =~ /^(mysql|SQLite|pgsql)$/i) {
58 &ERROR("No support for DBI::" . $param{'DBType'} . ", exiting!");
61 &status("Loading " . $param{'DBType'} . " support.");
62 $f = "$bot_src_dir/dbi.pl";
64 $moduleAge{$f} = (stat $f)[9];
66 &showProc(" (DBI::" . $param{'DBType'} . ")");
68 &WARN("DB support DISABLED.");
73 sub loadFactoidsModules {
74 if (!&IsParam("factoids")) {
75 &status("Factoid support DISABLED.");
79 &status("Loading Factoids modules...");
81 foreach ( &getPerlFiles("$bot_src_dir/Factoids") ) {
82 my $mod = "$bot_src_dir/Factoids/$_";
84 eval "require \"$mod\"";
90 $moduleAge{$mod} = (stat $mod)[9];
91 &showProc(" ($_)") if (&IsParam("DEBUG"));
96 my ($interface) = &whatInterface();
97 if ($interface =~ /IRC/) {
98 &status("Loading IRC modules...");
102 &ERROR("libnet-irc-perl is not installed!");
105 &showProc(" (Net::IRC)");
107 &status("IRC support DISABLED.");
108 # disabling forking. Why?
109 #$param{forking} = 0;
113 foreach ( &getPerlFiles("$bot_src_dir/$interface") ) {
114 my $mod = "$bot_src_dir/$interface/$_";
116 # hrm... use another config option besides DEBUG to display
117 # change in memory usage.
118 &status("Loading Modules \"$mod\"") if (!&IsParam("DEBUG"));
119 eval "require \"$mod\"";
121 &ERROR("require \"$mod\" => $@");
126 $moduleAge{$mod} = (stat $mod)[9];
127 &showProc(" ($_)") if (&IsParam("DEBUG"));
131 sub loadMyModulesNow {
135 &status("Loading MyModules...");
136 foreach (@myModulesLoadNow) {
139 &WARN("mMLN: null element.");
143 if (!&IsParam($_) and !&IsChanConf($_) and !&getChanConfList($_)) {
144 &DEBUG("loadMyModuleNow: $_ (2) not loaded.");
152 &status("Module: Runtime: Loaded/Total [$loaded/$total]");
155 ### rename to moduleReloadAll?
156 sub reloadAllModules {
159 &VERB("Module: reloading all.",2);
161 # obscure usage of map and regex :)
162 foreach (map { s/.*?\/?src/src/; $_ } keys %moduleAge) {
163 $retval .= &reloadModule($_);
166 &VERB("Module: reloading done.",2);
170 ### rename to modulesReload?
173 my $file = (grep /\/$mod/, keys %INC)[0];
176 # don't reload if it's not our module.
177 if ($mod =~ /::/ or $mod !~ /pl$/) {
178 &VERB("Not reloading $mod.",3);
182 if (!defined $file) {
183 &WARN("rM: Cannot reload $mod since it was not loaded anyway.");
188 &ERROR("rM: file '$file' does not exist?");
192 if (grep /$mod/, @myModulesReloadNot) {
193 &DEBUG("rM: should not reload $mod");
197 my $age = (stat $file)[9];
199 if (!exists $moduleAge{$file}) {
200 &DEBUG("Looks like $file was not loaded; fixing.");
202 return $retval if ($age == $moduleAge{$file});
204 if ($age < $moduleAge{$file}) {
205 &WARN("rM: we're not gonna downgrade '$file'; use touch.");
206 &DEBUG("age => $age");
207 &DEBUG("mA{$file} => $moduleAge{$file}");
211 my $dc = &Time2String($age - $moduleAge{$file});
212 my $ago = &Time2String(time() - $moduleAge{$file});
214 &VERB("Module: delta change: $dc",2);
215 &VERB("Module: ago: $ago",2);
218 &status("Module: Loading $mod...");
221 eval "require \"$file\""; # require or use?
223 &DEBUG("rM: failure: @$ ");
225 my $basename = $file;
226 $basename =~ s/^.*\///;
227 &status("Module: reloaded $basename");
228 $retval = " $basename";
229 $moduleAge{$file} = $age;
235 ### OPTIONAL MODULES.
238 my %perlModulesLoaded = ();
239 my %perlModulesMissing = ();
242 return 0 if (exists $perlModulesMissing{$_[0]});
243 &reloadModule($_[0]);
244 return 1 if (exists $perlModulesLoaded{$_[0]});
248 &WARN("Module: $_[0] is not installed!");
249 $perlModulesMissing{$_[0]} = 1;
252 $perlModulesLoaded{$_[0]} = 1;
253 &status("Loaded $_[0]");
254 &showProc(" ($_[0])");
260 my ($modulename) = @_;
261 if (!defined $modulename) {
262 &WARN("loadMyModule: module is NULL.");
266 my $modulefile = "$bot_src_dir/Modules/$modulename.pl";
268 # call reloadModule() which checks age of file and reload.
269 if (grep /\/$modulename$/, keys %INC) {
270 &reloadModule($modulename);
271 return 1; # depend on reloadModule?
274 if (! -f $modulefile) {
275 &ERROR("lMM: module ($modulename) does not exist.");
276 if ($$ == $bot_pid) { # parent.
277 &shutdown() if (defined $shm and defined $dbh);
279 &DEBUG("b4 delfork 1");
280 &delForked($modulename);
286 eval "require \"$modulefile\"";
288 &ERROR("cannot load my module: $modulename");
289 if ($bot_pid != $$) { # child.
290 &DEBUG("b4 delfork 2");
291 &delForked($modulename);
297 $moduleAge{$modulefile} = (stat $modulefile)[9];
299 &status("Loaded $modulename");
300 &showProc(" ($modulename)");
306 eval "use Time::HiRes qw(gettimeofday tv_interval)";
308 &WARN("No Time::HiRes?");
311 &showProc(" (Time::HiRes)");
314 if (!defined $AUTOLOAD and defined $::AUTOLOAD) {
315 &DEBUG("AUTOLOAD: hrm.. ::AUTOLOAD defined!");
317 return unless (defined $AUTOLOAD);
318 return if ($AUTOLOAD =~ /__/); # internal.
320 my $str = join(', ', @_);
321 my ($package, $filename, $line) = caller;
322 &ERROR("UNKNOWN FUNCTION CALLED: $AUTOLOAD ($str) $filename line $line");
324 $AUTOLOAD =~ s/^(\S+):://g;
326 # hopefully this will work.
327 &DEBUG("Trying to load module $AUTOLOAD...");
328 &loadMyModule($AUTOLOAD);
334 if (!opendir(DIR, $dir)) {
335 &ERROR("Cannot open source directory ($dir): $!");
340 while (defined(my $file = readdir DIR)) {
341 next unless $file =~ /\.pl$/;
342 next unless $file =~ /^[A-Z]/;
347 return reverse sort @mods;