our
$VERSION
=
'0.16'
;
our
$AUTHORITY
=
'cpan:SACHINJSK'
;
sub
get_contacts {
my
(
$self
,
$email
,
$password
) =
@_
;
$self
->errstr(
undef
);
my
@contacts
;
my
$ua
=
$self
->ua;
$self
->debug(
"start get_contacts from Rediff"
);
$email
=~ /(.*)@.*/;
my
$username
= $1;
$self
->submit_form(
form_name
=>
'loginform'
,
fields
=> {
login
=>
$username
,
passwd
=>
$password
,
},
) ||
return
;
my
$content
=
$ua
->content();
if
(
$content
=~ /Your login failed/ig) {
$self
->errstr(
'Wrong Username or Password'
);
return
;
}
$self
->debug(
'Login OK'
);
$ua
->follow_link(
url_regex
=>
qr/login=/
i );
my
$link
=
$ua
->follow_link(
url_regex
=>
qr/folder=inbox/
i );
$content
=
$ua
->content;
if
(
$content
=~ /new Rediffmail/ig) {
$link
=
$ua
->follow_link(
text_regex
=>
qr/new Rediffmail/
i );
}
my
$base_link
=
$link
->base();
$base_link
=~ /(.*)\?.
*session_id
=(.*?)&/;
my
$base_url
= $1;
my
$session_id
= $2;
$self
->get(
"$base_url?do=downaddrbook&login=$username&session_id=$session_id&service=thunderbird"
);
my
$address_content
=
$ua
->content();
@contacts
= get_contacts_from_thunderbird_csv(
$address_content
);
return
wantarray
?
@contacts
: \
@contacts
;
}
sub
get_contacts_from_thunderbird_csv {
my
(
$csv
) =
shift
;
my
@contacts
;
my
@lines
=
split
(/\n/,
$csv
);
foreach
my
$line
(
@lines
) {
$line
=~ s/"//g;
my
@cols
=
split
(
','
,
$line
);
push
@contacts
, {
name
=>
$cols
[2],
email
=>
$cols
[4]
};
}
return
wantarray
?
@contacts
: \
@contacts
;
}
no
Moose;
__PACKAGE__->meta->make_immutable;
1;