Finance-Bank-IE-PermanentTSB

 view release on metacpan or  search on metacpan

lib/ptsb_util.pm  view on Meta::CPAN

package 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'},



( run in 0.920 second using v1.01-cache-2.11-cpan-e1769b4cff6 )