#!/usr/bin/perl -w

package MyUserAgent;
require LWP::UserAgent;
@ISA =qw(LWP::UserAgent);
sub redirect_ok {
    return 0;
}

package Hotmail;

use strict;

use POSIX qw(strftime);

use HTML::LinkExtor;
use HTTP::Request::Common qw(POST GET);
use LWP::Simple;
use URI;
use HTTP::Cookies;

use Dumpvalue;
my $dumper = new Dumpvalue;

sub new
{
	my $class = shift;

	my $self;

	$self = bless {
		jar => HTTP::Cookies->new(),
		ua => MyUserAgent->new(),
		loginurl=> "http://hotmail.itsme/cgi-bin/dologin",
        #loginurl=> "https://lc1.law5.hotmail.passport.com/cgi-bin/dologin",
        # "https://login.msnia.passport.com/ppsecure/post.asp";
	}, $class;
	$self->{ua}->env_proxy();
	$self->{ua}->agent("Mozilla/4.75 [en] (X11; U; Linux 2.2.17 i686)");
print "redir_ok : ", $self->{ua}->redirect_ok(), "\n";
	return $self;
}


sub hotmaillogin
{
	my $self = shift;

	my ($username, $password) = @_;

	my $rq = POST $self->{loginurl}, [
		login => $username,
		domain => "hotmail.com",
		svc => "mail",
		RemoteDAPost => "https://login.msnia.passport.com/ppsecure/post.asp",
		passwd => $password,
		enter => "Sign in",
		sec => "no",
		curmbox => "ACTIVE",
		js => "yes",     # set by javascript in login page
		_lang => "EN",
		beta => "",
		ishotmail => "1",
        mspp_shared=> "",   # set to 1 by javascript in loginpage, when 'sec=share'
		id => "2",
		fs => "1",
		cb => "_lang%3dEN",
		ct => time,
        ru => "http://lw2fd.hotmail.msn.com/cgi-bin/sbox",
	];

$rq->header("Accept"=> "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*");
$rq->header("Accept-Encoding"=> "gzip");
$rq->header("Accept-Language"=> "en");
$rq->header("Accept-Charset"=> "iso-8859-1,*,utf-8");

	my $response = $self->{ua}->request($rq);
	$self->{jar}->extract_cookies($response);
	print "=================login request============================\n";
    print $rq->as_string, "\n";
	print "=================login answer============================\n";
	print $response->as_string, "\n";
	print "\n";

#MSPAuth is different for each session
#MSPSec, MSPProf are always the same.

	while ($response->is_redirect) {
		my $url= URI->new($response->header("Location"), $response->base)->abs($response->base);
		$rq = GET $url;

$rq->header("Accept"=> "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*");
$rq->header("Accept-Encoding"=> "gzip");
$rq->header("Accept-Language"=> "en");
$rq->header("Accept-Charset"=> "iso-8859-1,*,utf-8");

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

		print "================login redirect request=============================\n";
        print $rq->as_string, "\n";
		print "================login redirect answer=============================\n";
		print $response->as_string, "\n";
    }
    my $refresh = $response->header("Refresh");
    $refresh =~ s/^0; url=//;
    $self->{mailboxurl} = "$refresh&utf8=0";
#a,t = different for each session
#p   = the same.
}

sub listmailbox
{
	my $self = shift;

	my $rq = GET $self->{mailboxurl};
    $rq->referer($self->{loginurl});
$rq->header("Accept"=> "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*");
$rq->header("Accept-Encoding"=> "gzip");
$rq->header("Accept-Language"=> "en");
$rq->header("Accept-Charset"=> "iso-8859-1,*,utf-8");

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

	while ($response->is_redirect) {
		my $url= URI->new($response->header("Location"), $response->base)->abs($response->base);
		$rq = GET $url;

$rq->header("Accept"=> "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png, */*");
$rq->header("Accept-Encoding"=> "gzip");
$rq->header("Accept-Language"=> "en");
$rq->header("Accept-Charset"=> "iso-8859-1,*,utf-8");

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

		print "================mail redirect request=============================\n";
        print $rq->as_string, "\n";
		print "================mail redirect answer=============================\n";
		print $response->as_string, "\n";
    }
}

1

