]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Factoids/Reply.pl
- make ignoreAdd a little more verbose.
[infobot.git] / src / Factoids / Reply.pl
index 78c13d876be57231c52e292d62f791b76b97d373..5c472fc5b87f257090670bcfb9888aee7da6042a 100644 (file)
@@ -48,7 +48,7 @@ sub getReply {
        $result =~ s/^\s*//;
     }
 
-    my $fauthor = &dbGet("factoids", "created_by", "factoid_key='$message'");
+    my $fauthor = &dbGet("factoids", "created_by", "factoid_key=".&dbQuote($message) );
     $result    = &SARit($result);
 
     $reply     = $result;
@@ -67,7 +67,7 @@ sub getReply {
            &setFactInfo($lhs,"requested_time", time());
            &setFactInfo($lhs,"requested_count", $count);
        } else {
-           &dbReplace("factoids", (
+           &dbReplace("factoids", "factoid_key", (
                factoid_key     => $lhs,
                requested_by    => $nuh,
                requested_time  => time(),
@@ -136,8 +136,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 +248,22 @@ sub SARit {
 }
 
 sub substVars {
-    my($reply) = @_;
+    my($reply,$flag) = @_;
 
     # $date, $time.
     my $date   =  scalar(localtime());
     $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.
-    $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,24 +278,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;
        }
 
+       # eg: $rand100.3
        ### TODO: number of digits. 'x.y'
-       if ($reply =~ /\$rand(\d+)/) {
-           # will this work as it does in C?
-           $rand = sprintf("%*f", $1, $rand);
+       # 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 = $&;
+
+           &status("swapping $orig to $rand");
+           &status("reply => $reply");
+           $reply =~ s/$orig/$rand/eg;
+           &status("reply => $reply");
        }
+
        $reply =~ s/\$rand/$rand/g;
     }
 
-    $reply     =~ s/\$factoid/$lhs/g;
     $reply     =~ s/\$ident/$ident/g;
 
     if ($reply =~ /\$startTime/) {