Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 343 lines (269 sloc) 9.35 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
#!/usr/bin/perl
# A simple currency converter using Locale::Object::Currency::Converter.
# For irssi :)
#
# A word of warning:
# If you have a packaging system, PLEASE USE IT to install the required
# modules. Loading this into irssi without all required modules might
# result in erratic behaviour.
#
# If you're going to use cpan, something like this should suffice:
#
# cpan> force install Locale::Object::Currency
# cpan> force install Locale::Object::Currency::Converter
# cpan> force install Finance::Currency::Convert::Yahoo
#
# TODO:
# - Allow users in a channel to query? Or? Hm.
# - the $Public printing crap is really stupid.
#
# Settings:
#
# /set currency_allow_external=<0 or 1> [not yet used of course]
# /set currency_convert_internally=<0 or 1> (1 is faster)
# /set currency_converter=<Yahoo or XE> (Yahoo is better)
#
#
# Forking code heavily inspired by dns.pl v2.1.1 by inch <inch@stmpd.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Changes:
# 0.3:
# - Added granularity cutoff
# - Workaround (suggested by mauke) for suppressing carp warnings in
# Locale::Object::Currency in order to avoid ugly console output requiring
# a /redraw.
# - Placed most Currency conversion code in an eval BLOCK in case
# Locale::Object::Currency screws up (it's somewhat flaky) -
# if a dependency is missing we only find out in realtime..
# This also catches common errors such as invalid currency codes.
# - Added check to verify that the exchange rate is numeric
# (Locale::Object::Currency just returns an arbitrary error string
# if it fails here)
#
use warnings;
use strict;

use constant {
    'VERSION' => '0.3', # Small doesn't mean bad!
    'GRANULARITY' => 6
};

use POSIX;
use Irssi;
use Irssi::Irc;

use Locale::Object::Currency;
use Locale::Object::Currency::Converter;

## Global
use vars qw(%IRSSI);

# Dirty dirty
our ($InternalConvert, $AllowExternal, $ConverterSite) = (1, 0, 'Yahoo');
our ($Converter, $PipeTag);
our ($Public, $Waiting) = (0, 0);
our ($WindowItem, $Server);

my %IRSSI = (
        authors => 'gammy',
        contact => 'gamAPPLEmy at pulappleia dot nu without fruits',
        name => "Simple currency converter",
        license => 'GPLv2',
        url => 'http://www.pulia.nu',
);

## Functions

# This is really stupid :(
sub pPrint{
    my $Text = shift;

    $Public = 0 if index $Text, 'Failure' != -1;

    if($Public){
        if("$WindowItem" eq '0'){
            Irssi::active_win->print("Your active window is not public; printing locally.");
            Irssi::active_win->print($Text);
            return;
        }
        my $WindowType = $WindowItem->{'type'};
        if($WindowItem && ($WindowType eq 'CHANNEL' || $WindowType eq 'QUERY')){
            $WindowItem->command('MSG ' . $WindowItem->{'name'} . ' ' . $Text);
        }else{
            Irssi::active_win->print("Can't post to window type $WindowType!");
            Irssi::active_win->print($Text);
        }
    }else{
        Irssi::active_win->print($Text);
    }
}

sub initConverter{

    undef($Converter);
    # I am using Yahoo because I noticed that XE only give .2 in accuracy
    # on the rate, resulting in quite a disrepency if you want to calculate
    # the rate on your own based on its result. Yahoo is much more accurate.
    our $Converter =
        Locale::Object::Currency::Converter->new(service => $ConverterSite);

}

sub help{

    $Public = 0;

    pPrint 'Simple currency converter v' . VERSION;
    pPrint 'SETTINGS';
    pPrint 'currency_allow_external = ' . $AllowExternal;
    pPrint 'currency_convert_internally = ' . $InternalConvert;
    pPrint 'currency_converter = ' . $ConverterSite;
    pPrint 'HELP';
    pPrint '/set currency_allow_external=<0/1> (1 allows external queries)';
    pPrint '/set currency_convert_internally=<0/1> (1 is faster)';
    pPrint '/set currency_converter=<Yahoo/XE> (Yahoo is better)';
    pPrint '-o: Send output to active channel/query';
    pPrint 'USAGE';
    pPrint '/currency [-o] 100[ ]USD [to|in] EUR';
    pPrint 'EXAMPLES';
    pPrint '/currency 100usd in eur';
    pPrint '/currency -o 100 USD to EUR';
    pPrint '/currency 100.203USD in EUR';
    pPrint '.. and so on';

}

sub loadSettings{

    $AllowExternal = Irssi::settings_get_bool('currency_allow_external');
    $InternalConvert = Irssi::settings_get_bool('currency_convert_internally');
    $ConverterSite = Irssi::settings_get_str('currency_converter');

    initConverter();

}

