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