File Coverage

blib/lib/Test/Reporter/Transport/Net/SMTP/Authen.pm
Criterion Covered Total %
statement 15 132 11.3
branch 0 70 0.0
condition 0 17 0.0
subroutine 5 12 41.6
pod 2 4 50.0
total 22 235 9.3


line stmt bran cond sub pod time code
1 1     1   82466 use strict;
  1         3  
  1         78  
2 1     1   7 use warnings;
  1         3  
  1         72  
3             package Test::Reporter::Transport::Net::SMTP::Authen;
4 1     1   8 use base 'Test::Reporter::Transport::Net::SMTP';
  1         7  
  1         2121  
5 1     1   9 use vars qw/$VERSION/;
  1         3  
  1         84  
6             $VERSION = '1.02';
7             $VERSION = eval $VERSION;
8 1     1   5 use Exporter 'import';
  1         2  
  1         2900  
9             our @EXPORT_OK=qw(install);
10              
11             sub install($$$){
12 0     0 0   my ($smtp, $user, $password) = @_;
13 0 0         if ( @_ != 3){
14 0           croak( "Usage: install(\$smtp_server, \$user, \$password)" );
15             };
16 0 0 0       unless( $ENV{PERL_CPAN_REPORTER_DIR} || $ENV{HOME} ){
17 0           require Carp;
18 0           croak( "Please define your enviroment variable PERL_CPAN_REPORTER_DIR or HOME" );
19             }
20 0   0       my $reporter_folder = $ENV{PERL_CPAN_REPORTER_DIR} ||= $ENV{HOME} . '/.cpanreporter';
21 0           require File::Path;
22 0           require Carp;
23 0           File::Path->import('mkpath');
24 0           mkpath( $reporter_folder );
25 0 0         open my $fh , '>', $reporter_folder .'/config.ini' or Carp::croak( "Can't write config.ini in $reporter_folder $!");
26 0           $a = select ($fh);
27 0           print "edit_report=no\n";
28 0           print "email_from=$user\n";
29 0           print "send_report=yes\n";
30 0           print "smtp_server=$smtp\n";
31 0           print "transport=Net::SMTP::Authen User $user Password $password\n";
32 0 0         close $fh or Carp::croak ("Can't finish write $reporter_folder/config.ini $!");
33 0           select $a;
34 0           print STDERR "Writen $reporter_folder/config.ini .\n\tType cpan \n";
35 0           print STDERR "\to conf test_report 1\n";
36 0           print STDERR "\to conf commit\n";
37             }
38              
39             sub new {
40 0     0 1   my ($class, @args) = @_;
41 0           bless { args => \@args } => $class;
42             }
43              
44             sub _net_class {
45 0     0     my ($self) = @_;
46 0 0         my $class = ref $self ? ref $self : $self;
47 0           my ($net_class) = ($class =~ /^Test::Reporter::Transport::(.+)\z/);
48 0 0         return 'Net::SMTP' if $net_class eq 'Net::SMTP::Authen';
49 0           return $net_class;
50             }
51              
52             sub _perform_auth{
53 0     0     my $self = shift;
54 0           my $transport = shift;
55 0           my $args = $$self{args};
56 0           my %opts = ( @$args );
57            
58 0   0       my ($user_name) = $opts{'User'} || $opts{'Username'};
59 0   0       my ($password) = $opts{'Password'} || $opts{'Pass'};
60 0 0         die "No user_name" unless $user_name;
61 0 0         return 1 if ($self->_net_class() eq 'Net::SMTP::TLS');
62 0           return $transport->auth($user_name, $password);
63             }
64              
65              
66             sub send {
67 0     0 1   my ($self, $report, $recipients) = @_;
68 0   0       $recipients ||= [];
69              
70 0           my $helo = $report->_maildomain(); # XXX: tight -- rjbs, 2008-04-06
71 0           my $from = $report->from();
72 0           my $via = $report->via();
73 0           my @tmprecipients = ();
74 0           my @bad = ();
75 0           my $smtp;
76              
77             my $mx;
78              
79 0           my $transport = $self->_net_class;
80              
81             # Sorry. Tight coupling happened before I got here. -- rjbs, 2008-04-06
82 0           for my $server (@{$report->{_mx}}) {
  0            
83 0           eval {
84 0           $smtp = $transport->new(
85             $server,
86             Hello => $helo,
87             Timeout => $report->timeout(),
88             Debug => $report->debug(),
89             $report->transport_args(),
90             );
91             };
92              
93 0 0         if (defined $smtp) {
94 0           $mx = $server;
95 0           last;
96             }
97             }
98              
99 0 0 0       die "Unable to connect to any MX's: $@" unless $mx && $smtp;
100              
101 0           my $cc_str;
102 0 0         if (@$recipients) {
103 0 0         if ($mx =~ /(?:^|\.)(?:perl|cpan)\.org$/) {
104 0           for my $recipient (sort @$recipients) {
105 0 0         if ($recipient =~ /(?:@|\.)(?:perl|cpan)\.org$/) {
106 0           push @tmprecipients, $recipient;
107             } else {
108 0           push @bad, $recipient;
109             }
110             }
111              
112 0 0         if (@bad) {
113 0           warn __PACKAGE__, ": Will not attempt to cc the following recipients since perl.org MX's will not relay for them. Either use Test::Reporter::Transport::Mail::Send, use other MX's, or only cc address ending in cpan.org or perl.org: ${\(join ', ', @bad)}.\n";
  0            
114             }
115              
116 0           $recipients = \@tmprecipients;
117             }
118              
119 0           $cc_str = join ', ', @$recipients;
120 0           chomp $cc_str;
121 0           chomp $cc_str;
122             }
123              
124 0 0         $via = ', via ' . $via if $via;
125              
126 0           my $envelope_sender = $from;
127 0           $envelope_sender =~ s/\s\([^)]+\)$//; # email only; no name
128              
129             # Net::SMTP returns 1 or undef for pass/fail
130             # Net::SMTP::TLS croaks on fail but may not return 1 on pass
131             # so this closure lets us die on an undef return only for Net::SMTP
132 0 0   0     my $die = sub { die $smtp->message if ref $smtp eq 'Net::SMTP' };
  0            
