]> git.donarmstrong.com Git - infobot.git/blobdiff - blootbot/src/Factoids/Reply.pl
factoidSearch handles "$chan", allow _default on normal queries
[infobot.git] / blootbot / src / Factoids / Reply.pl
index 55d02a07ac8a83e8f965d71259cfd801fc97961b..7dc720fc8f5f55b5401e4a20dd4cc2b22dd632e1 100644 (file)
@@ -18,7 +18,7 @@ use vars qw(%lang %lastWho);
 sub getReply {
     my($message) = @_;
     my($lhs,$mhs,$rhs);
-    my($reply);
+    my($reply, $count, $fauthor, $result, $factoid, $search, @searches);
     $orig{message} = $message;
 
     if (!defined $message or $message =~ /^\s*$/) {
@@ -27,18 +27,42 @@ sub getReply {
     }
 
     $message =~ tr/A-Z/a-z/;
+    $message =~ s/^cmd:/CMD:/;
 
-    my ($count, $fauthor, $result) = &sqlSelect("factoids", 
-       "requested_count,created_by,factoid_value",
-       { factoid_key => $message }
-    );
+    if (&IsChanConf("factoidSearch")) {
+       @searches = split(/\s+/, &getChanConf("factoidSearch"));
+    } else {
+       @searches = ('_default');
+    }
+
+    # requesting the _default one, ignore factoidSearch
+    if ($message =~ /^_default\s+/) {
+       @searches = ('_default');
+       $message =~ s/^_default\s+//;
+    }
+
+    # check for factoids with each prefix
+    foreach $search (@searches) {
+       if ($search eq '$chan') {
+           $factoid = "$chan $message";
+       } elsif ($search eq '_default') {
+           $factoid = $message;
+       } else {
+           $factoid = "$search $message";
+       }
+       ($count, $fauthor, $result) = &sqlSelect("factoids",
+           "requested_count,created_by,factoid_value",
+           { factoid_key => $factoid }
+       );
+       last if ($result);
+    }
 
     if ($result) {
        $lhs = $message;
        $mhs = "is";
        $rhs = $result;
 
-       return "$lhs $mhs $rhs" if ($literal);
+       return "\"$factoid\" $mhs \"$rhs\"" if ($literal);
     } else {
        return '';
     }
@@ -64,13 +88,13 @@ sub getReply {
        ### FIXME: old mysql doesn't support
        ### "requested_count=requested_count+1".
        $count++;
-       &sqlSet("factoids", {'factoid_key' => $lhs}, {
+       &sqlSet("factoids", {'factoid_key' => $factoid}, {
                requested_by    => $nuh,
                requested_time  => time(),
                requested_count => $count
        } );
 
-       # todo: rename $real to something else!
+       # TODO: rename $real to something else!
        my $real   = 0;
 #      my $author = &getFactInfo($lhs,"created_by") || '';
 #      $real++ if ($author =~ /^\Q$who\E\!/);
@@ -115,8 +139,6 @@ sub getReply {
        }
     }
 
-    return $reply if ($literal);
-
     # remove excessive beginning and end whitespaces.
     $reply     =~ s/^\s+|\s+$//g;
 
@@ -152,10 +174,7 @@ sub smart_replace {
     foreach (split //, $string) {
 
        if ($_ eq "(") {
-###        print "( l=>$l, r=>$r\n";
-
            if (!$l and !$r) {
-#              print "STARTING at $i\n";
                $s = $i;
                $t = $i;
            }
@@ -165,25 +184,21 @@ sub smart_replace {
        }
 
        if ($_ eq ")") {
-###        print ") l=>$l, r=>$r\n";
-
            $r++;
            $l--;
 
            if (!$l and !$r) {
                my $substr = substr($old,$s,$i-$s+1);
-#              print "STOP at $i $substr\n";
                push(@rand, substr($old,$t+1,$i-$t-1) );
 
                my $rand = $rand[rand @rand];
-               &status("SARing '$substr' to '$rand'.");
+#              &status("SARing '$substr' to '$rand'.");
                $string =~ s/\Q$substr\E/$rand/;
                undef @rand;
            }
        }
 
        if ($_ eq "|" and $l+$r== 0 and $l==1) {
-#          print "| at $i (l=>$l,r=>$r)\n";
            push(@rand, substr($old,$t+1,$i-$t-1) );
            $t = $i;
        }
@@ -246,7 +261,7 @@ sub substVars {
     my($reply,$flag) = @_;
 
     # $date, $time.
-    # todo: support localtime.
+    # TODO: support localtime.
     my $date   =  scalar(gmtime());
     $date      =~ s/\:\d+(\s+\w+)\s+\d+$/$1/;
     $reply     =~ s/\$date/$date/gi;