]> git.donarmstrong.com Git - infobot.git/blobdiff - src/CommandStubs.pl
news: added news->factoid redirection
[infobot.git] / src / CommandStubs.pl
index 1b7f132aeea69d01cbf8ea8e5f376387c45020a2..a4c3679c315e2c49f88d9efe248a5f19c79d7a10 100644 (file)
@@ -35,9 +35,9 @@ sub addCmdHook {
 sub parseCmdHook {
     my ($hashname, $line) = @_;
     $line =~ /^(\S+)( (.*))?$/;
-    my @args   = split(' ', $3 || '');
-    my $flatarg        = $3;
     my $cmd    = $1;   # command name is whitespaceless.
+    my $flatarg        = $3;
+    my @args   = split(/\s+/, $flatarg || '');
     my $done   = 0;
 
     &shmFlush();
@@ -57,7 +57,7 @@ sub parseCmdHook {
            next;
        }
 
-       &DEBUG("pCH(hooks_$hashname): $cmd matched $ident");
+       &status("hooks($hashname): $cmd matched '$ident'");
        my %hash = %{ ${"hooks_$hashname"}{$ident} };
 
        if (!scalar keys %hash) {
@@ -65,6 +65,11 @@ sub parseCmdHook {
            return 1;
        }
 
+       if ($hash{NoArgs} and $flatarg) {
+           &DEBUG("cmd $ident does not take args; skipping.");
+           next;
+       }
+
        if (!exists $hash{CODEREF}) {
            &ERROR("CODEREF undefined for $cmd or $ident.");
            return 1;
@@ -72,7 +77,7 @@ sub parseCmdHook {
 
        ### DEBUG.
        foreach (keys %hash) {
-           &DEBUG(" $cmd->$_ => '$hash{$_}'.");
+           &VERB(" $cmd->$_ => '$hash{$_}'.",2);
        }
 
        ### HELP.
@@ -95,18 +100,24 @@ sub parseCmdHook {
        if (exists $hash{'Forker'}) {
            $hash{'Identifier'} .= "-" if ($hash{'Forker'} eq "NULL");
 
-           ### FLAT_ARG / ARRAY option.
+           if (exists $hash{'ArrayArgs'}) {
+               &Forker($hash{'Identifier'}, sub { \&{$hash{'CODEREF'}}(@args) } );
+           } else {
+               &Forker($hash{'Identifier'}, sub { \&{$hash{'CODEREF'}}($flatarg) } );
+           }
 
-           &Forker($hash{'Identifier'}, sub { \&{$hash{'CODEREF'}}(@args) } );
        } else {
            if (exists $hash{'Module'}) {
                &loadMyModule($myModules{ $hash{'Module'} });
            }
 
-           ### TODO: check if CODEREF exists.
+           # check if CODEREF exists.
+           if (!defined &{ $hash{'CODEREF'} }) {
+               &WARN("coderef $hash{'CODEREF'} don't exist.");
+               return 1;
+           }
 
-           if (exists $hash{'FlatArg'} and $hash{'FlatArg'} == 0) {
-               &status("CmdHook: using args as array.");
+           if (exists $hash{'ArrayArgs'}) {
                &{$hash{'CODEREF'}}(@args);
            } else {
                &{$hash{'CODEREF'}}($flatarg);
@@ -118,7 +129,7 @@ sub parseCmdHook {
            $cmdstats{ $hash{'Cmdstats'} }++;
        }
 
-       &DEBUG("pCH: ended.");
+       &VERB("hooks: End of command.",2);
 
        $done = 1;
     }
@@ -156,7 +167,7 @@ sub parseCmdHook {
        'Forker' => 1, 'Identifier' => 'insult', 'Help' => "insult" ) );
 &addCmdHook("extra", 'kernel', ('CODEREF' => 'Kernel::Kernel',
        'Forker' => 1, 'Identifier' => 'kernel',
-       'Cmdstats' => 'Kernel') );
+       'Cmdstats' => 'Kernel', 'NoArgs' => 1) );
 &addCmdHook("extra", 'listauth', ('CODEREF' => 'CmdListAuth',
        'Identifier' => 'search', Module => 'factoids', 
        'Help' => 'listauth') );
@@ -189,7 +200,6 @@ sub parseCmdHook {
 &addCmdHook("extra", 'uptime', ('CODEREF' => 'uptime', 'Identifier' => 'uptime',
        'Cmdstats' => 'Uptime') );
 &addCmdHook("extra", 'nullski', ('CODEREF' => 'nullski', ) );
-       sub nullski { my ($arg) = @_; foreach (`$arg`) { &msg($who,$_); } }
 &addCmdHook("extra", '(fm|freshmeat)', ('CODEREF' => 'Freshmeat::Freshmeat',
        'Identifier' => 'freshmeat', 'Cmdstats' => 'Freshmeat',
        'Forker' => 1, 'Help' => 'freshmeat') );
@@ -256,6 +266,12 @@ sub Modules {
 
        my $thiscmd     = lc($1);
        my $args        = $3;
+       $args           =~ s/\s+$//g;
+       # suggested by asuffield nad \broken.
+       if ($args =~ /^["']/ and $args =~ /["']$/) {
+           &DEBUG("list*: removed quotes.");
+           $args       =~ s/^["']|["']$//g;
+       }
 
        $thiscmd =~ s/^vals$/values/;
        return if ($thiscmd ne "keys" && $thiscmd ne "values");
@@ -345,7 +361,7 @@ sub Modules {
        return unless (&hasParam("topic"));
 
        my $chan        = $talkchannel;
-       my @args        = split(/ /, $2);
+       my @args        = split / /, $2 || "";
 
        if (!scalar @args) {
            &msg($who,"Try 'help topic'");
@@ -674,14 +690,18 @@ sub do_verstats {
 
     &msg($who, "Sending CTCP VERSION...");
     $conn->ctcp("VERSION", $chan);
-    $cache{verstats}   = $chan;
+    $cache{verstats}{chan}     = $chan;
+    $cache{verstats}{who}      = $who;
+    $cache{verstats}{msgType}  = $msgType;
 
     $conn->schedule(60, sub {
        my $vtotal      = 0;
-       my $c           = lc $cache{verstats};
+       my $c           = lc $cache{verstats}{chan};
        my $total       = keys %{ $channels{$c}{''} };
-       delete $cache{verstats};
        $chan           = $c;
+       $who            = $cache{verstats}{who};
+       $msgType        = $cache{verstats}{msgType};
+       delete $cache{verstats};        # sufficient?
 
        foreach (keys %ver) {
            $vtotal     += scalar keys %{ $ver{$_} };
@@ -720,4 +740,7 @@ sub do_verstats {
     return;
 }
 
+sub nullski { my ($arg) = @_; return unless (defined $arg);
+       foreach (`$arg`) { &msg($who,$_); } }
+
 1;