]> git.donarmstrong.com Git - infobot.git/blob - src/Factoids/Question.pl
- added support/hack for channel +o as "+o" in bot user file.
[infobot.git] / src / Factoids / Question.pl
1 ###
2 ### Question.pl: Kevin Lenzo  (c) 1997
3 ###
4
5 ##  doQuestion --
6 ##      if ($query == query) {
7 ##              return $value;
8 ##      } else {
9 ##              return NULL;
10 ##      }
11 ##
12 ##
13
14 if (&IsParam("useStrict")) { use strict; }
15
16 use vars qw($query $reply $finalQMark $nuh $result $talkok $who $nuh);
17 use vars qw(%bots %forked);
18
19 sub doQuestion {
20     # my doesn't allow variables to be inherinted, local does.
21     # following is used in math()...
22     local($query)       = @_;
23     local($reply)       = "";
24     local $finalQMark   = $query =~ s/\?+\s*$//;
25     $finalQMark         += $query =~ s/\?\s*$//;
26     $query              =~ s/^\s+|\s+$//g;
27
28     if (!defined $query or $query =~ /^\s*$/) {
29         return '';
30     }
31
32     my $questionWord    = "";
33
34     if (!$addressed) {
35         return '' unless ($finalQMark);
36         return '' if (&IsParam("minVolunteerLength") == 0);
37         return '' if (length $query < $param{'minVolunteerLength'});
38     } else {
39         ### TODO: this should be caught in Process.pl?
40         return '' unless ($talkok);
41     }
42
43     # dangerous; common preambles should be stripped before here
44     if ($query =~ /^forget /i or $query =~ /^no, /) {
45         return if (exists $bots{$nuh});
46     }
47
48     if ($query =~ s/^literal\s+//i) {
49         &status("literal ask of '$query'.");
50         $literal = 1;
51     }
52
53     # convert to canonical reference form
54     my $x;
55     my @query;
56
57     push(@query, $query);       # 1: push original.
58
59     # valid factoid.
60     if ($query =~ s/[!.]$//) {
61         push(@query,$query);
62     }
63
64     $x = &normquery($query);
65     push(@query, $x) if ($x ne $query);
66     $query = $x;
67
68     $x = &switchPerson($query);
69     push(@query, $x) if ($x ne $query);
70     $query = $x;
71
72     $query =~ s/\s+at\s*(\?*)$/$1/;     # where is x at?
73     $query =~ s/^explain\s*(\?*)/$1/i;  # explain x
74     $query = " $query ";                # side whitespaces.
75
76     my $qregex = join '|', keys %{ $lang{'qWord'} };
77
78     # what's whats => what is; who'?s => who is, etc
79     $query =~ s/ ($qregex)\'?s / $1 is /i;
80     if ($query =~ s/\s+($qregex)\s+//i) { # check for question word
81         $questionWord = lc($1);
82     }
83
84     if ($questionWord eq "" and $finalQMark and $addressed) {
85         $questionWord = "where";
86     }
87
88     if (&IsChanConf("factoidArguments")) {
89         $result = &factoidArgs($query[0]);
90
91         return $result if (defined $result);
92     }
93
94     my @link;
95     for (my$i=0; $i<scalar @query; $i++) {
96         $query  = $query[$i];
97         $result = &getReply($query);
98         next if (!defined $result or $result eq "");
99
100         # 'see also' factoid redirection support.
101
102         while ($result =~ /^see( also)? (.*?)\.?$/) {
103             my $link    = $2;
104
105             if (grep /^$link$/i, @link) {
106                 &status("recursive link found; bailing out.");
107                 last;
108             }
109
110             if (scalar @link >= 5) {
111                 &status("recursive link limit reached.");
112                 last;
113             }
114
115             push(@link, $link);
116             my $newr = &getReply($link);
117             last if (!defined $newr or $newr eq "");
118             $result  = $newr;
119         }
120
121         if (@link) {
122             &status("'$query' linked to: ".join(" => ", @link) );
123         }
124
125         if ($i != 0) {
126             &VERB("Question.pl: '$query[0]' did not exist; '$query[$i]' ($i) did",2);
127         }
128
129         return $result;
130     }
131
132     ### TODO: Use &Forker(); move function to Freshmeat.pl.
133     if (&IsChanConf("freshmeatForFactoid")) {
134         &loadMyModule($myModules{'freshmeat'});
135         $result = &Freshmeat::showPackage($query);
136         return $result if (defined $result);
137     }
138
139     ### TODO: Use &Forker(); move function to Debian.pl
140     if (&IsChanConf("debianForFactoid")) {
141         &loadMyModule($myModules{'debian'});
142         $result = &Debian::DebianFind($query);  # ???
143         ### TODO: debian module should tell, through shm, that it went
144         ###       ok or not.
145 ###     return $result if (defined $result);
146     }
147
148     if ($questionWord ne "" or $finalQMark) {
149         # if it has not been explicitly marked as a question
150         if ($addressed and $reply eq "") {
151             &status("notfound: <$who> ".join(' :: ', @query))
152                                                 if ($finalQMark);
153
154             return '' unless (&IsParam("friendlyBots"));
155
156             foreach (split /\s+/, $param{'friendlyBots'}) {
157                 &msg($_, ":INFOBOT:QUERY <$who> $query");
158             }
159         }
160     }
161
162     return $reply;
163 }
164
165 sub factoidArgs {
166     my($str)    = @_;
167     my $result;
168
169     # to make it eleeter, split each arg and use "blah OR blah or BLAH"
170     # which will make it less than linear => quicker!
171     # todo: cache this, update cache when altered. !!! !!! !!!
172 #    my $t = &timeget();
173     my @list = &searchTable("factoids", "factoid_key", "factoid_key", "^CMD: ");
174 #    my $delta_time = &timedelta($t);
175 #    &DEBUG("factArgs: delta_time = $delta_time s");
176 #    &DEBUG("factArgs: list => ".scalar(@list) );
177
178     # from a design perspective, it's better to have the regex in
179     # the factoid key to reduce repetitive processing.
180
181     # it does not matter if it's not alphabetically sorted.
182     foreach (sort { length($b) <=> length($a) } @list) {
183         next if (/#DEL#/);      # deleted.
184
185         s/^CMD: //i;
186 #       &DEBUG("factarg: ''$str' =~ /^$_\$/'");
187         my $arg = $_;
188
189         # todo: <greycat> ~punish apt for (Eating) (Parentheses)
190         # how the hell do I fix the above? -dms.
191
192         # eval (evil!) code. cleaned up courtesy of lear.
193         my @vals;
194         eval {
195             @vals = ($str =~ /^$arg$/i);
196         };
197
198         if ($@) {
199             &WARN("factargs: regex failed! '$str' =~ /^$_\$/");
200             next;
201         }
202
203         next unless (@vals);
204
205         if (defined $result) {
206             &WARN("factargs: '$_' matches aswell.");
207             next;
208         }
209
210 #       &DEBUG("vals => @vals");
211
212         &status("Question: factoid Arguments for '$str'");
213         # todo: use getReply() - need to modify it :(
214         my $i   = 0;
215         my $q   = "CMD: $_";
216         my $r   = &getFactoid($q);
217         if (!defined $r) {
218             &DEBUG("question: !result... should this happen?");
219             return;
220         }
221
222         # update stats.
223         my $count = &getFactInfo($q, "requested_count") || 0;
224         $count++;
225         &setFactInfo($q, "requested_by", $nuh);
226         &setFactInfo($q, "requested_time", time());
227         &setFactInfo($q, "requested_count", $count);
228         # end of update stats.
229
230         $result = $r;
231         $result =~ s/^\((.*?)\): //;
232
233         foreach ( split(',', $1) ) {
234             my $val = $vals[$i];
235 #           &DEBUG("val => $val");
236
237             if (!defined $val) {
238                 &status("factArgs: vals[$i] == undef; not SARing '$_' for '$str'");
239                 next;
240             }
241
242             my $done = 0;
243             my $old = $result;
244             while (1) {
245 #               &DEBUG("Q: result => $result (1before)");
246                 $result = &substVars($result);
247 #               &DEBUG("Q: result => $result (1after)");
248
249                 last if ($old eq $result);
250                 $old = $result;
251                 $done++;
252             }
253
254             # hack.
255             $vals[$i] =~ s/^me$/$who/gi;
256
257             if (!$done) {
258                 &status("factArgs: SARing '$_' to '$vals[$i]'.");
259                 $result =~ s/\Q$_\E/$vals[$i]/g;
260             }
261             $i++;
262         }
263
264         # nasty hack to get partial &getReply() functionality.
265         $result =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i;
266         $result =~ s/^\s*<reply>\s*//i;
267         $result = &SARit($result);
268
269 # well... lets go through all of them. not advisable if we have like
270 # 1000 commands, heh.
271 #       return $result;
272         $cmdstats{'Factoid Commands'}++;
273     }
274
275     return $result;
276 }
277
278 1;