[3] | 1 | #! /usr/bin/perl |
---|
| 2 | # |
---|
| 3 | # Copyright (c) 2004-2007 - Consultas, PKG.fr |
---|
| 4 | # |
---|
| 5 | # This file is part of A2P. |
---|
| 6 | # |
---|
| 7 | # A2P is free software; you can redistribute it and/or modify |
---|
| 8 | # it under the terms of the GNU General Public License as published by |
---|
| 9 | # the Free Software Foundation; either version 2 of the License, or |
---|
| 10 | # (at your option) any later version. |
---|
| 11 | # |
---|
| 12 | # A2P is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
| 18 | # along with A2P; if not, write to the Free Software |
---|
| 19 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 20 | # |
---|
| 21 | # $Id: e-adresse.pl 3 2007-10-18 16:20:19Z guillaume $ |
---|
| 22 | # |
---|
| 23 | # Program to handle service XML structure to send e-mail and return a status |
---|
| 24 | # |
---|
| 25 | |
---|
| 26 | # System libraries |
---|
| 27 | use strict; |
---|
| 28 | |
---|
| 29 | # A2P libraries |
---|
| 30 | use A2P::EService::Tools qw( try abort get_conf get_id get_lib |
---|
| 31 | get_useragent get_work ); |
---|
| 32 | use A2P::EService::SimpleXML ; |
---|
| 33 | |
---|
| 34 | BEGIN { |
---|
| 35 | our $REVISION = sprintf "%s", q$Rev: 906 $ =~ /(\d[0-9.]+)\s+/ ; |
---|
| 36 | } |
---|
| 37 | our $REVISION ; |
---|
| 38 | |
---|
| 39 | # Step 0: Read configuration and load libraries |
---|
| 40 | my ( $service ) = $0 =~ m|([^/.]+)(\.pl)*$| ; |
---|
| 41 | |
---|
| 42 | # Expected lists in configuration |
---|
| 43 | my @getfromconf = qw( timeout SMTP body attachment From To Cc Bcc ) ; |
---|
| 44 | |
---|
| 45 | try "Reading $service rev$REVISION configuration" ; |
---|
| 46 | my $conf = &get_conf( $service, @getfromconf ); |
---|
| 47 | abort "Configuration not generated" unless ( ref($conf) =~ /^HASH/i ); |
---|
| 48 | |
---|
| 49 | try "Loading libraries" ; |
---|
| 50 | map { &get_lib(@{$_}) or abort "$_ not found" } ( |
---|
| 51 | # Library to use supported version |
---|
| 52 | [ "Net::SMTP" => 2.29 ], |
---|
| 53 | [ "MIME::Types" => 1.17 ], |
---|
| 54 | [ "MIME::Lite" => 3.01 ], |
---|
| 55 | [ "Mail::Address" => 1.74 ] |
---|
| 56 | ) ; |
---|
| 57 | |
---|
| 58 | # Step 1: Get pdf file and e-adresse file as job list |
---|
| 59 | try "Reading job to do" ; |
---|
| 60 | my @job = &get_work() ; |
---|
| 61 | |
---|
| 62 | try "Checking '@job' work to do" ; |
---|
| 63 | abort unless ( @job == 2 and grep { defined and $_ } @job == 2 ); |
---|
| 64 | |
---|
| 65 | my ( $pdf, $xmlfile ) = @job ; |
---|
| 66 | |
---|
| 67 | # Step 2: Read e-adresse file and verify its format |
---|
| 68 | try "Loading new $service XML file" ; |
---|
| 69 | my $xml = new A2P::EService::SimpleXML($xmlfile); |
---|
| 70 | abort "Bad '$xmlfile' XML file" |
---|
| 71 | unless ( defined($xml) and $xml ); |
---|
| 72 | |
---|
| 73 | my $name = $xml->name ; |
---|
| 74 | abort "Unexpected '$name' root node for $xmlfile" |
---|
| 75 | unless ( $name =~ /^eadresse$/ ); |
---|
| 76 | |
---|
| 77 | # Read attributs for the mail |
---|
| 78 | try "Reading mail attributs" ; |
---|
| 79 | my $xmlid = $xml->attribut('id') ; |
---|
| 80 | my $importance = $xml->attribut('importance') || 'normal' ; |
---|
| 81 | my $sensitivity = $xml->attribut('sensitivity') || 'normal' ; |
---|
| 82 | my $notification = $xml->attribut('notification') || 'no' ; |
---|
| 83 | my $confirmation = $xml->attribut('confirmation') || 'none' ; |
---|
| 84 | |
---|
| 85 | # Read recipients |
---|
| 86 | try "Reading mail recipients" ; |
---|
| 87 | my @from = $xml->nodes( 'rcpt', { type => qr/^from$/ }, 'name' ); |
---|
| 88 | my @to = $xml->nodes( 'rcpt', { type => qr/^to|$/ }, 'name' ); |
---|
| 89 | my @cc = $xml->nodes( 'rcpt', { type => qr/^cc$/ }, 'name' ); |
---|
| 90 | my @bcc = $xml->nodes( 'rcpt', { type => qr/^bcc$/ }, 'name' ); |
---|
| 91 | |
---|
| 92 | # Basic checks on recipients |
---|
| 93 | try "Checking From recipient" ; |
---|
| 94 | abort "Only one From recipient supported" unless ( @from < 2 ); |
---|
| 95 | abort "No From recipient found" unless @from ; |
---|
| 96 | try "Checking To recipients" ; |
---|
| 97 | abort "No To recipient found" unless @to ; |
---|
| 98 | |
---|
| 99 | # Recomposition |
---|
| 100 | foreach my $key (qw(From To Cc Bcc)) { |
---|
| 101 | my $keyconf = $conf->{$key} ; |
---|
| 102 | next unless (defined($keyconf) and ref($keyconf) =~ /^HASH/i); |
---|
| 103 | next unless (defined($keyconf->{active}) and $keyconf->{active}); |
---|
| 104 | |
---|
| 105 | my @address = grep { defined } |
---|
| 106 | ( $keyconf->{address}, { name => $keyconf->{name} } ); |
---|
| 107 | |
---|
| 108 | abort "Bad $key reconfiguration with '@address'" |
---|
| 109 | unless ( @address == 2 ); |
---|
| 110 | |
---|
| 111 | $keyconf->{replace} = 1 if ( $key eq 'From' ); |
---|
| 112 | |
---|
| 113 | if (defined($keyconf->{replace}) and $keyconf->{replace}) { |
---|
| 114 | eval '@' . lc($key) . ' = ( \@address )' ; |
---|
| 115 | |
---|
| 116 | } else { |
---|
| 117 | eval 'push @' . lc($key) . ', \@address' ; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | # Sub to recompose recipient in RFC standard |
---|
| 122 | sub recipient { |
---|
| 123 | return $_[0] unless ( ref($_[0]) =~ /^ARRAY/i ); |
---|
| 124 | my ( $content, $attributs ) = @{$_[0]} ; |
---|
| 125 | my $name = exists($attributs->{name}) ? $attributs->{name} : "" ; |
---|
| 126 | return $content unless ($name); |
---|
| 127 | my $address = new Mail::Address( $name, $content) ; |
---|
| 128 | return $address->format ; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | # Advanced check of recipients |
---|
| 132 | try "Scanning recipients" ; |
---|
| 133 | @from = map { &recipient($_) } @from ; |
---|
| 134 | @to = map { &recipient($_) } @to ; |
---|
| 135 | @cc = map { &recipient($_) } @cc ; |
---|
| 136 | @bcc = map { &recipient($_) } @bcc ; |
---|
| 137 | |
---|
| 138 | # Read subject and text of the mail |
---|
| 139 | try "Getting subject" ; |
---|
| 140 | my @subject = $xml->nodes( 'subject' ); |
---|
| 141 | |
---|
| 142 | try "Checking subject" ; |
---|
| 143 | abort "Only one subject supported" unless ( @subject < 2 ); |
---|
| 144 | abort "No subject found" unless @subject ; |
---|
| 145 | |
---|
| 146 | # Extract the subject |
---|
| 147 | my ( $subject ) = @subject ; |
---|
| 148 | chomp $subject ; |
---|
| 149 | abort "Subject must not contain line feed" if ( $subject =~ /\n/ms ); |
---|
| 150 | |
---|
| 151 | # This last call must be done after nodes calls to avoid getting the content of |
---|
| 152 | # nodes in the body. |
---|
| 153 | try "Getting mail body" ; |
---|
| 154 | my @body = $xml->nodes( 'body' ); |
---|
| 155 | |
---|
| 156 | try "Checking mail body" ; |
---|
| 157 | abort "Only one body node supported" unless ( @body < 2 ); |
---|
| 158 | abort "No body found" unless @body ; |
---|
| 159 | my ( $body ) = @body ; |
---|
| 160 | |
---|
| 161 | # Set unique id for this mail and user-agent |
---|
| 162 | my $mailid = &get_id( $xmlid ) ; |
---|
| 163 | my $user_agent = &get_useragent() ; |
---|
| 164 | |
---|
| 165 | # Step 3: Prepare the mail and attach the PDF to the mail |
---|
| 166 | try "Preparing the mail" ; |
---|
| 167 | |
---|
| 168 | # Legal values |
---|
| 169 | my %supported = ( |
---|
| 170 | Disposition => {'inline' => 1, 'attachment' => 1}, |
---|
| 171 | Type => {'multipart/mixed' => 1 , 'text/plain' => 1, |
---|
| 172 | 'application/pdf' => 1}, |
---|
| 173 | Encoding => {'7bit' => 1, '8bit' => 1, 'quoted-printable' => 0, |
---|
| 174 | 'base64' => 1}, |
---|
| 175 | Charset => {'iso-8859-1' => 1, 'UTF-8' => 1}, |
---|
| 176 | Importance => {'low' => 1, 'normal' => 1, 'high'=> 1}, |
---|
| 177 | Sensitivity => {'normal' => 1, 'confidential' => 'Company-Confidential', |
---|
| 178 | 'private' => 'Private', 'personal' => 'Personal'}, |
---|
| 179 | Notification => {'no' => 1, 'yes' => 1}, |
---|
| 180 | Confirmation => { 'no' => 1, 'none' => 1, 'yes' => 1 } |
---|
| 181 | ); |
---|
| 182 | |
---|
| 183 | # Preparing headers |
---|
| 184 | my ( $from ) = @from; |
---|
| 185 | |
---|
| 186 | my $mail = MIME::Lite -> new ( |
---|
| 187 | From => $from, |
---|
| 188 | To => \@to, |
---|
| 189 | Cc => \@cc, |
---|
| 190 | Bcc => \@bcc, |
---|
| 191 | Subject => $subject, |
---|
| 192 | Type => 'multipart/mixed', |
---|
| 193 | 'Message-ID' => $mailid, |
---|
| 194 | 'User-Agent:' => $user_agent, |
---|
| 195 | 'Return-Path' => $from |
---|
| 196 | ); |
---|
| 197 | |
---|
| 198 | # Preparing optionnal headers |
---|
| 199 | abort "Bad xml attribute importance: $importance" |
---|
| 200 | unless(exists($supported{Importance} -> {$importance}) and |
---|
| 201 | $supported{Importance} -> {$importance}); |
---|
| 202 | $mail -> add('Importance' => $importance) |
---|
| 203 | unless($importance eq 'normal'); |
---|
| 204 | |
---|
| 205 | abort "Bad xml attribute sensitivity: $sensitivity" |
---|
| 206 | unless(exists($supported{Sensitivity} -> {$sensitivity}) and |
---|
| 207 | $supported{Sensitivity} -> {$sensitivity}); |
---|
| 208 | $mail -> add('Sensitivity' => $supported{Sensitivity} -> {$sensitivity}) |
---|
| 209 | unless($sensitivity eq 'normal'); |
---|
| 210 | |
---|
| 211 | abort "Bad xml attribute notification: $notification" |
---|
| 212 | unless(exists($supported{Notification} -> {$notification}) and |
---|
| 213 | $supported{Notification} -> {$notification}); |
---|
| 214 | $mail -> add('Return-Receipt-To' => $from) |
---|
| 215 | unless($notification eq 'no'); |
---|
| 216 | |
---|
| 217 | abort "Bad xml attribute confirmation: $confirmation" |
---|
| 218 | unless(exists($supported{Confirmation} -> {$confirmation}) and |
---|
| 219 | $supported{Confirmation} -> {$confirmation}); |
---|
| 220 | $mail -> add('Disposition-Notification-To' => $from) |
---|
| 221 | unless($confirmation eq 'no' or $confirmation eq 'none'); |
---|
| 222 | |
---|
| 223 | # Preparing and attach body message |
---|
| 224 | my $attr = $conf -> {body}; |
---|
| 225 | |
---|
| 226 | my $dispo = defined($attr -> {disposition})? |
---|
| 227 | $attr -> {disposition}:'inline'; |
---|
| 228 | abort "Bad conf for mail disposition: $dispo" |
---|
| 229 | unless(exists($supported{Disposition} -> {$dispo}) and |
---|
| 230 | $supported{Disposition} -> {$dispo}); |
---|
| 231 | my $type = defined($attr -> {type})?$attr -> {type}:'TEXT'; |
---|
| 232 | abort "Bad conf for mail type: $type" |
---|
| 233 | unless(exists($supported{Type} -> {$type}) and |
---|
| 234 | $supported{Type} -> {$type}); |
---|
| 235 | my $encoding = defined($attr -> {encoding})? |
---|
| 236 | $attr -> {encoding}:'8bit'; |
---|
| 237 | abort "Bad conf for mail encoding: $encoding" |
---|
| 238 | unless(exists($supported{Encoding} -> {$encoding}) and |
---|
| 239 | $supported{Encoding} -> {$encoding}); |
---|
| 240 | my $charset = defined($attr -> {charset})? |
---|
| 241 | $attr -> {charset}:'iso-8859-1'; |
---|
| 242 | abort "Bad conf for mail charset: $charset" |
---|
| 243 | unless(exists($supported{Charset} -> {$charset}) and |
---|
| 244 | $supported{Charset} -> {$charset}); |
---|
| 245 | |
---|
| 246 | my $msg = MIME::Lite -> new ( |
---|
| 247 | Disposition => $dispo, |
---|
| 248 | Type => $type, |
---|
| 249 | Encoding => $encoding, |
---|
| 250 | Data => $body, |
---|
| 251 | ); |
---|
| 252 | $msg -> attr('content-type.charset' => $charset); |
---|
| 253 | $mail -> attach($msg); |
---|
| 254 | |
---|
| 255 | # Attach the PDF |
---|
| 256 | my ( $filename ) = $pdf =~ m|([^/]*)$|; |
---|
| 257 | my $attachment = $conf -> {attachment}; |
---|
| 258 | |
---|
| 259 | unless (defined($attachment->{skip}) and $attachment->{skip}) { |
---|
| 260 | $dispo = defined($attachment -> {disposition})? |
---|
| 261 | $attachment -> {disposition}:'inline'; |
---|
| 262 | abort "Bad conf for attachment disposition: $dispo" |
---|
| 263 | unless(exists($supported{Disposition} -> {$dispo}) and |
---|
| 264 | $supported{Disposition} -> {$dispo}); |
---|
| 265 | $type = defined($attachment -> {type})? |
---|
| 266 | $attachment -> {type}:'application/pdf'; |
---|
| 267 | abort "Bad conf for attachment type: $type" |
---|
| 268 | unless(exists($supported{Type} -> {$type}) and |
---|
| 269 | $supported{Type} -> {$type}); |
---|
| 270 | $encoding = defined($attachment -> {encoding})? |
---|
| 271 | $attachment -> {encoding}:'base64'; |
---|
| 272 | abort "Bad conf for attachment encoding: $encoding" |
---|
| 273 | unless(exists($supported{Encoding} -> {$encoding}) and |
---|
| 274 | $supported{Encoding} -> {$encoding}); |
---|
| 275 | |
---|
| 276 | # By default, we are trying to extract name from subject using |
---|
| 277 | # eventually a regex an sprintf template from configuration |
---|
| 278 | my $pdfname_re = qr/:\s+([^#]+)\s+#\s+([^#]+)\s+#\s+([^#]+)\s+#/ ; |
---|
| 279 | my $pdfname_printf = '%s-%s-%s.pdf' ; |
---|
| 280 | if (!defined($attachment -> {name}) |
---|
| 281 | or $attachment -> {name} =~ /^from subject$/i) { |
---|
| 282 | # Get regexp from conf if available |
---|
| 283 | $pdfname_re = $attachment -> {name_re} |
---|
| 284 | if (defined($attachment -> {name_re}) |
---|
| 285 | and ref($attachment -> {name_re}) =~ /^Regexp/i); |
---|
| 286 | |
---|
| 287 | # Get template name if available |
---|
| 288 | $pdfname_printf = $attachment -> {name_printf} |
---|
| 289 | if (defined($attachment -> {name_printf}) |
---|
| 290 | and $attachment -> {name_printf} ); |
---|
| 291 | |
---|
| 292 | my @elements = $subject =~ $pdfname_re ; |
---|
| 293 | $filename = sprintf( $pdfname_printf, @elements ); |
---|
| 294 | abort "Can't compute valid pdfname from '@elements' and $pdfname_printf" |
---|
| 295 | unless ( $filename and $filename =~ /^.+\.pdf$/i ); |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | $mail -> attach ( |
---|
| 299 | Disposition => $dispo, |
---|
| 300 | Type => $type, |
---|
| 301 | Encoding => $encoding, |
---|
| 302 | Path => $pdf, |
---|
| 303 | Filename => $filename, |
---|
| 304 | ); |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | # Step 4: Send the mail |
---|
| 308 | try "Send the mail" ; |
---|
| 309 | |
---|
| 310 | $! = 0; |
---|
| 311 | my $SMTP = $conf->{SMTP} ; |
---|
| 312 | my %supported_smtp = ( |
---|
| 313 | 'smtp' => 1 , |
---|
| 314 | 'sub' => 1 , |
---|
| 315 | 'sendmail' => 1 |
---|
| 316 | ); |
---|
| 317 | |
---|
| 318 | abort "No server defined" |
---|
| 319 | unless ( defined($conf -> {SMTP}) and $conf -> {SMTP} ); |
---|
| 320 | |
---|
| 321 | our $host = "" ; |
---|
| 322 | my $sent = 0 ; |
---|
| 323 | |
---|
| 324 | my @send_errors = () ; |
---|
| 325 | $SIG{__DIE__} = sub { |
---|
| 326 | my ( $mesg ) = "@_" =~ /^(.*) at \(eval.*$/ms ; |
---|
| 327 | push @send_errors, $host . ": $mesg" ; |
---|
| 328 | } ; |
---|
| 329 | |
---|
| 330 | # Check also the order |
---|
| 331 | my @types = grep { exists($supported_smtp{$_}) and $supported_smtp{$_} } |
---|
| 332 | keys %{$SMTP} ; |
---|
| 333 | abort "No supported type in configuration" unless @types ; |
---|
| 334 | |
---|
| 335 | if (exists($SMTP->{order})) { |
---|
| 336 | my @order = @{$SMTP->{order}} ; |
---|
| 337 | my @newlist = () ; |
---|
| 338 | while (@order) { |
---|
| 339 | my $next = shift @order ; |
---|
| 340 | push @newlist, $next if ( grep { /^$next$/ } @types ); |
---|
| 341 | } |
---|
| 342 | # Check we still have the same number of types |
---|
| 343 | abort "Bad order list in configuration" unless ( @newlist == @types ); |
---|
| 344 | @types = @newlist ; |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | foreach my $type ( @types ) { |
---|
| 348 | |
---|
| 349 | my $server = $SMTP -> {$type}; |
---|
| 350 | foreach $host (keys %{$server}) { |
---|
| 351 | if ($type eq 'smtp') { |
---|
| 352 | my $port = $server -> {$host} || 25 ; |
---|
| 353 | $sent ++ and last |
---|
| 354 | if eval '$mail->send($type, $host, Port => $port)' ; |
---|
| 355 | |
---|
| 356 | } elsif ($type eq 'sendmail') { |
---|
| 357 | my @args = @{$server -> {$host}} ; |
---|
| 358 | $sent ++ and last |
---|
| 359 | if eval '$mail->send($type, @args)' ; |
---|
| 360 | |
---|
| 361 | } elsif ($type eq 'sub') { |
---|
| 362 | my $sub = $server -> {$host} ; |
---|
| 363 | $sent ++ and last |
---|
| 364 | if eval '$mail->send($type, $sub, $host)' ; |
---|
| 365 | } |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | last if $sent ; |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | abort "@send_errors" unless $sent ; |
---|
| 372 | |
---|
| 373 | # Step 5: Report status |
---|
| 374 | |
---|
| 375 | exit(0); |
---|