diff --git a/src/API/Std.pm b/src/API/Std.pm index 976db83..e367b15 100644 --- a/src/API/Std.pm +++ b/src/API/Std.pm @@ -25,35 +25,37 @@ sub mod_init # 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. + + # 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; } - + # Run the module's _init sub. - my $mi = eval($pkg."::_init();"); - + 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."); - + + 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; + API::Log::dbug('MODULES: Failed to load '.$name.q{.}); + API::Log::alog('MODULES: Failed to load '.$name.q{.}); + + return; } } @@ -61,43 +63,46 @@ sub mod_init sub mod_exists { my ($name) = @_; - - return 1 if defined $API::Std::MODULE{$name}; - return 0; + + if (defined $API::Std::MODULE{$name}) { return 1; } + + return; } # 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."..."); - + 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; + API::Log::dbug('MODULES: Failed to unload '.$module.'. No such module?'); + API::Log::alog('MODULES: Failed to unload '.$module.'. No such module?'); + return; } - + # Run the module's _void sub. - my $mi = eval($MODULE{$module}{pkg}."::_void();"); - + 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."."); + API::Log::dbug('MODULES: Successfully unloaded '.$module.q{.}); + API::Log::alog('MODULES: Successfully unloaded '.$module.q{.}); 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; + API::Log::dbug('MODULES: Failed to unload '.$module.q{.}); + API::Log::alog('MODULES: Failed to unload '.$module.q{.}); + return; } } @@ -105,16 +110,16 @@ sub mod_void sub cmd_add { my ($cmd, $lvl, $priv, $help, $sub) = @_; - $cmd = uc($cmd); - - return 0 if (defined $API::Std::CMDS{$cmd}); - return 0 if ($lvl =~ m/[^0-2]/); - + $cmd = uc $cmd; + + if (defined $API::Std::CMDS{$cmd}) { return; } + if ($lvl =~ m/[^0-2]/) { return; } + $API::Std::CMDS{$cmd}{lvl} = $lvl; $API::Std::CMDS{$cmd}{help} = $help; $API::Std::CMDS{$cmd}{priv} = $priv; $API::Std::CMDS{$cmd}{sub} = $sub; - + return 1; } @@ -123,15 +128,15 @@ sub cmd_add sub cmd_del { my ($cmd) = @_; - $cmd = uc($cmd); - + $cmd = uc $cmd; + if (defined $API::Std::CMDS{$cmd}) { delete $API::Std::CMDS{$cmd}; } else { - return 0; + return; } - + return 1; } @@ -139,14 +144,14 @@ sub cmd_del sub event_add { my ($name) = @_; - - unless (defined $EVENTS{lc($name)}) { + + if (!defined $EVENTS{lc($name)}) { $EVENTS{lc($name)} = 1; return 1; } else { - API::Log::dbug("DEBUG: Attempt to add a pre-existing event[".lc($name)."]! Ignoring..."); - return 0; + API::Log::dbug('DEBUG: Attempt to add a pre-existing event ('.lc $name.')! Ignoring...'); + return; } } @@ -154,15 +159,15 @@ sub event_add sub event_del { my ($name) = @_; - + if (defined $EVENTS{lc($name)}) { delete $EVENTS{lc($name)}; delete $HOOKS{lc($name)}; return 1; } else { - API::Log::dbug("DEBUG: Attempt to delete a non-existing event[".lc($name)."! Ignoring..."); - return 0; + API::Log::dbug('DEBUG: Attempt to delete a non-existing event ('.lc $name.')! Ignoring...'); + return; } } @@ -170,13 +175,13 @@ sub event_del sub event_run { my ($event, @args) = @_; - - if (defined $EVENTS{lc($event)} and defined $HOOKS{lc($event)}) { - foreach my $hk (keys %{ $HOOKS{lc($event)} }) { - &{ $HOOKS{lc($event)}{$hk} }(@args); + + if (defined $EVENTS{lc $event} and defined $HOOKS{lc $event}) { + foreach my $hk (keys %{ $HOOKS{lc $event} }) { + &{ $HOOKS{lc $event}{$hk} }(@args); } } - + return 1; } @@ -184,18 +189,18 @@ sub event_run sub hook_add { my ($event, $name, $sub) = @_; - - if (!defined $API::Std::HOOKS{lc($name)}) { - if (defined $API::Std::EVENTS{lc($event)}) { - $API::Std::HOOKS{lc($event)}{lc($name)} = $sub; + + if (!defined $API::Std::HOOKS{lc $name}) { + if (defined $API::Std::EVENTS{lc $event}) { + $API::Std::HOOKS{lc $event}{lc $name} = $sub; return 1; } else { - return 0; + return; } } else { - return 0; + return; } } @@ -203,13 +208,13 @@ sub hook_add sub hook_del { my ($event, $name) = @_; - - if (defined $API::Std::HOOKS{lc($event)}{lc($name)}) { - delete $API::Std::HOOKS{lc($event)}{lc($name)}; + + if (defined $API::Std::HOOKS{lc $event}{lc $name}) { + delete $API::Std::HOOKS{lc $event}{lc $name}; return 1; } else { - return 0; + return; } } @@ -217,52 +222,51 @@ sub hook_del sub timer_add { my ($name, $type, $time, $sub) = @_; - $name = lc($name); - + $name = lc $name; + # Check for invalid type/time. if ($type =~ m/[^1-2]/) { - return 0; + return; } if ($time =~ m/[^0-9]/) { - return 0; + return; } - - unless (defined $Auto::TIMERS{$name}) { + + if (!defined $Auto::TIMERS{$name}) { $Auto::TIMERS{$name}{type} = $type; $Auto::TIMERS{$name}{time} = time + $time; - $Auto::TIMERS{$name}{secs} = $time if ($type == 2); + if ($type == 2) { $Auto::TIMERS{$name}{secs} = $time; } $Auto::TIMERS{$name}{sub} = $sub; return 1; } - else { - return 0; - } + + return 1; } # Delete a timer from Auto. sub timer_del { my ($name) = @_; - $name = lc($name); - + $name = lc $name; + if (defined $Auto::TIMERS{$name}) { delete $Auto::TIMERS{$name}; return 1; } - - return 0; + + return; } # Hook onto a raw command. sub rchook_add { my ($cmd, $sub) = @_; - $cmd = uc($cmd); - - return 0 if defined $Parser::IRC::RAWC{$cmd}; - + $cmd = uc $cmd; + + if (defined $Parser::IRC::RAWC{$cmd}) { return; } + $Parser::IRC::RAWC{$cmd} = $sub; - + return 1; } @@ -270,12 +274,12 @@ sub rchook_add sub rchook_del { my ($cmd) = @_; - $cmd = uc($cmd); - - return 0 if !defined $Parser::IRC::RAWC{$cmd}; - + $cmd = uc $cmd; + + if (!defined $Parser::IRC::RAWC{$cmd}) { return; } + delete $Parser::IRC::RAWC{$cmd}; - + return 1; } @@ -283,24 +287,24 @@ sub rchook_del sub conf_get { my ($value) = @_; - + # Create an array out of the value. my @val; if ($value =~ m/:/) { - @val = split(':', $value); + @val = split ':', $value; } else { @val = ($value); } # Undefine this as it's unnecessary now. undef $value; - + # Get the count of elements in the array. my $count = scalar(@val); - + # Return the requested configuration value(s). if ($count == 1) { - if (ref($Auto::SETTINGS{$val[0]}) eq 'HASH') { + if (ref $Auto::SETTINGS{$val[0]} eq 'HASH') { return %{ $Auto::SETTINGS{$val[0]} }; } else { @@ -308,7 +312,7 @@ sub conf_get } } elsif ($count == 2) { - if (ref($Auto::SETTINGS{$val[0]}{$val[1]}) eq 'HASH') { + if (ref $Auto::SETTINGS{$val[0]}{$val[1]} eq 'HASH') { return %{ $Auto::SETTINGS{$val[0]}{$val[1]} }; } else { @@ -316,7 +320,7 @@ sub conf_get } } elsif ($count == 3) { - if (ref($Auto::SETTINGS{$val[0]}{$val[1]}{$val[2]}) eq 'HASH') { + if (ref $Auto::SETTINGS{$val[0]}{$val[1]}{$val[2]} eq 'HASH') { return %{ $Auto::SETTINGS{$val[0]}{$val[1]}{$val[2]} }; } else { @@ -324,8 +328,8 @@ sub conf_get } } else { - return 0; - } + return; + } } # Translation subroutine. @@ -333,7 +337,7 @@ sub trans { my ($id) = @_; $id =~ s/ /_/g; - + if (defined $API::Std::LANGE{$id}) { return $API::Std::LANGE{$id}; } @@ -349,9 +353,9 @@ sub match_user my (%user) = @_; # Get data from config. - return 0 if (!conf_get("user")); + if (!conf_get("user")) { return; } my %uhp = conf_get("user"); - + foreach my $userkey (keys %uhp) { # For each user block. my %ulhp = %{ $uhp{$userkey} }; @@ -360,7 +364,7 @@ sub match_user if ($uhk eq "net") { if (defined $user{svr}) { - if (lc($user{svr}) ne lc(($ulhp{$uhk})[0][0])) { + if (lc $user{svr} ne lc ($ulhp{$uhk})[0][0]) { # config.user:net conflicts with irc.user:svr. last; } @@ -375,14 +379,14 @@ sub match_user } } elsif ($uhk eq "chanstatus" and defined $ulhp{'net'}) { - my ($ccst, $ccnm) = split(':', ($ulhp{$uhk})[0][0]); + my ($ccst, $ccnm) = split ':', ($ulhp{$uhk})[0][0]; my $svr = $ulhp{net}[0]; if (defined $Auto::SOCKET{$svr}) { foreach my $bcj (@{ $Parser::IRC::botchans{$svr} }) { if (API::IRC::match_mask($bcj, $ccnm)) { API::IRC::names($svr, $bcj); if (defined $Parser::IRC::chanusers{$svr}{$bcj}{$user{nick}}) { - return $userkey if $Parser::IRC::chanusers{$svr}{$bcj}{$user{nick}} =~ m/($ccst)/; + if ($Parser::IRC::chanusers{$svr}{$bcj}{$user{nick}} =~ m/($ccst)/) { return $userkey; } } } } @@ -390,41 +394,41 @@ sub match_user } } } - - return 0; + + return; } # Privilege subroutine. sub has_priv { my ($cuser, $cpriv) = @_; - + if (conf_get("user:$cuser:privs")) { my $cups = (conf_get("user:$cuser:privs"))[0][0]; - + if (defined $Auto::PRIVILEGES{$cups}) { foreach (@{ $Auto::PRIVILEGES{$cups} }) { - return 1 if ($_ eq $cpriv or $_ eq "ALL"); + if ($_ eq $cpriv or $_ eq "ALL") { return 1; } } } } - - return 0; + + return; } # Error subroutine. sub err { my ($lvl, $msg, $fatal) = @_; - + # Check for an invalid level. if ($lvl =~ m/[^0-9]/) { - return 0; + return; } if ($fatal =~ m/[^0-1]/) { - return 0; + return; } - + # Level 1: Print to screen. if ($lvl >= 1) { API::Log::println("ERROR: $msg"); @@ -433,12 +437,10 @@ sub err if ($lvl >= 2) { API::Log::alog("ERROR: $msg"); } - + # If it's a fatal error, exit the program. - if ($fatal) { - exit; - } - + if ($fatal) { exit; } + return 1; } @@ -446,12 +448,12 @@ sub err sub awarn { my ($lvl, $msg) = @_; - + # Check for an invalid level. if ($lvl =~ m/[^0-9]/) { - return 0; + return; } - + # Level 1: Print to screen. if ($lvl >= 1) { API::Log::println("WARNING: $msg"); @@ -460,7 +462,7 @@ sub awarn if ($lvl >= 2) { API::Log::alog("WARNING: $msg"); } - + return 1; }