#!/usr/bin/perl # # leo.pl Version 1.15b: English<->German Dictionary # # Author/Copyright (c) 2005: Christian Birchinger # All rights reserved. # # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, # publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, # subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE # FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. # require 5.002; use Socket; use Getopt::Std; # -p plaintext(no bold) / -i ircstyle bold / -h help / -n no header / -c getopts('iphnc:'); # display usage if no textstring is defined or -h requested if ($opt_h || ! $ARGV[0]) { ($basename = $0) =~ s/.*\///; print "Usage: $basename \[-pinh\] \[-c \\] \\n"; exit } if ($opt_n) {$noheader = 1;} if ( $opt_c =~ /\d+/ ) {$maxwords = $opt_c;} else {$maxwords = 5;} if ($opt_i) {$outputmode = 'irc';} elsif ($opt_p) {$outputmode = 'plain';} else {$outputmode = 'bold';} $words = join ' ',@ARGV; $owords = $words; $words =~ s/([^[a-zA-Z0..9])/uc(sprintf("%%%02lx", ord $1))/eg; if (!defined $noheader) { if ( $outputmode ne 'irc' ) { $| = 1; }; print "Searching for \"$owords\", max. output is $maxwords ... "; } $remote = "dict.leo.org" ; $port = 80; $iaddr = inet_aton($remote) || die "no such host: $remote"; $paddr = sockaddr_in($port, $iaddr); $proto = getprotobyname('tcp'); socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; connect(SOCK, $paddr) || die "connect: $!"; send SOCK, "GET /ende?lang=en\§Hdr=off\&search=${words}\r\n\r\n",0; while (chomp($line=) && ! $foundhits) { if ($line =~ /search result/) { $html = $line; ($foundhits) = ($line =~ /\s(\d+)\ssearch result/); if (!defined $noheader) {print "$foundhits matches found.\n";}; if ($foundhits < $maxwords) {$maxwords = $foundhits} } elsif ($line =~ /The dictionary does not contain any entries for/) { print "Search for \"$owords\" produced no results.\n"; exit } } close(SOCK); @output = split /\<\/TR\>/i, $html; splice (@output, 0, 4); for ($i=1; $i <= $maxwords; $i++){ $text = $output[$i]; $text =~ s/\<\/?(font.*?|I|sub|small|A.*?)\>//ig; $text =~ s/<(sup)>.*?<\/\1>//g; if ($outputmode eq 'plain') {$text =~ s/\<\/*B\>//ig;} elsif ($outputmode eq 'irc') {$text =~ s/\<\/*B\>/\002/ig;} else {$text =~ s/\/\033\[1m/g; $text =~ s/\<\/B\>/\033\[0m/ig;} ($english,$german) = $text =~ /"middle" width="43%">(.*?)<\/TD\>.*?"middle" width="43%">(.*?)<\/TD\>/i; $english =~ s/ \;/\ /g ; $german =~ s/ \;/\ /ig; if ($english =~ /\033\[0m/) {printf "%-36s %s\n", $english,$german; } elsif ($english =~ /\002/) {printf "%-30s %s\n", $english,$german; } else { printf "%-28s %s\n", $english,$german; } }