]> git.donarmstrong.com Git - infobot.git/blob - src/Factoids/Question.pl
- irctextcounters: add percentile/ranking.
[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 @vals;
188         my $arg = $_;
189
190         # todo: <greycat> ~punish apt for (Eating) (Parentheses)
191         # how the hell do I fix the above?
192
193         # todo: make eval work with $$i's :(
194         # fuck this is an ugly hack. it works though.
195         eval {
196             if ($str =~ /^$arg$/i) {
197                 for ($i=1; $i<=5; $i++) {
198                     $val = $$i;
199                     last unless (defined $val);
200
201                     push(@vals, $val);
202                 }
203             }
204         };
205
206         if ($@) {   # it failed!!!
207             &WARN("factargs: regex failed! '$str' =~ /^$_\$/");
208             next;
209         }
210
211         next unless (@vals);
212
213         if (defined $result) {
214             &WARN("factargs: '$_' matches aswell.");
215             next;
216         }
217
218 #       &DEBUG("vals => @vals");
219
220         &status("Question: factoid Arguments for '$str'");
221         # todo: use getReply() - need to modify it :(
222         my $i   = 0;
223         my $q   = "CMD: $_";
224         my $r   = &getFactoid($q);
225         if (!defined $r) {
226             &DEBUG("question: !result... should this happen?");
227             return;
228         }
229
230         # update stats.
231         my $count = &getFactInfo($q, "requested_count") || 0;
232         $count++;   
233         &setFactInfo($q, "requested_by", $nuh);
234         &setFactInfo($q, "requested_time", time());
235         &setFactInfo($q, "requested_count", $count);
236         # end of update stats.
237
238         $result = $r;
239         $result =~ s/^\((.*?)\): //;
240
241         foreach ( split(',', $1) ) {
242             my $val = $vals[$i];
243 #           &DEBUG("val => $val");
244
245             if (!defined $val) {
246                 &status("factArgs: vals[$i] == undef; not SARing '$_' for '$str'");
247                 next;
248             }
249
250             my $done = 0;
251             my $old = $result;
252             while (1) {
253 #               &DEBUG("Q: result => $result (1before)");
254                 $result = &substVars($result);
255 #               &DEBUG("Q: result => $result (1after)");
256
257                 last if ($old eq $result);
258                 $old = $result;
259                 $done++;
260             }
261
262             # hack.
263             $vals[$i] =~ s/^me$/$who/gi;
264
265             if (!$done) {
266                 &status("factArgs: SARing '$_' to '$vals[$i]'.");
267                 $result =~ s/\Q$_\E/$vals[$i]/g;
268             }
269             $i++;
270         }
271
272         # nasty hack to get partial &getReply() functionality.
273         $result =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i;
274         $result =~ s/^\s*<reply>\s*//i;
275         $result = &SARit($result);
276
277 # well... lets go through all of them. not advisable if we have like
278 # 1000 commands, heh.
279 #       return $result;
280         $cmdstats{'Factoid Commands'}++;
281     }
282
283     return $result;
284 }
285
286 1;