2 ### Question.pl: Kevin Lenzo (c) 1997
6 ## if ($query == query) {
16 use vars qw($query $reply $finalQMark $nuh $result $talkok $who $nuh);
17 use vars qw(%bots %forked);
20 # my doesn't allow variables to be inherinted, local does.
21 # following is used in math()...
24 local $finalQMark = $query =~ s/\?+\s*$//;
25 $finalQMark += $query =~ s/\?\s*$//;
26 $query =~ s/^\s+|\s+$//g;
28 if (!defined $query or $query =~ /^\s*$/) {
32 my $questionWord = "";
35 return '' unless ($finalQMark);
36 return '' if (&IsParam("minVolunteerLength") == 0);
37 return '' if (length $query < $param{'minVolunteerLength'});
39 ### TODO: this should be caught in Process.pl?
40 return '' unless ($talkok);
42 # there is no flag to disable/enable asking factoids...
43 # so it was added... thanks zyxep! :)
44 if (&IsFlag("a") ne "a" && &IsFlag("o") ne "o") {
45 &status("$who tried to ask us when not allowed.");
50 # dangerous; common preambles should be stripped before here
51 if ($query =~ /^forget /i or $query =~ /^no, /) {
52 return if (exists $bots{$nuh});
55 if ($query =~ s/^literal\s+//i) {
56 &status("literal ask of '$query'.");
60 # convert to canonical reference form
64 push(@query, $query); # 1: push original.
67 if ($query =~ s/[!.]$//) {
71 $x = &normquery($query);
72 push(@query, $x) if ($x ne $query);
75 $x = &switchPerson($query);
76 push(@query, $x) if ($x ne $query);
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.
83 my $qregex = join '|', keys %{ $lang{'qWord'} };
85 # purge prefix question string.
86 if ($query =~ s/^ ($qregex)//i) {
87 $questionWord = lc($1);
90 if ($questionWord eq "" and $finalQMark and $addressed) {
91 $questionWord = "where";
93 $query =~ s/^\s+|\s+$//g; # bleh. hacked.
94 push(@query, $query) if ($query ne $x);
96 if (&IsChanConf("factoidArguments")) {
97 $result = &factoidArgs($query[0]);
99 return $result if (defined $result);
103 for (my$i=0; $i<scalar @query; $i++) {
105 $result = &getReply($query);
106 next if (!defined $result or $result eq "");
108 # 'see also' factoid redirection support.
110 while ($result =~ /^see( also)? (.*?)\.?$/) {
113 # #debian@OPN was having problems with libstdc++ factoid
114 # redirection :) 20021116. -xk.
115 # hrm... allow recursive loops... next if statement handles
117 if (grep /^\Q$link\E$/i, @link) {
118 &status("recursive link found; bailing out.");
122 if (scalar @link >= 5) {
123 &status("recursive link limit (5) reached.");
128 my $newr = &getReply($link);
129 last if (!defined $newr or $newr eq "");
134 &status("'$query' linked to: ".join(" => ", @link) );
138 &VERB("Question.pl: '$query[0]' did not exist; '$query[$i]' ($i) did",2);
144 ### TODO: Use &Forker(); move function to Freshmeat.pl.
145 if (&IsChanConf("freshmeatForFactoid")) {
146 &loadMyModule($myModules{'freshmeat'});
147 $result = &Freshmeat::showPackage($query);
148 return $result if (defined $result);
151 ### TODO: Use &Forker(); move function to Debian.pl
152 if (&IsChanConf("debianForFactoid")) {
153 &loadMyModule($myModules{'debian'});
154 $result = &Debian::DebianFind($query); # ???
155 ### TODO: debian module should tell, through shm, that it went
157 ### return $result if (defined $result);
160 if ($questionWord ne "" or $finalQMark) {
161 # if it has not been explicitly marked as a question
162 if ($addressed and $reply eq "") {
163 &status("notfound: <$who> ".join(' :: ', @query))
166 return '' unless (&IsParam("friendlyBots"));
168 foreach (split /\s+/, $param{'friendlyBots'}) {
169 &msg($_, ":INFOBOT:QUERY <$who> $query");
181 # to make it eleeter, split each arg and use "blah OR blah or BLAH"
182 # which will make it less than linear => quicker!
183 # todo: cache this, update cache when altered. !!! !!! !!!
184 # my $t = &timeget();
185 my ($first) = split(/\s+/, $str);
186 my @list = &searchTable("factoids", "factoid_key", "factoid_key", "^CMD: $first ");
187 # my $delta_time = &timedelta($t);
188 # &DEBUG("factArgs: delta_time = $delta_time s");
189 # &DEBUG("factArgs: list => ".scalar(@list) );
191 # from a design perspective, it's better to have the regex in
192 # the factoid key to reduce repetitive processing.
194 # it does not matter if it's not alphabetically sorted.
195 foreach (sort { length($b) <=> length($a) } @list) {
196 next if (/#DEL#/); # deleted.
199 # &DEBUG("factarg: '$str' =~ /^$_\$/");
202 # todo: <greycat> ~punish apt for (Eating) (Parentheses)
203 # how the hell do I fix the above? -dms.
205 # eval (evil!) code. cleaned up courtesy of lear.
208 @vals = ($str =~ /^$arg$/i);
212 &WARN("factargs: regex failed! '$str' =~ /^$_\$/");
218 if (defined $result) {
219 &WARN("factargs: '$_' matches aswell.");
223 # &DEBUG("vals => @vals");
225 &status("Question: factoid Arguments for '$str'");
226 # todo: use getReply() - need to modify it :(
229 my $r = &getFactoid($q);
231 &DEBUG("question: !result... should this happen?");
237 my $count = &getFactInfo($q, "requested_count") || 0;
239 &setFactInfo($q, "requested_by", $nuh);
240 &setFactInfo($q, "requested_time", time());
241 &setFactInfo($q, "requested_count", $count);
243 &sqlUpdate("factoids", { factoid_key => $q }, {
244 requested_by => $nuh,
245 requested_time => time(),
246 -requested_count => "requested_count+1",
250 # end of update stats.
253 $result =~ s/^\((.*?)\): //;
255 foreach ( split(',', $1) ) {
257 # &DEBUG("val => $val");
260 &status("factArgs: vals[$i] == undef; not SARing '$_' for '$str'");
267 # &DEBUG("Q: result => $result (1before)");
268 $result = &substVars($result);
269 # &DEBUG("Q: result => $result (1after)");
271 last if ($old eq $result);
277 $vals[$i] =~ s/^me$/$who/gi;
280 &status("factArgs: SARing '$_' to '$vals[$i]'.");
281 $result =~ s/\Q$_\E/$vals[$i]/g;
286 # nasty hack to get partial &getReply() functionality.
287 $result =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i;
288 $result =~ s/^\s*<reply>\s*//i;
289 $result = &SARit($result);
291 # well... lets go through all of them. not advisable if we have like
292 # 1000 commands, heh.
294 $cmdstats{'Factoid Commands'}++;