]> git.donarmstrong.com Git - infobot.git/blob - src/Factoids/Reply.pl
Changed $infobot_ to $bot_
[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     my $literal = 0;
23     $orig{message} = $message;
24
25     if (!defined $message or $message =~ /^\s*$/) {
26         &WARN("getR: message == NULL.");
27         return '';
28     }
29
30     $message =~ tr/A-Z/a-z/;
31
32     if ($result = &getFactoid($message)) {
33         $lhs = $message;
34         $mhs = "is";
35         $rhs = $result;
36     } else {
37         return '';
38     }
39
40     # if there was a head...
41     my(@poss) = split '\|\|', $result;
42     $poss[0] =~ s/^\s//;
43     $poss[$#poss] =~ s/\s$//;
44
45     if ((@poss > 1) && ($msgType =~ /public/)) {
46         $result = &getRandom(@poss);
47         $result =~ s/^\s*//;
48     }
49
50     my $fauthor = &dbGet("factoids", "factoid_key", $message, "created_by");
51     ### we need non-evaluating regex like in factoid sar.
52     if ($msgType =~ /^private$/) {
53         if (defined $fauthor and $fauthor =~ /^\Q$who\E\!/i) {
54             &status("Reply.pl: author requested own factoid in private; literal!");
55             $literal = 1;
56         }
57     } else {
58         my $done = 0;
59
60         # (blah1|blah2)?
61         while ($result =~ /\((.*?)\)\?/) {
62             my $str = $1;
63             if (rand() > 0.5) {         # fix.
64                 &status("Factoid transform: keeping '$str'.");
65                 $result =~ s/\(\Q$str\E\)\?/$str/;
66             } else {                    # remove
67                 &status("Factoid transform: removing '$str'.");
68                 $result =~ s/\(\Q$str\E\)\?\s?//;
69             }
70             $done++;
71             last if ($done >= 10);      # just in case.
72         }
73         $done = 0;
74
75         # EG: (0-32768) => 6325
76         ### TODO: (1-10,20-30,40) => 24
77         while ($result =~ /\((\d+)-(\d+)\)/) {
78             my ($lower,$upper) = ($1,$2);
79             my $new = int(rand $upper-$lower) + $lower;
80
81             &status("Reply.pl: SARing '$&' to '$new'.");
82             $result =~ s/$&/$new/;
83             $done++;
84             last if ($done >= 10);      # just in case.
85         }
86         $done = 0;
87
88         # EG: (blah1|blah2|blah3|) => blah1
89         while ($result =~ /\((.*?\|.*?)\)/) {
90             my $str = $1;
91             my @rand = split /\|/, $str;
92             my $rand = $rand[rand @rand];
93
94             &status("Reply.pl: SARing '($str)' to '$rand'.");
95             $result =~ s/\(\Q$str\E\)/$rand/;
96             $done++;
97             last if ($done >= 10);      # just in case.
98         }
99         &status("Reply.pl: $done SARs done.") if ($done);
100     }
101
102     $reply = $result;
103     if ($result ne "") {
104         ### AT LAST, REPEAT PREVENTION CODE REMOVED IN FAVOUR OF GLOBAL
105         ### FLOOD REPETION AND PROTECTION. -20000124
106
107         # stats code.
108         &setFactInfo($lhs,"requested_by", $nuh);
109         &setFactInfo($lhs,"requested_time", time());
110         ### FIXME: old mysql doesn't support
111         ###     "requested_count=requested_count+1".
112         my $count = &getFactInfo($lhs,"requested_count") || 0;
113         $count++;
114         &setFactInfo($lhs,"requested_count", $count);
115
116         my $real   = 0;
117         my $author = &getFactInfo($lhs,"created_by") || '';
118
119         $real++ if ($author =~ /^\Q$who\E\!/);
120         $real++ if (&IsFlag("n"));
121         $real = 0 if ($msgType =~ /public/);
122
123         ### fix up the reply.
124         # only remove '<reply>'
125         if (!$real and $reply =~ s/^\s*<reply>\s*//i) {
126             # 'are' fix.
127             if ($reply =~ s/^are //i) {
128                 &DEBUG("Reply.pl: el-cheapo 'are' fix executed.");
129                 $mhs = "are";
130             }
131
132         } elsif (!$real and $reply =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i) {
133             # only remove '<action>' and make it an action.
134         } else {                # not a short reply
135
136             ### bot->bot reply.
137             if (exists $bots{$nuh} and $rhs !~ /^\s*$/) {
138                 return "$lhs $mhs $rhs";
139             }
140
141             ### bot->person reply.
142             # result is random if separated by '||'.
143             # rhs is full factoid with '||'.
144             if ($mhs eq "is") {
145                 $reply = &getRandom(keys %{$lang{'factoid'}});
146                 $reply =~ s/##KEY/$lhs/;
147                 $reply =~ s/##VALUE/$result/;
148             } else {
149                 $reply = "$lhs $mhs $result";
150             }
151
152             if ($reply =~ s/^\Q$who\E is/you are/i) {
153                 # fix the person.
154             } else {
155                 if ($reply =~ /^you are / or $reply =~ / you are /) {
156                     return $noreply if ($addressed);
157                 }
158             }
159         }
160     }
161
162     return $reply if ($literal);
163
164     # remove excessive beginning and end whitespaces.
165     $reply      =~ s/^\s+|\s+$//g;
166
167     if (length($reply) < 5 or $reply =~ /^\s+$/) {
168         &DEBUG("Reply: FIXME: reply => '$reply'.");
169         return '';
170     }
171
172     return $reply unless ($reply =~ /\$/);
173
174     ###
175     ### $ SUBSTITUTION.
176     ###
177     
178     # $date, $time.
179     my $date    =  scalar(localtime());
180     $date       =~ s/\:\d+(\s+\w+)\s+\d+$/$1/;
181     $reply      =~ s/\$date/$date/gi;
182     $date       =~ s/\w+\s+\w+\s+\d+\s+//;
183     $reply      =~ s/\$time/$date/gi;
184
185     # dollar variables.
186     $reply      =~ s/\$nick/$who/g;
187     $reply      =~ s/\$who/$who/g;      # backward compat.
188     if ($reply =~ /\$(user(name)?|host)/) {
189         my ($username, $hostname) = split /\@/, $uh;
190         $reply  =~ s/\$user(name)?/$username/g;
191         $reply  =~ s/\$host(name)?/$hostname/g;
192     }
193     $reply      =~ s/\$chan(nel)?/$talkchannel/g;
194     if ($msgType =~ /public/) {
195         $reply  =~ s/\$lastspeaker/$lastWho{$talkchannel}/g;
196     } else {
197         $reply  =~ s/\$lastspeaker/$lastWho/g;
198     }
199
200     if ($reply =~ /\$rand/) {
201         my $rand  = rand();
202         my $randp = int($rand*100);
203         $reply =~ s/\$randpercentage/$randp/g;
204         ### TODO: number of digits. 'x.y'
205         if ($reply =~ /\$rand(\d+)/) {
206             # will this work as it does in C?
207             $rand = sprintf("%*f", $1, $rand);
208         }
209         $reply =~ s/\$rand/$rand/g;
210     }
211
212     $reply      =~ s/\$factoid/$lhs/g;
213     $reply      =~ s/\$ident/$ident/g;
214
215     if ($reply =~ /\$startTime/) {
216         my $time = scalar(localtime $^T);
217         $reply =~ s/\$startTime/$time/;
218     }
219
220     if ($reply =~ /\$uptime/) {
221         my $uptime = &Time2String(time() - $^T);
222         $reply =~ s/\$uptime/$uptime/;
223     }
224
225     if ($reply =~ /\$factoids/) {
226         my $count = &countKeys("factoids");
227         $reply =~ s/\$factoids/$factoids/;
228     }
229
230     if ($reply =~ /\$Fupdate/) {
231         my $x = "\002$count{'Update'}\002 ".
232                 &fixPlural("modification", $count{'Update'});
233         $reply =~ s/\$Fupdate/$x/;
234     }
235
236     if ($reply =~ /\$Fquestion/) {
237         my $x = "\002$count{'Question'}\002 ".
238                 &fixPlural("question", $count{'Question'});
239         $reply =~ s/\$Fquestion/$x/;
240     }
241
242     if ($reply =~ /\$Fdunno/) {
243         my $x = "\002$count{'Dunno'}\002 ".
244                 &fixPlural("dunno", $count{'Dunno'});
245         $reply =~ s/\$Fdunno/$x/;
246     }
247
248     $reply      =~ s/\$memusage/$memusage/;
249
250     $reply;
251 }
252
253 1;