#!/usr/bin/perl -w

package Girotel;

use strict;

use POSIX qw(strftime);

use LWP::UserAgent;
use HTTP::Request::Common qw(POST GET);
use LWP::Simple;
use URI::URL;
use HTTP::Cookies;

sub debugoutput {
	my ($title, $content) = @_;
	print "\n------------$title---------------\n";
	print $content;
	print "\n--------------------------------\n";
}

sub new
{
	my $class = shift;

	my $self;

	$self = bless {
		jar => HTTP::Cookies->new(),
		ua => LWP::UserAgent->new(),
		base => "https://www.p3.postbank.nl",
		debug => 0,
#		base => "http://girotel.itsme.nl",
		pbpath => undef,
	}, $class;
	$self->{ua}->env_proxy();

	return $self;
}


sub pblogin
{
	my $self = shift;

	my ($girotelnummer, $girotelcode, $gin) = @_;

# with an older version of gtonline (until 2000-3-5)
#   this was prefixed with 'tf' iso 'txt'
	my $rq = POST $self->{base}."/login.formproc", [
		txtAbonnementID => $girotelnummer,
		txtCodewoord => $girotelcode,
		txtGin => $gin,
		command => "0",
#		sBegin => strftime("%H:%M:%S:000", localtime),
#		x => "71",
#		y => "10",
	];


	my $response = $self->{ua}->request($rq);

	if ($self->{debug}) { debugoutput("login", $response->as_string); }

	$self->{jar}->extract_cookies($response);
	if ($response->is_redirect) {
		my $uri = url($response->header("Location"));
		$self->{pbpath} = $self->{base}.$uri->path;
		my $rq = GET $uri;

		if ($self->{debug}) { print "\npbpath=".$self->{pbpath}."\n"; }
		$self->{jar}->add_cookie_header($rq);
		my $response = $self->{ua}->request($rq);

		if ($self->{debug}) { debugoutput("part2", $response->as_string); }

		$self->{jar}->extract_cookies($response);
	}
}

sub pbrequest
{
	my $self = shift;

	my ($command, $rekeningnr) = @_;

	my $uri = url($self->{pbpath}."?command=$command&ba=~c=$rekeningnr~d=NLG~du=20000101~aw=20011231~e=Alle~bg=0&qx=true");

	my $rq = GET $uri;

	$self->{jar}->add_cookie_header($rq);
	my $response = $self->{ua}->request($rq);

	if ($self->{debug}) { debugoutput("request($command,$rekeningnr)", $response->as_string); }

	$self->{jar}->extract_cookies($response);

	return $response->content;
}

sub overzicht
{
	my $self = shift;

	my $uri = url($self->{pbpath}."?command=610");

	my $rq = POST $uri, [
		selPeriode => "28",
		selRekNummer => "Alle"
	];

	$self->{jar}->add_cookie_header($rq);
	my $response = $self->{ua}->request($rq);

	if ($self->{debug}) { debugoutput("overzicht", $response->as_string); }

	$self->{jar}->extract_cookies($response);

	return $response->content;
}


sub checkaccount 
{
	my $self = shift;

	my $uri = url($self->{pbpath}."?command=141&ba=~c=xxxxxxx~d=NLG~du=20000101~aw=20011231~e=Alle~bg=0&qx=true~w=xxxxxxx~ad=0|1~ae=415947893~ah=~ai=~aj=~ak=~ao=~as=~at=~iv=true~al=0~am=01~ar=27~aq=12~ap=2001~av=0~au=1");

	my $rq = GET $uri;

	$self->{jar}->add_cookie_header($rq);
	my $response = $self->{ua}->request($rq);
	$self->{jar}->extract_cookies($response);

	return $response->content;
}

sub geturl
{
	my $self = shift;

	my $uri = shift;

	my $rq = GET $uri;

	$self->{jar}->add_cookie_header($rq);
	my $response = $self->{ua}->request($rq);
	$self->{jar}->extract_cookies($response);

	return $response->content;
}


1
