#!/usr/bin/perl -w
# Btbill - Retrieve BT Bill
# When a e-mail arrives anouncing that a bill is ready go to the
# web-site, logon, retrieve and print it. Not as simple as it sounds;
# authentication is required, plus the bill is not immediately ready and
# requires a polling mechanism.

# 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 (version 3).
     
# 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.
 
# For a copy of the GNU General Public License see
# <http://www.gnu.org/licenses/>.

# Author: Martin Sullivan.  ZOIS Ltd., Stag House, 55 Kirkgate,
# Cockermouth CA13 9PH Email: sullivan@zois.co.uk

# $Id: btbill.pl,v 1.1 2007/11/01 13:57:14 sullivan Exp sullivan $

use strict;
use LWP::UserAgent;
use Mail::Internet;
#use LWP::Debug qw(+);

my $password = "";		# A password is required. Fill it in here.

my $ua = LWP::UserAgent->new ();
push @{ $ua->requests_redirectable }, 'POST';
                                # BT depends on redirects after POST
$ua->cookie_jar({});		# and lots of dumb to dubious cookie stuff
$ua->timeout (360);		# double it

my $mail = Mail::Internet->new (\*STDIN);
my $body = $mail->as_string ();

my $mylatestbill;
my $eid;
if ($body =~ /(https:\/\/www2.bt.com\/mylatestbill\?eid=\d+)/) {
	$mylatestbill = $1;
	$mylatestbill =~ /eid=(\d+)/;
	$eid = $1;
} else {
	die "Can't get bill URL:" . $mail->head->get ("Subject");
} # else

my $user;
if ($body =~ /BT.com Username : (\S+)/) {
	$user = $1;
} else {
	die "Can't get user:" . $mail->head->get ("Subject");
} # else

my @datetime = localtime ();
my $filename = $ENV{"HOME"} . "/btbill/" . $user . "-" 
	. (sprintf "%d-%02d-%02d", $datetime[5] + 1900, 
	$datetime[4] + 1, $datetime[3]) . ".pdf";

my $doc;
$doc = $ua->get ($mylatestbill) or die $doc->status_line;

# The above provokes a login page, which is filled in with the
# following (strings require $eid, found above).

my $login = "https://www2.bt.com:443/siteminderagent/forms/login.fcc"
	. "?pageid=my_account&siteArea=con.mya&eid=$eid&"
	. "ebpp_event=latest_bill&pageid=null&";

my $target = "https://www2.bt.com/btPortal/application"
	. "?namespace=security&origin=login.jsp&"
	. "event=link.smlogin&pageid=my_account&siteArea=con.mya&"
	. "eid=$eid&ebpp_event=latest_bill&pageid=null&";

$doc = $ua->post ($login, {
	'target' => $target,
	'smauthreason' => "0",
	'USER' => $user,
	'PASSWORD' =>$password
}) or die $doc->status_line;

# Then ask for a PDF bill. This seems to provoke a semi-background process
# that producess a bill in PDF and eventually displays a page inviting
# you to download it. In the meantime: wait, reload, refresh.

my $ebp = "https://www2.bt.com/eBillPayments/";
my $pdf_request = $ebp
	. "page_cssbilldownload.css.do"
	. "?viewName=css_billdownload&amp;viewType=HTML";

$doc = $ua->get ($pdf_request) or die $doc->status_line;

if ($doc->content =~ /Request whole bill download/) {
my $pdf_initial_request = $ebp
	. "page_cssrequestdetailbill.pdf.do"
	. "?viewName=css_billdet_pdf";
	$doc = $ua->get ($pdf_initial_request) or die $doc->status_line;
} # if
		
my $ix = 1;
until ($doc->content =~ /Download invoice in PDF/) {
	sleep (10 * $ix);
	$doc = $ua->get ($pdf_request) or die $doc->status_line;
	if ($ix++ > 6) {
		open FH, '>/tmp/btbill.html';
		print FH $doc->content;
		close FH;
		die "Gave up trying to get PDF: " 
			. $mail->head->get ("Subject");
	} # if
} # until

# Finally, after all that, retrieve the bill ...

my $pdf_download = $ebp
	. "page_cssdownloaddetailbill.pdf.do"
	. "?viewName=ccs_billdet_pdf";

$doc = $ua->get ($pdf_download, ':content_file' => $filename)
	or die $doc->status_line;

`pdftops $filename - | lpr`;

0;

