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/;
31 @searches = split(/\s+/, &getChanConfDefault('factoidSearch', '_default'));
33 # requesting the _default one, ignore factoidSearch
34 if ($message =~ /^_default\s+/) {
35 @searches = ('_default');
36 $message =~ s/^_default\s+//;
39 # check for factoids with each prefix
40 foreach $search (@searches) {
41 if ($search eq '$chan') {
42 $factoid = "$chan $message";
43 } elsif ($search eq '_default') {
46 $factoid = "$search $message";
48 ($count, $fauthor, $result) = &sqlSelect("factoids",
49 "requested_count,created_by,factoid_value",
50 { factoid_key => $factoid }
60 return "\"$factoid\" $mhs \"$rhs\"" if ($literal);
65 # if there was a head...
66 my(@poss) = split '\|\|', $result;
68 $poss[$#poss] =~ s/\s$//;
71 $result = &getRandom(@poss);
75 $result = &SARit($result);
79 ### AT LAST, REPEAT PREVENTION CODE REMOVED IN FAVOUR OF GLOBAL
80 ### FLOOD REPETION AND PROTECTION. -20000124
83 ### FIXME: old mysql doesn't support
84 ### "requested_count=requested_count+1".
86 &sqlSet("factoids", {'factoid_key' => $factoid}, {
88 requested_time => time(),
89 requested_count => $count
92 # TODO: rename $real to something else!
94 # my $author = &getFactInfo($lhs,"created_by") || '';
95 # $real++ if ($author =~ /^\Q$who\E\!/);
96 # $real++ if (&IsFlag("n"));
97 $real = 0 if ($msgType =~ /public/);
100 # only remove '<reply>'
101 if (!$real and $reply =~ s/^\s*<reply>\s*//i) {
103 if ($reply =~ s/^are /$lhs are /i) {
104 &VERB("Reply.pl: el-cheapo 'are' fix executed.",2);
107 } elsif (!$real and $reply =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i) {
108 # only remove '<action>' and make it an action.
109 } else { # not a short reply
112 if (exists $bots{$nuh} and $rhs !~ /^\s*$/) {
113 return "$lhs $mhs $rhs";
116 ### bot->person reply.
117 # result is random if separated by '||'.
118 # rhs is full factoid with '||'.
120 $reply = &getRandom(keys %{ $lang{'factoid'} });
121 $reply =~ s/##KEY/$lhs/;
122 $reply =~ s/##VALUE/$result/;
124 $reply = "$lhs $mhs $result";
127 if ($reply =~ s/^\Q$who\E is/you are/i) {
130 if ($reply =~ /^you are / or $reply =~ / you are /) {
131 return if ($addressed);
137 # remove excessive beginning and end whitespaces.
138 $reply =~ s/^\s+|\s+$//g;
140 if ($reply =~ /^\s+$/) {
141 &DEBUG("Reply: Null factoid ($message)");
145 return $reply unless ($reply =~ /\$/);
151 # don't evaluate if it has factoid arguments.
152 if ($message =~ /^cmd:/i) {
153 &status("Reply: not doing substVars (eval dollar vars)");
155 $reply = &substVars($reply,1);
163 my ($l,$r) = (0,0); # l = left, r = right.
164 my ($s,$t) = (0,0); # s = start, t = marker.
169 foreach (split //, $string) {
186 my $substr = substr($old,$s,$i-$s+1);
187 push(@rand, substr($old,$t+1,$i-$t-1) );
189 my $rand = $rand[rand @rand];
190 # &status("SARing '$substr' to '$rand'.");
191 $string =~ s/\Q$substr\E/$rand/;
196 if ($_ eq "|" and $l+$r== 0 and $l==1) {
197 push(@rand, substr($old,$t+1,$i-$t-1) );
204 if ($old eq $string) {
205 &WARN("smart_replace: no subst made. (string => $string)");
216 while ($txt =~ /\((.*?)\)\?/) {
218 if (rand() > 0.5) { # fix.
219 &status("Factoid transform: keeping '$str'.");
220 $txt =~ s/\(\Q$str\E\)\?/$str/;
222 &status("Factoid transform: removing '$str'.");
223 $txt =~ s/\(\Q$str\E\)\?\s?//;
226 last if ($done >= 10); # just in case.
230 # EG: (0-32768) => 6325
231 ### TODO: (1-10,20-30,40) => 24
232 while ($txt =~ /\((\d+)-(\d+)\)/) {
233 my ($lower,$upper) = ($1,$2);
234 my $new = int(rand $upper-$lower) + $lower;
236 &status("SARing '$&' to '$new' (2).");
239 last if ($done >= 10); # just in case.
243 # EG: (blah1|blah2|blah3|) => blah1
244 while ($txt =~ /.*\((.*\|.*?)\).*/) {
245 $txt = &smart_replace($txt);
248 last if ($done >= 10); # just in case.
250 &status("Reply.pl: $done SARs done.") if ($done);
254 while ($txt =~ /<URL>(.*)<\/URL>/){
255 &status("we have to norm this <URL></URL> stuff, SARing");
257 if ($foobar =~ m/(http:\/\/[^?]+)\?(.*)/){
258 my ($pig1,$pig2) = ($1,$2);
259 &status("SARing using URLencode");
260 $pig2=~s/([^\w])/sprintf("%%%02x",ord($1))/gie;
261 $foobar=$pig1."?".$pig2;
263 $txt =~ s/<URL>(.*)<\/URL>/$foobar/;
269 my($reply,$flag) = @_;
272 # TODO: support localtime.
273 my $date = scalar(gmtime());
274 $date =~ s/\:\d+(\s+\w+)\s+\d+$/$1/;
275 $reply =~ s/\$date/$date/gi;
276 $date =~ s/\w+\s+\w+\s+\d+\s+//;
277 $reply =~ s/\$time/$date/gi;
281 $reply =~ s/\$nick/$who/g;
282 $reply =~ s/\$who/$who/g; # backward compat.
285 if ($reply =~ /\$(user(name)?|host)/) {
286 my ($username, $hostname) = split /\@/, $uh;
287 $reply =~ s/\$user(name)?/$username/g;
288 $reply =~ s/\$host(name)?/$hostname/g;
290 $reply =~ s/\$chan(nel)?/$talkchannel/g;
291 if ($msgType =~ /public/) {
292 $reply =~ s/\$lastspeaker/$lastWho{$talkchannel}/g;
294 $reply =~ s/\$lastspeaker/$lastWho/g;
297 if ($reply =~ /\$rand/) {
301 if ($reply =~ /\$randnick/) {
302 my @nicks = keys %{ $channels{$chan}{''} };
303 my $randnick = $nicks[ int($rand*$#nicks) ];
304 $reply =~ s/\$randnick/$randnick/g;
308 if ($reply =~ /\$rand(\d+)(\.(\d+))?/) {
312 #&DEBUG("dot => $dot, max => $max, rand=>$rand");
313 $rand = sprintf("%.*f", $dot, $rand*$max);
315 &DEBUG("swapping $orig to $rand");
316 $reply =~ s/\Q$orig\E/$rand/eg;
318 $reply =~ s/\$rand/$rand/g;
322 $reply =~ s/\$ident/$ident/g;
324 if ($reply =~ /\$startTime/) {
325 my $time = scalar(gmtime $^T);
326 $reply =~ s/\$startTime/$time/;
329 if ($reply =~ /\$uptime/) {
330 my $uptime = &Time2String(time() - $^T);
331 $reply =~ s/\$uptime/$uptime/;
334 if ($reply =~ /\$factoids/) {
335 my $factoids = &countKeys("factoids");
336 $reply =~ s/\$factoids/$factoids/;
339 if ($reply =~ /\$Fupdate/) {
340 my $x = "\002$count{'Update'}\002 ".
341 &fixPlural("modification", $count{'Update'});
342 $reply =~ s/\$Fupdate/$x/;
345 if ($reply =~ /\$Fquestion/) {
346 my $x = "\002$count{'Question'}\002 ".
347 &fixPlural("question", $count{'Question'});
348 $reply =~ s/\$Fquestion/$x/;
351 if ($reply =~ /\$Fdunno/) {
352 my $x = "\002$count{'Dunno'}\002 ".
353 &fixPlural("dunno", $count{'Dunno'});
354 $reply =~ s/\$Fdunno/$x/;
357 $reply =~ s/\$memusage/$memusage/;