#!/usr/bin/perl # # dico.leo.pl Version 1.11: French<->German Dictionary # # Author/Copyright (c) 2004: Christian Birchinger # All rights reserved. # French version adapted by Niko Ehrenfeuchter # # 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/\ /%20/g; $words =~ s/ö/%F6/g; $words =~ s/ä/%E4/g; $words =~ s/ü/%FC/g; $words =~ s/Ö/%D6/g; $words =~ s/Ä/%C4/g; $words =~ s/Ü/%DC/g; if (!defined $noheader) { if ( $outputmode ne 'irc' ) { $| = 1; }; print "recherche du mot \"$owords\", indication de $maxwords mots max... "; } $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 /frde?lang=fr\&lp=frde\§Hdr=off\&search=${words}\r\n\r\n",0; while (chomp($line=) && ! $foundhits) { if ($line =~ /résultats trouvés/) { $html = $line; ($foundhits) = ($line =~ /\s(\d+)\srésultats trouvés/); if (!defined $noheader) {print "$foundhits résultats trouvés.\n";}; if ($foundhits < $maxwords) {$maxwords = $foundhits} } elsif ($line =~ /aucun résultat/) { print "\nla recherche du mot-clé \"$words\" ne donne aucun résultat.\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;} ($francais,$german) = $text =~ /"middle" width="43%">(.*?)<\/TD\>.*?"middle" width="43%">(.*?)<\/TD\>/i; $francais =~ s/ \;/\ /g ; $german =~ s/ \;/\ /ig; if ($francais =~ /\033\[0m/) {printf "%-36s %s\n", $francais,$german; } elsif ($francais =~ /\002/) {printf "%-30s %s\n", $francais,$german; } else { printf "%-28s %s\n", $francais,$german; } }