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