#!/usr/bin/perl

use strict;
use warnings;
use LWP::UserAgent;
use Term::ReadPassword;
use JSON;

sub login {

    my $ua = shift;
    my $username = shift;

    $ua->cookie_jar( {} );

    $ua->get("https://internetbanking.caixa.gov.br/SIIBC/index.processa");

    my $action = [
		'swAction' => '7',
		'navegacao' => '1',
		'acessoExterno' => '',
		'imagemCaixa' => 'true',
		'compativel' => 'applet', #? 
		'idGBusterOK' => 'true',
		'versaoGBuster' => '1.2.7.0', # From gbas.jar
		'fluxoNormal' => 'true',
		'novaUrl' => 'https://internetbanking.caixa.gov.br/SIIBC',
		'url' => 'https://internetbanking.caixa.gov.br/SIIBC',
		'mgbasId' => '2222222222222222222222222222222222222222222222222222222222222222', #?
		'mgbasDna' => '', #?
		'mgbasSo' => '', #?
		'AppVersion' => 'SEF', #?
		'SOVersion' => 'FEZ', #?
		'extensaoChromeOk' => '',
		'usuario' => $username,
		'tpPessoa' => '1',
	];
    $ua->post("https://internetbanking.caixa.gov.br/SIIBC/siwinCtrl", $action);

    $action = [
		'swAction' => '7',
        'fluxoInstalacaoGB' => '',
		'navegacao' => '2',
		'idGBusterOK' => 'true',
		'isCertificadoDigital' => 'false',
        'usuario' => '',
		'tpPessoa' => '',
		'acessoExterno' => '',
    ];
	$ua->post("https://internetbanking.caixa.gov.br/SIIBC/siwinCtrl", $action);

    my $password = read_password('Senha: ');

	$action = [
		'swAction' => '7',
		'navegacao' => '3',
		'versaoJVM' => '0.0',
		'modSeg' => 'true',
		'encryptionkey' => '',
		'retornoF10' => '',
		'idMaquina' => '1111111111111111111111111111111111111111111111111111111111111111',
		'criptoId' => 'false',
		'aux1' => '',
		'85SenhaAtual' => $password,
	];
	$ua->post("https://internetbanking.caixa.gov.br/SIIBC/siwinCtrl", $action);

    my $config = encode_json {"session" => {}, "request" => {"contaSelecionada" => undef, "listContas" => undef}, "todos" => {}};
    $action = [
        'ajax' => 'true',
        'nocache' => time,
        'isPaginaInterna' => 'false',
        'config' => $config,
    ];
	my $txt = $ua->post("https://internetbanking.caixa.gov.br/SIIBC/selecionaConta.processa", $action);
    my $j = decode_json $txt->content;
    return $j->{'contaSelecionada'}->{'chaveCriptoF10'};
}

my $ua = LWP::UserAgent->new;
my $key = login($ua, $ARGV[0]);

sub saldo {
    my $ua = shift;
    my $config = encode_json {
        "request" => {
            "saldo_disponivel" => ["saldoDisponivel"],
            "saldo_total" => ["saldoTotal"],
            "sinal_saldo_total" => ["sinalSaldo"],
            "msgErro" => ["msgErro"],
        },
        "session" => {},
        "todos" => {},
    };
    my $action = [
        'ajax' => 'true',
        'nocache' => time,
        'isPaginaInterna' => 'false',
        'config' => $config,
    ];
    my $txt = $ua->post("https://internetbanking.caixa.gov.br/SIIBC/consulta_saldo.processa", $action);
    return $txt->content;
}

my $saldo = saldo($ua);
print $saldo . "\n";
$saldo = decode_json $saldo;
print $saldo->{saldoTotal} . "\n";

my $action = [ "aaccdlnrrv" => time, ];
#my $txt = $ua->post("https://internetbanking.caixa.gov.br/SIIBC/extrato.processa", $action);
#print $txt->content . "\n";

