]> git.donarmstrong.com Git - infobot.git/blobdiff - src/Modules/Dict.pl
- strictify
[infobot.git] / src / Modules / Dict.pl
index 3e613a58361425ed82cad08740a23e42dc36bab4..9c49a5bde7d94a96874fee637fac355cf3804ff1 100644 (file)
@@ -1,7 +1,7 @@
 #
 #  Dict.pl: Frontend to dict.org.
 #   Author: dms
-#  Version: v0.6b (19991224).
+#  Version: v0.6c (20000924).
 #  Created: 19990914.
 #
 
@@ -10,22 +10,31 @@ package Dict;
 use IO::Socket;
 use strict;
 
+#use vars qw(PF_INET);
+
 my $server     = "dict.org";   # need a specific host||ip.
-my $port       = 2628;
-my $proto      = getprotobyname('tcp');
 
 ###local $SIG{ALRM} = sub { die "alarm\n" };
 
 sub Dict {
     my ($query) = @_;
-###    return unless &main::loadPerlModule("IO::Socket");
-    my $socket = new IO::Socket;
+#    return unless &::loadPerlModule("IO::Socket");
+    my $socket = new IO::Socket;
+    my $port   = 2628;
+    my $proto  = getprotobyname('tcp');
     my @results;
 
+    for ($query) {
+       s/^[\s\t]+//;
+       s/[\s\t]+$//;
+       s/[\s\t]+/ /;
+    }
+
     # connect.
+# TODO: make strict-safe constants... so we can defer IO::Socket load.
     socket($socket, PF_INET, SOCK_STREAM, $proto) or return "error: socket: $!";
     eval {
-       alarm 15;
+       alarm 10;
        connect($socket, sockaddr_in($port, inet_aton($server))) or return "error: connect: $!";
        alarm 0;
     };
@@ -37,7 +46,7 @@ sub Dict {
        $socket->autoflush(1);  # required.
 
        my $num;
-       if ($query =~ /^(\d+)\s+/) {
+       if ($query =~ s/^(\d+)\s+//) {
            $num = $1;
        }
 
@@ -51,19 +60,23 @@ sub Dict {
 
        my $total = scalar @results;
 
-       if (defined $num and ($num > $total or $num < 0)) {
-           &msg($main::who, "error: choice in definition is out of range.");
+       if ($total == 0) {
+           $num = undef;
+       }
+
+       if (defined $num and ($num > $total or $num < 1)) {
+           &::msg($::who, "error: choice in definition is out of range.");
            return;
        }
 
        # parse the results.
        if ($total > 1) {
            if (defined $num) {
-               $retval = sprintf("[%d/%d] %s", $num, $total, $results[$num]);
+               $retval = sprintf("[%d/%d] %s", $num, $total, $results[$num-1]);
            } else {
                # suggested by larne and others.
                my $prefix = "Dictionary '$query' ";
-               $retval = &main::formListReply(1, $prefix, @results);
+               $retval = &::formListReply(1, $prefix, @results);
            }
        } elsif ($total == 1) {
            $retval = "Dictionary '$query' ".$results[0];
@@ -72,14 +85,14 @@ sub Dict {
        }
     }
 
-    &main::performStrictReply($retval);
+    &::performStrictReply($retval);
 }
 
 sub Dict_Wordnet {
     my ($socket, $query) = @_;
     my @results;
 
-    &main::status("Dict: asking Wordnet.");
+    &::status("Dict: asking Wordnet.");
     print $socket "DEFINE wn \"$query\"\n";
 
     my $def            = "";
@@ -98,14 +111,14 @@ sub Dict_Wordnet {
        } elsif (/^\s+(\S+ )?(\d+)?: (.*)/) {   # start of sub def.
            my $text = $3;
            $def =~ s/\s+$//;
-###        &main::DEBUG("def => '$def'.");
+###        &::DEBUG("def => '$def'.");
            push(@results, $def)                if ($def ne "");
            $def = $text;
 
            if (0) {    # old non-fLR format.
                $def = "$query $wordtype: $text" if (defined $text);
                $wordtype = substr($1,0,-1)     if (defined $1);
-###            &main::DEBUG("_ => '$_'.") if (!defined $text);
+###            &::DEBUG("_ => '$_'.") if (!defined $text);
            }
 
        } elsif (/^\s+(.*)/) {
@@ -115,7 +128,7 @@ sub Dict_Wordnet {
        }
     }
 
-    &main::status("Dict: wordnet: found ". scalar(@results) ." defs.");
+    &::status("Dict: wordnet: found ". scalar(@results) ." defs.");
 
     return if (!scalar @results);
 
@@ -126,7 +139,7 @@ sub Dict_Foldoc {
     my ($socket,$query) = @_;
     my @results;
 
-    &main::status("Dict: asking Foldoc.");
+    &::status("Dict: asking Foldoc.");
     print $socket "DEFINE foldoc \"$query\"\n";
 
     my $firsttime = 1;
@@ -159,7 +172,7 @@ sub Dict_Foldoc {
        $string .= $_." ";
     }
 
-    &main::status("Dict: foldoc: found ". scalar(@results) ." defs.");
+    &::status("Dict: foldoc: found ". scalar(@results) ." defs.");
 
     return if (!scalar @results);
     pop @results;      # last def is date of entry.