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, $factoid);
34 if (!$literal and &IsChanConf("channelFactoids")) {
35 # check for factoids specific to this channel
36 ($count, $fauthor, $result) = &sqlSelect("factoids",
37 "requested_count,created_by,factoid_value",
38 { factoid_key => "$chan $message" }
43 # got channel specific reply above
44 $factoid="$chan $message"
46 # no channel specific factoid was requested / found
47 ($count, $fauthor, $result) = &sqlSelect("factoids",
48 "requested_count,created_by,factoid_value",
49 { factoid_key => $message }
51 $factoid=$message if ($result);
59 return "\"$factoid\" $mhs \"$rhs\"" if ($literal);
64 # if there was a head...
65 my(@poss) = split '\|\|', $result;
67 $poss[$#poss] =~ s/\s$//;
70 $result = &getRandom(@poss);
74 $result = &SARit($result);
78 ### AT LAST, REPEAT PREVENTION CODE REMOVED IN FAVOUR OF GLOBAL
79 ### FLOOD REPETION AND PROTECTION. -20000124
82 ### FIXME: old mysql doesn't support
83 ### "requested_count=requested_count+1".
85 &sqlSet("factoids", {'factoid_key' => $factoid}, {
87 requested_time => time(),
88 requested_count => $count
91 # TODO: rename $real to something else!
93 # my $author = &getFactInfo($lhs,"created_by") || '';
94 # $real++ if ($author =~ /^\Q$who\E\!/);
95 # $real++ if (&IsFlag("n"));
96 $real = 0 if ($msgType =~ /public/);
99 # only remove '<reply>'
100 if (!$real and $reply =~ s/^\s*<reply>\s*//i) {
102 if ($reply =~ s/^are /$lhs are /i) {
103 &VERB("Reply.pl: el-cheapo 'are' fix executed.",2);
106 } elsif (!$real and $reply =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i) {
107 # only remove '<action>' and make it an action.
108 } else { # not a short reply
111 if (exists $bots{$nuh} and $rhs !~ /^\s*$/) {
112 return "$lhs $mhs $rhs";
115 ### bot->person reply.
116 # result is random if separated by '||'.
117 # rhs is full factoid with '||'.
119 $reply = &getRandom(keys %{ $lang{'factoid'} });
120 $reply =~ s/##KEY/$lhs/;
121 $reply =~ s/##VALUE/$result/;
123 $reply = "$lhs $mhs $result";
126 if ($reply =~ s/^\Q$who\E is/you are/i) {
129 if ($reply =~ /^you are / or $reply =~ / you are /) {
130 return if ($addressed);
136 return $reply if ($literal);
138 # remove excessive beginning and end whitespaces.
139 $reply =~ s/^\s+|\s+$//g;
141 if ($reply =~ /^\s+$/) {
142 &DEBUG("Reply: Null factoid ($message)");
146 return $reply unless ($reply =~ /\$/);
152 # don't evaluate if it has factoid arguments.
153 if ($message =~ /^CMD:/i) {
154 &status("Reply: not doing substVars (eval dollar vars)");
156 $reply = &substVars($reply,1);
164 my ($l,$r) = (0,0); # l = left, r = right.
165 my ($s,$t) = (0,0); # s = start, t = marker.
170 foreach (split //, $string) {
187 my $substr = substr($old,$s,$i-$s+1);
188 push(@rand, substr($old,$t+1,$i-$t-1) );
190 my $rand = $rand[rand @rand];
191 # &status("SARing '$substr' to '$rand'.");
192 $string =~ s/\Q$substr\E/$rand/;
197 if ($_ eq "|" and $l+$r== 0 and $l==1) {
198 push(@rand, substr($old,$t+1,$i-$t-1) );
205 if ($old eq $string) {
206 &WARN("smart_replace: no subst made. (string => $string)");
217 while ($txt =~ /\((.*?)\)\?/) {
219 if (rand() > 0.5) { # fix.
220 &status("Factoid transform: keeping '$str'.");
221 $txt =~ s/\(\Q$str\E\)\?/$str/;
223 &status("Factoid transform: removing '$str'.");
224 $txt =~ s/\(\Q$str\E\)\?\s?//;
227 last if ($done >= 10); # just in case.
231 # EG: (0-32768) => 6325
232 ### TODO: (1-10,20-30,40) => 24
233 while ($txt =~ /\((\d+)-(\d+)\)/) {
234 my ($lower,$upper) = ($1,$2);
235 my $new = int(rand $upper-$lower) + $lower;
237 &status("SARing '$&' to '$new' (2).");
240 last if ($done >= 10); # just in case.
244 # EG: (blah1|blah2|blah3|) => blah1
245 while ($txt =~ /.*\((.*\|.*?)\).*/) {
246 $txt = &smart_replace($txt);
249 last if ($done >= 10); # just in case.
251 &status("Reply.pl: $done SARs done.") if ($done);
257 my($reply,$flag) = @_;
260 # TODO: support localtime.
261 my $date = scalar(gmtime());
262 $date =~ s/\:\d+(\s+\w+)\s+\d+$/$1/;
263 $reply =~ s/\$date/$date/gi;
264 $date =~ s/\w+\s+\w+\s+\d+\s+//;
265 $reply =~ s/\$time/$date/gi;
269 $reply =~ s/\$nick/$who/g;
270 $reply =~ s/\$who/$who/g; # backward compat.
273 if ($reply =~ /\$(user(name)?|host)/) {
274 my ($username, $hostname) = split /\@/, $uh;
275 $reply =~ s/\$user(name)?/$username/g;
276 $reply =~ s/\$host(name)?/$hostname/g;
278 $reply =~ s/\$chan(nel)?/$talkchannel/g;
279 if ($msgType =~ /public/) {
280 $reply =~ s/\$lastspeaker/$lastWho{$talkchannel}/g;
282 $reply =~ s/\$lastspeaker/$lastWho/g;
285 if ($reply =~ /\$rand/) {
289 if ($reply =~ /\$randnick/) {
290 my @nicks = keys %{ $channels{$chan}{''} };
291 my $randnick = $nicks[ int($rand*$#nicks) ];
292 $reply =~ s/\$randnick/$randnick/g;
296 if ($reply =~ /\$rand(\d+)(\.(\d+))?/) {
300 #&DEBUG("dot => $dot, max => $max, rand=>$rand");
301 $rand = sprintf("%.*f", $dot, $rand*$max);
303 &DEBUG("swapping $orig to $rand");
304 $reply =~ s/\Q$orig\E/$rand/eg;
306 $reply =~ s/\$rand/$rand/g;
310 $reply =~ s/\$ident/$ident/g;
312 if ($reply =~ /\$startTime/) {
313 my $time = scalar(gmtime $^T);
314 $reply =~ s/\$startTime/$time/;
317 if ($reply =~ /\$uptime/) {
318 my $uptime = &Time2String(time() - $^T);
319 $reply =~ s/\$uptime/$uptime/;
322 if ($reply =~ /\$factoids/) {
323 my $factoids = &countKeys("factoids");
324 $reply =~ s/\$factoids/$factoids/;
327 if ($reply =~ /\$Fupdate/) {
328 my $x = "\002$count{'Update'}\002 ".
329 &fixPlural("modification", $count{'Update'});
330 $reply =~ s/\$Fupdate/$x/;
333 if ($reply =~ /\$Fquestion/) {
334 my $x = "\002$count{'Question'}\002 ".
335 &fixPlural("question", $count{'Question'});
336 $reply =~ s/\$Fquestion/$x/;
339 if ($reply =~ /\$Fdunno/) {
340 my $x = "\002$count{'Dunno'}\002 ".
341 &fixPlural("dunno", $count{'Dunno'});
342 $reply =~ s/\$Fdunno/$x/;
345 $reply =~ s/\$memusage/$memusage/;