#!/usr/bin/perl -w
use strict;
use LWP;
use Digest::SHA qw(sha1 sha1_hex sha1_base64);

my $inifile = "credentials.ini";
my $kb = "https://www.kb24.pl";
my $debug = 0;

print "KB24\n" if $debug;

my %cfg = ();
open(F, "<", $inifile) or die "cannot open $inifile ($!)\n";
while (<F>)
{
    chomp;

    next if /^#/; # comments
    next if /^\s*$/; # empty

    my ($n, $v) = split(/\s*=\s*/, $_, 2);
    $cfg{$n} = $v;
}
close(F);

my $browser = LWP::UserAgent->new(
    'agent' => 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.2) Gecko/20060420 Firefox/1.5.0.2',
    'cookie_jar' =>
        {
            file => "cookies.txt",
            autosave => 1,
            #ignore_discard => 1,
        },
);

my $url = $kb;

my $response = $browser->get($url);
die "Can't get $url -- ", $response->status_line unless $response->is_success;
die "Hey, I was expecting HTML, not ", $response->content_type unless $response->content_type eq 'text/html';

my $ticket = 0;
if($response->decoded_content =~ m/INPUT value="(\d+)" type="hidden" name="wartoscBiletu"/i)
{
    $ticket = $1;
}
else
{
    die "no ticket input";
}

my $lctx = 0;
if($response->decoded_content =~ m/INPUT value="([0-9a-f]+)" type="hidden" name="loginCtx"/i)
{
    $lctx = $1;
}
else
{
    die "no lctx input";
}

print "url($url)\n" if $debug;
print "login($cfg{id})\n" if $debug;
print "pin($cfg{pin})\n" if $debug;
print "ticket($ticket)\n" if $debug;
print "lctx($lctx)\n" if $debug;

my $cookies = $browser->cookie_jar->as_string;
print "cookies($cookies)\n"  if $debug;
my $pin4 = substr($cfg{pin},0,4);

sub hash($) { return sha1_hex(shift); }

my $password = hash($pin4);
my $signature = hash(hash($cfg{id}.$cfg{pin}).$ticket);

print "pass($password)\n" if $debug;
print "sig($signature)\n" if $debug;

############ login

$url = "$kb/Login?zaloguj=T";
$response = $browser->post( $url,
   [
     'typPrzegladarki' => 'NS',
     'w' => 'n',
     'login' => $cfg{id},
     'pin' => '', # removed $cfg{pin},
     'opisBledu' => '',
     'wartoscBiletu' => $ticket,
     'haslo' => $password,
     'podpis' => $signature,
     'loginCtx' => $lctx,
   ],
#   headerkey1 => value1,
   #headerkey2 => value2,
 );

$url = "$kb/KontekstPracy";
$response = $browser->get($url);

$url = "$kb/MonitOtp";
$response = $browser->get($url);

$url = "$kb/MonitOtp";
$response = $browser->post( $url,
   [
     'wyslij' => 't',
     'dalej' => '%A0%A0Dalej%A0%A0',
   ],
 );

############ ror

$url = "$kb/HistoriaRachunku?menu=t";
$response = $browser->get($url);

$url = "$kb/HistoriaRachunku";
$response = $browser->post( $url,
   [
     'operacje' => 'ostatnie',
     'dni' => '90',
     'dm' => 'd',
     'rozmiarPaczki' => '50',
     'Submit' => '++Szukaj++',
   ],
 );

$url = "$kb/HistoriaRachunku?z=csv";
$response = $browser->get($url);

open F, ">ror.csv";
print F $response->decoded_content;
close F;

############ visa

$url = "$kb/MojeKarty?menu=t";
$response = $browser->get($url);

$url = "$kb/MojeKarty?sk=1";
$response = $browser->get($url);

$url = "$kb/HistoriaOperacjiKarta?menu=t";
$response = $browser->get($url);

$url = "$kb/HistoriaOperacjiKarta";
$response = $browser->post( $url,
   [
     'operacje' => 'ostatnie',
     'dni' => '90',
     'dm' => 'd',
     'rozmiarPaczki' => '50',
   ],
 );

#open F, ">visa0.txt";
#print F $response->decoded_content;
#close F;

$url = "$kb/HistoriaOperacjiKarta?z=csv";
$response = $browser->get($url);

open F, ">visa.csv";
print F $response->decoded_content;
close F;

############ logout

$url = "$kb/Logout";
$response = $browser->get($url);
