2 ### Reply.pl: Kevin Lenzo (c) 1997
6 # x is y === $lhs $mhs $rhs
10 # rhs - factoid message.
13 if (&IsParam("useStrict")) { use strict; }
15 use vars qw($msgType $uh $lastWho $ident);
16 use vars qw(%lang %lastWho);
23 $orig{message} = $message;
25 if (!defined $message or $message =~ /^\s*$/) {
26 &WARN("getR: message == NULL.");
30 $message =~ tr/A-Z/a-z/;
32 if ($result = &getFactoid($message)) {
40 # if there was a head...
41 my(@poss) = split '\|\|', $result;
43 $poss[$#poss] =~ s/\s$//;
45 if ((@poss > 1) && ($msgType =~ /public/)) {
46 $result = &getRandom(@poss);
50 my $fauthor = &dbGet("factoids", "factoid_key", $message, "created_by");
51 ### we need non-evaluating regex like in factoid sar.
52 if ($msgType =~ /^private$/) {
53 if (defined $fauthor and $fauthor =~ /^\Q$who\E\!/i) {
54 &status("Reply.pl: author requested own factoid in private; literal!");
61 while ($result =~ /\((.*?)\)\?/) {
63 if (rand() > 0.5) { # fix.
64 &status("Factoid transform: keeping '$str'.");
65 $result =~ s/\(\Q$str\E\)\?/$str/;
67 &status("Factoid transform: removing '$str'.");
68 $result =~ s/\(\Q$str\E\)\?\s?//;
71 last if ($done >= 10); # just in case.
75 # EG: (0-32768) => 6325
76 ### TODO: (1-10,20-30,40) => 24
77 while ($result =~ /\((\d+)-(\d+)\)/) {
78 my ($lower,$upper) = ($1,$2);
79 my $new = int(rand $upper-$lower) + $lower;
81 &status("Reply.pl: SARing '$&' to '$new'.");
82 $result =~ s/$&/$new/;
84 last if ($done >= 10); # just in case.
88 # EG: (blah1|blah2|blah3|) => blah1
89 while ($result =~ /.*\((.*\|.*?)\).*/) {
90 $result = &smart_replace($result);
93 last if ($done >= 10); # just in case.
95 &status("Reply.pl: $done SARs done.") if ($done);
100 ### AT LAST, REPEAT PREVENTION CODE REMOVED IN FAVOUR OF GLOBAL
101 ### FLOOD REPETION AND PROTECTION. -20000124
104 &setFactInfo($lhs,"requested_by", $nuh);
105 &setFactInfo($lhs,"requested_time", time());
106 ### FIXME: old mysql doesn't support
107 ### "requested_count=requested_count+1".
108 my $count = &getFactInfo($lhs,"requested_count") || 0;
110 &setFactInfo($lhs,"requested_count", $count);
113 my $author = &getFactInfo($lhs,"created_by") || '';
115 $real++ if ($author =~ /^\Q$who\E\!/);
116 $real++ if (&IsFlag("n"));
117 $real = 0 if ($msgType =~ /public/);
119 ### fix up the reply.
120 # only remove '<reply>'
121 if (!$real and $reply =~ s/^\s*<reply>\s*//i) {
123 if ($reply =~ s/^are /$lhs are /i) {
124 &DEBUG("Reply.pl: el-cheapo 'are' fix executed.");
125 $mhs = "are"; # what's this for?
128 } elsif (!$real and $reply =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i) {
129 # only remove '<action>' and make it an action.
130 } else { # not a short reply
133 if (exists $bots{$nuh} and $rhs !~ /^\s*$/) {
134 return "$lhs $mhs $rhs";
137 ### bot->person reply.
138 # result is random if separated by '||'.
139 # rhs is full factoid with '||'.
141 $reply = &getRandom(keys %{$lang{'factoid'}});
142 $reply =~ s/##KEY/$lhs/;
143 $reply =~ s/##VALUE/$result/;
145 $reply = "$lhs $mhs $result";
148 if ($reply =~ s/^\Q$who\E is/you are/i) {
151 if ($reply =~ /^you are / or $reply =~ / you are /) {
152 return $noreply if ($addressed);
158 return $reply if ($literal);
160 # remove excessive beginning and end whitespaces.
161 $reply =~ s/^\s+|\s+$//g;
163 if ($reply =~ /^\s+$/) {
164 &DEBUG("Reply: Null factoid ($message)");
168 return $reply unless ($reply =~ /\$/);
175 my $date = scalar(localtime());
176 $date =~ s/\:\d+(\s+\w+)\s+\d+$/$1/;
177 $reply =~ s/\$date/$date/gi;
178 $date =~ s/\w+\s+\w+\s+\d+\s+//;
179 $reply =~ s/\$time/$date/gi;
182 $reply =~ s/\$nick/$who/g;
183 $reply =~ s/\$who/$who/g; # backward compat.
184 if ($reply =~ /\$(user(name)?|host)/) {
185 my ($username, $hostname) = split /\@/, $uh;
186 $reply =~ s/\$user(name)?/$username/g;
187 $reply =~ s/\$host(name)?/$hostname/g;
189 $reply =~ s/\$chan(nel)?/$talkchannel/g;
190 if ($msgType =~ /public/) {
191 $reply =~ s/\$lastspeaker/$lastWho{$talkchannel}/g;
193 $reply =~ s/\$lastspeaker/$lastWho/g;
196 if ($reply =~ /\$rand/) {
198 my $randp = int($rand*100);
199 $reply =~ s/\$randpercentage/$randp/g; # ???
201 if ($reply =~ /\$randnick/) {
202 my @nicks = keys %{$channels{$chan}{''}};
203 my $randnick = $nicks[ int($rand*$#nicks) ];
204 s/\$randnick/$randnick/;
207 ### TODO: number of digits. 'x.y'
208 if ($reply =~ /\$rand(\d+)/) {
209 # will this work as it does in C?
210 $rand = sprintf("%*f", $1, $rand);
212 $reply =~ s/\$rand/$rand/g;
215 $reply =~ s/\$factoid/$lhs/g;
216 $reply =~ s/\$ident/$ident/g;
218 if ($reply =~ /\$startTime/) {
219 my $time = scalar(localtime $^T);
220 $reply =~ s/\$startTime/$time/;
223 if ($reply =~ /\$uptime/) {
224 my $uptime = &Time2String(time() - $^T);
225 $reply =~ s/\$uptime/$uptime/;
228 if ($reply =~ /\$factoids/) {
229 my $count = &countKeys("factoids");
230 $reply =~ s/\$factoids/$factoids/;
233 if ($reply =~ /\$Fupdate/) {
234 my $x = "\002$count{'Update'}\002 ".
235 &fixPlural("modification", $count{'Update'});
236 $reply =~ s/\$Fupdate/$x/;
239 if ($reply =~ /\$Fquestion/) {
240 my $x = "\002$count{'Question'}\002 ".
241 &fixPlural("question", $count{'Question'});
242 $reply =~ s/\$Fquestion/$x/;
245 if ($reply =~ /\$Fdunno/) {
246 my $x = "\002$count{'Dunno'}\002 ".
247 &fixPlural("dunno", $count{'Dunno'});
248 $reply =~ s/\$Fdunno/$x/;
251 $reply =~ s/\$memusage/$memusage/;
258 my ($l,$r) = (0,0); # l = left, r = right.
259 my ($s,$t) = (0,0); # s = start, t = marker.
264 foreach (split //, $string) {
267 ### print "( l=>$l, r=>$r\n";
270 # print "STARTING at $i\n";
280 ### print ") l=>$l, r=>$r\n";
286 my $substr = substr($old,$s,$i-$s+1);
287 # print "STOP at $i $substr\n";
288 push(@rand, substr($old,$t+1,$i-$t-1) );
290 my $rand = $rand[rand @rand];
291 &status("Reply.pl: SARing '$substr' to '$rand'.");
292 $string =~ s/\Q$substr\E/$rand/;
297 if ($_ eq "|" and $l+$r== 0 and $l==1) {
298 # print "| at $i (l=>$l,r=>$r)\n";
299 push(@rand, substr($old,$t+1,$i-$t-1) );
306 if ($old eq $string) {
307 &WARN("smart_replace: no subst made.");