]> git.donarmstrong.com Git - infobot.git/blob - src/Factoids/Question.pl
Initial revision
[infobot.git] / src / Factoids / Question.pl
1 # infobot :: Kevin Lenzo  (c) 1997
2
3 ##  doQuestion --
4 ##      if ($query == query) {
5 ##              return $value;
6 ##      } else {
7 ##              return NULL;
8 ##      }
9 ##
10 ##
11
12 if (&IsParam("useStrict")) { use strict; }
13
14 use vars qw($query $reply $finalQMark $nuh $result $talkok $who $nuh);
15 use vars qw(%infobots %forked);
16
17 sub doQuestion {
18     # my doesn't allow variables to be inherinted, local does.
19     # following is used in math()...
20     local($query) = @_;
21     local($reply) = "";
22     local $finalQMark = $query =~ s/\?+\s*$//;
23     $finalQMark += $query =~ s/\?\s*$//;
24
25     if (!defined $query or $query =~ /^\s*$/) {
26         &FIXME("doQ: query == NULL");
27         return '';
28     }
29
30     my $origQuery = $query;
31
32     my $questionWord    = "";
33
34     if (!$addressed) {
35         return '' unless ($finalQMark);
36
37         if (&IsParam("minVolunteerLength") == 0 or
38                 length($query) < $param{'minVolunteerLength'})
39         {
40             return '';
41         }
42     } else {
43         ### TODO: this should be caught in Process.pl?
44         return '' unless ($talkok);
45     }
46
47     # dangerous; common preambles should be stripped before here
48     if ($query =~ /^forget /i or $query =~ /^no, /) {
49         return 'NOREPLY' if (exists $infobots{$nuh});
50     }
51
52     # convert to canonical reference form
53     $query = &normquery($query);
54     $query = &switchPerson($query);
55
56     $query =~ s/\s+at\s*(\?*)$/$1/;     # where is x at?
57     $query =~ s/^explain\s*(\?*)/$1/i;  # explain x
58     $query = " $query ";                # side whitespaces.
59
60     my $qregex = join '|', keys %{$lang{'qWord'}};
61
62     # what's whats => what is; who'?s => who is, etc
63     $query =~ s/ ($qregex)\'?s / $1 is /i;
64     if ($query =~ s/\s+($qregex)\s+//i) { # check for question word
65         $questionWord = lc($1);
66     }
67
68     if ($questionWord eq "" and $finalQMark and $addressed) {
69         $questionWord = "where";
70     }
71
72     $query =~ s/^\s+|\s+$//g;
73
74     # valid factoid.
75     if (defined( $result = &getReply($query) )) {
76         # 'see also' factoid redirection support.
77         if ($result =~ /^see( also)? (.*?)\.?$/) {
78             my $newr = &getReply($2);
79             $result  = $newr    if ($newr ne "");
80         }
81
82         return $result if ($result ne "");
83
84         ### TODO: Use &Forker(); move function to Freshmeat.pl.
85         if (&IsParam("freshmeatForFactoid")) {
86             &loadMyModule($myModules{'freshmeat'});
87             $result = &Freshmeat::showPackage($query);
88             return $result unless ($result eq 'NOREPLY');
89         }
90
91         &DEBUG("Question: hrm... result => '$result'.");
92     }
93
94     if ($questionWord ne "" or $finalQMark) {
95         # if it has not been explicitly marked as a question
96         if ($addressed and $reply eq "") {
97             if ($origQuery eq $query) {
98                 &status("notfound: <$who> $origQuery");
99             } else {
100                 &status("notfound: <$who> $origQuery :: $query");
101             }
102
103             return '' unless (&IsParam("friendlyBots"));
104
105             foreach (split /\s+/, $param{'friendlyBots'}) {
106                 &msg($_, ":INFOBOT:QUERY <$who> $query");
107             }
108         }
109     }
110
111     $reply;
112 }
113
114 1;