]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Factoids/Question.pl
- converted 3 instances of setFactInfo to 1 sqlUpdate.
[infobot.git] / src / Factoids / Question.pl
index fa73b9f80ec849f132c4afd16c0f93d07e8bfc28..2338e4454cd58533d1e227a92915ff79c05d52df 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
@@ -45,6 +52,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;
@@ -82,6 +94,7 @@ sub doQuestion {
 
     if (&IsChanConf("factoidArguments")) {
        $result = &factoidArgs($query[0]);
+
        return $result if (defined $result);
     }
 
@@ -96,13 +109,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;
            }
 
@@ -162,50 +179,81 @@ sub factoidArgs {
 
     # 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: ");
+    # 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.
 
-    foreach (@list) {
+    # it does not matter if it's not alphabetically sorted.
+    foreach (sort { length($b) <=> length($a) } @list) {
        next if (/#DEL#/);      # deleted.
 
-       s/^CMD: //;
-#      &DEBUG("factarg: ''$str' =~ /^$_\$/'");
-       my @vals;
+       s/^CMD: //i;
+#      &DEBUG("factarg: '$str' =~ /^$_\$/");
        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);
+       # 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;
        }
 
        next unless (@vals);
 
-       &DEBUG("vals => @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;
-       $result = &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/^\((.*?)\): //;
 
        foreach ( split(',', $1) ) {
            my $val = $vals[$i];
-           &DEBUG("val => $val");
+#          &DEBUG("val => $val");
+
            if (!defined $val) {
                &status("factArgs: vals[$i] == undef; not SARing '$_' for '$str'");
                next;
@@ -214,7 +262,10 @@ sub factoidArgs {
            my $done = 0;
            my $old = $result;
            while (1) {
-               $result = &substVars($result,1);
+#              &DEBUG("Q: result => $result (1before)");
+               $result = &substVars($result);
+#              &DEBUG("Q: result => $result (1after)");
+
                last if ($old eq $result);
                $old = $result;
                $done++;
@@ -225,7 +276,7 @@ sub factoidArgs {
 
            if (!$done) {
                &status("factArgs: SARing '$_' to '$vals[$i]'.");
-               $result =~ s/\Q$_\E/$vals[$i]/;
+               $result =~ s/\Q$_\E/$vals[$i]/g;
            }
            $i++;
        }
@@ -234,12 +285,14 @@ sub factoidArgs {
        $result =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i;
        $result =~ s/^\s*<reply>\s*//i;
        $result = &SARit($result);
-#      $result = &substVars($result);
 
-       return $result;
+# well... lets go through all of them. not advisable if we have like
+# 1000 commands, heh.
+#      return $result;
+       $cmdstats{'Factoid Commands'}++;
     }
 
-    return;
+    return $result;
 }
 
 1;