]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Factoids/Reply.pl
- strictify
[infobot.git] / src / Factoids / Reply.pl
index 78c13d876be57231c52e292d62f791b76b97d373..4ba2135ad4259879418dd9150b3c6d8810331cbf 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*$/) {
@@ -28,7 +28,9 @@ sub getReply {
 
     $message =~ tr/A-Z/a-z/;
 
-    if ($result = &getFactoid($message)) {
+    my ($result, $fauthor, $count) = &dbGet("factoids", 
+       "factoid_value,created_by,requested_count", "factoid_key=".&dbQuote($message) );
+    if ($result) {
        $lhs = $message;
        $mhs = "is";
        $rhs = $result;
@@ -48,7 +50,6 @@ sub getReply {
        $result =~ s/^\s*//;
     }
 
-    my $fauthor = &dbGet("factoids", "created_by", "factoid_key='$message'");
     $result    = &SARit($result);
 
     $reply     = $result;
@@ -59,21 +60,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     => $lhs,
+       &dbSet("factoids", {'factoid_key' => $lhs}, {
                requested_by    => $nuh,
                requested_time  => time(),
                requested_count => $count
-           ) );
-       }
+       } );
 
        # todo: rename $real to something else!
        my $real   = 0;
@@ -136,8 +128,13 @@ sub getReply {
     ### $ SUBSTITUTION.
     ###
 
-    $reply = &substVars($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;
 }
 
@@ -243,18 +240,22 @@ sub SARit {
 }
 
 sub substVars {
-    my($reply) = @_;
+    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+//;
     $reply     =~ s/\$time/$date/gi;
 
     # dollar variables.
-    $reply     =~ s/\$nick/$who/g;
-    $reply     =~ s/\$who/$who/g;      # backward compat.
+    if ($flag) {
+       $reply  =~ s/\$nick/$who/g;
+       $reply  =~ s/\$who/$who/g;      # backward compat.
+    }
+
     if ($reply =~ /\$(user(name)?|host)/) {
        my ($username, $hostname) = split /\@/, $uh;
        $reply  =~ s/\$user(name)?/$username/g;
@@ -269,28 +270,33 @@ sub substVars {
 
     if ($reply =~ /\$rand/) {
        my $rand  = rand();
-       my $randp = int($rand*100);
-       $reply =~ s/\$randpercentage/$randp/g;  # ???
-       # randnick.
+
+       # $randnick.
        if ($reply =~ /\$randnick/) {
            my @nicks = keys %{ $channels{$chan}{''} };
            my $randnick = $nicks[ int($rand*$#nicks) ];
-           s/\$randnick/$randnick/;
+           $reply =~ s/\$randnick/$randnick/g;
        }
 
-       ### TODO: number of digits. 'x.y'
-       if ($reply =~ /\$rand(\d+)/) {
-           # will this work as it does in C?
-           $rand = sprintf("%*f", $1, $rand);
+       # eg: $rand100.3
+       if ($reply =~ /\$rand(\d+)(\.(\d+))?/) {
+           my $max = $1;
+           my $dot = $3 || 0;
+           my $orig = $&;
+           #&DEBUG("dot => $dot, max => $max, rand=>$rand");
+           $rand = sprintf("%.*f", $dot, $rand*$max);
+
+           &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/\$factoid/$lhs/g;
     $reply     =~ s/\$ident/$ident/g;
 
     if ($reply =~ /\$startTime/) {
-       my $time = scalar(localtime $^T);
+       my $time = scalar(gmtime $^T);
        $reply =~ s/\$startTime/$time/;
     }
 
@@ -300,7 +306,7 @@ sub substVars {
     }
 
     if ($reply =~ /\$factoids/) {
-       my $count = &countKeys("factoids");
+       my $factoids = &countKeys("factoids");
        $reply =~ s/\$factoids/$factoids/;
     }