]> git.donarmstrong.com Git - infobot.git/blob - src/Factoids/Reply.pl
literal searches and shows where
[infobot.git] / src / Factoids / Reply.pl
1 ###
2 ### Reply.pl: Kevin Lenzo   (c) 1997
3 ###
4
5 ##
6 # x is y === $lhs $mhs $rhs
7 #
8 #   lhs - factoid.
9 #   mhs - verb.
10 #   rhs - factoid message.
11 ##
12
13 # use strict;   # TODO
14
15 use vars qw($msgType $uh $lastWho $ident);
16 use vars qw(%lang %lastWho);
17
18 sub getReply {
19     my($message) = @_;
20     my($lhs,$mhs,$rhs);
21     my($reply, $count, $fauthor, $result, $factoid, $search, @searches);
22     $orig{message} = $message;
23
24     if (!defined $message or $message =~ /^\s*$/) {
25         &WARN("getR: message == NULL.");
26         return '';
27     }
28
29     $message =~ tr/A-Z/a-z/;
30     $message =~ s/^cmd:/CMD:/;
31
32     if (&IsChanConf("factoidSearch")) {
33         @searches = split(/\s+/, &getChanConf("factoidSearch"));
34     } else {
35         @searches = ('_default');
36     }
37
38     # check for factoids with each prefix
39     foreach $search (@searches) {
40         if ($search eq '_default') {
41             $factoid = $message;
42         } else {
43             $factoid = "$search $message";
44         }
45         ($count, $fauthor, $result) = &sqlSelect("factoids",
46             "requested_count,created_by,factoid_value",
47             { factoid_key => $factoid }
48         );
49         last if ($result);
50     }
51
52     if ($result) {
53         $lhs = $message;
54         $mhs = "is";
55         $rhs = $result;
56
57         return "\"$factoid\" $mhs \"$rhs\"" if ($literal);
58     } else {
59         return '';
60     }
61
62     # if there was a head...
63     my(@poss) = split '\|\|', $result;
64     $poss[0] =~ s/^\s//;
65     $poss[$#poss] =~ s/\s$//;
66
67     if (@poss > 1) {
68         $result = &getRandom(@poss);
69         $result =~ s/^\s*//;
70     }
71
72     $result     = &SARit($result);
73
74     $reply      = $result;
75     if ($result ne "") {
76         ### AT LAST, REPEAT PREVENTION CODE REMOVED IN FAVOUR OF GLOBAL
77         ### FLOOD REPETION AND PROTECTION. -20000124
78
79         # stats code.
80         ### FIXME: old mysql doesn't support
81         ### "requested_count=requested_count+1".
82         $count++;
83         &sqlSet("factoids", {'factoid_key' => $factoid}, {
84                 requested_by    => $nuh,
85                 requested_time  => time(),
86                 requested_count => $count
87         } );
88
89         # TODO: rename $real to something else!
90         my $real   = 0;
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/);
95
96         ### fix up the reply.
97         # only remove '<reply>'
98         if (!$real and $reply =~ s/^\s*<reply>\s*//i) {
99             # 'are' fix.
100             if ($reply =~ s/^are /$lhs are /i) {
101                 &VERB("Reply.pl: el-cheapo 'are' fix executed.",2);
102             }
103
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
107
108             ### bot->bot reply.
109             if (exists $bots{$nuh} and $rhs !~ /^\s*$/) {
110                 return "$lhs $mhs $rhs";
111             }
112
113             ### bot->person reply.
114             # result is random if separated by '||'.
115             # rhs is full factoid with '||'.
116             if ($mhs eq "is") {
117                 $reply = &getRandom(keys %{ $lang{'factoid'} });
118                 $reply =~ s/##KEY/$lhs/;
119                 $reply =~ s/##VALUE/$result/;
120             } else {
121                 $reply = "$lhs $mhs $result";
122             }
123
124             if ($reply =~ s/^\Q$who\E is/you are/i) {
125                 # fix the person.
126             } else {
127                 if ($reply =~ /^you are / or $reply =~ / you are /) {
128                     return if ($addressed);
129                 }
130             }
131         }
132     }
133
134     # remove excessive beginning and end whitespaces.
135     $reply      =~ s/^\s+|\s+$//g;
136
137     if ($reply =~ /^\s+$/) {
138         &DEBUG("Reply: Null factoid ($message)");
139         return '';
140     }
141
142     return $reply unless ($reply =~ /\$/);
143
144     ###
145     ### $ SUBSTITUTION.
146     ###
147
148     # don't evaluate if it has factoid arguments.
149     if ($message =~ /^CMD:/i) {
150         &status("Reply: not doing substVars (eval dollar vars)");
151     } else {
152         $reply = &substVars($reply,1);
153     }
154
155     $reply;
156 }
157
158 sub smart_replace {
159     my ($string) = @_;
160     my ($l,$r)  = (0,0);        # l = left,  r = right.
161     my ($s,$t)  = (0,0);        # s = start, t = marker.
162     my $i       = 0;
163     my $old     = $string;
164     my @rand;
165
166     foreach (split //, $string) {
167
168         if ($_ eq "(") {
169             if (!$l and !$r) {
170                 $s = $i;
171                 $t = $i;
172             }
173
174             $l++;
175             $r--;
176         }
177
178         if ($_ eq ")") {
179             $r++;
180             $l--;
181
182             if (!$l and !$r) {
183                 my $substr = substr($old,$s,$i-$s+1);
184                 push(@rand, substr($old,$t+1,$i-$t-1) );
185
186                 my $rand = $rand[rand @rand];
187 #               &status("SARing '$substr' to '$rand'.");
188                 $string =~ s/\Q$substr\E/$rand/;
189                 undef @rand;
190             }
191         }
192
193         if ($_ eq "|" and $l+$r== 0 and $l==1) {
194             push(@rand, substr($old,$t+1,$i-$t-1) );
195             $t = $i;
196         }
197
198         $i++;
199     }
200
201     if ($old eq $string) {
202         &WARN("smart_replace: no subst made. (string => $string)");
203     }
204
205     return $string;
206 }
207
208 sub SARit {
209     my($txt) = @_;
210     my $done = 0;
211
212     # (blah1|blah2)?
213     while ($txt =~ /\((.*?)\)\?/) {
214         my $str = $1;
215         if (rand() > 0.5) {             # fix.
216             &status("Factoid transform: keeping '$str'.");
217             $txt =~ s/\(\Q$str\E\)\?/$str/;
218         } else {                        # remove
219             &status("Factoid transform: removing '$str'.");
220             $txt =~ s/\(\Q$str\E\)\?\s?//;
221         }
222         $done++;
223         last if ($done >= 10);  # just in case.
224     }
225     $done = 0;
226
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;
232
233         &status("SARing '$&' to '$new' (2).");
234         $txt =~ s/$&/$new/;
235         $done++;
236         last if ($done >= 10);  # just in case.
237     }
238     $done = 0;
239
240     # EG: (blah1|blah2|blah3|) => blah1
241     while ($txt =~ /.*\((.*\|.*?)\).*/) {
242         $txt = &smart_replace($txt);
243
244         $done++;
245         last if ($done >= 10);  # just in case.
246     }
247     &status("Reply.pl: $done SARs done.") if ($done);
248
249     return $txt;
250 }
251
252 sub substVars {
253     my($reply,$flag) = @_;
254
255     # $date, $time.
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;
262
263     # dollar variables.
264     if ($flag) {
265         $reply  =~ s/\$nick/$who/g;
266         $reply  =~ s/\$who/$who/g;      # backward compat.
267     }
268
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;
273     }
274     $reply      =~ s/\$chan(nel)?/$talkchannel/g;
275     if ($msgType =~ /public/) {
276         $reply  =~ s/\$lastspeaker/$lastWho{$talkchannel}/g;
277     } else {
278         $reply  =~ s/\$lastspeaker/$lastWho/g;
279     }
280
281     if ($reply =~ /\$rand/) {
282         my $rand  = rand();
283
284         # $randnick.
285         if ($reply =~ /\$randnick/) {
286             my @nicks = keys %{ $channels{$chan}{''} };
287             my $randnick = $nicks[ int($rand*$#nicks) ];
288             $reply =~ s/\$randnick/$randnick/g;
289         }
290
291         # eg: $rand100.3
292         if ($reply =~ /\$rand(\d+)(\.(\d+))?/) {
293             my $max = $1;
294             my $dot = $3 || 0;
295             my $orig = $&;
296             #&DEBUG("dot => $dot, max => $max, rand=>$rand");
297             $rand = sprintf("%.*f", $dot, $rand*$max);
298
299             &DEBUG("swapping $orig to $rand");
300             $reply =~ s/\Q$orig\E/$rand/eg;
301         } else {
302             $reply =~ s/\$rand/$rand/g;
303         }
304     }
305
306     $reply      =~ s/\$ident/$ident/g;
307
308     if ($reply =~ /\$startTime/) {
309         my $time = scalar(gmtime $^T);
310         $reply =~ s/\$startTime/$time/;
311     }
312
313     if ($reply =~ /\$uptime/) {
314         my $uptime = &Time2String(time() - $^T);
315         $reply =~ s/\$uptime/$uptime/;
316     }
317
318     if ($reply =~ /\$factoids/) {
319         my $factoids = &countKeys("factoids");
320         $reply =~ s/\$factoids/$factoids/;
321     }
322
323     if ($reply =~ /\$Fupdate/) {
324         my $x = "\002$count{'Update'}\002 ".
325                 &fixPlural("modification", $count{'Update'});
326         $reply =~ s/\$Fupdate/$x/;
327     }
328
329     if ($reply =~ /\$Fquestion/) {
330         my $x = "\002$count{'Question'}\002 ".
331                 &fixPlural("question", $count{'Question'});
332         $reply =~ s/\$Fquestion/$x/;
333     }
334
335     if ($reply =~ /\$Fdunno/) {
336         my $x = "\002$count{'Dunno'}\002 ".
337                 &fixPlural("dunno", $count{'Dunno'});
338         $reply =~ s/\$Fdunno/$x/;
339     }
340
341     $reply      =~ s/\$memusage/$memusage/;
342
343     return $reply;
344 }
345
346 1;