2 ### Reply.pl: Kevin Lenzo (c) 1997
6 # x is y === $lhs $mhs $rhs
10 # rhs - factoid message.
14 use POSIX qw(strftime);
16 use vars qw($msgType $uh $lastWho $ident);
17 use vars qw(%lang %lastWho);
22 my($reply, $count, $fauthor, $result, $factoid, $search, @searches);
23 $orig{message} = $message;
25 if (!defined $message or $message =~ /^\s*$/) {
26 &WARN("getR: message == NULL.");
30 $message =~ tr/A-Z/a-z/;
32 @searches = split(/\s+/, &getChanConfDefault('factoidSearch', '_default', $chan));
33 &::DEBUG("factoidSearch: $chan is: " . join(':', @searches));
34 # requesting the _default one, ignore factoidSearch
35 if ($message =~ /^_default\s+/) {
36 @searches = ('_default');
37 $message =~ s/^_default\s+//;
40 # check for factoids with each prefix
41 foreach $search (@searches) {
42 if ($search eq '$chan') {
43 $factoid = "$chan $message";
44 } elsif ($search eq '_default') {
47 $factoid = "$search $message";
49 ($count, $fauthor, $result) = &sqlSelect("factoids",
50 "requested_count,created_by,factoid_value",
51 { factoid_key => $factoid }
61 return "\"$factoid\" $mhs \"$rhs\"" if ($literal);
66 # if there was a head...
67 my(@poss) = split '\|\|', $result;
69 $poss[$#poss] =~ s/\s$//;
72 $result = &getRandom(@poss);
76 $result = &SARit($result);
80 ### AT LAST, REPEAT PREVENTION CODE REMOVED IN FAVOUR OF GLOBAL
81 ### FLOOD REPETION AND PROTECTION. -20000124
84 ### FIXME: old mysql/sqlite doesn't support
85 ### "requested_count=requested_count+1".
87 &sqlSet("factoids", {'factoid_key' => $factoid}, {
89 requested_time => time(),
90 requested_count => $count
93 # TODO: rename $real to something else!
95 # my $author = &getFactInfo($lhs,"created_by") || '';
96 # $real++ if ($author =~ /^\Q$who\E\!/);
97 # $real++ if (&IsFlag("n"));
98 $real = 0 if ($msgType =~ /public/);
100 ### fix up the reply.
101 # only remove '<reply>'
102 if (!$real and $reply =~ s/^\s*<reply>\s*//i) {
104 if ($reply =~ s/^are /$lhs are /i) {
105 &VERB("Reply.pl: el-cheapo 'are' fix executed.",2);
108 } elsif (!$real and $reply =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i) {
109 # only remove '<action>' and make it an action.
110 } else { # not a short reply
113 if (exists $bots{$nuh} and $rhs !~ /^\s*$/) {
114 return "$lhs $mhs $rhs";
117 ### bot->person reply.
118 # result is random if separated by '||'.
119 # rhs is full factoid with '||'.
121 $reply = &getRandom(keys %{ $lang{'factoid'} });
122 $reply =~ s/##KEY/$lhs/;
123 $reply =~ s/##VALUE/$result/;
125 $reply = "$lhs $mhs $result";
128 if ($reply =~ s/^\Q$who\E is/you are/i) {
131 if ($reply =~ /^you are / or $reply =~ / you are /) {
132 return if ($addressed);
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);
255 while ($txt =~ /<URL>(.*)<\/URL>/){
256 &status("we have to norm this <URL></URL> stuff, SARing");
258 if ($foobar =~ m/(http:\/\/[^?]+)\?(.*)/){
259 my ($pig1,$pig2) = ($1,$2);
260 &status("SARing using URLencode");
261 $pig2=~s/([^\w])/sprintf("%%%02x",ord($1))/gie;
262 $foobar=$pig1."?".$pig2;
264 $txt =~ s/<URL>(.*)<\/URL>/$foobar/;
270 my($reply,$flag) = @_;
272 # $date, $time, $day.
273 # TODO: support localtime.
274 my $date = strftime("%Y.%m.%d", gmtime());
275 $reply =~ s/\$date/$date/gi;
276 my $time = strftime("%k:%M:%S", gmtime());
277 $reply =~ s/\$time/$time/gi;
278 my $day = strftime("%A", gmtime());
279 $reply =~ s/\$day/$day/gi;
281 # support $ident when I have multiple nicks
282 my $mynick = $conn->nick() if $conn;
286 $reply =~ s/\$nick/$who/g;
287 $reply =~ s/\$who/$who/g; # backward compat.
290 if ($reply =~ /\$(user(name)?|host)/) {
291 my ($username, $hostname) = split /\@/, $uh;
292 $reply =~ s/\$user(name)?/$username/g;
293 $reply =~ s/\$host(name)?/$hostname/g;
295 $reply =~ s/\$chan(nel)?/$talkchannel/g;
296 if ($msgType =~ /public/) {
297 $reply =~ s/\$lastspeaker/$lastWho{$talkchannel}/g;
299 $reply =~ s/\$lastspeaker/$lastWho/g;
302 if ($reply =~ /\$rand/) {
306 if ($reply =~ /\$randnick/) {
307 my @nicks = keys %{ $channels{$chan}{''} };
308 my $randnick = $nicks[ int($rand*$#nicks) ];
309 $reply =~ s/\$randnick/$randnick/g;
313 if ($reply =~ /\$rand(\d+)(\.(\d+))?/) {
317 #&DEBUG("dot => $dot, max => $max, rand=>$rand");
318 $rand = sprintf("%.*f", $dot, $rand*$max);
320 &DEBUG("swapping $orig to $rand");
321 $reply =~ s/\Q$orig\E/$rand/eg;
323 $reply =~ s/\$rand/$rand/g;
327 $reply =~ s/\$ident/$mynick/g;
329 if ($reply =~ /\$startTime/) {
330 my $time = scalar(gmtime $^T);
331 $reply =~ s/\$startTime/$time/;
334 if ($reply =~ /\$uptime/) {
335 my $uptime = &Time2String(time() - $^T);
336 $reply =~ s/\$uptime/$uptime/;
339 if ($reply =~ /\$factoids/) {
340 my $factoids = &countKeys("factoids");
341 $reply =~ s/\$factoids/$factoids/;
344 if ($reply =~ /\$Fupdate/) {
345 my $x = "\002$count{'Update'}\002 ".
346 &fixPlural("modification", $count{'Update'});
347 $reply =~ s/\$Fupdate/$x/;
350 if ($reply =~ /\$Fquestion/) {
351 my $x = "\002$count{'Question'}\002 ".
352 &fixPlural("question", $count{'Question'});
353 $reply =~ s/\$Fquestion/$x/;
356 if ($reply =~ /\$Fdunno/) {
357 my $x = "\002$count{'Dunno'}\002 ".
358 &fixPlural("dunno", $count{'Dunno'});
359 $reply =~ s/\$Fdunno/$x/;
362 $reply =~ s/\$memusage/$memusage/;