Finance-Bank-IE-PermanentTSB-0.4/0000755000175000017500000000000011146533335017176 5ustar pallotronpallotronFinance-Bank-IE-PermanentTSB-0.4/ptsb0000755000175000017500000001152111146533144020072 0ustar pallotronpallotron#!/usr/bin/perl # this is a shell command the user can use to deal with his homebanking use FindBin; use lib "$FindBin::RealBin/lib"; # load modules from "lib" subdir relative to this script use ptsb_util; use Data::Dumper; use strict; use warnings; # main program sub main { my $cf = {}; # parse and validate options ptsb_util::parse_options($cf); ptsb_util::validate_options($cf); print "Parsing configuration file...\n"; # parse config file ($cf->{open24_num}, $cf->{pass}, $cf->{pan}) = ptsb_util::parse_configfile($cf->{cfgfile}); print STDERR Dumper($cf) if($cf->{debug}); # execute options if($cf->{balance}) { print "\nConnecting...\n\n"; ptsb_util::balance($cf); } if($cf->{statement}) { print "\nConnecting...\n\n"; ptsb_util::statement($cf); } # exit exit 0; } main; __END__ =head1 NAME ptsb - Interact with you Permanent TSB homebanking from your shell! =head1 SYNOPSIS ptsb [options] =head2 Print accounts balance: =over =item C ] -b> =back =head2 Print account statement: =over =item C ] -s -a c -n <4digits> -f yyyy/mm/dd -t yyyy/mm/dd> =back If your are running Linux you can use the F command to simplify the date range definition. You can put these aliases on your F<~/.bashrc>: alias ptsb_stat='ptsb --statement -a c -n xyzt' alias ptst_last_month="statement_current -f `date +%Y/%m/%d --date='1 month ago'` -t `date +%Y/%m/%d`" =head1 OPTIONS =over 8 =item B<-h | --help> this help usage message! =item B<-D | --debug> Enable debug. This will be more verbose and will leave html on the current working directory. =item B<-F F | --file F> filename configuration file path (default: F<~/.ptsbrc>). Please refer to manual page to see how this file is built. =item B<-b | --balance> print account balance =item B<-s | --statement> print account statement =item B<-T type | --transation-type type> Type of transaction to look on the statement. Type can be: ALL, WITHDRAWAL, DEPOSIT =item B<-f | --from--date yyyy/mm/dd> from date =item B<-t | --to-date yyyy/mm/dd> to date =item B<-a [c | v] | --account-type [c | v]> account type: can be 'c' (current account) or 'v' for (visa card) =item B<-n digits | --account-num digits> 4 digits representing the last 4 digits of the account number or visa card number. =item B<-N | --no-balance> Do not print balance when printing statement. =item B<-r | --regexp> When printing statement grep using the regexp provided =item B<-e | --expr> where expression is a mathematical expression like '<300' '=300' '>=300' the expression will test the euro amount column only lines matching the expression will be printed =item B<-g | --graph> Print graph of the statement using gnuplot. Gnuplot is required! =item B<-i F | --image F> Save PNG image in path. =item B<-o F | --output F> Save the output of the statement inside a CSV file named F. CSV format is: date,description,amount,balance last field presence depends on the -N flag. =item B<-v | --version> Print version =back =head1 CONFIGURATION FILE Configuration file default location is F<~/.ptsbrc> and it looks like this: -------8< -- ~/.ptsbrc -->8---------- open24_number=your_open24_number password=your_internet_password pan=your_personal_access_number -------8< --------------->8---------- However you can put wherever you want. Just remember to use then the option -F F. Lines beginning with # will be interpreted as comment. Starting from release 0.08 ptsb will use GnuPG to encrypt the configuration file using the private key of the user. Therefore you'll have to create your key if you didn't do this yet. In case you want to decrypt your ptsbrc file use this command: $ gpg -d /path/to/your/ptsbrc This will print the encrypted file on the standard output. =head1 SEE ALSO =over =item * B L =item * B L =item * B L =item * B L =back =head1 AUTHOR Angelo "pallotron" Failla - =head1 BUGS Please report bugs to the author, no bug tracking system is set up yet. =head1 COPYRIGHT AND LICENSE Copyright (C) 2009 by Angelo "pallotron" Failla This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available. Finance-Bank-IE-PermanentTSB-0.4/COPYING0000644000175000017500000000065511141071370020226 0ustar pallotronpallotronAUTHOR Angelo "pallotron" Failla, - - COPYRIGHT AND LICENSE Copyright (C) 2009 by Angelo "pallotron" Failla This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available. Finance-Bank-IE-PermanentTSB-0.4/Changes0000644000175000017500000000442211146522016020465 0ustar pallotronpallotronRevision history for Perl extension Finance::Bank::IE::PermanentTSB. 0.4 - Implement CSV export 0.2 and 0.3 Fri Jan 30 13:49:07 GMT 2009 - Implemented graphs using gnuplot Graph can be viewed and saved on the filesystem using the -i and -g options. 0.1 Thu Jan 29 23:11:58 GMT 2009 - exit gracefully when open24.ie site is on mantainance - create package ptsb_util to remove all the functions from the main CLI utility - ptsb new option: -e | --expression where expression is a mathematical expression like '<300' '=300' '>=300' the expression will test the euro amount column only lines matching the expression will be printed 0.09 Wed Jan 28 13:23:04 GMT 2009 - starting defining test units for both module and CLI 0.08 Tue Jan 27 12:16:44 GMT 2009 - ~/.ptsbrc file now encrypted using gnupg GnuPG and a priv/pub is now required to use ptsb 0.07 Tue Jan 27 00:13:00 GMT 2009 - FIXED in PermanentTSB.pm: duplicated values on the array of statement row. last element of statement page was the same of the fist one of the next page. fucking permanent tsb web page! :) - ptsb: printing intial and final balance in statement() (note: put this feature in PermanentTSB.pm?) 0.06 Mon Jan 26 22:09:56 GMT 2009 - added --regexp feature to the ptsb script - added date check to account_statement() from_date should be > of to_date - added date chedk: from_date cannot be in the future - added option -v to ptsb 0.05 Sun Jan 25 16:00:00 GMT 2009 - account_statement() prototype has been changed - account type constants have been introduced - created CLI utility called ptsb that can be used by a user to interact with his/her homebanking account. this is still working in progress... 0.04 Sun Jan 25 23:40:00 GMT 2009 - minor changes 0.03 Sun Jan 25 01:41:41 GMT 2009 - implemented function account_statement() - perlpod documentation improved - starting migration to git 0.02 Wed Jan 21 15:19:23 GMT 2009 - check_balance implemented 0.01 Thu Jan 15 14:58:59 2009 - original version; created by h2xs 1.23 with options -AX Finance::Bank::IE::PermanentTSB Finance-Bank-IE-PermanentTSB-0.4/TODO0000644000175000017500000000032311146533333017662 0ustar pallotronpallotronTODO ==== - convert to OOP rather than functional programming - mobile_topup and money_transfer (with check >50) - Graphical Interface? IN PROGRESS =========== - test units (using Mock Objects) in progress Finance-Bank-IE-PermanentTSB-0.4/MANIFEST0000644000175000017500000000035311141071440020315 0ustar pallotronpallotronChanges Makefile.PL MANIFEST README t/Finance-Bank-IE-PermanentTSB.t lib/Finance/Bank/IE/PermanentTSB.pm lib/Finance/Bank/IE/templates/gnuplot_print.gp lib/ptsb_util.pm ptsb TODO META.yml #Module meta-data (added by MakeMaker) COPYING Finance-Bank-IE-PermanentTSB-0.4/lib/0000755000175000017500000000000011146533335017744 5ustar pallotronpallotronFinance-Bank-IE-PermanentTSB-0.4/lib/ptsb_util.pm0000644000175000017500000003617711146533010022313 0ustar pallotronpallotronpackage ptsb_util; =head1 NAME ptsb_util - functions used by the Finance::Bank::IE::PermanentTSB CLI This is a package which contains a set of functions used by the CLI utility. =cut use Getopt::Long; use Date::Calc qw(check_date); use Switch; use File::Copy; use Data::Dumper; use FindBin; use lib "$FindBin::RealBin/lib"; use Finance::Bank::IE::PermanentTSB; use strict; use warnings; sub usage { use Pod::Usage; pod2usage(-verbose =>1); } # parse config file to retrieve acc_no, password and pan sub parse_configfile { my $cfgfile = shift; my $gpg = `which gpg`; chop $gpg; if($gpg =~ /not found/ or not -x $gpg) { print "You need to install and use GnuPG to secure your config file\n"; print "Please see the documentation on \n". "http://search.cpan.org/~pallotron/Finance-Bank-IE-PermanentTSB/ptsb\n"; exit -1; } # encryption dance # use the 'file' command to check the cfgfile my $res = `file $cfgfile`; if($res !~ /GPG encrypted data/is) { # not encrypted: encrypt it! print("Config file not encrypted. I'm gonna encrypt it!\n"); print("Executing gpg.. \n"); print("You'll have to type the name of the key you want to use\n"); system('gpg -e '.$cfgfile); # checking exit status if($? != 0 ) { # problem with gpg? print "Exiting...\n"; exit -1; } # If file has been create overwrite the original one if(-e $cfgfile.'.gpg') { move($cfgfile.'.gpg', $cfgfile); } # now the config file is crypted! } # decrypt file in memory my @res = `gpg -d $cfgfile`; # go thru the lines... my($user, $pass, $pan); foreach my $line (@res) { $line =~ s/\n//g; $_ = $line; if(not /^\s{0,}#/ or not /^\s{0,}/) { my ($key, $val) = split '='; $val =~ s/^\s+//; $val =~ s/\s+$//; $user = $val if($key eq 'open24_number'); $pass = $val if($key eq 'password'); $pan = $val if($key eq 'pan'); } } # die if some of the arguments are undef foreach my $i ($user, $pass, $pan) { if(not defined $i) { die ("invalid configuration file!"); } } return ($user, $pass, $pan); } sub parse_options { my $cf = shift; # set some defaults # default config file is in ~/.ptsbrc $cf->{cfgfile} = $ENV{HOME}."/.ptsbrc"; $cf->{no_balance} = 0; $cf->{statement_type} = 'all'; $cf->{image} = '/tmp/file.png.'.$$.'.'.time; # =o -> Extended integer, Perl style. This can be either an optional # leading plus or minus sign, followed by a sequence of digits, or # an octal string (a zero, optionally followed by '0', '1', .. '7'), # or a hexadecimal string (0x followed by '0' .. '9', 'a' .. 'f', # case insensitive), or a binary string (0b followed by a series of # '0' and '1'). # # =s -> string # =i -> integer my $error; # be case sensitive! Getopt::Long::Configure ("bundling"); # pase opts, put everything in the $cf hash_ref Getopt::Long::GetOptions( "file|f=s" => \$cf->{'cfgfile'}, "help|h" => \$cf->{'help'}, "debug|D" => \$cf->{'debug'}, "balance|b" => \$cf->{'balance'}, "statement|s" => \$cf->{'statement'}, "statement-type|T=s" => \$cf->{'statement_type'}, "from-date|f=s" => \$cf->{'fromdate'}, "to-date|t=s" => \$cf->{'todate'}, "account-type|a=s" => \$cf->{'acc_type'}, "account-num|n=s" => \$cf->{'acc_no'}, "no-balance|N" => \$cf->{'no_balance'}, "regexp|r=s" => \$cf->{'regexp'}, "version|v" => \$cf->{'version'}, "expr|e=s" => \$cf->{'expr'}, "i|image=s" => \$cf->{'image'}, "g|graph" => \$cf->{'graph'}, "o|output=s" => \$cf->{'csv'}, ) or $error = 1; usage if($error or $cf->{'help'}); usage if(defined $cf->{graph} and $cf->{graph} eq ''); } sub validate_options { my $cf = shift; # balance and statement cannot stay together if($cf->{balance} and $cf->{statement}) { print "You can not select -s and -b together!\n"; print "You can only select one operation at a time!\n"; exit -1; } if(defined $cf->{acc_type}) { # acc_type must be 'c' or 'v' switch ($cf->{acc_type}) { case "c" { $cf->{acc_type} = SWITCH_ACCOUNT; } case "v" { $cf->{acc_type} = VISA_ACCOUNT; } else { print("Account type invalid\n"); exit -1; } } } if(defined $cf->{statement_type}) { switch ($cf->{statement_type}) { case /WITHDRAWAL/is { $cf->{statement_type} = WITHDRAWAL; } case /DEPOSIT/is { $cf->{statement_type} = DEPOSIT; } case /ALL/is { $cf->{statement_type} = ALL; } else { print("Statement type invalid\n"); exit -1; } } } if(defined $cf->{acc_type}) { if($cf->{acc_type} =~ /VISA/is) { $cf->{no_balance} = 1; $cf->{statement_type} = ALL; } } if(defined $cf->{version}) { print Finance::Bank::IE::PermanentTSB->VERSION, "\n"; exit 0; } # all the other check are made by Finance::Bank::IE::PermanentTSB } sub print_statement_header { my $nobal = shift; if ($nobal) { for(0...54) { print("-"); } } else { for(0...68) { print("-"); } } print "\n"; my $coldate = "Date"; my $coldesc = "Description"; my $colam = "Amount"; my $colbal = "Balance"; printf("| %10s | %25s | %11s |", $coldate, $coldesc, $colam); printf(" %11s |", $colbal) if(not $nobal); print "\n"; if ($nobal) { for(0...54) { print("-"); } } else { for(0...68) { print("-"); } } print "\n"; } sub print_statement_footer { my $nobal = shift; if ($nobal) { for(0...54) { print("-"); } } else { for(0...68) { print("-"); } } print "\n"; } sub print_balance_header { my $colname = "Account name"; my $coldigit = "Acc. #"; my $colbal = "Balance"; my $colavail = "Available"; for(0...58) { print("-"); } print "\n"; printf("| %18s | %6s | %11s | %11s |", $colname, $coldigit, $colbal, $colavail); print "\n"; for(0...58) { print("-"); } print "\n"; } sub print_balance_footer { for(0...58) { print("-"); } print "\n"; } sub balance { my $cf = shift; my %config = ( "open24numba" => $cf->{open24_num}, "password" => $cf->{pass}, "pan" => $cf->{pan}, "debug" => $cf->{debug}, ); my $balance = Finance::Bank::IE::PermanentTSB->check_balance( \%config, $cf->{acc_type}, $cf->{acc_no}, $cf->{fromdate}, $cf->{todate}); if(defined $balance->[0]->{accno}) { if($balance->[0]->{accno} eq '') { print "Error while retrieving the content...\n"; return; } } else { exit -1; } print STDERR Dumper(\$balance) if($cf->{debug}); print_balance_header; foreach my $row (@$balance) { printf("| %18s | %6s | %11s | %11s |\n", $row->{accname}, $row->{accno}, $row->{accbal}, $row->{availbal}); } print_balance_footer; } sub statement { my $cf = shift; my $plot = shift; $plot ||= 0; my $counter_deposit = 0; my $counter_withdrawal = 0; my $initial_balance = 0; my $final_balance = 0; my $gnuplot_tmpfile; my $error = 0; my $csv = 0; my %config = ( "open24numba" => $cf->{open24_num}, "password" => $cf->{pass}, "pan" => $cf->{pan}, "debug" => $cf->{debug}, ); my $statement = Finance::Bank::IE::PermanentTSB->account_statement( \%config, $cf->{acc_type}, $cf->{acc_no}, $cf->{fromdate}, $cf->{todate}, $cf->{statement_type}, ); if(not defined $statement) { return; } print STDERR Dumper(\$statement) if($cf->{debug}); if(defined $cf->{csv}) { if(open(CSV, ">".$cf->{csv})) { $csv = 1; } else { print STDERR "Error opening file ".$cf->{csv}.": $!\n"; $csv = 0; } } print_statement_header($cf->{no_balance}); my $print = 1; #foreach my $row (@$statement) { for(my $i=0; $i[$i]; my $regex = $cf->{regexp}; my $expr = $cf->{expr}; if(defined $row->{description} and defined $cf->{regexp}) { if($row->{description} =~ /$regex/is) { $print = 1; } else { $print = 0; } } # matches the value of --expr if(defined $expr) { if($expr =~ /([<,>,=])\s*(\d*)/) { my $oper = $1; my $val = scalar($2); my $am = $row->{euro_amount}; $am =~ s/[\+-]//g; if($cf->{debug}) { print "\n"; print "am: '$am'\n"; print "1: '$oper'\n"; print "2: '$val'\n"; } switch ($oper) { case '<' { if ($am < $val) { $print = 1; } else { $print = 0; } } case '>' { if ($am > $val) { $print = 1; } else { $print = 0; } } case '=' { if ($am == $val) { $print = 1; } else { $print = 0; } } case '<=' { if ($am <= $val) { $print = 1; } else { $print = 0; } } case '>=' { if ($am >= $val) { $print = 1; } else { $print = 0; } } else { $print = 0; } } } } if($print) { printf("| %s | %25s | %11s ", $row->{date}, $row->{description}, $row->{euro_amount}, ); if(not $cf->{no_balance}) { printf "| %11s ", $row->{balance}; if ($statement->[($i+1)]->{date} ne $statement->[$i]->{date}) { $gnuplot_tmpfile .= $row->{date}."\t".$row->{balance}."\n"; } } print "|\n"; if($row->{euro_amount}<0) { $counter_withdrawal += $row->{euro_amount}; } else { $counter_deposit += $row->{euro_amount}; } if($csv) { printf CSV "%s,%s,%s", $row->{date}, $row->{description}, $row->{euro_amount}; print CSV ",", $row->{balance} if(not $cf->{no_balance}); print CSV "\n"; } } } close CSV if ($csv); $gnuplot_tmpfile .= $statement->[$#$statement]->{date}."\t". $statement->[$#$statement]->{balance}."\n"; print_statement_footer($cf->{no_balance}); print "\n=== Totals ===\n\n"; if($cf->{statement_type} == ALL or $cf->{statement_type} == DEPOSIT) { print "Total deposit: $counter_deposit\n"; } if($cf->{statement_type} == ALL or $cf->{statement_type} == WITHDRAWAL) { print "Total withdrawal: $counter_withdrawal\n"; } if($cf->{statement_type} == ALL) { printf("Profit: %f\n", $counter_deposit+$counter_withdrawal); } $initial_balance = $statement->[0]->{balance}; $initial_balance =~ s/[\+|-]//; $final_balance = $statement->[$#$statement]->{balance}; $final_balance =~ s/[\+|-]//; if(not $cf->{no_balance}) { print "\n"; print "Initial Balance: ", $initial_balance, "\n"; print "Final Balance: ", $final_balance, "\n"; printf "Delta Balance: %f\n", $final_balance-$initial_balance; } if(defined $cf->{graph} and $cf->{graph}) { # writing $gnuplot_tmpfile to file my $tmp_data = '/tmp/gnuplot_data'.$$.'.'.time; open(TMP, ">$tmp_data"); print TMP $gnuplot_tmpfile; close(TMP); # open lib/Finance/Bank/IE/templates/gnuplot_print.gp # an parse it replacing [% var %]: # # - [% ACCOUNT %] # - [% OUTPUT %] # - [% FILENAME %] # - [% TITLE %] # open the template and load it in @data open(GNUPLOT, "<$FindBin::RealBin/lib/Finance/Bank/IE/templates/gnuplot_print.gp"); my @data=; close(GNUPLOT); # define substitutions my $template_hash = { ACCOUNT => $cf->{acc_type}."-".$cf->{acc_no}, OUTPUT => $cf->{image}, FILENAME => $tmp_data, TITLE => $cf->{acc_no}, }; print Dumper($template_hash) if($cf->{debug}); # fill template for(my $i = 0; $i<$#data; $i++) { $data[$i] =~ s{\[%\s*([_a-zA-Z0-9]+)\s*%\]}{ _fill_template_element($template_hash, $1); }gsex; } # create the gnuplog command file my $gnu_print = '/tmp/gnuplot_print'.$$.'.'.time; open(TMP, ">$gnu_print"); foreach my $d (@data) { print TMP $d; } close(TMP); # calling gnuplot print "\n=== Plotting === \n"; print "Calling GnuPlot...\n"; my $res = `which gnuplot`; chop $res; if(!defined $res or not -e $res or not -x $res) { print STDERR "problems with gnuplot! maybe not installed or permission problem?\n"; $error = 1; } else { system("gnuplot $gnu_print"); print "done.\n"; } print "Cleaning up...\n"; # unlink the tmp files unlink ($tmp_data); unlink ($gnu_print); if($cf->{image} !~ /^\/tmp\/file\.png\.\d+\.\d+$/ and not $error){ print "PNG file has been created in ".$cf->{image}."\n"; } else { unlink ($cf->{image}); } } } sub _fill_template_element { my ($hash, $name) = @_; if (!defined $hash->{$name}) { warn "undefined template tag: [\% $name \%]\n"; return ''; } else { return $hash->{$name}; } } 1; Finance-Bank-IE-PermanentTSB-0.4/lib/Finance/0000755000175000017500000000000011146533335021307 5ustar pallotronpallotronFinance-Bank-IE-PermanentTSB-0.4/lib/Finance/Bank/0000755000175000017500000000000011146533335022162 5ustar pallotronpallotronFinance-Bank-IE-PermanentTSB-0.4/lib/Finance/Bank/IE/0000755000175000017500000000000011146533335022457 5ustar pallotronpallotronFinance-Bank-IE-PermanentTSB-0.4/lib/Finance/Bank/IE/templates/0000755000175000017500000000000011146533335024455 5ustar pallotronpallotronFinance-Bank-IE-PermanentTSB-0.4/lib/Finance/Bank/IE/templates/gnuplot_print.gp0000644000175000017500000000076711140633572027721 0ustar pallotronpallotron# Gnuplot script to graph the balance # Expected format for data is 2 columns: # # Date Balance # # Each column data is separated by space or tabulation # Date should be in format dd/mm/yyyy set title '[% ACCOUNT %]' set xlabel 'Date'; set ylabel 'Balance'; set xdata time; set timefmt "%d/%m/%Y"; set grid set terminal png set output "[% OUTPUT %]" plot '[% FILENAME %]' using 1:2 title "[% TITLE %]" with linespoints; set terminal wxt replot set terminal dumb replot pause -1 "Hit return to exit" Finance-Bank-IE-PermanentTSB-0.4/lib/Finance/Bank/IE/PermanentTSB.pm0000644000175000017500000005752011142667730025333 0ustar pallotronpallotron=head1 NAME Finance::Bank::IE::PermanentTSB - Perl Interface to the PermanentTSB Open24 homebanking on L =head1 DESCRIPTION This is a set of functions that can be used in your Perl code to perform some operations with a Permanent TSB homebanking account. Features: =over =item * B: retrieves the balance for all the accounts you have set up (current account, visa card, etc.) =item * B: retrieves the statement for a particular account, in a range of date. =item * B (to be implemented): top up your mobile phone! =item * B (to be implemented): transfer money between your accounts or third party accounts. =back =cut package Finance::Bank::IE::PermanentTSB; our $VERSION = '0.4'; use strict; use warnings; use Data::Dumper; use WWW::Mechanize; use HTML::TokeParser; use Carp qw(croak carp); use Date::Calc qw(check_date Delta_Days); use base 'Exporter'; # export by default the check_balance function and the constants our @EXPORT = qw(check_balance ALL WITHDRAWAL DEPOSIT VISA_ACCOUNT SWITCH_ACCOUNT); our @EXPORT_OK = qw(mobile_topup account_statement); my %cached_cfg; my $agent; my $lastop = 0; my $BASEURL = "https://www.open24.ie/"; my $error = 0; =head1 CONSTANTS The constants below are used with the account_statement() function: =over =item * C: shortcut for (WITHDRAWAL and DEPOSIT); =item * C: shows only the WITHDRAWALs; =item * C: shows only the DEPOSITs; =item * C: the account refers to a Visa Card; =item * C: the account is a normal Current Account; =back =cut # constant to be used with the account_statement() function use constant { # statement types ALL => 0, # prints all the transactions # (WITHDRAWAL and DEPOSIT) WITHDRAWAL => 1, # shows only the WITHDRAWALs DEPOSIT => 2, # shows only the DEPOSITs # account types VISA_ACCOUNT => 'Visa Card', # visa card account SWITCH_ACCOUNT => 'Switch Current A/C', # switch current account }; =head1 METHODS / FUNCTIONS Every function in this module requires, as the first argument, a reference to an hash which contains the configuration: my %config = ( "open24numba" => "your open24 number", "password" => "your internet password", "pan" => "your personal access number", "debug" => 1, ); =head2 C<$boolean = login($config_ref)> - B B This function performs the login. It takes just one required argument, which is an hash reference for the configuration. The function returns true (1) if success or undef for any other state. If debug => 1 then it will dump the html page on the current working directory. Please be aware that this has a security risk. The information will persist on your filesystem until you reboot your machine (and /var/tmp get clean at boot time). =cut sub login { my $self = shift; my $config_ref = shift; my $content; $config_ref ||= \%cached_cfg; my $croak = ($config_ref->{croak} || 1); for my $reqfield ("open24numba", "password", "pan") { if (! defined( $config_ref->{$reqfield})) { if ($croak) { carp("$reqfield not there!"); return undef; } else { carp("$reqfield not there!"); return undef; } } } if(!defined($agent)) { $agent = WWW::Mechanize->new( env_proxy => 1, autocheck => 1, keep_alive => 10); $agent->env_proxy; $agent->quiet(0); $agent->agent('Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20071126 Fedora/1.5.0.12-7.fc6 Firefox/1.5.0.12' ); my $jar = $agent->cookie_jar(); $jar->{hide_cookie2} = 1; $agent->add_header('Accept' => 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'); $agent->add_header('Accept-Language' => 'en-US,en;q=0.5'); $agent->add_header( 'Accept-Charset' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7' ); $agent->add_header( 'Accept-Encoding' => 'gzip,deflate' ); } else { # simple check to see if the login is live # this based on Waider Finance::Bank::IE::BankOfIreland.pm! if ( time - $lastop < 60 ) { carp "Last operation 60 seconds ago, reusing old session" if $config_ref->{debug}; $lastop = time; return 1; } my $res = $agent->get( $BASEURL . '/online/Account.aspx' ); if ( $res->is_success ) { $content = $agent->content; if($agent->content =~ /ACCOUNT SUMMARY/is) { $lastop = time; carp "Short-circuit: session still valid" if $config_ref->{debug}; return 1; } } carp "Session has timed out, redoing login" if $config_ref->{debug}; } # retrieve the login page my $res = $agent->get($BASEURL . '/online/login.aspx'); $agent->save_content('./loginpage.html') if $config_ref->{debug}; # something wrong? if(!$res->is_success) { carp("Unable to get page!"); return undef; } # 2nd agent->content call $content = $agent->content; if($content =~ /Interruption Page/is) { carp "Cannot authenticate on Open24.ie: site mantainance"; $error = 1; return undef; } # page not found? if($content =~ /Page Not Found/is) { carp("HTTP ERROR 404: Page Not Found"); return undef; } # Login - Step 1 of 2 $agent->field('txtLogin', $config_ref->{open24numba}); $agent->field('txtPassword', $config_ref->{password}); # PermanentTSB website sucks... # there's no normal submit button, the "continue" button is a # link # that launches a Javascript function. This function sets # the __EVENTTARGET to 'lbtnContinue'. Here we are simulating this # bypassing the Javascript code :) $agent->field('__EVENTTARGET', 'lbtnContinue'); $res = $agent->submit(); # something wrong? if(!$res->is_success) { carp("Unable to get page!"); return undef; } $agent->save_content("./step1_result.html") if $config_ref->{debug}; # 3rd agent->content call $content = $agent->content; # Login - Step 2 of 2 if($content !~ /LOGIN STEP 2 OF 2/is) { carp("Problem 1 while authenticating!\nPlease don't retry ". "this 3 times in a row or you account will be locked!"); return undef; } else { if($content !~ /txtDigit/is) { carp("Problem 2 while authenticating!\nPlease don't retry ". "this 3 times in a row or you account will be locked!"); return undef; } print $content if($config_ref->{debug}); set_pan_fields($agent, $config_ref); $res = $agent->submit(); $agent->save_content("./step2_pan_result.html") if $config_ref->{debug}; print $content if($config_ref->{debug}); } return 1; } =head2 C - B B This is used for the second step of the login process. The web interface ask you to insert 3 of the 6 digits that form the PAN code. The PAN is a secret code that only the PermanentTSB customer knows. If your PAN code is 123234 and the web interface is asking for this: =over =item Digit no. 2: =item Digit no. 5: =item Digit no. 6: =back The function will fill out the form providing 2,3,4 respectively. This function doesn't return anything. =cut sub set_pan_fields { my $agent = shift; my $config_ref = shift; # 4th agent->content call my $p = HTML::TokeParser->new(\$agent->content()); # convert the pan string into an array my @pan_digits = (); my @pan_arr = split('',$config_ref->{pan}); # look for with ids "lblDigit1", "lblDigit2" and "lblDigit3" # and build an array # the PAN, Personal Access Number is formed by 6 digits. while (my $tok = $p->get_tag("span")){ if(defined $tok->[1]{id}) { if($tok->[1]{id} =~ m/lblDigit[123]/) { my $text = $p->get_trimmed_text("/span"); # normally the webpage shows Digit No. x # where x is the position of the digit inside # the PAN number assigne by the bank to the owner of the # account # here we are building the @pan_digits array push @pan_digits, $pan_arr[substr($text,10)-1]; } } } $agent->field('txtDigitA', $pan_digits[0]); $agent->field('txtDigitB', $pan_digits[1]); $agent->field('txtDigitC', $pan_digits[2]); $agent->field('__EVENTTARGET', 'btnContinue'); } =head2 C<@accounts_balance = check_balance($config_ref)> - B This function require the configuration hash reference as argument. It returns an reference to an array of hashes, one hash for each account. In case of error it return undef; Each hash has these keys: =over =item * 'accname': account name, i.e. "Switch Current A/C". =item * 'accno': account number. An integer representing the last 4 digits of the account. =item * 'accbal': account balance. In EURO. =back Here is an example: $VAR1 = { 'availbal' => 'euro amount', 'accno' => '0223', 'accbal' => 'euro amount', 'accname' => 'Switch Current A/C' }; $VAR2 = { 'availbal' => 'euro amount', 'accno' => '2337', 'accbal' => 'euro amount', 'accname' => 'Visa Card' }; The array can be printed using, for example, a foreach loop like this one: foreach my $acc (@$balance) { printf ("%s ending with %s: %s\n", $acc->{'accname'}, $acc->{'accno'}, $acc->{'accbal'} ); } =cut sub check_balance { my $self = shift; my $config_ref = shift; my $res; $config_ref ||= \%cached_cfg; my $croak = ($config_ref->{croak} || 1); $self->login($config_ref) or return undef; my $p = HTML::TokeParser->new(\$agent->content()); my $i = 0; my @array; my $hash_ref = {}; while (my $tok = $p->get_tag("td")){ if(defined $tok->[1]{style}) { if($tok->[1]{style} eq 'width:25%;') { my $text = $p->get_trimmed_text("/td"); if($i == 0) { $hash_ref = {}; $hash_ref->{'accname'} = $text; } if($i == 1) { $hash_ref->{'accno'} = $text; } if($i == 2) { $hash_ref->{'accbal'} = $text; } if($i == 3) { $hash_ref->{'availbal'} = $text; } $i++; if($i == 4) { $i = 0; push @array, $hash_ref; } } } } return \@array; } =head2 C<@account_statement = account_statement($config_ref, $acc_type, $acc_no, $from, $to, [$type])> - B This function requires 4 mandatory arguments, the 5th is optional. =over =item 1. B<$config_ref>: the hash reference to the configuration =item 2. B<$acc_type>: this is a constant: can be VISA_ACCOUNT or SWITCH_ACCOUNT =item 3. B<$acc_no>: this is a 4 digits field representing the last 4 digits of the account number (or Visa card number) =item 4. B<$from>: from date, in format dd/mm/yyyy =item 5. B<$to>: to date, in format dd/mm/yyyy =item 6. B<$type> (optional): type of statement (optional). Default: ALL. It can be WITHDRAWAL, DEPOSIT or ALL. =back The function returns an reference to an array of hashes, one hash for each row of the statement. The array of hashes can be printed using, for example, a foreach loop like this one: foreach my $row (@$statement) { printf("%s | %s | %s | %s \n", $row->{date}, $row->{description}, $row->{euro_amount}, $row->{balance}); } Undef is returned in case of error; =cut sub account_statement { my ($self, $config_ref, $acc_type, $acc_no, $from, $to, $type) = @_; my ($res, @ret_array); $config_ref ||= \%cached_cfg; my $croak = ($config_ref->{croak} || 1); if(defined $acc_type) { if($acc_type ne SWITCH_ACCOUNT and $acc_type ne VISA_ACCOUNT) { carp("Account type is invalid"); return undef; } } else { carp("Account type not defined"); return undef; } if(not defined $acc_no) { carp("Account number not defined."); return undef; } my $account = $acc_type." - ".$acc_no; if(defined $from and defined $to) { # $from should be > of $to my @d_from = split "/", $from; my @d_to = split "/", $to; if (Delta_Days($d_from[0],$d_from[1],$d_from[2], $d_to[0],$d_to[1],$d_to[2]) <= 0) { carp("Date range $from -> $to invalid."); return undef; } # check date_from, date_to foreach my $date ($from, $to) { # date should be in format yyyy/mm/dd if(not $date =~ /^\d{4}\/\d{2}\/\d{2}$/) { carp("Date $date should be in format 'yyyy/mm/dd'"); return undef; } # date should be valid, this is using Date::Calc->check_date() my @d = split "/", $date; if (not check_date($d[0],$d[1],$d[2])) { carp("Date $date is not valid!"); return undef; } } } else { carp("Date range not defined"); return undef; } if(defined $account) { if(not $account =~ m/.+ - \d{4}$/) { carp("$account is invalid"); return undef; } } # verify if the account exists inside the homebanking my $acc = $self->check_balance($config_ref); if(not defined $acc) { return undef; } my $found = 0; foreach my $c (@$acc) { if($account eq $c->{'accname'}." - ".$c->{'accno'}) { $found = 1; last; } } if($found) { $self->login($config_ref) or return undef; # go to the Statement page $res = $agent->get($BASEURL . '/online/Statement.aspx'); $agent->save_content("./statement_page.html") if $config_ref->{debug}; $agent->field('ddlAccountName', $account); $agent->field('__EVENTTARGET', 'lbtnShow'); $res = $agent->submit(); # something wrong? if(!$res->is_success) { carp("Unable to get page!"); return undef; } $agent->save_content("./statement_page2.html") if $config_ref->{debug}; # fill out the "from" date my @d = split "/", $from; $agent->field('ddlFromDay', $d[2]); $agent->field('ddlFromMonth', $d[1]); $agent->field('ddlFromYear', $d[0]); # fill out the "to" date @d = split "/", $to; $agent->field('ddlToDay', $d[2]); $agent->field('ddlToMonth', $d[1]); $agent->field('ddlToYear', $d[0]); if(defined $type) { $agent->field('grpTransType', 'rbWithdrawal') if($type == WITHDRAWAL); $agent->field('grpTransType', 'rbDeposit') if($type == DEPOSIT); } $agent->field('__EVENTTARGET', 'lbtnShow'); $res = $agent->submit(); # something wrong? if(!$res->is_success) { carp("Unable to get page!"); return undef; } $agent->save_content("./statement_page1.html") if $config_ref->{debug}; my $content = $agent->content; # PermanentTSB doesn't support statements that include data # older than 6 months... in this case the interface will reset # to the default date range. We just need to print an warning # and submit the current form as is if($content =~ /YOU HAVE REQUESTED DATA OLDER THAN 6 MONTHS/is) { carp("PermanentTSB doesn't support queries older than 6". " months! Resetting to the default date."); $agent->field('__EVENTTARGET', 'lbtnShow'); $res = $agent->submit(); if(!$res->is_success) { carp("Unable to get page!"); return undef; } $agent->save_content("./statement_res_after_6months.html") if $config_ref->{debug}; } if($content =~ /INCORRECT DATE CRITERIA ENTERED: 'TO DATE' WAS IN THE FUTURE./is) { carp("Incorrect date criteria entered: 'to date' was in the". " future! Resetting to the default date. "); } # parse output page clicking "next" button until the # button "another statement" is present. all the data must # be inserted into an array of hashes. # the array should contain an hash per row. # every hash contains [date, description, euro_amount, balance] my $hash_ref = {}; my $visa = 0; my $page = 1; my $i = 1; while (1) { my $p = HTML::TokeParser->new(\$agent->content()); while (my $tok = $p->get_tag('table')) { if(defined $tok->[1]{id}) { if($tok->[1]{id} eq 'tblTransactions'){ while(my $tok2 = $p->get_tag('tr')) { $hash_ref = {}; my $text = $p->get_trimmed_text('/tr'); #TODO: improve regexp! # this matches the html row # dd/mm/yyyy description [-/+] amount balance [-/+] # example -> 29/09/2008 DUNNES STEPHEN 29/09 - 45.00 25000.00 + if($text =~ /^(\d{2}\/\d{2}\/\d{4}) (.+) ([-\+] [\d\.]+) ([\d\.]+ [-\+])$/) { # this is a normal current account # statement $hash_ref->{date} = $1; $hash_ref->{description} = $2; $hash_ref->{euro_amount} = $3; $hash_ref->{balance} = $4; $hash_ref->{euro_amount} =~ s/\s//g; if($hash_ref->{balance} =~ /^([\d\.]+) ([-\+])$/) { if($2 eq '+') { $hash_ref->{balance} = "+".$1; } if($2 eq '-') { $hash_ref->{balance} = "-".$1; } } push @ret_array, $hash_ref if(($i==1 and $page == 1) or ($i>1 and $page>=1)); $i++; } if($text =~ /^(\d{2}\/\d{2}\/\d{4}) (\d{2}\/\d{2}\/\d{4}) (.+) ([-\+] [\d\.]+)$/) { # this is a visa card statement $visa = 1; $hash_ref->{date} = $1; $hash_ref->{description} = $3; $hash_ref->{euro_amount} = $4; $hash_ref->{euro_amount} =~ s/\s//g; push @ret_array, $hash_ref; # if(($i==1 and $page == 1) or ($i>1 and $page>=1)); $i++; } } } } } # if we are at the last page we will find a button called # Another Statement, exit from the while loop if($agent->content =~ /Another Statement/is) { last; } else { # the "next" buttons have different target names # it depdends if we are watching a visa card or a normal # current account if($visa) { $agent->field('__EVENTTARGET', 'lBtnAnother1'); } else { $agent->field('__EVENTTARGET', 'lbtnShow'); } $page++; $i = 1; } $res = $agent->submit(); $agent->save_content("./statement_page".($page-1).".html") if $config_ref->{debug}; # something wrong? if(!$res->is_success) { carp("Unable to get page!"); return undef; } } } else { # account doesn't exist in the homebanking interface # return undef carp("Account $account not found!"); return undef; } return \@ret_array; } # TODO: implement this sub funds_transfer { } # TODO: implement this sub mobile_topup { } sub logoff { my $self = shift; my $config_ref = shift; if(defined $agent and not $error) { my $res = $agent->get($BASEURL . '/online/DoLogOff.aspx'); $agent->save_content("./logoff.html") if $config_ref->{debug}; $agent->field('__EVENTTARGET', 'lbtnContinue'); $agent->submit; } } END { logoff; } 1; __END__ =head1 INSTALLATION To install this module type the following: perl Makefile.PL make make test make install =head1 DEPENDENCIES This module requires these other modules and libraries: WWW::Mechanize HTML::TokeParser Date::Calc =head1 MODULE HOMEPAGES =over =item * Project homepage on Google code (with SVN repository): L =item * Project homepage on CPAN.org: L =back =head1 SYNOPSIS use Finance::Bank::IE::PermanentTSB; my %config = ( "open24numba" => "your open24 number", "password" => "your internet password", "pan" => "your personal access number", "debug" => 1, # <- enable debug messages ); my $balance = Finance::Bank::IE::PermanentTSB->check_balance(\%config); if(not defined $balance) { print "Error!\n" exit; } foreach my $acc (@$balance) { printf ("%s ending with %s: %s\n", $acc->{'accname'}, $acc->{'accno'}, $acc->{'accbal'} ); } my $statement = Finance::Bank::IE::PermanentTSB->account_statement( \%config, SWITCH_ACCOUNT, '2667','2008/12/01','2008/12/31'); if(not defined $statement) { print "Error!\n" exit; } foreach my $row (@$statement) { printf("%s | %s | %s | %s |\n", $row->{date}, $row->{description}, $row->{euro_amount}, $row->{balance} ); } =head1 SEE ALSO =over =item * B - CLI tool to interact with your home banking L =item * Ronan Waider's C - L =back =head1 AUTHOR Angelo "pallotron" Failla, Epallotron@freaknet.orgE - L - L =head1 COPYRIGHT AND LICENSE Copyright (C) 2009 by Angelo "pallotron" Failla This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available. =cut Finance-Bank-IE-PermanentTSB-0.4/README0000644000175000017500000000371711140565541020064 0ustar pallotronpallotronNAME Finance::Bank::IE::PermanentTSB - Perl Interface to the PermanentTSB Open24 homebanking on DESCRIPTION This is a set of functions that can be used in your Perl code to perform some operations with a Permanent TSB homebanking account. Features: * account(s) balance: retrieves the balance for all the accounts you have set up (current account, visa card, etc.). * account(s) statement: retrieves the statement for a particular account, in a range of date.Graph supported using GnuPlot. * mobile phone top-up (to be implemented): top up your mobile phone! * funds transfer (to be implemented): transfer money between your accounts or third party accounts. Together with the module you can find a CLI utility to perform operation on your homebanking. DEPENDENCIES This module requires these other modules and libraries: WWW::Mechanize HTML::TokeParser Date::Calc Test::MockObject The ptsb CLI interface requires this 3rd party components: * GnuPlot (not mandatory, to graph) * GnuPG (mandatory, to secure the ~/.ptsbrc config file) MODULE HOMEPAGES * Project homepage on Google code (with SVN repository): * Project homepage on CPAN.org: SEE ALSO * Ronan Waider's "Finance::Bank::IE::BankOfIreland" - AUTHOR Angelo "pallotron" Failla, - - COPYRIGHT AND LICENSE Copyright (C) 2009 by Angelo "pallotron" Failla This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available. Finance-Bank-IE-PermanentTSB-0.4/Makefile.PL0000644000175000017500000000127011140433504021137 0ustar pallotronpallotronuse strict; use warnings; use ExtUtils::MakeMaker; WriteMakefile( NAME => 'Finance::Bank::IE::PermanentTSB', VERSION_FROM => 'lib/Finance/Bank/IE/PermanentTSB.pm', # finds $VERSION PREREQ_PM => { 'WWW::Mechanize' => 1.34, 'HTML::TokeParser' => 2.37, 'Date::Calc' => 5.4, 'Test::More' => 0.62, 'Test::MockObject' => 1.07, }, ABSTRACT_FROM => 'lib/Finance/Bank/IE/PermanentTSB.pm', AUTHOR => 'Angelo "pallotron" Failla ', EXE_FILES => [qw(ptsb)], dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, clean => { FILES => qw(t/tmpl) }, ); Finance-Bank-IE-PermanentTSB-0.4/META.yml0000644000175000017500000000103511146533335020446 0ustar pallotronpallotron# http://module-build.sourceforge.net/META-spec.html #XXXXXXX This is a prototype!!! It will change in the future!!! XXXXX# name: Finance-Bank-IE-PermanentTSB version: 0.4 version_from: lib/Finance/Bank/IE/PermanentTSB.pm installdirs: site requires: Date::Calc: 5.4 HTML::TokeParser: 2.37 Test::MockObject: 1.07 Test::More: 0.62 WWW::Mechanize: 1.34 distribution_type: module generated_by: ExtUtils::MakeMaker version 6.30_01 Finance-Bank-IE-PermanentTSB-0.4/t/0000755000175000017500000000000011146533335017441 5ustar pallotronpallotronFinance-Bank-IE-PermanentTSB-0.4/t/Finance-Bank-IE-PermanentTSB.t0000644000175000017500000000023211141071311024614 0ustar pallotronpallotronuse Test::MockObject; use Data::Dumper; use Test::More tests => 1; use strict; use warnings; BEGIN { use_ok('Finance::Bank::IE::PermanentTSB') };