]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Factoids/Reply.pl
ws
[infobot.git] / src / Factoids / Reply.pl
index e73a73e254b6e4eb3079652dcdcb970d75168d25..056f947e4100475dfe3ddf69642111c7e33e6d1c 100644 (file)
@@ -10,7 +10,7 @@
 #   rhs - factoid message.
 ##
 
-if (&IsParam("useStrict")) { use strict; }
+# use strict;  # TODO
 
 use vars qw($msgType $uh $lastWho $ident);
 use vars qw(%lang %lastWho);
@@ -18,7 +18,7 @@ use vars qw(%lang %lastWho);
 sub getReply {
     my($message) = @_;
     my($lhs,$mhs,$rhs);
-    my($result,$reply);
+    my($reply);
     $orig{message} = $message;
 
     if (!defined $message or $message =~ /^\s*$/) {
@@ -27,8 +27,14 @@ sub getReply {
     }
 
     $message =~ tr/A-Z/a-z/;
+    $message =~ s/^cmd:/CMD:/;
 
-    if ($result = &getFactoid($message)) {
+    my ($count, $fauthor, $result) = &sqlSelect("factoids",
+       "requested_count,created_by,factoid_value",
+       { factoid_key => $message }
+    );
+
+    if ($result) {
        $lhs = $message;
        $mhs = "is";
        $rhs = $result;
@@ -48,7 +54,6 @@ sub getReply {
        $result =~ s/^\s*//;
     }
 
-    my $fauthor = &dbGet("factoids", "created_by", "factoid_key=".&dbQuote($message) );
     $result    = &SARit($result);
 
     $reply     = $result;
@@ -59,21 +64,12 @@ sub getReply {
        # stats code.
        ### FIXME: old mysql doesn't support
        ### "requested_count=requested_count+1".
-       my $count = &getFactInfo($lhs,"requested_count") || 0;
        $count++;
-       ### BROKEN!!!
-       if (1) {        # old code.
-           &setFactInfo($lhs,"requested_by", $nuh);
-           &setFactInfo($lhs,"requested_time", time());
-           &setFactInfo($lhs,"requested_count", $count);
-       } else {
-           &dbReplace("factoids", "factoid_key", (
-               factoid_key     => $lhs,
+       &sqlSet("factoids", {'factoid_key' => $lhs}, {
                requested_by    => $nuh,
                requested_time  => time(),
                requested_count => $count
-           ) );
-       }
+       } );
 
        # todo: rename $real to something else!
        my $real   = 0;
@@ -136,9 +132,12 @@ sub getReply {
     ### $ SUBSTITUTION.
     ###
 
-#    &DEBUG("Reply: reply => $reply");
-    $reply = &substVars($reply,1);
-#    &DEBUG("Reply: reply => $reply");
+    # don't evaluate if it has factoid arguments.
+    if ($message =~ /^CMD:/i) {
+       &status("Reply: not doing substVars (eval dollar vars)");
+    } else {
+       $reply = &substVars($reply,1);
+    }
 
     $reply;
 }
@@ -154,10 +153,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;
            }
@@ -167,25 +163,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;
        }
@@ -248,11 +240,11 @@ sub substVars {
     my($reply,$flag) = @_;
 
     # $date, $time.
-    my $date   =  scalar(localtime());
+    # todo: support localtime.
+    my $date   =  scalar(gmtime());
     $date      =~ s/\:\d+(\s+\w+)\s+\d+$/$1/;
     $reply     =~ s/\$date/$date/gi;
     $date      =~ s/\w+\s+\w+\s+\d+\s+//;
-    # todo: support UTC.
     $reply     =~ s/\$time/$date/gi;
 
     # dollar variables.
@@ -284,28 +276,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;
 
     if ($reply =~ /\$startTime/) {
-       my $time = scalar(localtime $^T);
+       my $time = scalar(gmtime $^T);
        $reply =~ s/\$startTime/$time/;
     }
 
@@ -315,7 +303,7 @@ sub substVars {
     }
 
     if ($reply =~ /\$factoids/) {
-       my $count = &countKeys("factoids");
+       my $factoids = &countKeys("factoids");
        $reply =~ s/\$factoids/$factoids/;
     }