2 # Dict.pl: Frontend to dict.org.
4 # Version: v0.6c (20000924).
6 # Updates: Copyright (c) 2005 - Tim Riker <Tim@Rikers.org>
8 # see http://luetzschena-stahmeln.de/dictd/
9 # for a list of dict servers
16 #use vars qw(PF_INET);
18 # need a specific host||ip.
19 my $server = "dict.org";
24 # return unless &::loadPerlModule("IO::Socket");
26 my $proto = getprotobyname('tcp');
37 # TODO: make strict-safe constants... so we can defer IO::Socket load.
38 my $socket = new IO::Socket;
39 socket( $socket, PF_INET, SOCK_STREAM, $proto )
40 or return "error: socket: $!";
42 local $SIG{ALRM} = sub { die 'alarm' };
44 connect( $socket, sockaddr_in( $port, inet_aton($server) ) )
45 or die "error: connect: $!";
52 $retval = "i could not get info from $server '$@'";
55 $socket->autoflush(1); # required.
58 if ( $query =~ s/^(\d+)\s+// ) {
62 if ( $query =~ s/\/(\S+)$// ) {
67 push( @results, &Define( $socket, $query, $dict ) );
69 #push(@results, &Define($socket,$query,'foldoc'));
70 #push(@results, &Define($socket,$query,'web1913'));
73 print $socket "QUIT\n";
79 &::DEBUG("$count: $_");
81 my $total = scalar @results;
87 if ( defined $num and ( $num > $total or $num < 1 ) ) {
88 &::msg( $::who, "error: choice in definition is out of range." );
96 sprintf( "[%d/%d] %s", $num, $total, $results[ $num - 1 ] );
100 # suggested by larne and others.
101 my $prefix = "Dictionary '$query' ";
102 $retval = &::formListReply( 1, $prefix, @results );
105 elsif ( $total == 1 ) {
106 $retval = "Dictionary '$query' " . $results[0];
109 $retval = "could not find definition for \002$query\002";
110 $retval .= " in $dict" if ( $dict ne '*' );
114 &::performStrictReply($retval);
118 my ( $socket, $query, $dict ) = @_;
121 &::DEBUG("Dict: asking $dict.");
122 print $socket "DEFINE $dict \"$query\"\n";
131 &::DEBUG("$term/$dict '$_'");
139 # end w/ optional stats
142 elsif (/^151 "([^"]*)" (\S+) .*/) {
144 # 151 "Good Thing" jargon "Jargon File (4.3.0, 30 APR 2001)"
148 &::DEBUG("term=$term dict=$dict");
153 # some dicts put part of the definition on the same line ie: jargon
154 $line =~ s/^$term//i;
156 if ( $dict eq 'wn' ) {
158 # special processing for sub defs in wordnet
159 if ( $line eq '.' ) {
163 $def =~ s/\[[^\]]*\]//g;
164 push( @results, $def );
166 elsif ( $line =~ m/^\s+(\S+ )?(\d+)?: (.*)/ ) {
172 #&::DEBUG("def => '$def'.");
173 $def =~ s/\[[^\]]*\]//g;
174 push( @results, $def ) if ( $def ne '' );
181 &::DEBUG("ignored '$line'");
186 # would be nice to divide other dicts
187 # but many are not always in a parsable format
188 if ( $line eq '.' ) {
191 next if ( $def eq '' );
192 push( @results, $def );
195 elsif ( $line =~ m/^\s+(\S.*\S)\s*$/ ) {
197 #&::DEBUG("got '$1'");
198 $def .= ' ' if ( $def ne '' );
202 &::DEBUG("ignored '$line'");
208 &::DEBUG( "Dict: $dict: found " . scalar(@results) . " defs." );
210 return if ( !scalar @results );
217 # vim:ts=4:sw=4:expandtab:tw=80