]> git.donarmstrong.com Git - infobot.git/blob - src/Factoids/Reply.pl
- if autoload fails, make poor attempt to load module.
[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 if (&IsParam("useStrict")) { use strict; }
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($result,$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     if ($result = &getFactoid($message)) {
32         $lhs = $message;
33         $mhs = "is";
34         $rhs = $result;
35
36         return "$lhs $mhs $rhs" if ($literal);
37     } else {
38         return '';
39     }
40
41     # if there was a head...
42     my(@poss) = split '\|\|', $result;
43     $poss[0] =~ s/^\s//;
44     $poss[$#poss] =~ s/\s$//;
45
46     if (@poss > 1) {
47         $result = &getRandom(@poss);
48         $result =~ s/^\s*//;
49     }
50
51     my $fauthor = &dbGet("factoids", "created_by", "factoid_key='$message'");
52     $result     = &SARit($result);
53
54     $reply      = $result;
55     if ($result ne "") {
56         ### AT LAST, REPEAT PREVENTION CODE REMOVED IN FAVOUR OF GLOBAL
57         ### FLOOD REPETION AND PROTECTION. -20000124
58
59         # stats code.
60         ### FIXME: old mysql doesn't support
61         ### "requested_count=requested_count+1".
62         my $count = &getFactInfo($lhs,"requested_count") || 0;
63         $count++;
64         ### BROKEN!!!
65         if (1) {        # old code.
66             &setFactInfo($lhs,"requested_by", $nuh);
67             &setFactInfo($lhs,"requested_time", time());
68             &setFactInfo($lhs,"requested_count", $count);
69         } else {
70             &dbReplace("factoids", (
71                 factoid_key     => $lhs,
72                 requested_by    => $nuh,
73                 requested_time  => time(),
74                 requested_count => $count
75             ) );
76         }
77
78         # todo: rename $real to something else!
79         my $real   = 0;
80 #       my $author = &getFactInfo($lhs,"created_by") || '';
81 #       $real++ if ($author =~ /^\Q$who\E\!/);
82 #       $real++ if (&IsFlag("n"));
83         $real = 0 if ($msgType =~ /public/);
84
85         ### fix up the reply.
86         # only remove '<reply>'
87         if (!$real and $reply =~ s/^\s*<reply>\s*//i) {
88             # 'are' fix.
89             if ($reply =~ s/^are /$lhs are /i) {
90                 &VERB("Reply.pl: el-cheapo 'are' fix executed.",2);
91             }
92
93         } elsif (!$real and $reply =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i) {
94             # only remove '<action>' and make it an action.
95         } else {                # not a short reply
96
97             ### bot->bot reply.
98             if (exists $bots{$nuh} and $rhs !~ /^\s*$/) {
99                 return "$lhs $mhs $rhs";
100             }
101
102             ### bot->person reply.
103             # result is random if separated by '||'.
104             # rhs is full factoid with '||'.
105             if ($mhs eq "is") {
106                 $reply = &getRandom(keys %{ $lang{'factoid'} });
107                 $reply =~ s/##KEY/$lhs/;
108                 $reply =~ s/##VALUE/$result/;
109             } else {
110                 $reply = "$lhs $mhs $result";
111             }
112
113             if ($reply =~ s/^\Q$who\E is/you are/i) {
114                 # fix the person.
115             } else {
116                 if ($reply =~ /^you are / or $reply =~ / you are /) {
117                     return if ($addressed);
118                 }
119             }
120         }
121     }
122
123     return $reply if ($literal);
124
125     # remove excessive beginning and end whitespaces.
126     $reply      =~ s/^\s+|\s+$//g;
127
128     if ($reply =~ /^\s+$/) {
129         &DEBUG("Reply: Null factoid ($message)");
130         return '';
131     }
132
133     return $reply unless ($reply =~ /\$/);
134
135     ###
136     ### $ SUBSTITUTION.
137     ###
138
139     $reply = &substVars($reply);
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     my $date    =  scalar(localtime());
250     $date       =~ s/\:\d+(\s+\w+)\s+\d+$/$1/;
251     $reply      =~ s/\$date/$date/gi;
252     $date       =~ s/\w+\s+\w+\s+\d+\s+//;
253     $reply      =~ s/\$time/$date/gi;
254
255     # dollar variables.
256     if ($flag) {
257         $reply  =~ s/\$nick/$who/g;
258         $reply  =~ s/\$who/$who/g;      # backward compat.
259     }
260
261     if ($reply =~ /\$(user(name)?|host)/) {
262         my ($username, $hostname) = split /\@/, $uh;
263         $reply  =~ s/\$user(name)?/$username/g;
264         $reply  =~ s/\$host(name)?/$hostname/g;
265     }
266     $reply      =~ s/\$chan(nel)?/$talkchannel/g;
267     if ($msgType =~ /public/) {
268         $reply  =~ s/\$lastspeaker/$lastWho{$talkchannel}/g;
269     } else {
270         $reply  =~ s/\$lastspeaker/$lastWho/g;
271     }
272
273     if ($reply =~ /\$rand/) {
274         my $rand  = rand();
275         my $randp = int($rand*100);
276         $reply =~ s/\$randpercentage/$randp/g;  # ???
277         # randnick.
278         if ($reply =~ /\$randnick/) {
279             my @nicks = keys %{ $channels{$chan}{''} };
280             my $randnick = $nicks[ int($rand*$#nicks) ];
281             s/\$randnick/$randnick/;
282         }
283
284         ### TODO: number of digits. 'x.y'
285         if ($reply =~ /\$rand(\d+)/) {
286             # will this work as it does in C?
287             $rand = sprintf("%*f", $1, $rand);
288         }
289         $reply =~ s/\$rand/$rand/g;
290     }
291
292     $reply      =~ s/\$factoid/$lhs/g;
293     $reply      =~ s/\$ident/$ident/g;
294
295     if ($reply =~ /\$startTime/) {
296         my $time = scalar(localtime $^T);
297         $reply =~ s/\$startTime/$time/;
298     }
299
300     if ($reply =~ /\$uptime/) {
301         my $uptime = &Time2String(time() - $^T);
302         $reply =~ s/\$uptime/$uptime/;
303     }
304
305     if ($reply =~ /\$factoids/) {
306         my $count = &countKeys("factoids");
307         $reply =~ s/\$factoids/$factoids/;
308     }
309
310     if ($reply =~ /\$Fupdate/) {
311         my $x = "\002$count{'Update'}\002 ".
312                 &fixPlural("modification", $count{'Update'});
313         $reply =~ s/\$Fupdate/$x/;
314     }
315
316     if ($reply =~ /\$Fquestion/) {
317         my $x = "\002$count{'Question'}\002 ".
318                 &fixPlural("question", $count{'Question'});
319         $reply =~ s/\$Fquestion/$x/;
320     }
321
322     if ($reply =~ /\$Fdunno/) {
323         my $x = "\002$count{'Dunno'}\002 ".
324                 &fixPlural("dunno", $count{'Dunno'});
325         $reply =~ s/\$Fdunno/$x/;
326     }
327
328     $reply      =~ s/\$memusage/$memusage/;
329
330     return $reply;
331 }
332
333 1;