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);
21 my($reply, $count, $fauthor, $result, $factoid, $search, @searches);
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 if (&IsChanConf("factoidSearch")) {
33 @searches = split(/\s+/, &getChanConf("factoidSearch"));
35 @searches = ('_default');
38 # requesting the _default one, ignore factoidSearch
39 if ($message =~ /^_default\s+/) {
40 @searches = ('_default');
41 $message =~ s/^_default\s+//;
44 # check for factoids with each prefix
45 foreach $search (@searches) {
46 if ($search eq '$chan') {
47 $factoid = "$chan $message";
48 } elsif ($search eq '_default') {
51 $factoid = "$search $message";
53 ($count, $fauthor, $result) = &sqlSelect("factoids",
54 "requested_count,created_by,factoid_value",
55 { factoid_key => $factoid }
65 return "\"$factoid\" $mhs \"$rhs\"" if ($literal);
70 # if there was a head...
71 my(@poss) = split '\|\|', $result;
73 $poss[$#poss] =~ s/\s$//;
76 $result = &getRandom(@poss);
80 $result = &SARit($result);
84 ### AT LAST, REPEAT PREVENTION CODE REMOVED IN FAVOUR OF GLOBAL
85 ### FLOOD REPETION AND PROTECTION. -20000124
88 ### FIXME: old mysql doesn't support
89 ### "requested_count=requested_count+1".
91 &sqlSet("factoids", {'factoid_key' => $factoid}, {
93 requested_time => time(),
94 requested_count => $count
97 # TODO: rename $real to something else!
99 # my $author = &getFactInfo($lhs,"created_by") || '';
100 # $real++ if ($author =~ /^\Q$who\E\!/);
101 # $real++ if (&IsFlag("n"));
102 $real = 0 if ($msgType =~ /public/);
104 ### fix up the reply.
105 # only remove '<reply>'
106 if (!$real and $reply =~ s/^\s*<reply>\s*//i) {
108 if ($reply =~ s/^are /$lhs are /i) {
109 &VERB("Reply.pl: el-cheapo 'are' fix executed.",2);
112 } elsif (!$real and $reply =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i) {
113 # only remove '<action>' and make it an action.
114 } else { # not a short reply
117 if (exists $bots{$nuh} and $rhs !~ /^\s*$/) {
118 return "$lhs $mhs $rhs";
121 ### bot->person reply.
122 # result is random if separated by '||'.
123 # rhs is full factoid with '||'.
125 $reply = &getRandom(keys %{ $lang{'factoid'} });
126 $reply =~ s/##KEY/$lhs/;
127 $reply =~ s/##VALUE/$result/;
129 $reply = "$lhs $mhs $result";
132 if ($reply =~ s/^\Q$who\E is/you are/i) {
135 if ($reply =~ /^you are / or $reply =~ / you are /) {
136 return if ($addressed);
142 # remove excessive beginning and end whitespaces.
143 $reply =~ s/^\s+|\s+$//g;
145 if ($reply =~ /^\s+$/) {
146 &DEBUG("Reply: Null factoid ($message)");
150 return $reply unless ($reply =~ /\$/);
156 # don't evaluate if it has factoid arguments.
157 if ($message =~ /^CMD:/i) {
158 &status("Reply: not doing substVars (eval dollar vars)");
160 $reply = &substVars($reply,1);
168 my ($l,$r) = (0,0); # l = left, r = right.
169 my ($s,$t) = (0,0); # s = start, t = marker.
174 foreach (split //, $string) {
191 my $substr = substr($old,$s,$i-$s+1);
192 push(@rand, substr($old,$t+1,$i-$t-1) );
194 my $rand = $rand[rand @rand];
195 # &status("SARing '$substr' to '$rand'.");
196 $string =~ s/\Q$substr\E/$rand/;
201 if ($_ eq "|" and $l+$r== 0 and $l==1) {
202 push(@rand, substr($old,$t+1,$i-$t-1) );
209 if ($old eq $string) {
210 &WARN("smart_replace: no subst made. (string => $string)");
221 while ($txt =~ /\((.*?)\)\?/) {
223 if (rand() > 0.5) { # fix.
224 &status("Factoid transform: keeping '$str'.");
225 $txt =~ s/\(\Q$str\E\)\?/$str/;
227 &status("Factoid transform: removing '$str'.");
228 $txt =~ s/\(\Q$str\E\)\?\s?//;
231 last if ($done >= 10); # just in case.
235 # EG: (0-32768) => 6325
236 ### TODO: (1-10,20-30,40) => 24
237 while ($txt =~ /\((\d+)-(\d+)\)/) {
238 my ($lower,$upper) = ($1,$2);
239 my $new = int(rand $upper-$lower) + $lower;
241 &status("SARing '$&' to '$new' (2).");
244 last if ($done >= 10); # just in case.
248 # EG: (blah1|blah2|blah3|) => blah1
249 while ($txt =~ /.*\((.*\|.*?)\).*/) {
250 $txt = &smart_replace($txt);
253 last if ($done >= 10); # just in case.
255 &status("Reply.pl: $done SARs done.") if ($done);
261 my($reply,$flag) = @_;
264 # TODO: support localtime.
265 my $date = scalar(gmtime());
266 $date =~ s/\:\d+(\s+\w+)\s+\d+$/$1/;
267 $reply =~ s/\$date/$date/gi;
268 $date =~ s/\w+\s+\w+\s+\d+\s+//;
269 $reply =~ s/\$time/$date/gi;
273 $reply =~ s/\$nick/$who/g;
274 $reply =~ s/\$who/$who/g; # backward compat.
277 if ($reply =~ /\$(user(name)?|host)/) {
278 my ($username, $hostname) = split /\@/, $uh;
279 $reply =~ s/\$user(name)?/$username/g;
280 $reply =~ s/\$host(name)?/$hostname/g;
282 $reply =~ s/\$chan(nel)?/$talkchannel/g;
283 if ($msgType =~ /public/) {
284 $reply =~ s/\$lastspeaker/$lastWho{$talkchannel}/g;
286 $reply =~ s/\$lastspeaker/$lastWho/g;
289 if ($reply =~ /\$rand/) {
293 if ($reply =~ /\$randnick/) {
294 my @nicks = keys %{ $channels{$chan}{''} };
295 my $randnick = $nicks[ int($rand*$#nicks) ];
296 $reply =~ s/\$randnick/$randnick/g;
300 if ($reply =~ /\$rand(\d+)(\.(\d+))?/) {
304 #&DEBUG("dot => $dot, max => $max, rand=>$rand");
305 $rand = sprintf("%.*f", $dot, $rand*$max);
307 &DEBUG("swapping $orig to $rand");
308 $reply =~ s/\Q$orig\E/$rand/eg;
310 $reply =~ s/\$rand/$rand/g;
314 $reply =~ s/\$ident/$ident/g;
316 if ($reply =~ /\$startTime/) {
317 my $time = scalar(gmtime $^T);
318 $reply =~ s/\$startTime/$time/;
321 if ($reply =~ /\$uptime/) {
322 my $uptime = &Time2String(time() - $^T);
323 $reply =~ s/\$uptime/$uptime/;
326 if ($reply =~ /\$factoids/) {
327 my $factoids = &countKeys("factoids");
328 $reply =~ s/\$factoids/$factoids/;
331 if ($reply =~ /\$Fupdate/) {
332 my $x = "\002$count{'Update'}\002 ".
333 &fixPlural("modification", $count{'Update'});
334 $reply =~ s/\$Fupdate/$x/;
337 if ($reply =~ /\$Fquestion/) {
338 my $x = "\002$count{'Question'}\002 ".
339 &fixPlural("question", $count{'Question'});
340 $reply =~ s/\$Fquestion/$x/;
343 if ($reply =~ /\$Fdunno/) {
344 my $x = "\002$count{'Dunno'}\002 ".
345 &fixPlural("dunno", $count{'Dunno'});
346 $reply =~ s/\$Fdunno/$x/;
349 $reply =~ s/\$memusage/$memusage/;