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 if ($literal and $message =~ /^_default\s+/) {
39 @searches = ('_default');
40 $message =~ s/^_default\s+//;
43 # check for factoids with each prefix
44 foreach $search (@searches) {
45 if ($search eq '_default') {
48 $factoid = "$search $message";
50 ($count, $fauthor, $result) = &sqlSelect("factoids",
51 "requested_count,created_by,factoid_value",
52 { factoid_key => $factoid }
62 return "\"$factoid\" $mhs \"$rhs\"" if ($literal);
67 # if there was a head...
68 my(@poss) = split '\|\|', $result;
70 $poss[$#poss] =~ s/\s$//;
73 $result = &getRandom(@poss);
77 $result = &SARit($result);
81 ### AT LAST, REPEAT PREVENTION CODE REMOVED IN FAVOUR OF GLOBAL
82 ### FLOOD REPETION AND PROTECTION. -20000124
85 ### FIXME: old mysql doesn't support
86 ### "requested_count=requested_count+1".
88 &sqlSet("factoids", {'factoid_key' => $factoid}, {
90 requested_time => time(),
91 requested_count => $count
94 # TODO: rename $real to something else!
96 # my $author = &getFactInfo($lhs,"created_by") || '';
97 # $real++ if ($author =~ /^\Q$who\E\!/);
98 # $real++ if (&IsFlag("n"));
99 $real = 0 if ($msgType =~ /public/);
101 ### fix up the reply.
102 # only remove '<reply>'
103 if (!$real and $reply =~ s/^\s*<reply>\s*//i) {
105 if ($reply =~ s/^are /$lhs are /i) {
106 &VERB("Reply.pl: el-cheapo 'are' fix executed.",2);
109 } elsif (!$real and $reply =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i) {
110 # only remove '<action>' and make it an action.
111 } else { # not a short reply
114 if (exists $bots{$nuh} and $rhs !~ /^\s*$/) {
115 return "$lhs $mhs $rhs";
118 ### bot->person reply.
119 # result is random if separated by '||'.
120 # rhs is full factoid with '||'.
122 $reply = &getRandom(keys %{ $lang{'factoid'} });
123 $reply =~ s/##KEY/$lhs/;
124 $reply =~ s/##VALUE/$result/;
126 $reply = "$lhs $mhs $result";
129 if ($reply =~ s/^\Q$who\E is/you are/i) {
132 if ($reply =~ /^you are / or $reply =~ / you are /) {
133 return if ($addressed);
139 # remove excessive beginning and end whitespaces.
140 $reply =~ s/^\s+|\s+$//g;
142 if ($reply =~ /^\s+$/) {
143 &DEBUG("Reply: Null factoid ($message)");
147 return $reply unless ($reply =~ /\$/);
153 # don't evaluate if it has factoid arguments.
154 if ($message =~ /^CMD:/i) {
155 &status("Reply: not doing substVars (eval dollar vars)");
157 $reply = &substVars($reply,1);
165 my ($l,$r) = (0,0); # l = left, r = right.
166 my ($s,$t) = (0,0); # s = start, t = marker.
171 foreach (split //, $string) {
188 my $substr = substr($old,$s,$i-$s+1);
189 push(@rand, substr($old,$t+1,$i-$t-1) );
191 my $rand = $rand[rand @rand];
192 # &status("SARing '$substr' to '$rand'.");
193 $string =~ s/\Q$substr\E/$rand/;
198 if ($_ eq "|" and $l+$r== 0 and $l==1) {
199 push(@rand, substr($old,$t+1,$i-$t-1) );
206 if ($old eq $string) {
207 &WARN("smart_replace: no subst made. (string => $string)");
218 while ($txt =~ /\((.*?)\)\?/) {
220 if (rand() > 0.5) { # fix.
221 &status("Factoid transform: keeping '$str'.");
222 $txt =~ s/\(\Q$str\E\)\?/$str/;
224 &status("Factoid transform: removing '$str'.");
225 $txt =~ s/\(\Q$str\E\)\?\s?//;
228 last if ($done >= 10); # just in case.
232 # EG: (0-32768) => 6325
233 ### TODO: (1-10,20-30,40) => 24
234 while ($txt =~ /\((\d+)-(\d+)\)/) {
235 my ($lower,$upper) = ($1,$2);
236 my $new = int(rand $upper-$lower) + $lower;
238 &status("SARing '$&' to '$new' (2).");
241 last if ($done >= 10); # just in case.
245 # EG: (blah1|blah2|blah3|) => blah1
246 while ($txt =~ /.*\((.*\|.*?)\).*/) {
247 $txt = &smart_replace($txt);
250 last if ($done >= 10); # just in case.
252 &status("Reply.pl: $done SARs done.") if ($done);
258 my($reply,$flag) = @_;
261 # TODO: support localtime.
262 my $date = scalar(gmtime());
263 $date =~ s/\:\d+(\s+\w+)\s+\d+$/$1/;
264 $reply =~ s/\$date/$date/gi;
265 $date =~ s/\w+\s+\w+\s+\d+\s+//;
266 $reply =~ s/\$time/$date/gi;
270 $reply =~ s/\$nick/$who/g;
271 $reply =~ s/\$who/$who/g; # backward compat.
274 if ($reply =~ /\$(user(name)?|host)/) {
275 my ($username, $hostname) = split /\@/, $uh;
276 $reply =~ s/\$user(name)?/$username/g;
277 $reply =~ s/\$host(name)?/$hostname/g;
279 $reply =~ s/\$chan(nel)?/$talkchannel/g;
280 if ($msgType =~ /public/) {
281 $reply =~ s/\$lastspeaker/$lastWho{$talkchannel}/g;
283 $reply =~ s/\$lastspeaker/$lastWho/g;
286 if ($reply =~ /\$rand/) {
290 if ($reply =~ /\$randnick/) {
291 my @nicks = keys %{ $channels{$chan}{''} };
292 my $randnick = $nicks[ int($rand*$#nicks) ];
293 $reply =~ s/\$randnick/$randnick/g;
297 if ($reply =~ /\$rand(\d+)(\.(\d+))?/) {
301 #&DEBUG("dot => $dot, max => $max, rand=>$rand");
302 $rand = sprintf("%.*f", $dot, $rand*$max);
304 &DEBUG("swapping $orig to $rand");
305 $reply =~ s/\Q$orig\E/$rand/eg;
307 $reply =~ s/\$rand/$rand/g;
311 $reply =~ s/\$ident/$ident/g;
313 if ($reply =~ /\$startTime/) {
314 my $time = scalar(gmtime $^T);
315 $reply =~ s/\$startTime/$time/;
318 if ($reply =~ /\$uptime/) {
319 my $uptime = &Time2String(time() - $^T);
320 $reply =~ s/\$uptime/$uptime/;
323 if ($reply =~ /\$factoids/) {
324 my $factoids = &countKeys("factoids");
325 $reply =~ s/\$factoids/$factoids/;
328 if ($reply =~ /\$Fupdate/) {
329 my $x = "\002$count{'Update'}\002 ".
330 &fixPlural("modification", $count{'Update'});
331 $reply =~ s/\$Fupdate/$x/;
334 if ($reply =~ /\$Fquestion/) {
335 my $x = "\002$count{'Question'}\002 ".
336 &fixPlural("question", $count{'Question'});
337 $reply =~ s/\$Fquestion/$x/;
340 if ($reply =~ /\$Fdunno/) {
341 my $x = "\002$count{'Dunno'}\002 ".
342 &fixPlural("dunno", $count{'Dunno'});
343 $reply =~ s/\$Fdunno/$x/;
346 $reply =~ s/\$memusage/$memusage/;