]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/spell.pl
* Msg user with "not found" status instead of reporting nil to the channel
[infobot.git] / src / Modules / spell.pl
1 #
2 #   spell.pl: interface to aspell/ispell/spell
3 #        Author: Tim Riker <Tim@Rikers.org>
4 #        Source: extracted from UserExtra
5 #  Licensing: Artistic License (as perl itself)
6 #       Version: v0.1
7 #
8 #  Copyright (c) 2005 Tim Riker
9 #
10
11 package spell;
12
13 use strict;
14
15 sub spell::spell {
16     my $query = shift;
17     if ( $query =~ m/[^[:alpha:]]/ ) {
18         return ('only one word of alphabetic characters supported');
19     }
20
21     my $binary;
22     my @binaries = ( '/usr/bin/aspell', '/usr/bin/ispell', '/usr/bin/spell' );
23
24     foreach (@binaries) {
25         if ( -x $_ ) {
26             $binary = $_;
27             last;
28         }
29     }
30
31     if ( !$binary ) {
32         return ('no binary found.');
33     }
34
35     if ( !&::validExec($query) ) {
36         return ('argument appears to be fuzzy.');
37     }
38
39     my $reply = "I can't find alternate spellings for '$query'";
40
41     foreach (`/bin/echo '$query' | $binary -a -S`) {
42         chop;
43         last if !length;    # end of query.
44
45         if (/^\@/) {        # intro line.
46             next;
47         }
48         elsif (/^\*/) {     # possibly correct.
49             $reply = "'$query' may be spelled correctly";
50             last;
51         }
52         elsif (/^\&/) {     # possible correction(s).
53             s/^\& (\S+) \d+ \d+: //;
54             my @array = split(/,? /);
55
56             $reply = "possible spellings for $query: @array";
57             last;
58         }
59         elsif (/^\+/) {
60             &::DEBUG("spell: '+' found => '$_'.");
61             last;
62         }
63         elsif (/^# (.*?) 0$/) {
64
65             # none found.
66             last;
67         }
68         else {
69             &::DEBUG("spell: unknown: '$_'.");
70         }
71     }
72
73     return ($reply);
74 }
75
76 sub spell::query {
77     &::performStrictReply( &spell(@_) );
78     return;
79 }
80
81 1;
82
83 # vim:ts=4:sw=4:expandtab:tw=80