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";
23 # return unless &::loadPerlModule("IO::Socket");
25 my $proto = getprotobyname('tcp');
36 # TODO: make strict-safe constants... so we can defer IO::Socket load.
37 my $socket = new IO::Socket;
38 socket($socket, PF_INET, SOCK_STREAM, $proto) or return "error: socket: $!";
40 local $SIG{ALRM} = sub { die 'alarm' };
42 connect($socket, sockaddr_in($port, inet_aton($server))) or die "error: connect: $!";
48 $retval = "i could not get info from $server '$@'";
50 $socket->autoflush(1); # required.
53 if ($query =~ s/^(\d+)\s+//) {
57 if ($query =~ s/\/(\S+)$//) {
62 push(@results, &Define($socket,$query,$dict));
63 #push(@results, &Define($socket,$query,'foldoc'));
64 #push(@results, &Define($socket,$query,'web1913'));
67 print $socket "QUIT\n";
73 &::DEBUG("$count: $_");
75 my $total = scalar @results;
81 if (defined $num and ($num > $total or $num < 1)) {
82 &::msg($::who, "error: choice in definition is out of range.");
89 $retval = sprintf("[%d/%d] %s", $num, $total, $results[$num-1]);
91 # suggested by larne and others.
92 my $prefix = "Dictionary '$query' ";
93 $retval = &::formListReply(1, $prefix, @results);
95 } elsif ($total == 1) {
96 $retval = "Dictionary '$query' ".$results[0];
98 $retval = "could not find definition for \002$query\002";
99 $retval .= " in $dict" if ($dict ne '*');
103 &::performStrictReply($retval);
107 my ($socket, $query, $dict) = @_;
110 &::DEBUG("Dict: asking $dict.");
111 print $socket "DEFINE $dict \"$query\"\n";
120 &::DEBUG("$term/$dict '$_'");
125 # end w/ optional stats
127 } elsif (/^151 "([^"]*)" (\S+) .*/) {
128 # 151 "Good Thing" jargon "Jargon File (4.3.0, 30 APR 2001)"
132 &::DEBUG("term=$term dict=$dict");
135 # some dicts put part of the definition on the same line ie: jargon
136 $line =~ s/^$term//i;
139 # special processing for sub defs in wordnet
143 $def =~ s/\[[^\]]*\]//g;
144 push(@results, $def);
145 } elsif ($line =~ m/^\s+(\S+ )?(\d+)?: (.*)/) {
149 #&::DEBUG("def => '$def'.");
150 $def =~ s/\[[^\]]*\]//g;
151 push(@results, $def) if ($def ne '');
153 } elsif (/^\s+(.*)/) {
156 &::DEBUG("ignored '$line'");
159 # would be nice to divide other dicts
160 # but many are not always in a parsable format
163 next if ($def eq '');
164 push(@results, $def);
166 } elsif ($line =~ m/^\s+(\S.*\S)\s*$/) {
167 #&::DEBUG("got '$1'");
168 $def .= ' ' if ($def ne '');
171 &::DEBUG("ignored '$line'");
177 &::DEBUG("Dict: $dict: found ". scalar(@results) ." defs.");
179 return if (!scalar @results);