133            
134              
135 0 0         eval {
136 0           trace(1);
137 0 0         $self->_perform_auth($smtp) or $die->();
138 0           trace();
139            
140 0 0         $smtp->mail($envelope_sender) or $die->();
141 0           trace();
142 0 0         $smtp->to($report->address) or $die->();
143 0           trace();
144 0 0         if ( @$recipients ) { $smtp->cc(@$recipients) or $die->() };
  0 0          
145 0           trace();
146 0 0         $smtp->data() or $die->();
147 0           trace();
148 0 0         $smtp->datasend("Date: ", $self->_format_date, "\n") or $die->();
149 0           trace();
150 0 0         $smtp->datasend("Subject: ", $report->subject, "\n") or $die->();
151 0           trace();
152 0 0         $smtp->datasend("From: $from\n") or $die->();
153 0           trace();
154 0 0         $smtp->datasend("To: ", $report->address, "\n") or $die->();
155 0           trace();
156 0 0         if ( @$recipients ) { $smtp->datasend("Cc: $cc_str\n") or $die->() };
  0 0          
157 0           trace();
158 0 0         $smtp->datasend("Message-ID: ", $report->message_id(), "\n") or $die->();
159 0           trace();
160 0 0         $smtp->datasend("X-Reported-Via: Test::Reporter $Test::Reporter::VERSION$via\n") or $die->();
161 0           trace();
162 0 0         $smtp->datasend("\n") or $die->();
163 0           trace();
164 0           trace();
165 0 0         $smtp->datasend($report->report()) or $die->();
166 0           trace();
167 0 0         $smtp->dataend() or $die->();
168 0           trace();
169 0 0         $smtp->quit or $die->();
170 0           trace();
171 0           1;
172             } or die "$transport - $@";
173              
174 0           return 1;
175             }
176              
177             1;
178              
179             sub trace{
180 0     0 0   my $mess = shift;
181 0           my ($package, $file, $line, $func) = caller 1;
182 0           my ($package1, $file1, $line1, $func1) = caller 0;
183             #print STDERR "[$line:$line1] ";
184             }
185            
186             __END__