diff --git a/doc/CHANGELOG b/doc/CHANGELOG index 9a6f8a9..3e9e2da 100644 --- a/doc/CHANGELOG +++ b/doc/CHANGELOG @@ -3,6 +3,7 @@ Auto IRC Bot 3.0: Change Log 3.0 Indev ================================================================== + * Added mod_init() and mod_void() to API::Std. * Use Class::Unload. * Handle SIGTERM, SIGINT and SIGHUP. * Added quit() to API::IRC. diff --git a/src/API/Std.pm b/src/API/Std.pm index 68c22f6..bdf4de8 100644 --- a/src/API/Std.pm +++ b/src/API/Std.pm @@ -11,19 +11,82 @@ use Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw(conf_get trans err); -my %LANGE; +my (%LANGE, %MODULE); # Initialize a module. sub mod_init { + my ($name, $author, $version, $autover, $pkg) = @_; + # Log/debug. + API::Log::dbug("MODULES: Attempting to load ".$name." (version ".$version.") by ".$author."..."); + API::Log::alog("MODULES: Attempting to load ".$name." (version ".$version.") by ".$author."..."); + + # Check if this module is compatible with this version of Auto. + if ($autover ne "3.0.0d") { + API::Log::dbug("MODULES: Failed to load ".$name.": Incompatible with your version of Auto."); + API::Log::alog("MODULES: Failed to load ".$name.": Incompatible with your version of Auto."); + return 0; + } + + # Run the module's _init sub. + my $mi = eval($pkg."::_init();"); + + if ($mi) { + # If successful, add to hash. + $MODULE{$name}{name} = $name; + $MODULE{$name}{version} = $version; + $MODULE{$name}{author} = $author; + $MODULE{$name}{pkg} = $pkg; + + API::Log::dbug("MODULES: ".$name." successfully loaded."); + API::Log::alog("MODULES: ".$name." successfully loaded."); + + return 1; + } + else { + # Otherwise, return a failed to load message. + API::Log::dbug("MODULES: Failed to load ".$name."."); + API::Log::alog("MODULES: Failed to load ".$name."."); + + return 0; + } } # Void a module. sub mod_void { + my ($module) = @_; + # Log/debug. + API::Log::dbug("MODULES: Attempting to unload module: ".$module."..."); + API::Log::alog("MODULES: Attempting to unload module: ".$module."..."); + + # Check if this module exists. + unless (defined $MODULE{$module}) { + API::Log::dbug("MODULES: Failed to unload ".$module.". No such module?"); + API::Log::alog("MODULES: Failed to unload ".$module.". No such module?"); + return 0; + } + + # Run the module's _init sub. + my $mi = eval($MODULE{$module}{pkg}."::_void();"); + + if ($mi) { + # If successful, delete class from program and delete module from hash. + Class::Unload->unload($MODULE{$module}{pkg}); + delete $MODULE{$module}; + API::Log::dbug("MODULES: Successfully unloaded ".$module."."); + API::Log::alog("MODULES: Successfully unloaded ".$module."."); + return 1; + } + else { + # Otherwise, return a failed to unload message. + API::Log::dbug("MODULES: Failed to unload ".$module."."); + API::Log::alog("MODULES: Failed to unload ".$module."."); + return 0; + } } # Add a command to Auto.