]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Factoids/Question.pl
- fixed listvals. why did this break anyway?
[infobot.git] / src / Factoids / Question.pl
index 3e689ab5bc86f40ee347e83199b1f1671ff0e2ae..eab0d5f050ffd4feef8e1b69acd22d420b0d6784 100644 (file)
@@ -80,6 +80,11 @@ 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++) {
        $query  = $query[$i];
@@ -112,7 +117,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 +156,94 @@ 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 @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) {
+       next if (/#DEL#/);      # deleted.
+
+       s/^CMD: //;
+#      &DEBUG("factarg: ''$str' =~ /^$_\$/'");
+       my @vals;
+       my $arg = $_;
+
+       # todo: make eval work with $$i's :(
+       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);
+
+#      &DEBUG("vals => @vals");
+
+       &status("Question: factoid Arguments for '$str'");
+       # todo: use getReply() - need to modify it :(
+       my $i   = 0;
+       $result = &getFactoid("CMD: $_");
+       $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]/;
+           }
+           $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);
+#      $result = &substVars($result);
+
+       return $result;
+    }
+
+    return;
+}
+
 1;