#!/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 new
{
	my $class = shift;

	my $self;

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

	return $self;
}


sub pblogin
{
	my $self = shift;

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

	my $rq = POST $self->{base}."/login.formproc", [
		tfAbonnementID => $girotelnummer,
		tfCodewoord => $girotelcode,
		tfGin => $gin,
		command => "0",
#		sBegin => strftime("%H:%M:%S:000", localtime),
#		x => "71",
#		y => "10",
	];


	my $response = $self->{ua}->request($rq);
	$self->{jar}->extract_cookies($response);
#print $response->as_string."\n\n\n";
	if ($response->is_redirect) {
		my $uri = url($response->header("Location"));
		$self->{pbpath} = $self->{base}.$uri->path;
		my $rq = GET $uri;

		$self->{jar}->add_cookie_header($rq);
		my $response = $self->{ua}->request($rq);
		$self->{jar}->extract_cookies($response);
#print $response->as_string."\n\n\n";
	}
}

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);
	$self->{jar}->extract_cookies($response);
#print $response->as_string."\n\n\n";

	return $response->content;
}

1
