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