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);
23 $orig{message} = $message;
25 if (!defined $message or $message =~ /^\s*$/) {
26 &WARN("getR: message == NULL.");
30 $message =~ tr/A-Z/a-z/;
32 if ($result = &getFactoid($message)) {
40 # if there was a head...
41 my(@poss) = split '\|\|', $result;
43 $poss[$#poss] =~ s/\s$//;
45 if ((@poss > 1) && ($msgType =~ /public/)) {
46 $result = &getRandom(@poss);
50 my $fauthor = &dbGet("factoids", "factoid_key", $message, "created_by");
51 ### we need non-evaluating regex like in factoid sar.
52 if ($msgType =~ /^private$/) {
53 if (defined $fauthor and $fauthor =~ /^\Q$who\E\!/i) {
54 &status("Reply.pl: author requested own factoid in private; literal!");
61 while ($result =~ /\((.*?)\)\?/) {
63 if (rand() > 0.5) { # fix.
64 &status("Factoid transform: keeping '$str'.");
65 $result =~ s/\(\Q$str\E\)\?/$str/;
67 &status("Factoid transform: removing '$str'.");
68 $result =~ s/\(\Q$str\E\)\?\s?//;
71 last if ($done >= 10); # just in case.
75 # EG: (0-32768) => 6325
76 ### TODO: (1-10,20-30,40) => 24
77 while ($result =~ /\((\d+)-(\d+)\)/) {
78 my ($lower,$upper) = ($1,$2);
79 my $new = int(rand $upper-$lower) + $lower;
81 &status("Reply.pl: SARing '$&' to '$new'.");
82 $result =~ s/$&/$new/;
84 last if ($done >= 10); # just in case.
88 # EG: (blah1|blah2|blah3|) => blah1
89 while ($result =~ /\((.*?\|.*?)\)/) {
91 my @rand = split /\|/, $str;
92 my $rand = $rand[rand @rand];
94 &status("Reply.pl: SARing '($str)' to '$rand'.");
95 $result =~ s/\(\Q$str\E\)/$rand/;
97 last if ($done >= 10); # just in case.
99 &status("Reply.pl: $done SARs done.") if ($done);
104 ### AT LAST, REPEAT PREVENTION CODE REMOVED IN FAVOUR OF GLOBAL
105 ### FLOOD REPETION AND PROTECTION. -20000124
108 &setFactInfo($lhs,"requested_by", $nuh);
109 &setFactInfo($lhs,"requested_time", time());
110 ### FIXME: old mysql doesn't support
111 ### "requested_count=requested_count+1".
112 my $count = &getFactInfo($lhs,"requested_count") || 0;
114 &setFactInfo($lhs,"requested_count", $count);
117 my $author = &getFactInfo($lhs,"created_by") || '';
119 $real++ if ($author =~ /^\Q$who\E\!/);
120 $real++ if (&IsFlag("n"));
121 $real = 0 if ($msgType =~ /public/);
123 ### fix up the reply.
124 # only remove '<reply>'
125 if (!$real and $reply =~ s/^\s*<reply>\s*//i) {
127 if ($reply =~ s/^are /$lhs are /i) {
128 &DEBUG("Reply.pl: el-cheapo 'are' fix executed.");
129 $mhs = "are"; # what's this for?
132 } elsif (!$real and $reply =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i) {
133 # only remove '<action>' and make it an action.
134 } else { # not a short reply
137 if (exists $bots{$nuh} and $rhs !~ /^\s*$/) {
138 return "$lhs $mhs $rhs";
141 ### bot->person reply.
142 # result is random if separated by '||'.
143 # rhs is full factoid with '||'.
145 $reply = &getRandom(keys %{$lang{'factoid'}});
146 $reply =~ s/##KEY/$lhs/;
147 $reply =~ s/##VALUE/$result/;
149 $reply = "$lhs $mhs $result";
152 if ($reply =~ s/^\Q$who\E is/you are/i) {
155 if ($reply =~ /^you are / or $reply =~ / you are /) {
156 return $noreply if ($addressed);
162 return $reply if ($literal);
164 # remove excessive beginning and end whitespaces.
165 $reply =~ s/^\s+|\s+$//g;
167 if ($reply =~ /^\s+$/) {
168 &DEBUG("Reply: Null factoid ($message)");
172 return $reply unless ($reply =~ /\$/);
179 my $date = scalar(localtime());
180 $date =~ s/\:\d+(\s+\w+)\s+\d+$/$1/;
181 $reply =~ s/\$date/$date/gi;
182 $date =~ s/\w+\s+\w+\s+\d+\s+//;
183 $reply =~ s/\$time/$date/gi;
186 $reply =~ s/\$nick/$who/g;
187 $reply =~ s/\$who/$who/g; # backward compat.
188 if ($reply =~ /\$(user(name)?|host)/) {
189 my ($username, $hostname) = split /\@/, $uh;
190 $reply =~ s/\$user(name)?/$username/g;
191 $reply =~ s/\$host(name)?/$hostname/g;
193 $reply =~ s/\$chan(nel)?/$talkchannel/g;
194 if ($msgType =~ /public/) {
195 $reply =~ s/\$lastspeaker/$lastWho{$talkchannel}/g;
197 $reply =~ s/\$lastspeaker/$lastWho/g;
200 if ($reply =~ /\$rand/) {
202 my $randp = int($rand*100);
203 $reply =~ s/\$randpercentage/$randp/g; # ???
205 if ($reply =~ /\$randnick/) {
206 my @nicks = keys %{$channels{$chan}{''}};
207 my $randnick = $nicks[ int($rand*$#nicks) ];
208 s/\$randnick/$randnick/;
211 ### TODO: number of digits. 'x.y'
212 if ($reply =~ /\$rand(\d+)/) {
213 # will this work as it does in C?
214 $rand = sprintf("%*f", $1, $rand);
216 $reply =~ s/\$rand/$rand/g;
219 $reply =~ s/\$factoid/$lhs/g;
220 $reply =~ s/\$ident/$ident/g;
222 if ($reply =~ /\$startTime/) {
223 my $time = scalar(localtime $^T);
224 $reply =~ s/\$startTime/$time/;
227 if ($reply =~ /\$uptime/) {
228 my $uptime = &Time2String(time() - $^T);
229 $reply =~ s/\$uptime/$uptime/;
232 if ($reply =~ /\$factoids/) {
233 my $count = &countKeys("factoids");
234 $reply =~ s/\$factoids/$factoids/;
237 if ($reply =~ /\$Fupdate/) {
238 my $x = "\002$count{'Update'}\002 ".
239 &fixPlural("modification", $count{'Update'});
240 $reply =~ s/\$Fupdate/$x/;
243 if ($reply =~ /\$Fquestion/) {
244 my $x = "\002$count{'Question'}\002 ".
245 &fixPlural("question", $count{'Question'});
246 $reply =~ s/\$Fquestion/$x/;
249 if ($reply =~ /\$Fdunno/) {
250 my $x = "\002$count{'Dunno'}\002 ".
251 &fixPlural("dunno", $count{'Dunno'});
252 $reply =~ s/\$Fdunno/$x/;
255 $reply =~ s/\$memusage/$memusage/;