2 # Dict.pl: Frontend to dict.org.
4 # Version: v0.6c (20000924).
13 #use vars qw(PF_INET);
15 my $server = "dict.org"; # need a specific host||ip.
17 ###local $SIG{ALRM} = sub { die "alarm\n" };
21 # return unless &::loadPerlModule("IO::Socket");
22 my $socket = new IO::Socket;
24 my $proto = getprotobyname('tcp');
34 # TODO: make strict-safe constants... so we can defer IO::Socket load.
35 socket($socket, PF_INET, SOCK_STREAM, $proto) or return "error: socket: $!";
38 connect($socket, sockaddr_in($port, inet_aton($server))) or return "error: connect: $!";
43 if ($@ && $@ ne "alarm\n") { # failure.
44 $retval = "i could not get info from dict.org";
46 $socket->autoflush(1); # required.
49 if ($query =~ s/^(\d+)\s+//) {
54 push(@results, &Dict_Wordnet($socket,$query));
55 push(@results, &Dict_Foldoc($socket,$query));
58 print $socket "QUIT\n";
61 my $total = scalar @results;
67 if (defined $num and ($num > $total or $num < 1)) {
68 &::msg($::who, "error: choice in definition is out of range.");
75 $retval = sprintf("[%d/%d] %s", $num, $total, $results[$num-1]);
77 # suggested by larne and others.
78 my $prefix = "Dictionary '$query' ";
79 $retval = &::formListReply(1, $prefix, @results);
81 } elsif ($total == 1) {
82 $retval = "Dictionary '$query' ".$results[0];
84 $retval = "could not find definition for \002$query\002";
88 &::performStrictReply($retval);
92 my ($socket, $query) = @_;
95 &::status("Dict: asking Wordnet.");
96 print $socket "DEFINE wn \"$query\"\n";
105 if ($_ eq ".") { # end of def.
106 push(@results, $def);
107 } elsif (/^250 /) { # stats.
109 } elsif (/^552 no match/) { # no match.
111 } elsif (/^\s+(\S+ )?(\d+)?: (.*)/) { # start of sub def.
114 ### &::DEBUG("def => '$def'.");
115 push(@results, $def) if ($def ne "");
118 if (0) { # old non-fLR format.
119 $def = "$query $wordtype: $text" if (defined $text);
120 $wordtype = substr($1,0,-1) if (defined $1);
121 ### &::DEBUG("_ => '$_'.") if (!defined $text);
124 } elsif (/^\s+(.*)/) {
127 $def =~ s/\[.*?\]$//g;
131 &::status("Dict: wordnet: found ". scalar(@results) ." defs.");
133 return if (!scalar @results);
139 my ($socket,$query) = @_;
142 &::status("Dict: asking Foldoc.");
143 print $socket "DEFINE foldoc \"$query\"\n";
151 return if /^552 /; # no match.
154 $firsttime-- if ($_ eq "");
158 last if (/^250/ or /^\.$/); # stats; end of def.
160 s/^\s+|\s+$//g; # each line.
162 if ($_ eq "") { # sub def separator.
163 $string =~ s/^\s+|\s+$//g; # sub def.
164 $string =~ s/[{}]//g;
166 next if ($string eq "");
168 push(@results, $string);
175 &::status("Dict: foldoc: found ". scalar(@results) ." defs.");
177 return if (!scalar @results);
178 pop @results; # last def is date of entry.