]> git.donarmstrong.com Git - infobot.git/blob - src/Factoids/Question.pl
notfound uses @query now; removed origQuery
[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         &FIXME("doQ: query == NULL");
30         return '';
31     }
32
33     my $questionWord    = "";
34
35     if (!$addressed) {
36         return '' unless ($finalQMark);
37         return '' if (&IsParam("minVolunteerLength") == 0);
38         return '' if (length($query) < $param{'minVolunteerLength'});
39     } else {
40         ### TODO: this should be caught in Process.pl?
41         return '' unless ($talkok);
42     }
43
44     # dangerous; common preambles should be stripped before here
45     if ($query =~ /^forget /i or $query =~ /^no, /) {
46         return $noreply if (exists $bots{$nuh});
47     }
48
49     # convert to canonical reference form
50     my $x;
51     my @query;
52
53     push(@query, $query);       # 1: push original.
54
55     $x = &normquery($query);
56     push(@query, $x) if ($x ne $query);
57     $query = $x;
58
59     $x = &switchPerson($query);
60     push(@query, $x) if ($x ne $query);
61     $query = $x;
62
63     $query =~ s/\s+at\s*(\?*)$/$1/;     # where is x at?
64     $query =~ s/^explain\s*(\?*)/$1/i;  # explain x
65     $query = " $query ";                # side whitespaces.
66
67     my $qregex = join '|', keys %{$lang{'qWord'}};
68
69     # what's whats => what is; who'?s => who is, etc
70     $query =~ s/ ($qregex)\'?s / $1 is /i;
71     if ($query =~ s/\s+($qregex)\s+//i) { # check for question word
72         $questionWord = lc($1);
73     }
74
75     if ($questionWord eq "" and $finalQMark and $addressed) {
76         $questionWord = "where";
77     }
78
79     # valid factoid.
80     if ($query =~ s/[\!\.]$//) {
81         &DEBUG("Question: Pushing query without trailing symbols.");
82         push(@query,$query);
83     }
84
85     for (my$i=0; $i<scalar(@query); $i++) {
86         $query = $query[$i];
87         $result = &getReply($query);
88         next if ($result eq "");
89
90         # 'see also' factoid redirection support.
91         if ($result =~ /^see( also)? (.*?)\.?$/) {
92             my $newr = &getReply($2);
93             $result  = $newr    if ($newr ne "");
94         }
95
96         if ($i != 0) {
97             &DEBUG("Question: guessed factoid correctly ($i) => '$query'.");
98         }
99
100         return $result;
101     }
102
103     ### TODO: Use &Forker(); move function to Freshmeat.pl.
104     if (&IsParam("freshmeatForFactoid")) {
105         &loadMyModule($myModules{'freshmeat'});
106         $result = &Freshmeat::showPackage($query);
107         return $result unless ($result eq $noreply);
108     }
109
110     ### TODO: Use &Forker(); move function to Debian.pl
111     if (&IsParam("debianForFactoid")) {
112         &loadMyModule($myModules{'debian'});
113         $result = &Debian::DebianFind($query);  # ???
114         ### TODO: debian module should tell, through shm, that it went
115         ###       ok or not.
116 ###     return $result unless ($result eq $noreply);
117     }
118
119     if ($questionWord ne "" or $finalQMark) {
120         # if it has not been explicitly marked as a question
121         if ($addressed and $reply eq "") {
122             &status("notfound: <$who> ".join(' :: ', @query));
123
124             return '' unless (&IsParam("friendlyBots"));
125
126             foreach (split /\s+/, $param{'friendlyBots'}) {
127                 &msg($_, ":INFOBOT:QUERY <$who> $query");
128             }
129         }
130     }
131
132     $reply;
133 }
134
135 1;