2 ### Reply.pl: Kevin Lenzo (c) 1997
6 # x is y === $lhs $mhs $rhs
10 # rhs - factoid message.
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/;
30 $message =~ s/^cmd:/CMD:/;
32 my ($count, $fauthor, $result) = &sqlSelect("factoids",
33 "requested_count,created_by,factoid_value",
34 { factoid_key => $message }
42 return "$lhs $mhs $rhs" if ($literal);
47 # if there was a head...
48 my(@poss) = split '\|\|', $result;
50 $poss[$#poss] =~ s/\s$//;
53 $result = &getRandom(@poss);
57 $result = &SARit($result);
61 ### AT LAST, REPEAT PREVENTION CODE REMOVED IN FAVOUR OF GLOBAL
62 ### FLOOD REPETION AND PROTECTION. -20000124
65 ### FIXME: old mysql doesn't support
66 ### "requested_count=requested_count+1".
68 &sqlSet("factoids", {'factoid_key' => $lhs}, {
70 requested_time => time(),
71 requested_count => $count
74 # todo: rename $real to something else!
76 # my $author = &getFactInfo($lhs,"created_by") || '';
77 # $real++ if ($author =~ /^\Q$who\E\!/);
78 # $real++ if (&IsFlag("n"));
79 $real = 0 if ($msgType =~ /public/);
82 # only remove '<reply>'
83 if (!$real and $reply =~ s/^\s*<reply>\s*//i) {
85 if ($reply =~ s/^are /$lhs are /i) {
86 &VERB("Reply.pl: el-cheapo 'are' fix executed.",2);
89 } elsif (!$real and $reply =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i) {
90 # only remove '<action>' and make it an action.
91 } else { # not a short reply
94 if (exists $bots{$nuh} and $rhs !~ /^\s*$/) {
95 return "$lhs $mhs $rhs";
98 ### bot->person reply.
99 # result is random if separated by '||'.
100 # rhs is full factoid with '||'.
102 $reply = &getRandom(keys %{ $lang{'factoid'} });
103 $reply =~ s/##KEY/$lhs/;
104 $reply =~ s/##VALUE/$result/;
106 $reply = "$lhs $mhs $result";
109 if ($reply =~ s/^\Q$who\E is/you are/i) {
112 if ($reply =~ /^you are / or $reply =~ / you are /) {
113 return if ($addressed);
119 return $reply if ($literal);
121 # remove excessive beginning and end whitespaces.
122 $reply =~ s/^\s+|\s+$//g;
124 if ($reply =~ /^\s+$/) {
125 &DEBUG("Reply: Null factoid ($message)");
129 return $reply unless ($reply =~ /\$/);
135 # don't evaluate if it has factoid arguments.
136 if ($message =~ /^CMD:/i) {
137 &status("Reply: not doing substVars (eval dollar vars)");
139 $reply = &substVars($reply,1);
147 my ($l,$r) = (0,0); # l = left, r = right.
148 my ($s,$t) = (0,0); # s = start, t = marker.
153 foreach (split //, $string) {
170 my $substr = substr($old,$s,$i-$s+1);
171 push(@rand, substr($old,$t+1,$i-$t-1) );
173 my $rand = $rand[rand @rand];
174 # &status("SARing '$substr' to '$rand'.");
175 $string =~ s/\Q$substr\E/$rand/;
180 if ($_ eq "|" and $l+$r== 0 and $l==1) {
181 push(@rand, substr($old,$t+1,$i-$t-1) );
188 if ($old eq $string) {
189 &WARN("smart_replace: no subst made. (string => $string)");
200 while ($txt =~ /\((.*?)\)\?/) {
202 if (rand() > 0.5) { # fix.
203 &status("Factoid transform: keeping '$str'.");
204 $txt =~ s/\(\Q$str\E\)\?/$str/;
206 &status("Factoid transform: removing '$str'.");
207 $txt =~ s/\(\Q$str\E\)\?\s?//;
210 last if ($done >= 10); # just in case.
214 # EG: (0-32768) => 6325
215 ### TODO: (1-10,20-30,40) => 24
216 while ($txt =~ /\((\d+)-(\d+)\)/) {
217 my ($lower,$upper) = ($1,$2);
218 my $new = int(rand $upper-$lower) + $lower;
220 &status("SARing '$&' to '$new' (2).");
223 last if ($done >= 10); # just in case.
227 # EG: (blah1|blah2|blah3|) => blah1
228 while ($txt =~ /.*\((.*\|.*?)\).*/) {
229 $txt = &smart_replace($txt);
232 last if ($done >= 10); # just in case.
234 &status("Reply.pl: $done SARs done.") if ($done);
240 my($reply,$flag) = @_;
243 # todo: support localtime.
244 my $date = scalar(gmtime());
245 $date =~ s/\:\d+(\s+\w+)\s+\d+$/$1/;
246 $reply =~ s/\$date/$date/gi;
247 $date =~ s/\w+\s+\w+\s+\d+\s+//;
248 $reply =~ s/\$time/$date/gi;
252 $reply =~ s/\$nick/$who/g;
253 $reply =~ s/\$who/$who/g; # backward compat.
256 if ($reply =~ /\$(user(name)?|host)/) {
257 my ($username, $hostname) = split /\@/, $uh;
258 $reply =~ s/\$user(name)?/$username/g;
259 $reply =~ s/\$host(name)?/$hostname/g;
261 $reply =~ s/\$chan(nel)?/$talkchannel/g;
262 if ($msgType =~ /public/) {
263 $reply =~ s/\$lastspeaker/$lastWho{$talkchannel}/g;
265 $reply =~ s/\$lastspeaker/$lastWho/g;
268 if ($reply =~ /\$rand/) {
272 if ($reply =~ /\$randnick/) {
273 my @nicks = keys %{ $channels{$chan}{''} };
274 my $randnick = $nicks[ int($rand*$#nicks) ];
275 $reply =~ s/\$randnick/$randnick/g;
279 if ($reply =~ /\$rand(\d+)(\.(\d+))?/) {
283 #&DEBUG("dot => $dot, max => $max, rand=>$rand");
284 $rand = sprintf("%.*f", $dot, $rand*$max);
286 &DEBUG("swapping $orig to $rand");
287 $reply =~ s/\Q$orig\E/$rand/eg;
289 $reply =~ s/\$rand/$rand/g;
293 $reply =~ s/\$ident/$ident/g;
295 if ($reply =~ /\$startTime/) {
296 my $time = scalar(gmtime $^T);
297 $reply =~ s/\$startTime/$time/;
300 if ($reply =~ /\$uptime/) {
301 my $uptime = &Time2String(time() - $^T);
302 $reply =~ s/\$uptime/$uptime/;
305 if ($reply =~ /\$factoids/) {
306 my $factoids = &countKeys("factoids");
307 $reply =~ s/\$factoids/$factoids/;
310 if ($reply =~ /\$Fupdate/) {
311 my $x = "\002$count{'Update'}\002 ".
312 &fixPlural("modification", $count{'Update'});
313 $reply =~ s/\$Fupdate/$x/;
316 if ($reply =~ /\$Fquestion/) {
317 my $x = "\002$count{'Question'}\002 ".
318 &fixPlural("question", $count{'Question'});
319 $reply =~ s/\$Fquestion/$x/;
322 if ($reply =~ /\$Fdunno/) {
323 my $x = "\002$count{'Dunno'}\002 ".
324 &fixPlural("dunno", $count{'Dunno'});
325 $reply =~ s/\$Fdunno/$x/;
328 $reply =~ s/\$memusage/$memusage/;