]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Factoids/Question.pl
- irctextcounters: add percentage to top3
[infobot.git] / src / Factoids / Question.pl
index e6b0818fd88794ebcb0418654993adc6115f4956..e61b30fa8fc51d84aa9fa571bb5f3b9414e98baa 100644 (file)
@@ -19,14 +19,13 @@ use vars qw(%bots %forked);
 sub doQuestion {
     # my doesn't allow variables to be inherinted, local does.
     # following is used in math()...
-    local($query) = @_;
-    local($reply) = "";
-    local $finalQMark = $query =~ s/\?+\s*$//;
-    $finalQMark += $query =~ s/\?\s*$//;
-    $query =~ s/^\s+|\s+$//g;
+    local($query)      = @_;
+    local($reply)      = "";
+    local $finalQMark  = $query =~ s/\?+\s*$//;
+    $finalQMark                += $query =~ s/\?\s*$//;
+    $query             =~ s/^\s+|\s+$//g;
 
     if (!defined $query or $query =~ /^\s*$/) {
-       &FIXME("doQ: query == NULL");
        return '';
     }
 
@@ -46,6 +45,11 @@ sub doQuestion {
        return if (exists $bots{$nuh});
     }
 
+    if ($query =~ s/^literal\s+//i) {
+       &status("literal ask of '$query'.");
+       $literal = 1;
+    }
+
     # convert to canonical reference form
     my $x;
     my @query;
@@ -69,7 +73,7 @@ sub doQuestion {
     $query =~ s/^explain\s*(\?*)/$1/i; # explain x
     $query = " $query ";               # side whitespaces.
 
-    my $qregex = join '|', keys %{$lang{'qWord'}};
+    my $qregex = join '|', keys %{ $lang{'qWord'} };
 
     # what's whats => what is; who'?s => who is, etc
     $query =~ s/ ($qregex)\'?s / $1 is /i;
@@ -81,8 +85,13 @@ sub doQuestion {
        $questionWord = "where";
     }
 
+    if (&IsChanConf("factoidArguments")) {
+       $result = &factoidArgs($query[0]);
+       return $result if (defined $result);
+    }
+
     my @link;
-    for (my$i=0; $i<scalar(@query); $i++) {
+    for (my$i=0; $i<scalar @query; $i++) {
        $query  = $query[$i];
        $result = &getReply($query);
        next if (!defined $result or $result eq "");
@@ -104,7 +113,8 @@ sub doQuestion {
 
            push(@link, $link);
            my $newr = &getReply($link);
-           $result  = $newr    if ($newr ne "");
+           last if (!defined $newr or $newr eq "");
+           $result  = $newr;
        }
 
        if (@link) {
@@ -112,7 +122,7 @@ sub doQuestion {
        }
 
        if ($i != 0) {
-           &DEBUG("Question: '$query[0]' did not exist; '$query[$i]' ($i) did");
+           &VERB("Question.pl: '$query[0]' did not exist; '$query[$i]' ($i) did",2);
        }
 
        return $result;
@@ -151,4 +161,113 @@ sub doQuestion {
     return $reply;
 }
 
+sub factoidArgs {
+    my($str)   = @_;
+    my $result;
+
+    # to make it eleeter, split each arg and use "blah OR blah or BLAH"
+    # 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 $delta_time = &timedelta($t);
+#    &DEBUG("factArgs: delta_time = $delta_time s");
+#    &DEBUG("factArgs: list => ".scalar(@list) );
+
+    # from a design perspective, it's better to have the regex in
+    # the factoid key to reduce repetitive processing.
+
+    # it does not matter if it's not alphabetically sorted.
+    foreach (sort { length($b) <=> length($a) } @list) {
+       next if (/#DEL#/);      # deleted.
+
+       s/^CMD: //i;
+#      &DEBUG("factarg: ''$str' =~ /^$_\$/'");
+       my @vals;
+       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);
+
+                   push(@vals, $val);
+               }
+           }
+       };
+
+       if ($@) {   # it failed!!!
+           &WARN("factargs: regex failed! '$str' =~ /^$_\$/");
+           next;
+       }
+
+       next unless (@vals);
+
+       if (defined $result) {
+           &WARN("factargs: '$_' matches aswell.");
+           next;
+       }
+
+#      &DEBUG("vals => @vals");
+
+       &status("Question: factoid Arguments for '$str'");
+       # todo: use getReply() - need to modify it :(
+       my $i   = 0;
+       my $r   = &getFactoid("CMD: $_");
+       if (!defined $r) {
+           &DEBUG("question: !result... should this happen?");
+           return;
+       }
+
+       $result = $r;
+       $result =~ s/^\((.*?)\): //;
+
+       foreach ( split(',', $1) ) {
+           my $val = $vals[$i];
+#          &DEBUG("val => $val");
+
+           if (!defined $val) {
+               &status("factArgs: vals[$i] == undef; not SARing '$_' for '$str'");
+               next;
+           }
+
+           my $done = 0;
+           my $old = $result;
+           while (1) {
+#              &DEBUG("Q: result => $result (1)");
+               $result = &substVars($result);
+#              &DEBUG("Q: result => $result (1)");
+
+               last if ($old eq $result);
+               $old = $result;
+               $done++;
+           }
+
+           # hack.
+           $vals[$i] =~ s/^me$/$who/gi;
+
+           if (!$done) {
+               &status("factArgs: SARing '$_' to '$vals[$i]'.");
+               $result =~ s/\Q$_\E/$vals[$i]/g;
+           }
+           $i++;
+       }
+
+       # 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.
+#      return $result;
+       $cmdstats{'Factoid Commands'}++;
+    }
+
+    return $result;
+}
+
 1;