« blib/lib/Monitoring/Sneck/Checks/apparmor.pm
↑ Summary
blib/lib/Monitoring/Sneck/Checks/jails.pm »
| 1 | package Monitoring::Sneck::Checks::smtp; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use parent 'Monitoring::Sneck::Checks::base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Time::HiRes (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | Monitoring::Sneck::Checks::smtp - checks a SMTP server | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 VERSION | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Version 0.0.1 | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =cut | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 4 | our $VERSION = '0.0.1'; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | smtp|perl:smtp -H mail.example.org | ||||||||||||||||||||||||||||
| 24 | smtp_tls|perl:smtp -H mail.example.org -s -E SIZE -E PIPELINING -w 1 -c 3 | ||||||||||||||||||||||||||||
| 25 | submissions|perl:smtp -H mail.example.org -p 465 -S -E AUTH | ||||||||||||||||||||||||||||
| 26 | smtp_banner|perl:smtp -H 192.0.2.25 -e 'ESMTP Postfix' | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | Connects, reads the greeting, says EHLO, and QUITs, timing it all. A | ||||||||||||||||||||||||||||
| 31 | greeting other than 220, such as 554 for refusing, a EHLO not answered | ||||||||||||||||||||||||||||
| 32 | with 250, or a extension asked for not being offered, is CRITICAL, as | ||||||||||||||||||||||||||||
| 33 | is not being able to connect. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | With --starttls, STARTTLS must be offered, and is done, with EHLO said | ||||||||||||||||||||||||||||
| 36 | again over TLS. With --ssl, it connects over TLS from the start, as for | ||||||||||||||||||||||||||||
| 37 | port 465. The certificate is checked unless --insecure is given. See | ||||||||||||||||||||||||||||
| 38 | L<Monitoring::Sneck::Checks::https_cert> with --starttls smtp for when | ||||||||||||||||||||||||||||
| 39 | it expires. | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 OPTIONS | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | - -H, --hostname :: The host, and the name asked for through SNI, | ||||||||||||||||||||||||||||
| 44 | unless it is a IP. Required. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | - -p, --port :: The port. | ||||||||||||||||||||||||||||
| 47 | Default :: 25, or 465 with --ssl | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | - -S, --ssl :: Connect over TLS. Can not be used with --starttls. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | - -s, --starttls :: STARTTLS must be offered, and is done. Can not be | ||||||||||||||||||||||||||||
| 52 | used with --ssl. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | - -e, --expect :: Text the greeting must have. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | - -E, --extension :: A extension EHLO must offer, such as SIZE, AUTH, | ||||||||||||||||||||||||||||
| 57 | or PIPELINING. May be given more than once. | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | - -n, --helo-name :: The name to give in EHLO. | ||||||||||||||||||||||||||||
| 60 | Default :: the host name of this host | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | - -w, --warning :: Range of seconds for the whole of it. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | - -c, --critical :: Range of seconds for the whole of it. | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | - --insecure :: Do not check the certificate. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | - --ca-file :: The CA file to check the certificate against. | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 9 | ▶ | sub option_spec { | ||||||||||||||||||||||||||
|
Subroutine
option_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | return ( | ||||||||||||||||||||||||||||
| 74 | 9 | 'hostname|H=s', 'port|p=i', 'ssl|S', 'starttls|s', 'expect|e=s', 'extension|E=s@', | |||||||||||||||||||||||||||
| 75 | 'helo-name|n=s', 'warning|w=s', 'critical|c=s', 'insecure', 'ca-file=s', | ||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 9 | ▶ | sub required { return ('hostname') } | ||||||||||||||||||||||||||
|
Subroutine
required: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 9 | ▶ | sub ranges { return ( 'warning', 'critical' ) } | ||||||||||||||||||||||||||
|
Subroutine
ranges: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 9 | ▶ | sub validate { | ||||||||||||||||||||||||||
|
Subroutine
validate: called CC=7 SCAR=20.4
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | 9 | my ( $self, $options ) = @_; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 9 | my @problems; | |||||||||||||||||||||||||||
| 87 | push( @problems, '--hostname "' . $options->{hostname} . '" is not a IP or hostname' ) | ||||||||||||||||||||||||||||
| 88 | 9 | ▶ | if defined( $options->{hostname} ) && !$self->is_host( $options->{hostname} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $options->{"hostname"} and not $self->is_host($options->{"hostname"})
MC/DC0%0/2
defined $options->{"hostname"} and not $self->is_host($options->{"hostname"})
defined $options->{"hostname"}
not $self->is_host($options->{"hostname"})
Truth table33%1/3
A and not(B)
A= defined $options->{"hostname"}B= not $self->is_host($options->{"hostname"})
| |||||||||||||||||||||||||||||
| 89 | 9 | ▶ | push( @problems, '--ssl and --starttls can not both be given' ) if $options->{ssl} && $options->{starttls}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$options->{"ssl"} and $options->{"starttls"}
MC/DC100%2/2
$options->{"ssl"} and $options->{"starttls"}
$options->{"ssl"}
$options->{"starttls"}
Truth table100%3/3
A and B
A= $options->{"ssl"}B= $options->{"starttls"}
| |||||||||||||||||||||||||||||
| 90 | push( @problems, '--helo-name "' . $options->{'helo-name'} . '" is not a hostname' ) | ||||||||||||||||||||||||||||
| 91 | 9 | ▶ | if defined( $options->{'helo-name'} ) && !$self->is_host( $options->{'helo-name'} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $options->{"helo-name"} and not $self->is_host($options->{"helo-name"})
MC/DC0%0/2
defined $options->{"helo-name"} and not $self->is_host($options->{"helo-name"})
defined $options->{"helo-name"}
not $self->is_host($options->{"helo-name"})
Truth table66%2/3
A and not(B)
A= defined $options->{"helo-name"}B= not $self->is_host($options->{"helo-name"})
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 9 | return @problems; | |||||||||||||||||||||||||||
| 94 | } ## end sub validate | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head1 METHODS | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head2 check | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Does the check. | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =cut | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 8 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=28 SCAR=34.4
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 8 | require IO::Socket::SSL; | |||||||||||||||||||||||||||
| 108 | 8 | require IO::Socket::IP; | |||||||||||||||||||||||||||
| 109 | 8 | require Sys::Hostname; | |||||||||||||||||||||||||||
| 110 | 8 | require Data::Validate::IP; | |||||||||||||||||||||||||||
| 111 | 8 | my $host = $self->opt('hostname'); | |||||||||||||||||||||||||||
| 112 | 8 | ▶ | my $port = $self->opt('port') // ( $self->opt('ssl') ? 465 : 25 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$self->opt("port") // ($self->opt("ssl") ? 465 : 25)
MC/DC100%1/1
$self->opt("port") // ($self->opt("ssl") ? 465 : 25)
$self->opt("port")
Truth table100%2/2
A
A= $self->opt("port")
| |||||||||||||||||||||||||||||
| 113 | 8 | my $where = $host . ':' . $port; | |||||||||||||||||||||||||||
| 114 | 8 | ▶ | my %ssl = ( SSL_verify_mode => $self->opt('insecure') ? 0 : 1, Timeout => $self->timeout ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # SNI does not take a IP, and is set either way, as start_SSL has no PeerHost to take it from | ||||||||||||||||||||||||||||
| 117 | 8 | ▶ | $ssl{SSL_hostname} = Data::Validate::IP::is_ip($host) ? '' : $host; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 8 | ▶ | if ( !$self->opt('insecure') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 7 | $ssl{SSL_verifycn_name} = $host; | |||||||||||||||||||||||||||
| 120 | 7 | $ssl{SSL_verifycn_scheme} = 'smtp'; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 8 | ▶ | $ssl{SSL_ca_file} = $self->opt('ca-file') if defined( $self->opt('ca-file') ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 8 | my $start = Time::HiRes::time; | |||||||||||||||||||||||||||
| 125 | 8 | ▶ | my $socket | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | = $self->opt('ssl') | ||||||||||||||||||||||||||||
| 127 | ? IO::Socket::SSL->new( PeerHost => $self->opt('hostname'), PeerPort => $port, %ssl ) | ||||||||||||||||||||||||||||
| 128 | : IO::Socket::IP->new( | ||||||||||||||||||||||||||||
| 129 | PeerHost => $self->opt('hostname'), | ||||||||||||||||||||||||||||
| 130 | PeerPort => $port, | ||||||||||||||||||||||||||||
| 131 | Proto => 'tcp', | ||||||||||||||||||||||||||||
| 132 | Timeout => $self->timeout | ||||||||||||||||||||||||||||
| 133 | ); | ||||||||||||||||||||||||||||
| 134 | 8 | ▶ | if ( !$socket ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | 2 | ▶ | return $self->finish( $self->CRITICAL, | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$@ or $!
MC/DC0%0/2
$@ or $!
$@
$!
Truth table0%0/3
A or B
A= $@B= $!
| |||||||||||||||||||||||||||||
| 136 | $where . ', ' . ( $self->opt('ssl') ? IO::Socket::SSL::errstr() : ( $@ || $! ) ) ); | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | 6 | $socket->autoflush(1); | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 6 | my $greeting = $self->reply($socket); | |||||||||||||||||||||||||||
| 141 | 6 | ▶ | if ( $greeting->{code} != 220 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 1 | close($socket); | |||||||||||||||||||||||||||
| 143 | 1 | return $self->finish( $self->CRITICAL, $where . ' greeted with ' . _first($greeting) ); | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 5 | my @problems; | |||||||||||||||||||||||||||
| 146 | 5 | ▶ | if ( defined( $self->opt('expect') ) && index( $greeting->{text}, $self->opt('expect') ) < 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $self->opt("expect") and index($greeting->{"text"}, $self->opt("expect")) == -1
MC/DC100%2/2
defined $self->opt("expect") and index($greeting->{"text"}, $self->opt("expect")) == -1
defined $self->opt("expect")
index($greeting->{"text"}, $self->opt("expect")) == -1
Truth table100%3/3
A and B
A= defined $self->opt("expect")B= index($greeting->{"text"}, $self->opt("expect")) == -1
| |||||||||||||||||||||||||||||
| 147 | 1 | push( @problems, 'greeting does not have "' . $self->opt('expect') . '"' ); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 5 | ▶ | my $helo = $self->opt('helo-name') // Sys::Hostname::hostname(); | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->opt("helo-name") // Sys::Hostname::hostname()
MC/DC0%0/2
$self->opt("helo-name") // Sys::Hostname::hostname()
$self->opt("helo-name")
Sys::Hostname::hostname()
Truth table66%2/3
A // B
A= $self->opt("helo-name")B= Sys::Hostname::hostname()
| |||||||||||||||||||||||||||||
| 151 | 5 | my $ehlo = $self->command( $socket, 'EHLO ' . $helo ); | |||||||||||||||||||||||||||
| 152 | 5 | ▶ | if ( $ehlo->{code} != 250 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | close($socket); | |||||||||||||||||||||||||||
| 154 | 0 | return $self->finish( $self->CRITICAL, $where . ' answered EHLO with ' . _first($ehlo) ); | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | 5 | ▶ | if ( $self->opt('starttls') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 2 | ▶ | if ( !$ehlo->{extensions}{STARTTLS} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 1 | close($socket); | |||||||||||||||||||||||||||
| 159 | 1 | return $self->finish( $self->CRITICAL, $where . ' does not offer STARTTLS' ); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | 1 | my $go = $self->command( $socket, 'STARTTLS' ); | |||||||||||||||||||||||||||
| 162 | 1 | ▶ | if ( $go->{code} != 220 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 0 | close($socket); | |||||||||||||||||||||||||||
| 164 | 0 | return $self->finish( $self->CRITICAL, $where . ' answered STARTTLS with ' . _first($go) ); | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | 1 | ▶ | if ( !IO::Socket::SSL->start_SSL( $socket, %ssl ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | return $self->finish( $self->CRITICAL, $where . ' STARTTLS failed, ' . IO::Socket::SSL::errstr() ); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 1 | $ehlo = $self->command( $socket, 'EHLO ' . $helo ); | |||||||||||||||||||||||||||
| 170 | 1 | ▶ | if ( $ehlo->{code} != 250 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | close($socket); | |||||||||||||||||||||||||||
| 172 | 0 | return $self->finish( $self->CRITICAL, $where . ' answered EHLO over TLS with ' . _first($ehlo) ); | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | } ## end if ( $self->opt('starttls') ) | ||||||||||||||||||||||||||||
| 175 | 4 | ▶ | foreach my $extension ( @{ $self->opt('extension') // [] } ) { | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->opt("extension") // []
MC/DC100%1/1
$self->opt("extension") // []
$self->opt("extension")
Truth table100%2/2
A
A= $self->opt("extension")
| |||||||||||||||||||||||||||||
| 176 | 3 | ▶ | push( @problems, 'does not offer ' . uc($extension) ) if !$ehlo->{extensions}{ uc($extension) }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | 4 | $self->command( $socket, 'QUIT' ); | |||||||||||||||||||||||||||
| 179 | 4 | close($socket); | |||||||||||||||||||||||||||
| 180 | 4 | my $seconds = sprintf( '%.3f', Time::HiRes::time - $start ); | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 4 | ▶ | my @codes = ( defined( $problems[0] ) ? $self->CRITICAL : $self->OK ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 4 | my $time_code = $self->threshold_status( $seconds, $self->opt('warning'), $self->opt('critical') ); | |||||||||||||||||||||||||||
| 184 | 4 | push( @codes, $time_code ); | |||||||||||||||||||||||||||
| 185 | 4 | ▶ | push( @problems, 'slow' ) if $time_code != $self->OK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 4 | $self->perfdata( | |||||||||||||||||||||||||||
| 187 | label => 'time', | ||||||||||||||||||||||||||||
| 188 | value => $seconds, | ||||||||||||||||||||||||||||
| 189 | uom => 's', | ||||||||||||||||||||||||||||
| 190 | warning => $self->opt('warning'), | ||||||||||||||||||||||||||||
| 191 | critical => $self->opt('critical'), | ||||||||||||||||||||||||||||
| 192 | min => 0 | ||||||||||||||||||||||||||||
| 193 | ); | ||||||||||||||||||||||||||||
| 194 | 4 | ▶ | my $message | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | = $where . ' ' | ||||||||||||||||||||||||||||
| 196 | . _first($greeting) . ', in ' | ||||||||||||||||||||||||||||
| 197 | . $seconds | ||||||||||||||||||||||||||||
| 198 | . ' seconds' | ||||||||||||||||||||||||||||
| 199 | . ( $self->opt('starttls') ? ', over STARTTLS' : '' ); | ||||||||||||||||||||||||||||
| 200 | 4 | ▶ | $message = $message . ', ' . join( ', ', @problems ) if defined( $problems[0] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 4 | return $self->finish( $self->worst(@codes), $message ); | |||||||||||||||||||||||||||
| 203 | } ## end sub check | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head2 command | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | Sends a SMTP command and reads the reply. | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | - socket :: The socket. | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | - command :: The command, without the line ending. | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | Returns the reply, as from L</reply>. | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =cut | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 11 | ▶ | sub command { | ||||||||||||||||||||||||||
|
Subroutine
command: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 218 | 11 | my ( $self, $socket, $command ) = @_; | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 11 | print $socket $command . "\r\n"; | |||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 11 | return $self->reply($socket); | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | =head2 reply | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | Reads a SMTP reply, all of its lines. | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | Returns a hash ref of code, 0 if none came, text, the lines without | ||||||||||||||||||||||||||||
| 230 | their codes joined by newlines, and extensions, a hash ref of the | ||||||||||||||||||||||||||||
| 231 | upper cased first word of each line after the first, for EHLO. | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | =cut | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 17 | ▶ | sub reply { | ||||||||||||||||||||||||||
|
Subroutine
reply: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 236 | 17 | my ( $self, $socket ) = @_; | |||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | # a full reply, the last line of a multi line one having a space or nothing after the code | ||||||||||||||||||||||||||||
| 239 | 17 | ▶ | my $read = $self->read_socket( $socket, sub { return $_[0] =~ /^\d{3}(?: [^\n]*)?\r?\n/m } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 240 | 17 | my @lines = $read =~ /^(\d{3})[ -]?([^\r\n]*)/mg; | |||||||||||||||||||||||||||
| 241 | 17 | my %reply = ( code => 0, text => '', extensions => {} ); | |||||||||||||||||||||||||||
| 242 | 17 | ▶ | return \%reply if !@lines; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 15 | my @texts; | |||||||||||||||||||||||||||
| 245 | 15 | ▶ | while ( my ( $code, $text ) = splice( @lines, 0, 2 ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | 33 | $reply{code} = $code; | |||||||||||||||||||||||||||
| 247 | 33 | push( @texts, $text ); | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | 15 | $reply{text} = join( "\n", @texts ); | |||||||||||||||||||||||||||
| 250 | 15 | foreach my $text ( @texts[ 1 .. $#texts ] ) { | |||||||||||||||||||||||||||
| 251 | 18 | ▶ | $reply{extensions}{ uc($1) } = 1 if $text =~ /\A(\S+)/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 15 | return \%reply; | |||||||||||||||||||||||||||
| 255 | } ## end sub reply | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | # Describes a reply for messages, its code and first line. | ||||||||||||||||||||||||||||
| 258 | # | ||||||||||||||||||||||||||||
| 259 | # Args... | ||||||||||||||||||||||||||||
| 260 | # | ||||||||||||||||||||||||||||
| 261 | # - reply :: From reply. | ||||||||||||||||||||||||||||
| 262 | # | ||||||||||||||||||||||||||||
| 263 | # Returns the description, such as '220 mail.example.org ESMTP', or | ||||||||||||||||||||||||||||
| 264 | # 'nothing' if no reply came. | ||||||||||||||||||||||||||||
| 265 | # | ||||||||||||||||||||||||||||
| 266 | # Example... | ||||||||||||||||||||||||||||
| 267 | # | ||||||||||||||||||||||||||||
| 268 | # _first( { code => 554, text => "no thanks\nbye" } ); # '554 no thanks' | ||||||||||||||||||||||||||||
| 269 | 5 | ▶ | sub _first { | ||||||||||||||||||||||||||
|
Subroutine
_first: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 270 | 5 | my ($reply) = @_; | |||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | 5 | ▶ | return 'nothing' if !$reply->{code}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 5 | return $reply->{code} . ' ' . ( split( /\n/, $reply->{text} ) )[0]; | |||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | 4 | 1; | |||||||||||||||||||||||||||