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 # &DEBUG("Reply: reply => $reply");
140 $reply = &substVars($reply,1);
141 # &DEBUG("Reply: reply => $reply");
148 my ($l,$r) = (0,0); # l = left, r = right.
149 my ($s,$t) = (0,0); # s = start, t = marker.
154 foreach (split //, $string) {
157 ### print "( l=>$l, r=>$r\n";
160 # print "STARTING at $i\n";
170 ### print ") l=>$l, r=>$r\n";
176 my $substr = substr($old,$s,$i-$s+1);
177 # print "STOP at $i $substr\n";
178 push(@rand, substr($old,$t+1,$i-$t-1) );
180 my $rand = $rand[rand @rand];
181 &status("SARing '$substr' to '$rand'.");
182 $string =~ s/\Q$substr\E/$rand/;
187 if ($_ eq "|" and $l+$r== 0 and $l==1) {
188 # print "| at $i (l=>$l,r=>$r)\n";
189 push(@rand, substr($old,$t+1,$i-$t-1) );
196 if ($old eq $string) {
197 &WARN("smart_replace: no subst made. (string => $string)");
208 while ($txt =~ /\((.*?)\)\?/) {
210 if (rand() > 0.5) { # fix.
211 &status("Factoid transform: keeping '$str'.");
212 $txt =~ s/\(\Q$str\E\)\?/$str/;
214 &status("Factoid transform: removing '$str'.");
215 $txt =~ s/\(\Q$str\E\)\?\s?//;
218 last if ($done >= 10); # just in case.
222 # EG: (0-32768) => 6325
223 ### TODO: (1-10,20-30,40) => 24
224 while ($txt =~ /\((\d+)-(\d+)\)/) {
225 my ($lower,$upper) = ($1,$2);
226 my $new = int(rand $upper-$lower) + $lower;
228 &status("SARing '$&' to '$new' (2).");
231 last if ($done >= 10); # just in case.
235 # EG: (blah1|blah2|blah3|) => blah1
236 while ($txt =~ /.*\((.*\|.*?)\).*/) {
237 $txt = &smart_replace($txt);
240 last if ($done >= 10); # just in case.
242 &status("Reply.pl: $done SARs done.") if ($done);
248 my($reply,$flag) = @_;
251 my $date = scalar(localtime());
252 $date =~ s/\:\d+(\s+\w+)\s+\d+$/$1/;
253 $reply =~ s/\$date/$date/gi;
254 $date =~ s/\w+\s+\w+\s+\d+\s+//;
256 $reply =~ s/\$time/$date/gi;
260 $reply =~ s/\$nick/$who/g;
261 $reply =~ s/\$who/$who/g; # backward compat.
264 if ($reply =~ /\$(user(name)?|host)/) {
265 my ($username, $hostname) = split /\@/, $uh;
266 $reply =~ s/\$user(name)?/$username/g;
267 $reply =~ s/\$host(name)?/$hostname/g;
269 $reply =~ s/\$chan(nel)?/$talkchannel/g;
270 if ($msgType =~ /public/) {
271 $reply =~ s/\$lastspeaker/$lastWho{$talkchannel}/g;
273 $reply =~ s/\$lastspeaker/$lastWho/g;
276 if ($reply =~ /\$rand/) {
280 if ($reply =~ /\$randnick/) {
281 my @nicks = keys %{ $channels{$chan}{''} };
282 my $randnick = $nicks[ int($rand*$#nicks) ];
283 $reply =~ s/\$randnick/$randnick/g;
287 ### TODO: number of digits. 'x.y'
289 if ($reply =~ /\$rand(\d+)(\.(\d+))?/) {
292 &status("dot => $dot, max => $max, rand=>$rand");
293 $rand = sprintf("%.*f", $dot, $rand*$max);
296 &status("swapping $orig to $rand");
297 &status("reply => $reply");
298 $reply =~ s/$orig/$rand/eg;
299 &status("reply => $reply");
302 $reply =~ s/\$rand/$rand/g;
305 $reply =~ s/\$ident/$ident/g;
307 if ($reply =~ /\$startTime/) {
308 my $time = scalar(localtime $^T);
309 $reply =~ s/\$startTime/$time/;
312 if ($reply =~ /\$uptime/) {
313 my $uptime = &Time2String(time() - $^T);
314 $reply =~ s/\$uptime/$uptime/;
317 if ($reply =~ /\$factoids/) {
318 my $count = &countKeys("factoids");
319 $reply =~ s/\$factoids/$factoids/;
322 if ($reply =~ /\$Fupdate/) {
323 my $x = "\002$count{'Update'}\002 ".
324 &fixPlural("modification", $count{'Update'});
325 $reply =~ s/\$Fupdate/$x/;
328 if ($reply =~ /\$Fquestion/) {
329 my $x = "\002$count{'Question'}\002 ".
330 &fixPlural("question", $count{'Question'});
331 $reply =~ s/\$Fquestion/$x/;
334 if ($reply =~ /\$Fdunno/) {
335 my $x = "\002$count{'Dunno'}\002 ".
336 &fixPlural("dunno", $count{'Dunno'});
337 $reply =~ s/\$Fdunno/$x/;
340 $reply =~ s/\$memusage/$memusage/;