File Coverage

lib/AnyEvent/SMTP/Conn.pm
Criterion Covered Total %
statement 97 107 90.6
branch 31 60 51.6
condition 2 6 33.3
subroutine 20 21 95.2
pod 1 9 11.1
total 151 203 74.3


line stmt bran cond sub pod time code
1             package AnyEvent::SMTP::Conn;
2              
3 12     12   74 use AnyEvent;
  12         23  
  12         296  
4 12     12   78 use common::sense;
  12         145  
  12         104  
5             m{# trying to cheat with cpants game ;)
6             use strict;
7             use warnings;
8             }x;
9 12     12   1041 use base 'Object::Event';
  12         193  
  12         1220  
10 12     12   68 use AnyEvent::Handle;
  12         19  
  12         736  
11              
12 12     12   518 our $VERSION = $AnyEvent::SMTP::VERSION;use AnyEvent::SMTP ();
  12         22  
  12         25608  
13              
14             our $NL = "\015\012";
15             our $QRNL = qr<\015?\012>;
16              
17             sub new {
18 88     88 1 228 my $pkg = shift;
19 88         3794 my $self = bless { @_ }, $pkg;
20             $self->{h} = AnyEvent::Handle->new(
21             fh => $self->{fh},
22             on_eof => sub {
23 0     0   0 local *__ANON__ = 'conn.on_eof';
24 0         0 warn "eof on handle";
25 0 0       0 $self->{h} and $self->{h}->destroy;
26 0         0 delete $self->{h};
27 0         0 $self->event('disconnect');
28             },
29             on_error => sub {
30 45     45   54265 local *__ANON__ = 'conn.on_error';
31             #warn "error on handle: $!";
32 45 50       541 $self->{h} and $self->{h}->destroy;
33 45         3614 delete $self->{h};
34 45         638 $self->event( disconnect => "Error: $!" );
35             },
36 88         2213 );
37 88 100       9928 $self->{h}->timeout($self->{timeout}) if $self->{timeout};
38 88         6698 $self;
39             }
40              
41             sub close {
42 42     42 0 2504 my $self = shift;
43 42         163 delete $self->{fh};
44 42 50       310 $self->{h} and $self->{h}->destroy;
45 42         8375 delete $self->{h};
46 42         253 $self->event( disconnect => () );
47 42         1207 return;
48             }
49              
50             sub command {
51 205     205 0 337 my $self = shift;
52 205         366 my $write = shift;
53 205         1054 my %args = @_;
54 205 50       710 $args{ok} = '250' unless defined $args{ok};
55 205 50       480 $args{cb} or return $self->event( error => "no cb for command at @{[ (caller)[1,2] ]}" );
  0         0  
56 205 50       763 $self->{h} or return $args{cb}->(undef,"Not connected");
57             #my $i if 0;
58             #my $c = ++$i;
59 205 50       670 warn ">> $write " if $self->{debug};
60 205         1345 $self->{h}->push_write("$write$NL");
61             #$self->{h}->timeout( $self->{select_timeout} );
62 205 50 33     17372 warn "{debug} and $self->{debug} > 1;
63             $self->{h}->push_read( regex => $QRNL, sub {
64 205     205   852045 local *__ANON__ = 'conn.command.read';
65 205         320 shift;
66 205         442 for (@_) {
67 205         575 chomp;
68 205 50       1343 substr($_,-1,1) = '' if substr($_, -1,1) eq "\015";
69             }
70 205 50       747 warn "<< @_ " if $self->{debug};
71 205         575 my $line = join '',@_;
72 205 50       1144 if ( substr( $line,0,length($args{ok})+1 ) eq $args{ok}.' ' ) {
73 205         954 $args{cb}($line);
74             } else {
75 0         0 $args{cb}(undef, $line);
76             }
77 205         1727 } );
78             }
79              
80             sub line {
81 41     41 0 84 my $self = shift;
82 41         185 my %args = @_;
83 41 50       419 $args{ok} = '250' unless defined $args{ok};
84 41 50       128 $args{cb} or return $self->event( error => "no cb for command at @{[ (caller)[1,2] ]}" );
  0         0  
85 41 50 33     221 warn "{debug} and $self->{debug} > 1;
86             $self->{h}->push_read( regex => $QRNL, sub {
87 41     41   20362 local *__ANON__ = 'conn.line.read';
88 41         71 shift;
89 41         100 for (@_) {
90 41         94 chomp;
91 41 50       242 substr($_,-1,1) = '' if substr($_, -1,1) eq "\015";
92             }
93 41 50       168 warn "<< @_ " if $self->{debug};
94 41         112 my $line = join '',@_;
95 41 50       209 if ( substr( $line,0,length($args{ok})+1 ) eq $args{ok}.' ' ) {
96 41         166 $args{cb}(1);
97             } else {
98 0         0 $args{cb}(undef, $line);
99             }
100 41         453 } );
101            
102             }
103              
104             sub want_command {
105 211     211 0 363 my $self = shift;
106 211 50       577 $self->{h} or return warn "Not connected";
107             $self->{h}->push_read( regex => $QRNL, sub {
108 164     164   118370 local *__ANON__ = 'conn.want_command.read';
109 164         1583 shift;
110 164         1419 for (@_) {
111 164         824 chomp;
112 164 50       810 substr($_,-1,1) = '' if substr($_, -1,1) eq "\015";
113             }
114 164 50       794 warn "<< @_ " if $self->{debug};
115 164         637 $self->event(command => @_);
116 164 50       4081 $self->want_command if $self->{h};
117 211         3218 });
118             }
119              
120             sub ok {
121 164     164 0 393 my $self = shift;
122 164 50       1231 $self->{h} or return warn "Not connected";
123 164 100       508 @_ = ('Ok.') unless @_;
124 164         1139 $self->{h}->push_write("250 @_$NL");
125 164 50       32201 warn ">> 250 @_ " if $self->{debug};
126             }
127              
128             sub reply {
129 129     129 0 252 my $self = shift;
130 129 50       808 $self->{h} or return warn "Not connected";
131 129         948 $self->{h}->push_write("@_$NL");
132 129 50       21751 warn ">> @_ " if $self->{debug};
133             }
134              
135             sub data {
136 41     41 0 70 my $self = shift;
137 41         166 my %args = @_;
138 41 50       122 $args{cb} or return $self->event( error => "no cb for command at @{[ (caller)[1,2] ]}" );
  0         0  
139 41 50       133 $self->{h} or return $args{cb}->(undef,"Not connected");
140 41 50       118 warn '<+ read till \r\n.\r\n ' if $self->{debug};
141             $self->{h}->unshift_read( regex => qr/((?:\015?\012|^)\.\015?\012)/, sub {
142 41     41   696131 shift;
143 12     12   17926 use bytes;
  12         155  
  12         55  
144 41         433 $args{cb}(substr($_[0],0,length($_[0]) - length ($1)))
145 41         983 } );
146              
147             }
148              
149             sub new_m {
150 41     41 0 71 my $self = shift;
151 41         1354 $self->{m} = { host => $self->{host}, port => $self->{port}, helo => $self->{helo}, @_ };
152             }
153             1;