| 1 | package WWW::Contact::BG::Mail; | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | extends 'WWW::Contact::Base'; | ||
| 5 | |||
| 6 | our $VERSION = '0.26'; | ||
| 7 | our $AUTHORITY = 'cpan:FAYLAND'; | ||
| 8 | |||
| 9 | use Text::vCard::Addressbook; | ||
| 10 | |||
| 11 | sub get_contacts { | ||
| 12 | my ($self, $email, $password) = @_; | ||
| 13 | |||
| 14 | # reset | ||
| 15 | $self->errstr(undef); | ||
| 16 | my @contacts; | ||
| 17 | |||
| 18 | my $ua = $self->ua; | ||
| 19 | $self->debug("start get_contacts from mail.bg"); | ||
| 20 | |||
| 21 | # to form | ||
| 22 | $self->get('https://mail.bg') || return; | ||
| 23 | $self->submit_form( | ||
| 24 | form_number => 1, | ||
| 25 | fields => { | ||
| 26 | imapuser => $email, | ||
| 27 | pass => $password, | ||
| 28 | } | ||
| 29 | ) || return; | ||
| 30 | my $content = $ua->content(); | ||
| 31 | if ($content =~ /=[\'\"]loginError/) { | ||
| 32 | $self->errstr('Wrong Username or Password'); | ||
| 33 | return; | ||
| 34 | } | ||
| 35 | |||
| 36 | $self->debug('Login OK'); | ||
| 37 | |||
| 38 | $self->get('http://mail.bg/base/addr/data.php?actionID=export&exportID=102&source=localsql') || return; | ||
| 39 | |||
| 40 | $content = $ua->content(); | ||
| 41 | |||
| 42 | @contacts = $self->get_contacts_from_vcard( $content ); | ||
| 43 | |||
| 44 | return wantarray ? @contacts : \@contacts; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub get_contacts_from_vcard { | ||
| 48 | my ($self, $content) = @_; | ||
| 49 | |||
| 50 | my $address_book = Text::vCard::Addressbook->new({ 'source_text' => $content, }); | ||
| 51 | |||
| 52 | my @contacts; | ||
| 53 | foreach my $vcard ( $address_book->vcards() ) { | ||
| 54 | push @contacts, { | ||
| 55 | name => $vcard->fullname(), | ||
| 56 | email => $vcard->email(), | ||
| 57 | }; | ||
| 58 | } | ||
| 59 | |||
| 60 | return @contacts; | ||
| 61 | } | ||
| 62 | |||
| 63 | no Moose; | ||
| 64 | __PACKAGE__->meta->make_immutable; | ||
| 65 | |||
| 66 | 1; | ||
| 67 | __END__ | ||
| 68 | |||
| 69 | =head1 NAME | ||
| 70 | |||
| 71 | WWW::Contact::BG::Mail - Get contacts/addressbook from mail.bg | ||
| 72 | |||
| 73 | =head1 SYNOPSIS | ||
| 74 | |||
| 75 | use WWW::Contact; | ||
| 76 | |||
| 77 | my $wc = WWW::Contact->new(); | ||
| 78 | my @contacts = $wc->get_contacts('itsa@mail.bg', 'password'); | ||
| 79 | my $errstr = $wc->errstr; | ||
| 80 | if ($errstr) { | ||
| 81 | die $errstr; | ||
| 82 | } else { | ||
| 83 | print Dumper(\@contacts); | ||
| 84 | } | ||
| 85 | |||
| 86 | =head1 DESCRIPTION | ||
| 87 | |||
| 88 | Get contacts from mail.bg. extends L<WWW::Contact::Base> | ||
| 89 | |||
| 90 | =head1 SEE ALSO | ||
| 91 | |||
| 92 | L<WWW::Contact>, L<WWW::Mechanize>, L<Text::vCard::Addressbook> | ||
| 93 | |||
| 94 | =head1 AUTHOR | ||
| 95 | |||
| 96 | Dimitar Petrov, C<< <mitko at datamax.bg> >> | ||
| 97 | |||
| 98 | =head1 COPYRIGHT & LICENSE | ||
| 99 | |||
| 100 | Copyright 2009 Dimitar Petrov, all rights reserved. | ||
| 101 | |||
| 102 | This program is free software; you can redistribute it and/or modify it | ||
| 103 | under the same terms as Perl itself. | ||
| 104 | |||
| 105 | =cut |