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