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 ''; #never respond if we're not addressed
36 return '' unless ($finalQMark);
37 return '' unless &IsChanConf("minVolunteerLength") > 0;
38 return '' if (length $query < &::getChanConf('minVolunteerLength'));
40 ### TODO: this should be caught in Process.pl?
41 return '' unless ($talkok);
43 # there is no flag to disable/enable asking factoids...
44 # so it was added... thanks zyxep! :)
45 if (&IsFlag("a") ne "a" && &IsFlag("o") ne "o") {
46 &status("$who tried to ask us when not allowed.");
51 # dangerous; common preambles should be stripped before here
52 if ($query =~ /^forget /i or $query =~ /^no, /) {
53 return if (exists $bots{$nuh});
56 if ($query =~ s/^literal\s+//i) {
57 &status("literal ask of '$query'.");
61 # convert to canonical reference form
65 push(@query, $query); # 1: push original.
68 if ($query =~ s/[!.]$//) {
72 $x = &normquery($query);
73 push(@query, $x) if ($x ne $query);
76 $x = &switchPerson($query);
77 push(@query, $x) if ($x ne $query);
80 $query =~ s/\s+at\s*(\?*)$/$1/; # where is x at?
81 $query =~ s/^explain\s*(\?*)/$1/i; # explain x
82 $query = " $query "; # side whitespaces.
84 my $qregex = join '|', keys %{ $lang{'qWord'} };
86 # purge prefix question string.
87 if ($query =~ s/^ ($qregex)//i) {
88 $questionWord = lc($1);
91 if ($questionWord eq "" and $finalQMark and $addressed) {
92 $questionWord = "where";
94 $query =~ s/^\s+|\s+$//g; # bleh. hacked.
95 push(@query, $query) if ($query ne $x);
97 if (&IsChanConf("factoidArguments") > 0) {
98 $result = &factoidArgs($query[0]);
100 return $result if (defined $result);
104 for (my$i=0; $i<scalar @query; $i++) {
106 $result = &getReply($query);
107 next if (!defined $result or $result eq "");
109 # 'see also' factoid redirection support.
111 while ($result =~ /^see( also)? (.*?)\.?$/) {
114 # #debian@OPN was having problems with libstdc++ factoid
115 # redirection :) 20021116. -xk.
116 # hrm... allow recursive loops... next if statement handles
118 if (grep /^\Q$link\E$/i, @link) {
119 &status("recursive link found; bailing out.");
123 if (scalar @link >= 5) {
124 &status("recursive link limit (5) reached.");
129 my $newr = &getReply($link);
131 # no such factoid. try commands
132 if (!defined $newr || $newr =~ /^0?$/) {
133 # support command redirection.
134 # recursive cmdHooks aswell :)
136 $done++ if &parseCmdHook($link);
138 $done++ unless (&Modules());
142 last if (!defined $newr or $newr eq "");
147 &status("'$query' linked to: ".join(" => ", @link) );
151 &VERB("Question.pl: '$query[0]' did not exist; '$query[$i]' ($i) did",2);
157 ### TODO: Use &Forker(); move function to Debian.pl
158 if (&IsChanConf('debianForFactoid') > 0) {
159 &loadMyModule('Debian');
160 $result = &Debian::DebianFind($query); # ???
161 ### TODO: debian module should tell, through shm, that it went
163 ### return $result if (defined $result);
166 if ($questionWord ne "" or $finalQMark) {
167 # if it has not been explicitly marked as a question
168 if ($addressed and $reply eq "") {
169 &status("notfound: <$who> ".join(' :: ', @query))
172 return '' unless (&IsParam("friendlyBots"));
174 foreach (split /\s+/, $param{'friendlyBots'}) {
175 &msg($_, ":INFOBOT:QUERY <$who> $query");
187 # to make it eleeter, split each arg and use "blah OR blah or BLAH"
188 # which will make it less than linear => quicker!
189 # TODO: cache this, update cache when altered. !!! !!! !!!
190 # my $t = &timeget();
191 my ($first) = split(/\s+/, $str);
193 # ignore split to commands [dumb commands vs. factoids]
194 return undef if $str =~ /\s+\=\~\s+s[\#\/\:]/;
195 my @list = &searchTable("factoids", "factoid_key", "factoid_key", "^CMD: $first ");
196 # my $delta_time = &timedelta($t);
197 # &DEBUG("factArgs: delta_time = $delta_time s");
198 # &DEBUG("factArgs: list => ".scalar(@list) );
200 # from a design perspective, it's better to have the regex in
201 # the factoid key to reduce repetitive processing.
203 # it does not matter if it's not alphabetically sorted.
204 foreach (sort { length($b) <=> length($a) } @list) {
205 next if (/#DEL#/); # deleted.
208 # &DEBUG("factarg: '$str' =~ /^$_\$/");
211 # eval (evil!) code. cleaned up courtesy of lear.
214 @vals = ($str =~ /^$arg$/i);
218 &WARN("factargs: regex failed! '$str' =~ /^$_\$/");
224 if (defined $result) {
225 &WARN("factargs: '$_' matches aswell.");
229 # &DEBUG("vals => @vals");
231 &status("Question: factoid Arguments for '$str'");
232 # TODO: use getReply() - need to modify it :(
235 my $r = &getFactoid($q);
237 &DEBUG("question: !result... should this happen?");
241 # update stats. old mysql/sqlite don't do +1
242 my ($count) = &sqlSelect("factoids", "requested_count", { factoid_key => $q });
244 &sqlSet("factoids", {'factoid_key' => $q}, {
245 requested_by => $nuh,
246 requested_time => time(),
247 requested_count => $count
250 # end of update stats.
254 $result =~ s/^\((.*?)\): //;
257 # start nasty hack to get partial &getReply() functionality.
258 $result = &SARit($result);
260 foreach ( split(',', $vars) ) {
262 &DEBUG("val => $val");
265 &status("factArgs: vals[$i] == undef; not SARing '$_' for '$str'");
272 &DEBUG("Q: result => $result (1before)");
273 $result = &substVars($result,1);
274 &DEBUG("Q: result => $result (1after)");
276 last if ($old eq $result);
282 $vals[$i] =~ s/^me$/$who/gi;
285 &status("factArgs: SARing '$_' to '$vals[$i]'.");
286 $result =~ s/\Q$_\E/$vals[$i]/g;
291 # nasty hack to get partial &getReply() functionality.
292 $result = &SARit($result);
293 $result =~ s/^\s*<action>\s*(.*)/\cAACTION $1\cA/i;
294 $result =~ s/^\s*<reply>\s*//i;
296 # well... lets go through all of them. not advisable if we have like
297 # 1000 commands, heh.
299 $cmdstats{'Factoid Commands'}++;