]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Factoids/Question.pl
- moved scripts/setup_sql.pl to src/db_mysql as &createTables()
[infobot.git] / src / Factoids / Question.pl
index 1001c0b9ce8e32a99512becdfdb8fd1adc0652ba..bd1d47af26a98e50f37a7f9e721ab957c5daf7a4 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 '';
     }
 
@@ -35,7 +34,7 @@ sub doQuestion {
     if (!$addressed) {
        return '' unless ($finalQMark);
        return '' if (&IsParam("minVolunteerLength") == 0);
-       return '' if (length($query) < $param{'minVolunteerLength'});
+       return '' if (length $query < $param{'minVolunteerLength'});
     } else {
        ### TODO: this should be caught in Process.pl?
        return '' unless ($talkok);
@@ -52,6 +51,11 @@ sub doQuestion {
 
     push(@query, $query);      # 1: push original.
 
+    # valid factoid.
+    if ($query =~ s/[!.]$//) {
+       push(@query,$query);
+    }
+
     $x = &normquery($query);
     push(@query, $x) if ($x ne $query);
     $query = $x;
@@ -64,7 +68,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;
@@ -76,24 +80,96 @@ sub doQuestion {
        $questionWord = "where";
     }
 
-    # valid factoid.
-    if ($query =~ s/[\!\.]$//) {
-       push(@query,$query);
+    if (&IsChanConf("factoidArguments")) {
+       # 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 @list = &searchTable("factoids", "factoid_key", "factoid_key", "CMD: ");
+
+       # from a design perspective, it's better to have the regex in
+       # the factoid key to reduce repetitive processing.
+
+       foreach (@list) {
+           s/^CMD: //;
+#          &DEBUG("factarg: ''$query[0]' =~ /^$_\$/'");
+           my @vals;
+           my $arg = $_;
+
+           eval {
+               next unless ($query[0] =~ /^$arg$/i);
+
+               for ($i=1; $i<=5; $i++) {
+                   $val = $$i;
+                   last unless (defined $val);
+
+                   push(@vals, $val);
+               }
+           };
+
+           if ($@) {   # it failed!!!
+               &WARN("factargs: regex failed! '$query[0]' =~ /^$_\$/");
+               next;
+           }
+
+           &status("Question: factoid Arguments for '$query[0]'");
+           # todo: use getReply() - need to modify it :(
+           my $i       = 0;
+           my $result  = &getFactoid("CMD: $_");
+           $result     =~ s/^\((.*?)\): //;
+
+           foreach ( split(',', $1) ) {
+               my $val = $vals[$i];
+               if (!defined $val) {
+                   &status("factArgs: vals[$i] == undef; not SARing '$_' for '$query[0]'");
+                   next;
+               }
+
+               &status("factArgs: SARing '$_' to '$vals[$i]'.");
+               $result =~ s/\Q$_\E/$vals[$i]/;
+               $i++;
+           }
+
+           # nasty hack to get partial &getReply() functionality.
+           $result =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i;
+           $result =~ s/^\s*<reply>\s*//i;
+
+           return $result;
+       }
     }
 
-    for (my$i=0; $i<scalar(@query); $i++) {
+    my @link;
+    for (my$i=0; $i<scalar @query; $i++) {
        $query  = $query[$i];
        $result = &getReply($query);
        next if (!defined $result or $result eq "");
 
        # 'see also' factoid redirection support.
-       if ($result =~ /^see( also)? (.*?)\.?$/) {
-           my $newr = &getReply($2);
-           $result  = $newr    if ($newr ne "");
+
+       while ($result =~ /^see( also)? (.*?)\.?$/) {
+           my $link    = $2;
+
+           if (grep /^$link$/i, @link) {
+               &status("recursive link found; bailing out.");
+               last;
+           }
+
+           if (scalar @link >= 5) {
+               &status("recursive link limit reached.");
+               last;
+           }
+
+           push(@link, $link);
+           my $newr = &getReply($link);
+           last if (!defined $newr or $newr eq "");
+           $result  = $newr;
+       }
+
+       if (@link) {
+           &status("'$query' linked to: ".join(" => ", @link) );
        }
 
        if ($i != 0) {
-           &DEBUG("Question: guessed factoid correctly ($i) => '$query'.");
+           &VERB("Question.pl: '$query[0]' did not exist; '$query[$i]' ($i) did",2);
        }
 
        return $result;