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 my ($result, $fauthor, $count) = &dbGet("factoids",
32 "factoid_value,created_by,requested_count", "factoid_key=".&dbQuote($message) );
38 return "$lhs $mhs $rhs" if ($literal);
43 # if there was a head...
44 my(@poss) = split '\|\|', $result;
46 $poss[$#poss] =~ s/\s$//;
49 $result = &getRandom(@poss);
53 $result = &SARit($result);
57 ### AT LAST, REPEAT PREVENTION CODE REMOVED IN FAVOUR OF GLOBAL
58 ### FLOOD REPETION AND PROTECTION. -20000124
61 ### FIXME: old mysql doesn't support
62 ### "requested_count=requested_count+1".
64 &dbSet("factoids", {'factoid_key' => $lhs}, {
66 requested_time => time(),
67 requested_count => $count
70 # todo: rename $real to something else!
72 # my $author = &getFactInfo($lhs,"created_by") || '';
73 # $real++ if ($author =~ /^\Q$who\E\!/);
74 # $real++ if (&IsFlag("n"));
75 $real = 0 if ($msgType =~ /public/);
78 # only remove '<reply>'
79 if (!$real and $reply =~ s/^\s*<reply>\s*//i) {
81 if ($reply =~ s/^are /$lhs are /i) {
82 &VERB("Reply.pl: el-cheapo 'are' fix executed.",2);
85 } elsif (!$real and $reply =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i) {
86 # only remove '<action>' and make it an action.
87 } else { # not a short reply
90 if (exists $bots{$nuh} and $rhs !~ /^\s*$/) {
91 return "$lhs $mhs $rhs";
94 ### bot->person reply.
95 # result is random if separated by '||'.
96 # rhs is full factoid with '||'.
98 $reply = &getRandom(keys %{ $lang{'factoid'} });
99 $reply =~ s/##KEY/$lhs/;
100 $reply =~ s/##VALUE/$result/;
102 $reply = "$lhs $mhs $result";
105 if ($reply =~ s/^\Q$who\E is/you are/i) {
108 if ($reply =~ /^you are / or $reply =~ / you are /) {
109 return if ($addressed);
115 return $reply if ($literal);
117 # remove excessive beginning and end whitespaces.
118 $reply =~ s/^\s+|\s+$//g;
120 if ($reply =~ /^\s+$/) {
121 &DEBUG("Reply: Null factoid ($message)");
125 return $reply unless ($reply =~ /\$/);
131 # don't evaluate if it has factoid arguments.
132 if ($message =~ /^CMD:/i) {
133 &status("Reply: not doing substVars (eval dollar vars)");
135 $reply = &substVars($reply,1);
143 my ($l,$r) = (0,0); # l = left, r = right.
144 my ($s,$t) = (0,0); # s = start, t = marker.
149 foreach (split //, $string) {
152 ### print "( l=>$l, r=>$r\n";
155 # print "STARTING at $i\n";
165 ### print ") l=>$l, r=>$r\n";
171 my $substr = substr($old,$s,$i-$s+1);
172 # print "STOP at $i $substr\n";
173 push(@rand, substr($old,$t+1,$i-$t-1) );
175 my $rand = $rand[rand @rand];
176 &status("SARing '$substr' to '$rand'.");
177 $string =~ s/\Q$substr\E/$rand/;
182 if ($_ eq "|" and $l+$r== 0 and $l==1) {
183 # print "| at $i (l=>$l,r=>$r)\n";
184 push(@rand, substr($old,$t+1,$i-$t-1) );
191 if ($old eq $string) {
192 &WARN("smart_replace: no subst made. (string => $string)");
203 while ($txt =~ /\((.*?)\)\?/) {
205 if (rand() > 0.5) { # fix.
206 &status("Factoid transform: keeping '$str'.");
207 $txt =~ s/\(\Q$str\E\)\?/$str/;
209 &status("Factoid transform: removing '$str'.");
210 $txt =~ s/\(\Q$str\E\)\?\s?//;
213 last if ($done >= 10); # just in case.
217 # EG: (0-32768) => 6325
218 ### TODO: (1-10,20-30,40) => 24
219 while ($txt =~ /\((\d+)-(\d+)\)/) {
220 my ($lower,$upper) = ($1,$2);
221 my $new = int(rand $upper-$lower) + $lower;
223 &status("SARing '$&' to '$new' (2).");
226 last if ($done >= 10); # just in case.
230 # EG: (blah1|blah2|blah3|) => blah1
231 while ($txt =~ /.*\((.*\|.*?)\).*/) {
232 $txt = &smart_replace($txt);
235 last if ($done >= 10); # just in case.
237 &status("Reply.pl: $done SARs done.") if ($done);
243 my($reply,$flag) = @_;
246 # todo: support localtime.
247 my $date = scalar(gmtime());
248 $date =~ s/\:\d+(\s+\w+)\s+\d+$/$1/;
249 $reply =~ s/\$date/$date/gi;
250 $date =~ s/\w+\s+\w+\s+\d+\s+//;
251 $reply =~ s/\$time/$date/gi;
255 $reply =~ s/\$nick/$who/g;
256 $reply =~ s/\$who/$who/g; # backward compat.
259 if ($reply =~ /\$(user(name)?|host)/) {
260 my ($username, $hostname) = split /\@/, $uh;
261 $reply =~ s/\$user(name)?/$username/g;
262 $reply =~ s/\$host(name)?/$hostname/g;
264 $reply =~ s/\$chan(nel)?/$talkchannel/g;
265 if ($msgType =~ /public/) {
266 $reply =~ s/\$lastspeaker/$lastWho{$talkchannel}/g;
268 $reply =~ s/\$lastspeaker/$lastWho/g;
271 if ($reply =~ /\$rand/) {
275 if ($reply =~ /\$randnick/) {
276 my @nicks = keys %{ $channels{$chan}{''} };
277 my $randnick = $nicks[ int($rand*$#nicks) ];
278 $reply =~ s/\$randnick/$randnick/g;
282 if ($reply =~ /\$rand(\d+)(\.(\d+))?/) {
286 #&DEBUG("dot => $dot, max => $max, rand=>$rand");
287 $rand = sprintf("%.*f", $dot, $rand*$max);
289 &DEBUG("swapping $orig to $rand");
290 $reply =~ s/\Q$orig\E/$rand/eg;
292 $reply =~ s/\$rand/$rand/g;
296 $reply =~ s/\$ident/$ident/g;
298 if ($reply =~ /\$startTime/) {
299 my $time = scalar(gmtime $^T);
300 $reply =~ s/\$startTime/$time/;
303 if ($reply =~ /\$uptime/) {
304 my $uptime = &Time2String(time() - $^T);
305 $reply =~ s/\$uptime/$uptime/;
308 if ($reply =~ /\$factoids/) {
309 my $factoids = &countKeys("factoids");
310 $reply =~ s/\$factoids/$factoids/;
313 if ($reply =~ /\$Fupdate/) {
314 my $x = "\002$count{'Update'}\002 ".
315 &fixPlural("modification", $count{'Update'});
316 $reply =~ s/\$Fupdate/$x/;
319 if ($reply =~ /\$Fquestion/) {
320 my $x = "\002$count{'Question'}\002 ".
321 &fixPlural("question", $count{'Question'});
322 $reply =~ s/\$Fquestion/$x/;
325 if ($reply =~ /\$Fdunno/) {
326 my $x = "\002$count{'Dunno'}\002 ".
327 &fixPlural("dunno", $count{'Dunno'});
328 $reply =~ s/\$Fdunno/$x/;
331 $reply =~ s/\$memusage/$memusage/;