1# /sana command, translates english-finnish-english.
2
3# BUGS: Doesn't handle UTF-8.
4
5use warnings;
6use strict;
7use HTML::Entities ();
8use Irssi ();
9use LWP::Simple ();
10
11use vars qw($VERSION %IRSSI);
12
13$VERSION = "0.1";
14%IRSSI = (
15    authors     => 'Johan "Ion" Kiviniemi, idea taken from Riku Voipio\'s sana.pl',
16    contact     => 'ion at hassers.org',
17    name        => 'sana-cmd',
18    description => '/sana command, translates english-finnish-english.',
19    license     => 'Public Domain',
20    url         => 'http://ion.amigafin.org/irssi/',
21    changed     => 'Sat Mar 16 06:20 EET 2002',
22);
23
24Irssi::command_bind(
25    'sana' => sub {
26        my @params = split /\s+/, shift;
27        unless (@params) {
28            Irssi::print("Sana: Usage: "
29                . (substr(Irssi::settings_get_str('cmdchars'), 0, 1) || "/")
30                . "sana word");
31            return;
32        }
33
34        my $word = $params[0];
35        $word =~ s/ /+/g;
36        $word =~ s/(\W)/'%' . unpack "H*", $1/eg;
37
38        if (my $content =
39            LWP::Simple::get(
40                'http://www.tracetech.net:8081/?word=' . $word))
41        {
42            $content = HTML::Entities::decode($content);
43            $content =~ s/\015?\012/ /g;
44            $content =~ s/<[^>]+>/ /g;     # Ugly, but it does the trick here.
45
46            my @words = $content =~ /(\S+)\s+(\(\S+?\))/g;
47
48            if (@words) {
49                Irssi::print("Sana: $word: @words");
50            } else {
51                Irssi::print("Sana: $word: No translations.");
52            }
53        } else {
54            Irssi::print("Sana failed.");
55        }
56    }
57);
58