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+//) {
58 push(@results, &Dict_Wordnet($socket,$query));
59 push(@results, &Dict_Foldoc($socket,$query));
60 push(@results, &Dict_web1913($socket,$query));
63 print $socket "QUIT\n";
66 my $total = scalar @results;
72 if (defined $num and ($num > $total or $num < 1)) {
73 &::msg($::who, "error: choice in definition is out of range.");
80 $retval = sprintf("[%d/%d] %s", $num, $total, $results[$num-1]);
82 # suggested by larne and others.
83 my $prefix = "Dictionary '$query' ";
84 $retval = &::formListReply(1, $prefix, @results);
86 } elsif ($total == 1) {
87 $retval = "Dictionary '$query' ".$results[0];
89 $retval = "could not find definition for \002$query\002";
93 &::performStrictReply($retval);
97 my ($socket, $query) = @_;
100 &::status("Dict: asking Wordnet.");
101 print $socket "DEFINE wn \"$query\"\n";
110 &::DEBUG("got '$_'");
111 if ($_ eq ".") { # end of def.
112 push(@results, $def);
113 } elsif (/^250 /) { # stats.
115 } elsif (/^552 no match/) { # no match.
117 } elsif (/^\s+(\S+ )?(\d+)?: (.*)/) { # start of sub def.
120 ### &::DEBUG("def => '$def'.");
121 push(@results, $def) if ($def ne "");
124 if (0) { # old non-fLR format.
125 $def = "$query $wordtype: $text" if (defined $text);
126 $wordtype = substr($1,0,-1) if (defined $1);
127 ### &::DEBUG("_ => '$_'.") if (!defined $text);
130 } elsif (/^\s+(.*)/) {
133 $def =~ s/\[.*?\]$//g;
137 &::status("Dict: wordnet: found ". scalar(@results) ." defs.");
139 return if (!scalar @results);
145 my ($socket,$query) = @_;
148 &::status("Dict: asking Foldoc.");
149 print $socket "DEFINE foldoc \"$query\"\n";
156 &::DEBUG("got '$_'");
157 return if /^552 /; # no match.
159 last if (/^250/ or /^\.$/); # stats; end of def.
161 s/^\s+|\s+$//g; # each line.
163 if ($_ eq "") { # sub def separator.
164 $string =~ s/^\s+|\s+$//g; # sub def.
165 $string =~ s/[{}]//g;
167 next if ($string eq "");
169 push(@results, $string);
176 &::status("Dict: foldoc: found ". scalar(@results) ." defs.");
178 return if (!scalar @results);
179 pop @results; # last def is date of entry.
185 my ($socket,$query) = @_;
188 &::status("Dict: asking web1913.");
189 print $socket "DEFINE web1913 \"$query\"\n";
196 return if /^552/; # no match.
198 last if (/^250/); # stats; end of def.
199 next if (/^151/ or /^150/); # definition and/or retrieval
201 s/^\s+|\s+$//g; # each line.
203 if ($_ eq "" or $_ =~ /^\.$/) { # sub def separator.
204 $string =~ s/^\s+|\s+$//g; # sub def.
205 $string =~ s/[{}]//g;
207 next if ($string eq "");
209 push(@results, $string);
216 &::status("Dict: web1913: found ". scalar(@results) ." defs.");
218 return if (!scalar @results);
219 pop @results; # last def is date of entry.