sub pipeInput{
    my $ReadHandle = shift;

    my $Text = <$ReadHandle>;

    close($ReadHandle);
    Irssi::input_remove($PipeTag);

    $PipeTag = -1;

    pPrint($Text); # FIXME

    $Waiting = 0;
}

sub getConversion{
    my ($Amount, $OrigCurrency, $DestCurrency) = @_;

    my ($ReadHandle, $WriteHandle);
    pipe($ReadHandle, $WriteHandle);

    my $PID = fork();

    if(! defined($PID)){
        $Public = 0;
        pPrint("fork() failed!");
        close($ReadHandle);
        close($WriteHandle);
        return;
    }

    $Waiting = 1;

    if($PID > 0){ # Parent
        close($WriteHandle); # No write needed
        
        Irssi::pidwait_add($PID);

        $PipeTag = Irssi::input_add(
            fileno($ReadHandle),
            INPUT_READ,
            \&pipeInput,
            $ReadHandle
        );

        return;
    }
        
    # Child
    my $Text;

    eval{
        local $SIG{__WARN__} = sub {}; # Workaround hack to suppress internal
                                       # carp warnings - they ruin the curses
                                       # window...

        my $FromCurrency = Locale::Object::Currency->new(code=>$OrigCurrency);
        die "'$OrigCurrency': invalid currency" if ! $FromCurrency;

        my $ToCurrency = Locale::Object::Currency->new(code=>$DestCurrency);
        die "'$DestCurrency': invalid currency" if ! $DestCurrency;

        $Converter->from($FromCurrency);
        $Converter->to($ToCurrency);

        my $ResultAmount = -1;
        my $Rate = $Converter->rate;
        unless($Rate=~/^\d*(\.\d*|)$/){
            die 'Conversion rate variable is not numeric - ' .
                'you might be missing a few modules.';
        }

        if(! $InternalConvert){
            $ResultAmount = $Converter->convert($Amount),
        }else{
            $ResultAmount = ($Amount * $Rate);
        }

        # Adjust granularity a bit
        my $AdjustAmount = sprintf('%.'.GRANULARITY.'f', $ResultAmount);
        $ResultAmount = '~'.$AdjustAmount if "$ResultAmount" ne $AdjustAmount;

        undef($FromCurrency);
        undef($ToCurrency);
    
        my ($YY,$MM,$DD) = (localtime($Converter->timestamp))[5,4,3];
        my $HumanDate = sprintf("%02d%02d%02d", (1900+$YY)-2000, ++$MM, $DD);

        $Text = "$Amount $OrigCurrency = $ResultAmount $DestCurrency | Rate: $Rate ($HumanDate)";


    };

    if($@){
        $Text = "Failure: $@";
    }

    eval {
       print $WriteHandle $Text;
       close($WriteHandle);
   };

   POSIX::_exit(1);
}



# Won't catch errors if input is in the style of '100sek usd ass'..meh
sub parseInput{

    my $Input = uc shift;
    $Input =~tr/,/./;
    $Input =~s/IN//;
    $Input =~s/TO//;
    $Input =~s/\s+/ /g;

    my ($Amount, $From, $To);
    my @Segments = split / /, $Input;

    return -1 if @Segments != 2 && @Segments != 3;

    $To = pop @Segments; # Output currency should always be last

    if(@Segments == 1){ # presuming Amount and Input currency are grouped
        if($Segments[0] =~m/^([^a-z]*?)([a-z]+)/i){
            ($Amount, $From) = ($1, $2);
        }else{
            return -1;
        }
    }else{
        ($Amount, $From) = @Segments;
    }


    return ($Amount, $From, $To);
}

sub handleInput{
    my $Input = shift;
    ($Server, $WindowItem) = @_;

    if($Waiting > 0){
        $Public = 0;
        pPrint("Previous request not yet finished. Please wait.");
        $Public = 1;
        return;
    }

    my $Opt = substr($Input, 0, 3);
    if("$Opt" eq '-o '){
        $Public = 1;
        substr($Input, 0, 3, '');
    }else{
        $Public = 0;
    }
    
    my @Info = parseInput($Input);

    if(@Info != 3){
        help();
    }else{
        getConversion(@Info);
    }
    
}

## Init

# Defaults
Irssi::settings_add_bool('misc', 'currency_allow_external' , 0);
Irssi::settings_add_bool('misc', 'currency_convert_internally', 1);
Irssi::settings_add_str( 'misc', 'currency_converter' , 'Yahoo');

# Load user settings
loadSettings();

# Bindings
Irssi::command_bind('currency', 'handleInput');

# Signals
#Irssi::signal_add_first('server incoming', \&handleIncoming);
Irssi::signal_add('setup changed', 'loadSettings');

help();
Something went wrong with that request. Please try again.