my($message) = @_;
my($lhs,$mhs,$rhs);
my($result,$reply);
- my $literal = 0;
$orig{message} = $message;
if (!defined $message or $message =~ /^\s*$/) {
$lhs = $message;
$mhs = "is";
$rhs = $result;
+
+ return "$lhs $mhs $rhs" if ($literal);
} else {
return '';
}
$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.
}
# 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 {
# fix the person.
} else {
if ($reply =~ /^you are / or $reply =~ / you are /) {
- return $noreply if ($addressed);
+ return if ($addressed);
}
}
}
# remove excessive beginning and end whitespaces.
$reply =~ s/^\s+|\s+$//g;
- if (length($reply) < 5 or $reply =~ /^\s+$/) {
- &DEBUG("Reply: FIXME: reply => '$reply'.");
+ if ($reply =~ /^\s+$/) {
+ &DEBUG("Reply: Null factoid ($message)");
return '';
}
$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/;
}
$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;