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);
22 $orig{message} = $message;
24 if (!defined $message or $message =~ /^\s*$/) {
25 &WARN("getR: message == NULL.");
29 $message =~ tr/A-Z/a-z/;
31 if ($result = &getFactoid($message)) {
36 return "$lhs $mhs $rhs" if ($literal);
41 # if there was a head...
42 my(@poss) = split '\|\|', $result;
44 $poss[$#poss] =~ s/\s$//;
47 $result = &getRandom(@poss);
51 my $fauthor = &dbGet("factoids", "created_by", "factoid_key='$message'");
52 $result = &SARit($result);
56 ### AT LAST, REPEAT PREVENTION CODE REMOVED IN FAVOUR OF GLOBAL
57 ### FLOOD REPETION AND PROTECTION. -20000124
60 ### FIXME: old mysql doesn't support
61 ### "requested_count=requested_count+1".
62 my $count = &getFactInfo($lhs,"requested_count") || 0;
66 &setFactInfo($lhs,"requested_by", $nuh);
67 &setFactInfo($lhs,"requested_time", time());
68 &setFactInfo($lhs,"requested_count", $count);
70 &dbReplace("factoids", (
73 requested_time => time(),
74 requested_count => $count
78 # todo: rename $real to something else!
80 # my $author = &getFactInfo($lhs,"created_by") || '';
81 # $real++ if ($author =~ /^\Q$who\E\!/);
82 # $real++ if (&IsFlag("n"));
83 $real = 0 if ($msgType =~ /public/);
86 # only remove '<reply>'
87 if (!$real and $reply =~ s/^\s*<reply>\s*//i) {
89 if ($reply =~ s/^are /$lhs are /i) {
90 &VERB("Reply.pl: el-cheapo 'are' fix executed.",2);
93 } elsif (!$real and $reply =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i) {
94 # only remove '<action>' and make it an action.
95 } else { # not a short reply
98 if (exists $bots{$nuh} and $rhs !~ /^\s*$/) {
99 return "$lhs $mhs $rhs";
102 ### bot->person reply.
103 # result is random if separated by '||'.
104 # rhs is full factoid with '||'.
106 $reply = &getRandom(keys %{ $lang{'factoid'} });
107 $reply =~ s/##KEY/$lhs/;
108 $reply =~ s/##VALUE/$result/;
110 $reply = "$lhs $mhs $result";
113 if ($reply =~ s/^\Q$who\E is/you are/i) {
116 if ($reply =~ /^you are / or $reply =~ / you are /) {
117 return if ($addressed);
123 return $reply if ($literal);
125 # remove excessive beginning and end whitespaces.
126 $reply =~ s/^\s+|\s+$//g;
128 if ($reply =~ /^\s+$/) {
129 &DEBUG("Reply: Null factoid ($message)");
133 return $reply unless ($reply =~ /\$/);
139 $reply = &substVars($reply);
146 my ($l,$r) = (0,0); # l = left, r = right.
147 my ($s,$t) = (0,0); # s = start, t = marker.
152 foreach (split //, $string) {
155 ### print "( l=>$l, r=>$r\n";
158 # print "STARTING at $i\n";
168 ### print ") l=>$l, r=>$r\n";
174 my $substr = substr($old,$s,$i-$s+1);
175 # print "STOP at $i $substr\n";
176 push(@rand, substr($old,$t+1,$i-$t-1) );
178 my $rand = $rand[rand @rand];
179 &status("SARing '$substr' to '$rand'.");
180 $string =~ s/\Q$substr\E/$rand/;
185 if ($_ eq "|" and $l+$r== 0 and $l==1) {
186 # print "| at $i (l=>$l,r=>$r)\n";
187 push(@rand, substr($old,$t+1,$i-$t-1) );
194 if ($old eq $string) {
195 &WARN("smart_replace: no subst made. (string => $string)");
206 while ($txt =~ /\((.*?)\)\?/) {
208 if (rand() > 0.5) { # fix.
209 &status("Factoid transform: keeping '$str'.");
210 $txt =~ s/\(\Q$str\E\)\?/$str/;
212 &status("Factoid transform: removing '$str'.");
213 $txt =~ s/\(\Q$str\E\)\?\s?//;
216 last if ($done >= 10); # just in case.
220 # EG: (0-32768) => 6325
221 ### TODO: (1-10,20-30,40) => 24
222 while ($txt =~ /\((\d+)-(\d+)\)/) {
223 my ($lower,$upper) = ($1,$2);
224 my $new = int(rand $upper-$lower) + $lower;
226 &status("SARing '$&' to '$new' (2).");
229 last if ($done >= 10); # just in case.
233 # EG: (blah1|blah2|blah3|) => blah1
234 while ($txt =~ /.*\((.*\|.*?)\).*/) {
235 $txt = &smart_replace($txt);
238 last if ($done >= 10); # just in case.
240 &status("Reply.pl: $done SARs done.") if ($done);
249 my $date = scalar(localtime());
250 $date =~ s/\:\d+(\s+\w+)\s+\d+$/$1/;
251 $reply =~ s/\$date/$date/gi;
252 $date =~ s/\w+\s+\w+\s+\d+\s+//;
253 $reply =~ s/\$time/$date/gi;
256 $reply =~ s/\$nick/$who/g;
257 $reply =~ s/\$who/$who/g; # backward compat.
258 if ($reply =~ /\$(user(name)?|host)/) {
259 my ($username, $hostname) = split /\@/, $uh;
260 $reply =~ s/\$user(name)?/$username/g;
261 $reply =~ s/\$host(name)?/$hostname/g;
263 $reply =~ s/\$chan(nel)?/$talkchannel/g;
264 if ($msgType =~ /public/) {
265 $reply =~ s/\$lastspeaker/$lastWho{$talkchannel}/g;
267 $reply =~ s/\$lastspeaker/$lastWho/g;
270 if ($reply =~ /\$rand/) {
272 my $randp = int($rand*100);
273 $reply =~ s/\$randpercentage/$randp/g; # ???
275 if ($reply =~ /\$randnick/) {
276 my @nicks = keys %{ $channels{$chan}{''} };
277 my $randnick = $nicks[ int($rand*$#nicks) ];
278 s/\$randnick/$randnick/;
281 ### TODO: number of digits. 'x.y'
282 if ($reply =~ /\$rand(\d+)/) {
283 # will this work as it does in C?
284 $rand = sprintf("%*f", $1, $rand);
286 $reply =~ s/\$rand/$rand/g;
289 $reply =~ s/\$factoid/$lhs/g;
290 $reply =~ s/\$ident/$ident/g;
292 if ($reply =~ /\$startTime/) {
293 my $time = scalar(localtime $^T);
294 $reply =~ s/\$startTime/$time/;
297 if ($reply =~ /\$uptime/) {
298 my $uptime = &Time2String(time() - $^T);
299 $reply =~ s/\$uptime/$uptime/;
302 if ($reply =~ /\$factoids/) {
303 my $count = &countKeys("factoids");
304 $reply =~ s/\$factoids/$factoids/;
307 if ($reply =~ /\$Fupdate/) {
308 my $x = "\002$count{'Update'}\002 ".
309 &fixPlural("modification", $count{'Update'});
310 $reply =~ s/\$Fupdate/$x/;
313 if ($reply =~ /\$Fquestion/) {
314 my $x = "\002$count{'Question'}\002 ".
315 &fixPlural("question", $count{'Question'});
316 $reply =~ s/\$Fquestion/$x/;
319 if ($reply =~ /\$Fdunno/) {
320 my $x = "\002$count{'Dunno'}\002 ".
321 &fixPlural("dunno", $count{'Dunno'});
322 $reply =~ s/\$Fdunno/$x/;
325 $reply =~ s/\$memusage/$memusage/;