| 1 | # Declare our package | ||||||||||||||
| 2 | package Test::Reporter::POEGateway::Mailer::SMTP; | ||||||||||||||
| 3 | 1 | ▶ | use strict; use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | # Initialize our version | ||||||||||||||
| 6 | 1 | ▶ | use vars qw( $VERSION ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | $VERSION = '0.04'; | |||||||||||||
| 8 | |||||||||||||||
| 9 | # Load some necessary modules | ||||||||||||||
| 10 | 1 | ▶ | use POE::Filter::Reference; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 1 | ▶ | use Email::Simple; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 1 | ▶ | use Email::MessageID; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | # The mailer config we use | ||||||||||||||
| 15 | 1 | my $config = undef; | |||||||||||||
| 16 | |||||||||||||||
| 17 | # The smtp object | ||||||||||||||
| 18 | 1 | my $smtp = undef; | |||||||||||||
| 19 | |||||||||||||||
| 20 | # This is the subroutine that will get executed upon the fork() call by our parent | ||||||||||||||
| 21 | 0 | ▶ | sub main { | ||||||||||||
|
Subroutine
main: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||
| 22 | # Autoflush to avoid weirdness | ||||||||||||||
| 23 | 0 | $|++; | |||||||||||||
| 24 | |||||||||||||||
| 25 | # set binmode, thanks RT #43442 | ||||||||||||||
| 26 | 0 | binmode( STDIN ); | |||||||||||||
| 27 | |||||||||||||||
| 28 | # Our Filter object | ||||||||||||||
| 29 | 0 | my $filter = POE::Filter::Reference->new(); | |||||||||||||
| 30 | |||||||||||||||
| 31 | # Sysread error hits | ||||||||||||||
| 32 | 0 | my $sysreaderr = 0; | |||||||||||||
| 33 | |||||||||||||||
| 34 | MAINLOOP: | ||||||||||||||
| 35 | |||||||||||||||
| 36 | # Okay, now we listen for commands from our parent :) | ||||||||||||||
| 37 | 0 | ▶ | while ( sysread( STDIN, my $buffer = '', 1024 ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | # Feed the line into the filter | ||||||||||||||
| 39 | 0 | my $data = $filter->get( [ $buffer ] ); | |||||||||||||
| 40 | |||||||||||||||
| 41 | # INPUT STRUCTURE IS: | ||||||||||||||
| 42 | # $d->{'ACTION'} = SCALAR -> WHAT WE SHOULD DO | ||||||||||||||
| 43 | # $d->{'DATA'} = HASH -> DATA FOR THE ACTION | ||||||||||||||
| 44 | |||||||||||||||
| 45 | # Process each data structure | ||||||||||||||
| 46 | 0 | foreach my $input ( @$data ) { | |||||||||||||
| 47 | ## no critic ( ProhibitAccessOfPrivateData ) | ||||||||||||||
| 48 | # Now, we do the actual work depending on what kind of query it was | ||||||||||||||
| 49 | 0 | ▶ | if ( $input->{'ACTION'} eq 'CONFIG' ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 50 | # Setup the config | ||||||||||||||
| 51 | 0 | DO_CONFIG( $input->{'DATA'} ); | |||||||||||||
| 52 | } elsif ( $input->{'ACTION'} eq 'SEND' ) { | ||||||||||||||
| 53 | # Send a report! | ||||||||||||||
| 54 | 0 | my $ret = DO_SEND( $input->{'DATA'} ); | |||||||||||||
| 55 | 0 | ▶ | if ( $ret->[0] ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | 0 | print "OK $ret->[1]\n"; | |||||||||||||
| 57 | } else { | ||||||||||||||
| 58 | 0 | print "NOK $ret->[1]\n"; | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | } else { | ||||||||||||||
| 61 | # Unrecognized action! | ||||||||||||||
| 62 | 0 | print "ERROR Unknown action ($input->{'ACTION'})\n"; | |||||||||||||
| 63 | } | ||||||||||||||
| 64 | } | ||||||||||||||
| 65 | } | ||||||||||||||
| 66 | |||||||||||||||
| 67 | # Arrived here due to error in sysread/etc | ||||||||||||||
| 68 | 0 | print "ERROR SYSREAD\n"; | |||||||||||||
| 69 | |||||||||||||||
| 70 | # If we got more than 5 sysread errors, abort! | ||||||||||||||
| 71 | 0 | ▶ | if ( ++$sysreaderr == 5 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 72 | 0 | exit 0; | |||||||||||||
| 73 | } else { | ||||||||||||||
| 74 | 0 | goto MAINLOOP; | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | |||||||||||||||
| 77 | 0 | return; | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | |||||||||||||||
| 80 | # initializes our config | ||||||||||||||
| 81 | 0 | ▶ | sub DO_CONFIG { | ||||||||||||
|
Subroutine
DO_CONFIG: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||
| 82 | 0 | $config = shift; | |||||||||||||
| 83 | |||||||||||||||
| 84 | # set some sane defaults | ||||||||||||||
| 85 | ## no critic ( ProhibitAccessOfPrivateData ) | ||||||||||||||
| 86 | 0 | ▶ | $config->{'to'} = 'cpan-testers@perl.org' if ! exists $config->{'to'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | 0 | ▶ | $config->{'smtp_host'} = 'localhost' if ! exists $config->{'smtp_host'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 88 | 0 | ▶ | $config->{'smtp_opts'} = {} if ! exists $config->{'smtp_opts'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 89 | |||||||||||||||
| 90 | # Get rid of our old smtp if needed | ||||||||||||||
| 91 | 0 | undef $smtp; | |||||||||||||
| 92 | |||||||||||||||
| 93 | 0 | return; | |||||||||||||
| 94 | } | ||||||||||||||
| 95 | |||||||||||||||
| 96 | 0 | ▶ | sub setup_smtp { | ||||||||||||
|
Subroutine
setup_smtp: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||
| 97 | 0 | ▶ | return if defined $smtp; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 98 | ## no critic ( ProhibitAccessOfPrivateData ) | ||||||||||||||
| 99 | |||||||||||||||
| 100 | # Do we want ssl? | ||||||||||||||
| 101 | 0 | my $pkg = 'Net::SMTP'; | |||||||||||||
| 102 | 0 | ▶ | if ( exists $config->{'ssl'} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | 0 | $pkg .= '::SSL'; | |||||||||||||
| 104 | } | ||||||||||||||
| 105 | |||||||||||||||
| 106 | 0 | ▶ | eval "require $pkg"; die $@ if $@; ## no critic ( ProhibitStringyEval ) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 107 | |||||||||||||||
| 108 | $smtp = $pkg->new( | ||||||||||||||
| 109 | $config->{'smtp_host'}, | ||||||||||||||
| 110 | 0 | %{ $config->{'smtp_opts'} }, | |||||||||||||
| 111 | ); | ||||||||||||||
| 112 | |||||||||||||||
| 113 | 0 | ▶ | if ( ! defined $smtp ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 114 | 0 | return "Unable to connect to the smtp server"; | |||||||||||||
| 115 | } | ||||||||||||||
| 116 | |||||||||||||||
| 117 | # Do AUTH if needed | ||||||||||||||
| 118 | 0 | ▶ | if ( exists $config->{'auth_user'} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 119 | 0 | ▶ | if ( ! $smtp->auth( $config->{'auth_user'}, $config->{'auth_pass'} ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 120 | 0 | return smtp_error( "Unable to AUTH to the smtp server" ); | |||||||||||||
| 121 | } | ||||||||||||||
| 122 | } | ||||||||||||||
| 123 | |||||||||||||||
| 124 | 0 | return; | |||||||||||||
| 125 | } | ||||||||||||||
| 126 | |||||||||||||||
| 127 | # return an error and cleanup smtp | ||||||||||||||
| 128 | 0 | ▶ | sub smtp_error { | ||||||||||||
|
Subroutine
smtp_error: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 129 | 0 | my $err = shift; | |||||||||||||
| 130 | |||||||||||||||
| 131 | 0 | my $msg = $smtp->message; | |||||||||||||
| 132 | 0 | $msg =~ s/\s+\z//; # damn Net::SMTP/Cmd not chomping it! | |||||||||||||
| 133 | 0 | $err .= ": '$msg' (" . $smtp->code() . ")"; | |||||||||||||
| 134 | |||||||||||||||
| 135 | 0 | $smtp->quit; | |||||||||||||
| 136 | 0 | undef $smtp; | |||||||||||||
| 137 | |||||||||||||||
| 138 | 0 | return $err; | |||||||||||||
| 139 | } | ||||||||||||||
| 140 | |||||||||||||||
| 141 | 0 | ▶ | sub DO_SEND { | ||||||||||||
|
Subroutine
DO_SEND: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||
| 142 | 0 | my $data = shift; | |||||||||||||
| 143 | |||||||||||||||
| 144 | # init the smtp if needed | ||||||||||||||
| 145 | 0 | my $ret = setup_smtp(); | |||||||||||||
| 146 | 0 | ▶ | if ( defined $ret ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 147 | 0 | return [ 0, $ret ]; | |||||||||||||
| 148 | } | ||||||||||||||
| 149 | |||||||||||||||
| 150 | # send it! | ||||||||||||||
| 151 | ## no critic ( ProhibitAccessOfPrivateData ) | ||||||||||||||
| 152 | 0 | ▶ | if ( ! $smtp->mail( $data->{'from'} ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 153 | 0 | return [ 0, smtp_error( "Unable to set 'from' address" ) ]; | |||||||||||||
| 154 | } | ||||||||||||||
| 155 | |||||||||||||||
| 156 | 0 | ▶ | if ( ! $smtp->to( $config->{'to'} ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 157 | 0 | return [ 0, smtp_error( "Unable to set 'to' address" ) ]; | |||||||||||||
| 158 | } | ||||||||||||||
| 159 | |||||||||||||||
| 160 | # Prepare the data | ||||||||||||||
| 161 | 0 | my $id = Email::MessageID->new->as_string; | |||||||||||||
| 162 | my $email = Email::Simple->create( | ||||||||||||||
| 163 | 'body' => $data->{'report'}, | ||||||||||||||
| 164 | 'header' => [ | ||||||||||||||
| 165 | 'To' => $config->{'to'}, | ||||||||||||||
| 166 | 'From' => $data->{'from'}, | ||||||||||||||
| 167 | 'Subject' => $data->{'subject'}, | ||||||||||||||
| 168 | 0 | 'X-Reported-Via' => $data->{'via'}, | |||||||||||||
| 169 | 'Message-ID' => '<' . $id . '>', # required, look at Email::MessageID for the note! | ||||||||||||||
| 170 | ], | ||||||||||||||
| 171 | ); | ||||||||||||||
| 172 | |||||||||||||||
| 173 | 0 | ▶ | if ( ! $smtp->data( $email->as_string ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 174 | 0 | return [ 0, smtp_error( "Unable to send message" ) ]; | |||||||||||||
| 175 | } | ||||||||||||||
| 176 | |||||||||||||||
| 177 | # Successful send of message! | ||||||||||||||
| 178 | 0 | return [ 1, $id ]; | |||||||||||||
| 179 | } | ||||||||||||||
| 180 | |||||||||||||||
| 181 | 1 | 1; | |||||||||||||
| 182 | __END__ | ||||||||||||||