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 # check for factoids with each prefix
39 foreach $search (@searches) {
40 if ($search eq '_default') {
43 $factoid = "$search $message";
45 ($count, $fauthor, $result) = &sqlSelect("factoids",
46 "requested_count,created_by,factoid_value",
47 { factoid_key => $factoid }
57 return "\"$factoid\" $mhs \"$rhs\"" if ($literal);
62 # if there was a head...
63 my(@poss) = split '\|\|', $result;
65 $poss[$#poss] =~ s/\s$//;
68 $result = &getRandom(@poss);
72 $result = &SARit($result);
76 ### AT LAST, REPEAT PREVENTION CODE REMOVED IN FAVOUR OF GLOBAL
77 ### FLOOD REPETION AND PROTECTION. -20000124
80 ### FIXME: old mysql doesn't support
81 ### "requested_count=requested_count+1".
83 &sqlSet("factoids", {'factoid_key' => $factoid}, {
85 requested_time => time(),
86 requested_count => $count
89 # TODO: rename $real to something else!
91 # my $author = &getFactInfo($lhs,"created_by") || '';
92 # $real++ if ($author =~ /^\Q$who\E\!/);
93 # $real++ if (&IsFlag("n"));
94 $real = 0 if ($msgType =~ /public/);
97 # only remove '<reply>'
98 if (!$real and $reply =~ s/^\s*<reply>\s*//i) {
100 if ($reply =~ s/^are /$lhs are /i) {
101 &VERB("Reply.pl: el-cheapo 'are' fix executed.",2);
104 } elsif (!$real and $reply =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i) {
105 # only remove '<action>' and make it an action.
106 } else { # not a short reply
109 if (exists $bots{$nuh} and $rhs !~ /^\s*$/) {
110 return "$lhs $mhs $rhs";
113 ### bot->person reply.
114 # result is random if separated by '||'.
115 # rhs is full factoid with '||'.
117 $reply = &getRandom(keys %{ $lang{'factoid'} });
118 $reply =~ s/##KEY/$lhs/;
119 $reply =~ s/##VALUE/$result/;
121 $reply = "$lhs $mhs $result";
124 if ($reply =~ s/^\Q$who\E is/you are/i) {
127 if ($reply =~ /^you are / or $reply =~ / you are /) {
128 return if ($addressed);
134 # remove excessive beginning and end whitespaces.
135 $reply =~ s/^\s+|\s+$//g;
137 if ($reply =~ /^\s+$/) {
138 &DEBUG("Reply: Null factoid ($message)");
142 return $reply unless ($reply =~ /\$/);
148 # don't evaluate if it has factoid arguments.
149 if ($message =~ /^CMD:/i) {
150 &status("Reply: not doing substVars (eval dollar vars)");
152 $reply = &substVars($reply,1);
160 my ($l,$r) = (0,0); # l = left, r = right.
161 my ($s,$t) = (0,0); # s = start, t = marker.
166 foreach (split //, $string) {
183 my $substr = substr($old,$s,$i-$s+1);
184 push(@rand, substr($old,$t+1,$i-$t-1) );
186 my $rand = $rand[rand @rand];
187 # &status("SARing '$substr' to '$rand'.");
188 $string =~ s/\Q$substr\E/$rand/;
193 if ($_ eq "|" and $l+$r== 0 and $l==1) {
194 push(@rand, substr($old,$t+1,$i-$t-1) );
201 if ($old eq $string) {
202 &WARN("smart_replace: no subst made. (string => $string)");
213 while ($txt =~ /\((.*?)\)\?/) {
215 if (rand() > 0.5) { # fix.
216 &status("Factoid transform: keeping '$str'.");
217 $txt =~ s/\(\Q$str\E\)\?/$str/;
219 &status("Factoid transform: removing '$str'.");
220 $txt =~ s/\(\Q$str\E\)\?\s?//;
223 last if ($done >= 10); # just in case.
227 # EG: (0-32768) => 6325
228 ### TODO: (1-10,20-30,40) => 24
229 while ($txt =~ /\((\d+)-(\d+)\)/) {
230 my ($lower,$upper) = ($1,$2);
231 my $new = int(rand $upper-$lower) + $lower;
233 &status("SARing '$&' to '$new' (2).");
236 last if ($done >= 10); # just in case.
240 # EG: (blah1|blah2|blah3|) => blah1
241 while ($txt =~ /.*\((.*\|.*?)\).*/) {
242 $txt = &smart_replace($txt);
245 last if ($done >= 10); # just in case.
247 &status("Reply.pl: $done SARs done.") if ($done);
253 my($reply,$flag) = @_;
256 # TODO: support localtime.
257 my $date = scalar(gmtime());
258 $date =~ s/\:\d+(\s+\w+)\s+\d+$/$1/;
259 $reply =~ s/\$date/$date/gi;
260 $date =~ s/\w+\s+\w+\s+\d+\s+//;
261 $reply =~ s/\$time/$date/gi;
265 $reply =~ s/\$nick/$who/g;
266 $reply =~ s/\$who/$who/g; # backward compat.
269 if ($reply =~ /\$(user(name)?|host)/) {
270 my ($username, $hostname) = split /\@/, $uh;
271 $reply =~ s/\$user(name)?/$username/g;
272 $reply =~ s/\$host(name)?/$hostname/g;
274 $reply =~ s/\$chan(nel)?/$talkchannel/g;
275 if ($msgType =~ /public/) {
276 $reply =~ s/\$lastspeaker/$lastWho{$talkchannel}/g;
278 $reply =~ s/\$lastspeaker/$lastWho/g;
281 if ($reply =~ /\$rand/) {
285 if ($reply =~ /\$randnick/) {
286 my @nicks = keys %{ $channels{$chan}{''} };
287 my $randnick = $nicks[ int($rand*$#nicks) ];
288 $reply =~ s/\$randnick/$randnick/g;
292 if ($reply =~ /\$rand(\d+)(\.(\d+))?/) {
296 #&DEBUG("dot => $dot, max => $max, rand=>$rand");
297 $rand = sprintf("%.*f", $dot, $rand*$max);
299 &DEBUG("swapping $orig to $rand");
300 $reply =~ s/\Q$orig\E/$rand/eg;
302 $reply =~ s/\$rand/$rand/g;
306 $reply =~ s/\$ident/$ident/g;
308 if ($reply =~ /\$startTime/) {
309 my $time = scalar(gmtime $^T);
310 $reply =~ s/\$startTime/$time/;
313 if ($reply =~ /\$uptime/) {
314 my $uptime = &Time2String(time() - $^T);
315 $reply =~ s/\$uptime/$uptime/;
318 if ($reply =~ /\$factoids/) {
319 my $factoids = &countKeys("factoids");
320 $reply =~ s/\$factoids/$factoids/;
323 if ($reply =~ /\$Fupdate/) {
324 my $x = "\002$count{'Update'}\002 ".
325 &fixPlural("modification", $count{'Update'});
326 $reply =~ s/\$Fupdate/$x/;
329 if ($reply =~ /\$Fquestion/) {
330 my $x = "\002$count{'Question'}\002 ".
331 &fixPlural("question", $count{'Question'});
332 $reply =~ s/\$Fquestion/$x/;
335 if ($reply =~ /\$Fdunno/) {
336 my $x = "\002$count{'Dunno'}\002 ".
337 &fixPlural("dunno", $count{'Dunno'});
338 $reply =~ s/\$Fdunno/$x/;
341 $reply =~ s/\$memusage/$memusage/;