]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Factoids/Reply.pl
converted %{$blah{$blah}} to %{ $blah{$blah} }
[infobot.git] / src / Factoids / Reply.pl
index 83cd90684a4aaa4f65523532198aecf884038162..1c470c4ca98c1a8584616e20adb273f436c401a8 100644 (file)
@@ -19,7 +19,6 @@ sub getReply {
     my($message) = @_;
     my($lhs,$mhs,$rhs);
     my($result,$reply);
-    my $literal = 0;
     $orig{message} = $message;
 
     if (!defined $message or $message =~ /^\s*$/) {
@@ -33,6 +32,8 @@ sub getReply {
        $lhs = $message;
        $mhs = "is";
        $rhs = $result;
+
+       return "$lhs $mhs $rhs" if ($literal);
     } else {
        return '';
     }
@@ -86,13 +87,9 @@ sub getReply {
        $done = 0;
 
        # EG: (blah1|blah2|blah3|) => blah1
-       while ($result =~ /\((.*?\|.*?)\)/) {
-           my $str = $1;
-           my @rand = split /\|/, $str;
-           my $rand = $rand[rand @rand];
+       while ($result =~ /.*\((.*\|.*?)\).*/) {
+           $result = &smart_replace($result);
 
-           &status("Reply.pl: SARing '($str)' to '$rand'.");
-           $result =~ s/\(\Q$str\E\)/$rand/;
            $done++;
            last if ($done >= 10);      # just in case.
        }
@@ -142,7 +139,7 @@ sub getReply {
            # result is random if separated by '||'.
            # rhs is full factoid with '||'.
            if ($mhs eq "is") {
-               $reply = &getRandom(keys %{$lang{'factoid'}});
+               $reply = &getRandom(keys %{ $lang{'factoid'} });
                $reply =~ s/##KEY/$lhs/;
                $reply =~ s/##VALUE/$result/;
            } else {
@@ -153,7 +150,7 @@ sub getReply {
                # fix the person.
            } else {
                if ($reply =~ /^you are / or $reply =~ / you are /) {
-                   return $noreply if ($addressed);
+                   return if ($addressed);
                }
            }
        }
@@ -203,8 +200,8 @@ sub getReply {
        $reply =~ s/\$randpercentage/$randp/g;  # ???
        # randnick.
        if ($reply =~ /\$randnick/) {
-           my @nicks = keys %{$channels{$chan}{''}};
-           my $randnick = $nicks[$rand*$#nicks];
+           my @nicks = keys %{ $channels{$chan}{''} };
+           my $randnick = $nicks[ int($rand*$#nicks) ];
            s/\$randnick/$randnick/;
        }
 
@@ -257,4 +254,61 @@ sub getReply {
     $reply;
 }
 
+sub smart_replace {
+    my ($string) = @_;
+    my ($l,$r) = (0,0);                # l = left,  r = right.
+    my ($s,$t) = (0,0);                # s = start, t = marker.
+    my $i = 0;
+    my @rand;
+    my $old = $string;
+
+    foreach (split //, $string) {
+
+       if ($_ eq "(") {
+###        print "( l=>$l, r=>$r\n";
+
+           if (!$l and !$r) {
+#              print "STARTING at $i\n";
+               $s = $i;
+               $t = $i;
+           }
+
+           $l++;
+           $r--;
+       }
+
+       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("Reply.pl: 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;
+       }
+
+       $i++;
+    }
+
+    if ($old eq $string) {
+       &WARN("smart_replace: no subst made.");
+    }
+
+    return $string;
+}
+
 1;