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=".&dbQuote($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", "factoid_key", (
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 # don't evaluate if it has factoid arguments.
140 if ($message =~ /^CMD:/i) {
141 &status("Reply: not doing substVars (eval dollar vars)");
143 $reply = &substVars($reply,1);
151 my ($l,$r) = (0,0); # l = left, r = right.
152 my ($s,$t) = (0,0); # s = start, t = marker.
157 foreach (split //, $string) {
160 ### print "( l=>$l, r=>$r\n";
163 # print "STARTING at $i\n";
173 ### print ") l=>$l, r=>$r\n";
179 my $substr = substr($old,$s,$i-$s+1);
180 # print "STOP at $i $substr\n";
181 push(@rand, substr($old,$t+1,$i-$t-1) );
183 my $rand = $rand[rand @rand];
184 &status("SARing '$substr' to '$rand'.");
185 $string =~ s/\Q$substr\E/$rand/;
190 if ($_ eq "|" and $l+$r== 0 and $l==1) {
191 # print "| at $i (l=>$l,r=>$r)\n";
192 push(@rand, substr($old,$t+1,$i-$t-1) );
199 if ($old eq $string) {
200 &WARN("smart_replace: no subst made. (string => $string)");
211 while ($txt =~ /\((.*?)\)\?/) {
213 if (rand() > 0.5) { # fix.
214 &status("Factoid transform: keeping '$str'.");
215 $txt =~ s/\(\Q$str\E\)\?/$str/;
217 &status("Factoid transform: removing '$str'.");
218 $txt =~ s/\(\Q$str\E\)\?\s?//;
221 last if ($done >= 10); # just in case.
225 # EG: (0-32768) => 6325
226 ### TODO: (1-10,20-30,40) => 24
227 while ($txt =~ /\((\d+)-(\d+)\)/) {
228 my ($lower,$upper) = ($1,$2);
229 my $new = int(rand $upper-$lower) + $lower;
231 &status("SARing '$&' to '$new' (2).");
234 last if ($done >= 10); # just in case.
238 # EG: (blah1|blah2|blah3|) => blah1
239 while ($txt =~ /.*\((.*\|.*?)\).*/) {
240 $txt = &smart_replace($txt);
243 last if ($done >= 10); # just in case.
245 &status("Reply.pl: $done SARs done.") if ($done);
251 my($reply,$flag) = @_;
254 my $date = scalar(localtime());
255 $date =~ s/\:\d+(\s+\w+)\s+\d+$/$1/;
256 $reply =~ s/\$date/$date/gi;
257 $date =~ s/\w+\s+\w+\s+\d+\s+//;
259 $reply =~ s/\$time/$date/gi;
263 $reply =~ s/\$nick/$who/g;
264 $reply =~ s/\$who/$who/g; # backward compat.
267 if ($reply =~ /\$(user(name)?|host)/) {
268 my ($username, $hostname) = split /\@/, $uh;
269 $reply =~ s/\$user(name)?/$username/g;
270 $reply =~ s/\$host(name)?/$hostname/g;
272 $reply =~ s/\$chan(nel)?/$talkchannel/g;
273 if ($msgType =~ /public/) {
274 $reply =~ s/\$lastspeaker/$lastWho{$talkchannel}/g;
276 $reply =~ s/\$lastspeaker/$lastWho/g;
279 if ($reply =~ /\$rand/) {
283 if ($reply =~ /\$randnick/) {
284 my @nicks = keys %{ $channels{$chan}{''} };
285 my $randnick = $nicks[ int($rand*$#nicks) ];
286 $reply =~ s/\$randnick/$randnick/g;
290 ### TODO: number of digits. 'x.y'
292 if ($reply =~ /\$rand(\d+)(\.(\d+))?/) {
295 &status("dot => $dot, max => $max, rand=>$rand");
296 $rand = sprintf("%.*f", $dot, $rand*$max);
299 &status("swapping $orig to $rand");
300 &status("reply => $reply");
301 $reply =~ s/$orig/$rand/eg;
302 &status("reply => $reply");
305 $reply =~ s/\$rand/$rand/g;
308 $reply =~ s/\$ident/$ident/g;
310 if ($reply =~ /\$startTime/) {
311 my $time = scalar(localtime $^T);
312 $reply =~ s/\$startTime/$time/;
315 if ($reply =~ /\$uptime/) {
316 my $uptime = &Time2String(time() - $^T);
317 $reply =~ s/\$uptime/$uptime/;
320 if ($reply =~ /\$factoids/) {
321 my $count = &countKeys("factoids");
322 $reply =~ s/\$factoids/$factoids/;
325 if ($reply =~ /\$Fupdate/) {
326 my $x = "\002$count{'Update'}\002 ".
327 &fixPlural("modification", $count{'Update'});
328 $reply =~ s/\$Fupdate/$x/;
331 if ($reply =~ /\$Fquestion/) {
332 my $x = "\002$count{'Question'}\002 ".
333 &fixPlural("question", $count{'Question'});
334 $reply =~ s/\$Fquestion/$x/;
337 if ($reply =~ /\$Fdunno/) {
338 my $x = "\002$count{'Dunno'}\002 ".
339 &fixPlural("dunno", $count{'Dunno'});
340 $reply =~ s/\$Fdunno/$x/;
343 $reply =~ s/\$memusage/$memusage/;