blib/lib/WWW/Contact.pm

filter: Statementstmt 71.0% 59 / 83 Branchbran 50.0% 15 / 30 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 84.6% 11 / 13 Podpod 75.0% 3 / 4 totaltotal 65.7% 92 / 140 CC 21 SCAR 24.8
CC
21
Cov
65%
get_supplier_by_email
19.2
get_contacts
15.5
get_supplier_by_socialnetwork
6.0
CRAP 11.9
« blib/lib/WWW/Contact/Lycos.pm ↑ Summary blib/lib/WWW/Contact/BG/Mail.pm »
1 package WWW::Contact;
2  
3 8 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 our $VERSION = '0.50';
7 8 our $AUTHORITY = 'cpan:FAYLAND';
8  
9 8 has 'errstr' => ( is => 'rw', isa => 'Maybe[Str]' );
10 has 'supplier_pattern' => (
11 is => 'rw',
12 isa => 'ArrayRef',
13 auto_deref => 1,
14 2 ▶ default => sub { [] }
Subroutine __ANON__: called CC=1 SCAR=0.0
15 8 );
16 has 'known_supplier' => (
17 is => 'rw',
18 isa => 'HashRef',
19 auto_deref => 1,
20 default => sub {
21 {
22 2 ▶ 'gmail.com' => 'GoogleContactsAPI',
Subroutine __ANON__: called CC=1 SCAR=0.0
23 'ymail.com' => 'Yahoo',
24 'rocketmail.com' => 'Yahoo',
25 'rediffmail.com' => 'Rediffmail',
26 'aol.com' => 'AOL',
27 'indiatimes.com' => 'Indiatimes',
28 'lycos.com' => 'Lycos',
29  
30 # cn
31 '163.com' => 'CN::163',
32 'yeah.net' => 'CN::163',
33 'netease.com' => 'CN::163',
34 'popo.163.com' => 'CN::163',
35  
36 # Mail
37 'mail.com' => 'Mail',
38 'email.com' => 'Mail',
39 'iname.com' => 'Mail',
40 'cheerful.com' => 'Mail',
41 'consultant.com' => 'Mail',
42 'europe.com' => 'Mail',
43 'mindless.com' => 'Mail',
44 'earthling.com' => 'Mail',
45 'myself.com' => 'Mail',
46 'techie.com' => 'Mail',
47 'usa.com' => 'Mail',
48 'writeme.com' => 'Mail',
49  
50 # hotmail
51 'hotmail.com' => 'Hotmail',
52 'live.com' => 'Hotmail',
53 'compaq.net' => 'Hotmail',
54 'hotmail.co.jp' => 'Hotmail',
55 'hotmail.co.uk' => 'Hotmail',
56 'hotmail.de' => 'Hotmail',
57 'hotmail.fr' => 'Hotmail',
58 'hotmail.it' => 'Hotmail',
59 'messengeruser.com' => 'Hotmail',
60 'msn.com' => 'Hotmail',
61 'passport.com' => 'Hotmail',
62 'webtv.net' => 'Hotmail',
63 'live.co.uk' => 'Hotmail',
64  
65 # bg
66 'abv.bg' => 'BG::Abv',
67 'gbg.bg' => 'BG::Abv',
68 'gyuvectch.bg' => 'BG::Abv',
69 'mail.bg' => 'BG::Mail',
70 }
71 }
72 8 );
73  
74 has 'social_network' => (
75 is => 'rw',
76 isa => 'HashRef',
77 auto_deref => 1,
78 default => sub {
79 {
80 # Social networks.
81 2 ▶ 'plaxo' => 'Plaxo',
Subroutine __ANON__: called CC=1 SCAR=0.0
82 'Hotmail' => 'Hotmail',
83 'Gmail' => 'Gmail', # YYY? use GoogleContactsAPI?
84 }
85 }
86 8 );
87  
88 has 'supplier_args' => (
89 is => 'rw',
90 isa => 'HashRef',
91 2 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
92 8 );
93  
94 has 'resolve' => (
95 is => 'rw',
96 isa => 'HashRef',
97 2 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
98 8 );
99  
100 has 'resolve_domain' => (
101 is => 'ro',
102 isa => 'Net::DNS::Resolver',
103 lazy => 1,
104 default => sub {
105 0 ▶ require Net::DNS::Resolver;
Subroutine __ANON__: not called CC=1 SCAR=6.9
106 0 Net::DNS::Resolver->new;
107 },
108 8 );
109  
110 sub get_contacts {
111 2 ▶ my $self = shift;
Subroutine get_contacts: called CC=9 SCAR=27.4
Pod undocumented
112 2 my ( $email, $password, $social_network ) = @_;
113  
114 2 ▶ unless ( $email and $password ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($email and $password) 0 2 2
Condition33%1/3
$email and $password
!ll&&!rl&&r
002
MC/DC0%0/2
$email and $password
$email $password
Truth table33%1/3
A and B
A= $emailB= $password
ABresult
0X0
100
111
115 0 $self->errstr('Both email and password are required.');
116 0 return;
117 }
118  
119 2 ▶ unless ( $email =~ m/^(.+)\@(([^.]+)\.(.+))$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($email =~ /^(.+)\@(([^.]+)\.(.+))$/) 0 2 2
120 0 $self->errstr('You must supply full email address.');
121 0 return;
122 }
123  
124 # get supplier module
125 2 my $supplier;
126 2 ▶ if($social_network) {
Branch: 1 branch
BranchTrueFalseTotal
if ($social_network) { } 0 2 2
127 0 $social_network = lc($social_network);
128 0 $supplier = $self->get_supplier_by_socialnetwork($social_network);
129 } else {
130 2 $supplier = $self->get_supplier_by_email($email);
131 }
132 2 ▶ unless ($supplier) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($supplier) 0 2 2
133 0 ▶ if($social_network) {
Branch: 1 branch
BranchTrueFalseTotal
if ($social_network) { } 0 0 0
134 0 $self->errstr("$social_network is not supported yet.");
135 } else {
136 0 $self->errstr("$email is not supported yet.");
137 }
138 0 return;
139 }
140  
141 2 my $module = 'WWW::Contact::' . $supplier;
142 2 Class::Load::load_class($module);
143 2 my $wc = $module->new( $self->supplier_args );
144  
145 # reset
146 2 $self->errstr(undef);
147  
148 2 my $contacts = $wc->get_contacts( $email, $password );
149  
150 2 ▶ if ( $wc->errstr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wc->errstr) { } 1 1 2
151 1 $self->errstr( $wc->errstr );
152 1 return;
153 } else {
154 1 ▶ return wantarray ? @$contacts : $contacts;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
155 }
156 }
157  
158 sub get_supplier_by_email {
159 12 ▶ my ($self, $email) = @_;
Subroutine get_supplier_by_email: called CC=12 SCAR=29.5
Pod documented
160  
161 12 my %known_supplier = $self->known_supplier;
162  
163 12 my ($username, $domain) = split('@', $email);
164  
165 12 ▶ if ( exists $known_supplier{ $domain } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $known_supplier{$domain}) 9 3 12
166 9 return $known_supplier{ $domain };
167 }
168  
169 # @yahoo.com @yahoo.XX @XX.yahoo.XX
170 3 ▶ if ( $email =~ /[\@\.]yahoo\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($email =~ /[\@\.]yahoo\./) 1 2 3
171 1 return 'Yahoo';
172 }
173  
174 2 my @supplier_pattern = $self->supplier_pattern;
175 2 foreach my $supplier (@supplier_pattern) {
176 3 my $pattern = $supplier->{pattern};
177 3 my $mtype = ref($pattern);
178 3 ▶ if ( $mtype eq 'Regexp' and $email =~ $pattern ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mtype eq 'Regexp' and $email =~ /$pattern/) { } 1 2 3
elsif ($domain eq $pattern) { } 1 1 2
Condition66%2/3
$mtype eq "Regexp" and $email =~ /$pattern/
!ll&&!rl&&r
201
MC/DC50%1/2
$mtype eq "Regexp" and $email =~ /$pattern/
$mtype eq "Regexp" $email =~ /$pattern/
Truth table66%2/3
A and B
A= $mtype eq "Regexp"B= $email =~ /$pattern/
ABresult
0X0
100
111
179 1 return $supplier->{supplier};
180 } elsif ( $domain eq $pattern ) {
181 1 return $supplier->{supplier};
182 }
183 }
184  
185 # resolve domain
186 0 my $r = $self->resolve;
187 0 ▶ return $r->{ $domain } if exists $r->{ $domain };
Branch: 1 branch
BranchTrueFalseTotal
if exists $r->{$domain} 0 0 0
188  
189 # warn 'resolve domain';
190 0 foreach my $mx ($self->resolve_domain->query($domain, 'MX')) {
191 0 ▶ next unless $mx;
Branch: 1 branch
BranchTrueFalseTotal
unless $mx 0 0 0
192 0 for ($mx->answer) {
193 # google corporate mail
194 0 ▶ return $r->{ $domain } = $known_supplier{'gmail.com'} if $_->exchange =~ /google(?:mail)?\.com$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $_->exchange =~ /google(?:mail)?\.com$/i 0 0 0
195 }
196 }
197  
198 0 return;
199 }
200  
201 sub get_supplier_by_socialnetwork {
202 0 ▶ my ($self, $social_network) = @_;
Subroutine get_supplier_by_socialnetwork: not called CC=2 SCAR=17.9
Pod documented
203  
204 0 my %social_supplier = $self->social_network;
205  
206 0 ▶ if ( exists $social_supplier{ $social_network } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $social_supplier{$social_network}) 0 0 0
207 0 return $social_supplier{ $social_network };
208 }
209  
210 0 return;
211 }
212  
213 sub register_supplier {
214 2 ▶ my ($self, $pattern, $supplier) = @_;
Subroutine register_supplier: called CC=1 SCAR=0.0
Pod documented
215  
216 2 unshift @{ $self->supplier_pattern }, { pattern => $pattern, supplier => $supplier };
217 }
218  
219 8 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
220  
221 8 __PACKAGE__->meta->make_immutable;
222  
223 8 1;
224 __END__
225  
226 =head1 NAME
227  
228 WWW::Contact - Get contacts/addressbook from Web
229  
230 =head1 SYNOPSIS
231  
232 use WWW::Contact;
233 use Data::Dumper;
234  
235 # Get contacts from email providers.
236 my $wc = WWW::Contact->new();
237 my @contacts = $wc->get_contacts('fayland@gmail.com', 'password');
238 my $errstr = $wc->errstr;
239 if ($errstr) {
240 die $errstr; # like 'Wrong Username or Password'
241 } else {
242 print Dumper(\@contacts);
243 }
244  
245 # Get contacts from social networks (eg: Plaxo)
246 my $ws = WWW::Contact->new();
247 # Note that the last argument for get_contacts() is mandatory,
248 # or else it will try to fetch contacts from gmail.com
249 my @contacts = $ws->get_contacts('itsa@gmail.com', 'password', 'plaxo');
250 my $errstr = $ws->errstr;
251 if ($errstr) {
252 die $errstr; # like 'Wrong Username or Password'
253 } else {
254 print Dumper(\@contacts);
255 }
256  
257  
258 =head1 DESCRIPTION
259  
260 Get contacts/addressbook from public websites
261  
262 =head1 SUPPORTED EMAIL SUPPLIER
263  
264 =over 4
265  
266 =item Gmail
267  
268 L<WWW::Contact::Gmail> by Fayland Lam, DEPRECATED for L<WWW::Contact::GoogleContactsAPI>
269  
270 =item Yahoo! Mail
271  
272 L<WWW::Contact::Yahoo> by Fayland Lam
273  
274 =item Rediffmail
275  
276 L<WWW::Contact::Rediffmail> by Sachin Sebastian
277  
278 =item mail.163.com
279  
280 L<WWW::Contact::CN::163> by Fayland Lam
281  
282 =item AOL
283  
284 L<WWW::Contact::AOL> by Fayland Lam
285  
286 =item Mail
287  
288 L<WWW::Contact::Mail> by Sachin Sebastian
289  
290 =item Hotmail/Live Mail
291  
292 L<WWW::Contact::Hotmail> by Fayland Lam
293  
294 =item Indiatimes
295  
296 L<WWW::Contact::Indiatimes> by Sachin Sebastian
297  
298 =item Lycos
299  
300 L<WWW::Contact::Lycos> by Sachin Sebastian
301  
302 =item Plaxo
303  
304 L<WWW::Contact::Plaxo> by Sachin Sebastian
305  
306 =item GoogleContactsAPI
307  
308 L<WWW::Contact::GoogleContactsAPI> by Fayland Lam, using Google Contacts Data API
309  
310 =item abv.bg
311  
312 L<WWW::Contact::BG::Abv> by Dimitar Petrov
313  
314 =item mail.bg
315  
316 L<WWW::Contact::BG::Mail> by Dimitar Petrov
317  
318 =back
319  
320 =head1 METHODS
321  
322 =head2 register_supplier
323  
324 To use custom supplier, we must register within WWW::Contact
325  
326 $wc->register_supplier( qr/\@a\.com$/, 'Unknown' );
327 $wc->register_supplier( 'a.com', 'Unknown' );
328  
329 The first arg is a Regexp or domain from email postfix. The second arg is the according module postfix like 'Unknown' from WWW::Contact::Unknown
330  
331 =head2 get_supplier_by_email
332  
333 get supplier by email.
334  
335 my $supplier = $wc->get_supplier_by_email('a@gmail.com'); # 'GoogleContactsAPI'
336 my $supplier = $wc->get_supplier_by_email('a@a.com'); # 'Unknown'
337  
338 =head2 get_supplier_by_socialnetwork
339  
340 get supplier by social network name.
341  
342 my $supplier = $wc->get_supplier_by_socialnetwork('plaxo'); # 'Plaxo'
343  
344 =head1 HOW TO WRITE YOUR OWN MODULE
345  
346 Please read L<WWW::Contact::Base> and examples: L<WWW::Contact::Yahoo> and L<WWW::Contact::Plaxo>
347  
348 Assuming we write a custom module as WWW::Contact::Unknown
349  
350 package WWW::Contact::Unknown;
351  
352 use Moose;
353 extends 'WWW::Contact::Base';
354  
355 sub get_contacts {
356 my ($self, $email, $password) = @_;
357  
358 # reset
359 $self->errstr(undef);
360  
361 if ($email eq 'a@a.com' and $password ne 'a') {
362 $self->errstr('Wrong Username or Password');
363 return;
364 }
365  
366 my @contacts = ( {
367 email => 'b@b.com',
368 name => 'b',
369 }, {
370 email => 'c@c.com',
371 name => 'c'
372 } );
373 return wantarray ? @contacts : \@contacts;
374 }
375  
376 1;
377  
378 We can use it within WWW::Contact
379  
380 my $wc = WWW::Contact->new();
381 $wc->register_supplier( qr/\@a\.com$/, 'Unknown' );
382 # or
383 # $wc->register_supplier( 'a.com', 'Unknown' );
384  
385 my @contacts = $wc->get_contacts('a@a.com', 'b');
386 my $errstr = $wc->errstr;
387  
388 =head1 SEE ALSO
389  
390 L<WWW::Mechanize>, L<Moose>
391  
392 =head1 SUPPORTS
393  
394 =over 4
395  
396 =item Code trunk
397  
398 L<http://github.com/fayland/perl-www-contact/tree/master>
399  
400 =item Group
401  
402 L<http://groups.google.com/group/perl-www-contact>
403  
404 =back
405  
406 =head1 AUTHOR
407  
408 Fayland Lam, C<< <fayland at gmail.com> >>
409  
410 Sachin Sebastian, C<< <sachinjsk at cpan.org> >>
411  
412 Dimitar Petrov, C<< <mitakaa at gmail.com> >>
413  
414 =head1 COPYRIGHT & LICENSE
415  
416 Copyright 2008 *AUTHOR* all rights reserved.
417  
418 This program is free software; you can redistribute it and/or modify it
419 under the same terms as Perl itself.
420  
421 =cut
« blib/lib/WWW/Contact/Lycos.pm ↑ Summary blib/lib/WWW/Contact/BG/Mail.pm »