]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Factoids/Reply.pl
* Remove merge detrius from Reply.pl; stop breaking on 'you are'
[infobot.git] / src / Factoids / Reply.pl
index 0d518658fccd6b62e8cf4c9a6921412f9db693ef..38f65a083db6d8c534d9716887cdc629c953a236 100644 (file)
@@ -10,7 +10,8 @@
 #   rhs - factoid message.
 ##
 
-if (&IsParam("useStrict")) { use strict; }
+# use strict;  # TODO
+use POSIX qw(strftime);
 
 use vars qw($msgType $uh $lastWho $ident);
 use vars qw(%lang %lastWho);
@@ -18,7 +19,7 @@ use vars qw(%lang %lastWho);
 sub getReply {
     my($message) = @_;
     my($lhs,$mhs,$rhs);
-    my($result,$reply);
+    my($reply, $count, $fauthor, $result, $factoid, $search, @searches);
     $orig{message} = $message;
 
     if (!defined $message or $message =~ /^\s*$/) {
@@ -28,14 +29,36 @@ sub getReply {
 
     $message =~ tr/A-Z/a-z/;
 
-    my ($result, $fauthor, $count) = &dbGet("factoids", 
-       "factoid_value,created_by,requested_count", "factoid_key=".&dbQuote($message) );
+    @searches = split(/\s+/, &getChanConfDefault('factoidSearch', '_default', $chan));
+    &::DEBUG("factoidSearch: $chan is: " . join(':', @searches));
+    # 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 '';
     }
@@ -58,23 +81,16 @@ sub getReply {
        ### FLOOD REPETION AND PROTECTION. -20000124
 
        # stats code.
-       ### FIXME: old mysql doesn't support
+       ### FIXME: old mysql/sqlite doesn't support
        ### "requested_count=requested_count+1".
        $count++;
-       ### BROKEN!!! - Tim Riker <Tim@Rikers.org> says it's fixed now
-       if (0) {        # old code.
-           &setFactInfo($lhs,"requested_by", $nuh);
-           &setFactInfo($lhs,"requested_time", time());
-           &setFactInfo($lhs,"requested_count", $count);
-       } else {
-           &dbSet("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\!/);
@@ -109,18 +125,10 @@ sub getReply {
                $reply = "$lhs $mhs $result";
            }
 
-           if ($reply =~ s/^\Q$who\E is/you are/i) {
-               # fix the person.
-           } else {
-               if ($reply =~ /^you are / or $reply =~ / you are /) {
-                   return if ($addressed);
-               }
-           }
+           $reply =~ s/^\Q$who\E is/you are/i) {
        }
     }
 
-    return $reply if ($literal);
-
     # remove excessive beginning and end whitespaces.
     $reply     =~ s/^\s+|\s+$//g;
 
@@ -136,11 +144,11 @@ sub getReply {
     ###
 
     # don't evaluate if it has factoid arguments.
-    if ($message =~ /^CMD:/i) {
-       &status("Reply: not doing substVars (eval dollar vars)");
-    } else {
+#    if ($message =~ /^cmd:/i) {
+#      &status("Reply: not doing substVars (eval dollar vars)");
+#    } else {
        $reply = &substVars($reply,1);
-    }
+#    }
 
     $reply;
 }
@@ -156,10 +164,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;
            }
@@ -169,25 +174,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;
        }
@@ -243,19 +244,36 @@ sub SARit {
     }
     &status("Reply.pl: $done SARs done.") if ($done);
 
+    # <URL></URL> type
+    #
+    while ($txt =~ /<URL>(.*)<\/URL>/){
+       &status("we have to norm this <URL></URL> stuff, SARing");
+       my $foobar = $1;
+       if ($foobar =~ m/(http:\/\/[^?]+)\?(.*)/){
+           my ($pig1,$pig2) = ($1,$2);
+           &status("SARing using URLencode");
+           $pig2=~s/([^\w])/sprintf("%%%02x",ord($1))/gie;
+           $foobar=$pig1."?".$pig2;
+       }
+       $txt =~ s/<URL>(.*)<\/URL>/$foobar/;
+    }
     return $txt;
 }
 
 sub substVars {
     my($reply,$flag) = @_;
 
-    # $date, $time.
-    my $date   =  scalar(localtime());
-    $date      =~ s/\:\d+(\s+\w+)\s+\d+$/$1/;
+    # $date, $time, $day.
+    # TODO: support localtime.
+    my $date   =  strftime("%Y.%m.%d", gmtime());
     $reply     =~ s/\$date/$date/gi;
-    $date      =~ s/\w+\s+\w+\s+\d+\s+//;
-    # todo: support UTC.
-    $reply     =~ s/\$time/$date/gi;
+    my $time   =  strftime("%k:%M:%S", gmtime());
+    $reply     =~ s/\$time/$time/gi;
+    my $day    =  strftime("%A", gmtime());
+    $reply     =~ s/\$day/$day/gi;
+
+    # support $ident when I have multiple nicks
+    my $mynick = $conn->nick() if $conn;
 
     # dollar variables.
     if ($flag) {
@@ -286,28 +304,24 @@ sub substVars {
        }
 
        # eg: $rand100.3
-       ### TODO: number of digits. 'x.y'
-       # too hard.
        if ($reply =~ /\$rand(\d+)(\.(\d+))?/) {
            my $max = $1;
            my $dot = $3 || 0;
-           &status("dot => $dot, max => $max, rand=>$rand");
-           $rand = sprintf("%.*f", $dot, $rand*$max);
            my $orig = $&;
+           #&DEBUG("dot => $dot, max => $max, rand=>$rand");
+           $rand = sprintf("%.*f", $dot, $rand*$max);
 
-           &status("swapping $orig to $rand");
-           &status("reply => $reply");
-           $reply =~ s/$orig/$rand/eg;
-           &status("reply => $reply");
+           &DEBUG("swapping $orig to $rand");
+           $reply =~ s/\Q$orig\E/$rand/eg;
+       } else {
+           $reply =~ s/\$rand/$rand/g;
        }
-
-       $reply =~ s/\$rand/$rand/g;
     }
 
-    $reply     =~ s/\$ident/$ident/g;
+    $reply     =~ s/\$ident/$mynick/g;
 
     if ($reply =~ /\$startTime/) {
-       my $time = scalar(localtime $^T);
+       my $time = scalar(gmtime $^T);
        $reply =~ s/\$startTime/$time/;
     }
 
@@ -317,7 +331,7 @@ sub substVars {
     }
 
     if ($reply =~ /\$factoids/) {
-       my $count = &countKeys("factoids");
+       my $factoids = &countKeys("factoids");
        $reply =~ s/\$factoids/$factoids/;
     }