#!/usr/bin/perl use LWP::Simple; use strict; use Irssi; use vars qw($VERSION %IRSSI); my $feed = ""; %IRSSI = ( authors => 'Rob Mitchelmore', contact => 'rob@midworld.co.uk', name => 'Currency Convertor Script', description => 'Converts currencies', license => 'Common sense, folks. Share and enjoy.' ); my $url = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"; sub getcurr { my $content = get $url; if (!(defined $content)) { return 0; print "NO! NO! NO!"; } else { return $content; } } sub getRate($$) { my $stuff = shift; my $currency = shift; if ($stuff =~ //) { return $1; } } sub iterCurrs($) { my @fish = split /\n/, shift; my $currs = ""; foreach (@fish) { if ($_ =~ //) { if ($currs ne "") { $currs = $currs . ", " . $1; } else { $currs = $1; } } } return $currs; } sub toEuro($$$) { my $stuff = shift; my $currency = shift; my $amount = shift; my $rate = getRate($stuff, $currency); if ($rate != 0) { return $amount / getRate($stuff, $currency); } else { return 0; } } sub fromEuro($$$) { my $stuff = shift; my $currency = shift; my $amount = shift; return $amount * getRate($stuff, $currency); } sub fromto($$$$) { my $stuff = shift; my $from = shift; my $to = shift; my $amount = shift; $from =~ tr/[a-z]/[A-Z]/; $to =~ tr/[a-z]/[A-Z]/; my $val = $amount; if ($from ne "EUR") { $val = toEuro($stuff, $from, $amount); } if ($to ne "EUR") { $val = fromEuro($stuff, $to, $val); } return $val; } sub pt ($$) { my ($win, $data) = @_; if ($win) { $win->print($data); } else { Irssi::print($data); } } sub roundage($) { my $a = shift; return int($a * 1000) / 1000; } sub currconv { my ($data, $server, $win) = @_; my @whatever = split / /, $data; if (@whatever < 3) { pt $win, "Not enough parameters - requires "; } else { my ($amount, $from, $to) = @whatever; my $result = roundage(fromto($feed, $from, $to, $amount)); if (defined $result) { if ($amount == 0) { pt $win, "0 $from => 0 $to"; } elsif ($result == 0) { pt $win, "This currency does not exist. Use /curlist to get a list of available currencies."; } else { pt $win, "$amount $from => $result $to"; } } else { pt $win, "Something's gone a bit wrong, possibly I don't know that currency."; } } } sub listcurrs { my ($data, $server, $win) = @_; pt $win, "Currencies available: " . iterCurrs($feed); } sub refresh { $feed = getcurr; } $feed = getcurr; Irssi::command_bind cur => \&currconv; Irssi::command_bind curlist => \&listcurrs; Irssi::timeout_add(12 * 60 * 60 * 1000, \&refresh, undef);