]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Factoids/Question.pl
* Add vim formatting comments ( # vim:ts=4:sw=4:expandtab:tw=80 )
[infobot.git] / src / Factoids / Question.pl
index 4451aaa30d6e3f2224a312bf8e3b33ff4c4f85fd..39b5df2644fc7ca36e532233b2f697ceb4ce781b 100644 (file)
@@ -20,7 +20,7 @@ sub doQuestion {
     # my doesn't allow variables to be inherinted, local does.
     # following is used in math()...
     local($query)      = @_;
-    local($reply)      = "";
+    local($reply)      = '';
     local $finalQMark  = $query =~ s/\?+\s*$//;
     $finalQMark                += $query =~ s/\?\s*$//;
     $query             =~ s/^\s+|\s+$//g;
@@ -29,19 +29,19 @@ sub doQuestion {
        return '';
     }
 
-    my $questionWord   = "";
+    my $questionWord   = '';
 
     if (!$addressed) {
        return '' unless ($finalQMark);
-       return '' if (&IsParam("minVolunteerLength") == 0);
-       return '' if (length $query < $param{'minVolunteerLength'});
+       return '' unless &IsChanConf('minVolunteerLength') > 0;
+       return '' if (length $query < &::getChanConf('minVolunteerLength'));
     } else {
        ### TODO: this should be caught in Process.pl?
        return '' unless ($talkok);
 
        # there is no flag to disable/enable asking factoids...
        # so it was added... thanks zyxep! :)
-       if (&IsFlag("a") ne "a" && &IsFlag("o") ne "o") {
+       if (&IsFlag('a') ne 'a' && &IsFlag('o') ne 'o') {
            &status("$who tried to ask us when not allowed.");
            return;
        }
@@ -87,13 +87,13 @@ sub doQuestion {
        $questionWord = lc($1);
     }
 
-    if ($questionWord eq "" and $finalQMark and $addressed) {
-       $questionWord = "where";
+    if ($questionWord eq '' and $finalQMark and $addressed) {
+       $questionWord = 'where';
     }
     $query =~ s/^\s+|\s+$//g; # bleh. hacked.
     push(@query, $query) if ($query ne $x);
 
-    if (&IsChanConf("factoidArguments")) {
+    if (&IsChanConf('factoidArguments') > 0) {
        $result = &factoidArgs($query[0]);
 
        return $result if (defined $result);
@@ -103,7 +103,7 @@ sub doQuestion {
     for (my$i=0; $i<scalar @query; $i++) {
        $query  = $query[$i];
        $result = &getReply($query);
-       next if (!defined $result or $result eq "");
+       next if (!defined $result or $result eq '');
 
        # 'see also' factoid redirection support.
 
@@ -132,14 +132,13 @@ sub doQuestion {
                # support command redirection.
                # recursive cmdHooks aswell :)
                my $done = 0;
-               $done++ if &parseCmdHook("main", $link);
-               $done++ if &parseCmdHook("extra", $link);
+               $done++ if &parseCmdHook($link);
                $message        = $link;
                $done++ unless (&Modules());
 
                return;
            }
-           last if (!defined $newr or $newr eq "");
+           last if (!defined $newr or $newr eq '');
            $result  = $newr;
        }
 
@@ -155,7 +154,7 @@ sub doQuestion {
     }
 
     ### TODO: Use &Forker(); move function to Debian.pl
-    if (&IsChanConf('debianForFactoid')) {
+    if (&IsChanConf('debianForFactoid') > 0) {
        &loadMyModule('Debian');
        $result = &Debian::DebianFind($query);  # ???
        ### TODO: debian module should tell, through shm, that it went
@@ -163,13 +162,13 @@ sub doQuestion {
 ###    return $result if (defined $result);
     }
 
-    if ($questionWord ne "" or $finalQMark) {
+    if ($questionWord ne '' or $finalQMark) {
        # if it has not been explicitly marked as a question
-       if ($addressed and $reply eq "") {
+       if ($addressed and $reply eq '') {
            &status("notfound: <$who> ".join(' :: ', @query))
                                                if ($finalQMark);
 
-           return '' unless (&IsParam("friendlyBots"));
+           return '' unless (&IsParam('friendlyBots'));
 
            foreach (split /\s+/, $param{'friendlyBots'}) {
                &msg($_, ":INFOBOT:QUERY <$who> $query");
@@ -189,7 +188,10 @@ sub factoidArgs {
     # TODO: cache this, update cache when altered. !!! !!! !!!
 #    my $t = &timeget();
     my ($first) = split(/\s+/, $str);
-    my @list = &searchTable("factoids", "factoid_key", "factoid_key", "^CMD: $first ");
+
+    # ignore split to commands [dumb commands vs. factoids] (editing commands?)
+    return undef if $str =~ /\s+\=\~\s+s[\#\/\:]/;
+    my @list = &searchTable('factoids', 'factoid_key', 'factoid_key', "^cmd: $first ");
 #    my $delta_time = &timedelta($t);
 #    &DEBUG("factArgs: delta_time = $delta_time s");
 #    &DEBUG("factArgs: list => ".scalar(@list) );
@@ -201,13 +203,10 @@ sub factoidArgs {
     foreach (sort { length($b) <=> length($a) } @list) {
        next if (/#DEL#/);      # deleted.
 
-       s/^CMD: //i;
+       s/^cmd: //i;
 #      &DEBUG("factarg: '$str' =~ /^$_\$/");
        my $arg = $_;
 
-       # TODO: <greycat> ~punish apt for (Eating) (Parentheses)
-       # how the hell do I fix the above? -dms.
-
        # eval (evil!) code. cleaned up courtesy of lear.
        my @vals;
        eval {
@@ -231,26 +230,33 @@ sub factoidArgs {
        &status("Question: factoid Arguments for '$str'");
        # TODO: use getReply() - need to modify it :(
        my $i   = 0;
-       my $q   = "CMD: $_";
+       my $q   = "cmd: $_";
        my $r   = &getFactoid($q);
        if (!defined $r) {
            &DEBUG("question: !result... should this happen?");
            return;
        }
 
-       # update stats.
-       &sqlUpdate("factoids", { factoid_key => $q }, {
-           requested_by        => $nuh,
-           requested_time      => time(),
-           -requested_count    => "requested_count+1",
+       # update stats. old mysql/sqlite don't do +1
+       my ($count) = &sqlSelect('factoids', 'requested_count', { factoid_key => $q });
+       $count++;
+       &sqlSet('factoids', {'factoid_key' => $q}, {
+               requested_by    => $nuh,
+               requested_time  => time(),
+               requested_count => $count
        } );
 
        # end of update stats.
 
        $result = $r;
+
        $result =~ s/^\((.*?)\): //;
+       my $vars = $1;
+
+       # start nasty hack to get partial &getReply() functionality.
+       $result = &SARit($result);
 
-       foreach ( split(',', $1) ) {
+       foreach ( split(',', $vars) ) {
            my $val = $vals[$i];
 #          &DEBUG("val => $val");
 
@@ -263,7 +269,7 @@ sub factoidArgs {
            my $old = $result;
            while (1) {
 #              &DEBUG("Q: result => $result (1before)");
-               $result = &substVars($result);
+               $result = &substVars($result,1);
 #              &DEBUG("Q: result => $result (1after)");
 
                last if ($old eq $result);
@@ -274,17 +280,16 @@ sub factoidArgs {
            # hack.
            $vals[$i] =~ s/^me$/$who/gi;
 
-           if (!$done) {
+#          if (!$done) {
                &status("factArgs: SARing '$_' to '$vals[$i]'.");
                $result =~ s/\Q$_\E/$vals[$i]/g;
-           }
+#          }
            $i++;
        }
 
-       # nasty hack to get partial &getReply() functionality.
+       # rest of nasty hack to get partial &getReply() functionality.
        $result =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i;
        $result =~ s/^\s*<reply>\s*//i;
-       $result = &SARit($result);
 
 # well... lets go through all of them. not advisable if we have like
 # 1000 commands, heh.
@@ -296,3 +301,5 @@ sub factoidArgs {
 }
 
 1;
+
+# vim:ts=4:sw=4:expandtab:tw=80