]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Factoids/Question.pl
- "what is test?" now works. this was broken forever.
[infobot.git] / src / Factoids / Question.pl
index e61b30fa8fc51d84aa9fa571bb5f3b9414e98baa..5a0ddb299c5d7ddd62ca16fb7c1939f0e7d03d25 100644 (file)
@@ -11,7 +11,7 @@
 ##
 ##
 
-if (&IsParam("useStrict")) { use strict; }
+# use strict;  # TODO
 
 use vars qw($query $reply $finalQMark $nuh $result $talkok $who $nuh);
 use vars qw(%bots %forked);
@@ -38,6 +38,13 @@ sub doQuestion {
     } 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") {
+           &status("$who tried to ask us when not allowed.");
+           return;
+       }
     }
 
     # dangerous; common preambles should be stripped before here
@@ -58,7 +65,7 @@ sub doQuestion {
 
     # valid factoid.
     if ($query =~ s/[!.]$//) {
-       push(@query,$query);
+       push(@query, $query);
     }
 
     $x = &normquery($query);
@@ -75,18 +82,20 @@ sub doQuestion {
 
     my $qregex = join '|', keys %{ $lang{'qWord'} };
 
-    # what's whats => what is; who'?s => who is, etc
-    $query =~ s/ ($qregex)\'?s / $1 is /i;
-    if ($query =~ s/\s+($qregex)\s+//i) { # check for question word
+    # purge prefix question string.
+    if ($query =~ s/^ ($qregex)//i) {
        $questionWord = lc($1);
     }
 
     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")) {
        $result = &factoidArgs($query[0]);
+
        return $result if (defined $result);
     }
 
@@ -101,13 +110,17 @@ sub doQuestion {
        while ($result =~ /^see( also)? (.*?)\.?$/) {
            my $link    = $2;
 
-           if (grep /^$link$/i, @link) {
+           # #debian@OPN was having problems with libstdc++ factoid
+           # redirection :) 20021116. -xk.
+           # hrm... allow recursive loops... next if statement handles
+           # that.
+           if (grep /^\Q$link\E$/i, @link) {
                &status("recursive link found; bailing out.");
                last;
            }
 
            if (scalar @link >= 5) {
-               &status("recursive link limit reached.");
+               &status("recursive link limit (5) reached.");
                last;
            }
 
@@ -169,7 +182,7 @@ sub factoidArgs {
     # which will make it less than linear => quicker!
     # todo: cache this, update cache when altered. !!! !!! !!!
 #    my $t = &timeget();
-    my @list = &searchTable("factoids", "factoid_key", "factoid_key", "CMD: ");
+    my @list = &searchTable("factoids", "factoid_key", "factoid_key", "^CMD: ");
 #    my $delta_time = &timedelta($t);
 #    &DEBUG("factArgs: delta_time = $delta_time s");
 #    &DEBUG("factArgs: list => ".scalar(@list) );
@@ -182,24 +195,19 @@ sub factoidArgs {
        next if (/#DEL#/);      # deleted.
 
        s/^CMD: //i;
-#      &DEBUG("factarg: ''$str' =~ /^$_\$/'");
-       my @vals;
+#      &DEBUG("factarg: '$str' =~ /^$_\$/");
        my $arg = $_;
 
-       # todo: make eval work with $$i's :(
-       # fuck this is an ugly hack. it works though.
-       eval {
-           if ($str =~ /^$arg$/i) {
-               for ($i=1; $i<=5; $i++) {
-                   $val = $$i;
-                   last unless (defined $val);
+       # todo: <greycat> ~punish apt for (Eating) (Parentheses)
+       # how the hell do I fix the above? -dms.
 
-                   push(@vals, $val);
-               }
-           }
+       # eval (evil!) code. cleaned up courtesy of lear.
+       my @vals;
+       eval {
+           @vals = ($str =~ /^$arg$/i);
        };
 
-       if ($@) {   # it failed!!!
+       if ($@) {
            &WARN("factargs: regex failed! '$str' =~ /^$_\$/");
            next;
        }
@@ -216,12 +224,30 @@ sub factoidArgs {
        &status("Question: factoid Arguments for '$str'");
        # todo: use getReply() - need to modify it :(
        my $i   = 0;
-       my $r   = &getFactoid("CMD: $_");
+       my $q   = "CMD: $_";
+       my $r   = &getFactoid($q);
        if (!defined $r) {
            &DEBUG("question: !result... should this happen?");
            return;
        }
 
+       # update stats.
+       if (0) {        # old.
+           my $count = &getFactInfo($q, "requested_count") || 0;
+           $count++;
+           &setFactInfo($q, "requested_by", $nuh);
+           &setFactInfo($q, "requested_time", time());
+           &setFactInfo($q, "requested_count", $count);
+       } else {
+           &sqlUpdate("factoids", { factoid_key => $q }, {
+               requested_by            => $nuh,
+               requested_time          => time(),
+               -requested_count        => "requested_count+1",
+           } );
+       }
+
+       # end of update stats.
+
        $result = $r;
        $result =~ s/^\((.*?)\): //;
 
@@ -237,9 +263,9 @@ sub factoidArgs {
            my $done = 0;
            my $old = $result;
            while (1) {
-#              &DEBUG("Q: result => $result (1)");
+#              &DEBUG("Q: result => $result (1before)");
                $result = &substVars($result);
-#              &DEBUG("Q: result => $result (1)");
+#              &DEBUG("Q: result => $result (1after)");
 
                last if ($old eq $result);
                $old = $result;