| 1 | package EC::Attachments; | ||
| 2 | |||
| 3 | $VERSION=0.10; | ||
| 4 | |||
| 5 | use MIME::Base64; | ||
| 6 | |||
| 7 | my $outgoing_mime_boundary = "_----------------------------------"; | ||
| 8 | |||
| 9 | my @default_mime_headers = ('MIME-Version: 1.0', | ||
| 10 | 'Content-Type: text/plain; charset="us-ascii"', | ||
| 11 | 'Content-Transfer-Encoding: 7bit'); | ||
| 12 | |||
| 13 | my @base64_headers = ('MIME-Version: 1.0', | ||
| 14 | 'Content-Type: multipart/mixed; boundary="'. | ||
| 15 | $outgoing_mime_boundary.'"', | ||
| 16 | 'Content-Transfer-Encoding: base64'); | ||
| 17 | |||
| 18 | sub attachment_filenames { | ||
| 19 | my ($msg) = @_; | ||
| 20 | my (@filenames, $name); | ||
| 21 | my $boundary = mime_boundary ($msg); | ||
| 22 | my @msglines = split /\n/, $msg; | ||
| 23 | foreach my $l (@msglines) { | ||
| 24 | if ($l =~ /filename\=/si) { | ||
| 25 | ($name) = ($l =~ /filename=(.+)/si); | ||
| 26 | # In case the file name is in quotes. | ||
| 27 | $name =~ s/\"//g; | ||
| 28 | push @filenames, ($name); | ||
| 29 | } | ||
| 30 | } | ||
| 31 | return @filenames; | ||
| 32 | } | ||
| 33 | |||
| 34 | sub mime_boundary { | ||
| 35 | my ($msg) = @_; | ||
| 36 | my ($boundary) = ($msg =~ /boundary=(.*?)\n/si); | ||
| 37 | $boundary =~ s/\"//g; | ||
| 38 | return $boundary; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub save_attachment { | ||
| 42 | my ($msg, $attachmentfilename, $ofilename) = @_; | ||
| 43 | my $boundary = &mime_boundary ($msg); | ||
| 44 | # RFC 2046 - attachment body separated from preceding boundary | ||
| 45 | # and attachment headers by two CRLFs - translated to Unix line | ||
| 46 | # endings here. Boundary at the end of the attachment is preceded | ||
| 47 | # in practice by two newlines. | ||
| 48 | my ($cstr) = ($msg =~ | ||
| 49 | m"filename=\"?$attachmentfilename\"?.*?\n\n(.*?)\n+--$boundary"smi); | ||
| 50 | $line = MIME::Base64::decode_base64 ($cstr); | ||
| 51 | open FILE, ">$ofilename" or | ||
| 52 | warn "Could not save $ofilename: $!\n"; | ||
| 53 | print FILE $line; | ||
| 54 | close FILE; | ||
| 55 | } | ||
| 56 | |||
| 57 | ### Required plain and base64 message header fields. | ||
| 58 | |||
| 59 | sub default_mime_headers { return @default_mime_headers } | ||
| 60 | |||
| 61 | sub base64_headers { return @base64_headers } | ||
| 62 | |||
| 63 | |||
| 64 | ### Headers for each attachment | ||
| 65 | |||
| 66 | # | ||
| 67 | # This gets inserted before the message text so no | ||
| 68 | # additional formatting is necessary. | ||
| 69 | # | ||
| 70 | sub text_attachment_header { | ||
| 71 | return ( "", | ||
| 72 | "This is a multi-part message in MIME format.", | ||
| 73 | '--'.$outgoing_mime_boundary, | ||
| 74 | "Content-Type: text/plain; charset=us-ascii", | ||
| 75 | "Content-Transfer-Encoding: 7bit", | ||
| 76 | ""); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub format_attachment { | ||
| 80 | my ($filepath) = @_; | ||
| 81 | my (@formatted,$basename); | ||
| 82 | ($basename) = ($filepath =~ /.*\/(.*)/si); | ||
| 83 | push @formatted, ('--'.$outgoing_mime_boundary, | ||
| 84 | "Content-Type: application/octet-stream; name=\"$basename\"", | ||
| 85 | "Content-Transfer-Encoding: base64", | ||
| 86 | "Content-Disposition: attachment; filename=\"$basename\""); | ||
| 87 | push @formatted, (''); | ||
| 88 | |||
| 89 | open FILE, "$filepath" or | ||
| 90 | warn "Could not encode $filepath: $!\n"; | ||
| 91 | my $s = ''; | ||
| 92 | while (defined ($line = <FILE>)) { | ||
| 93 | $s .= $line; | ||
| 94 | } | ||
| 95 | close FILE; | ||
| 96 | my $encoded = MIME::Base64::encode_base64 ($s); | ||
| 97 | my @lines = split /\n/, $encoded; | ||
| 98 | push @formatted, @lines; | ||
| 99 | push @formatted, (''); | ||
| 100 | return @formatted; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub outgoing_mime_boundary { | ||
| 104 | return $outgoing_mime_boundary; | ||
| 105 | } | ||
| 106 | |||
| 107 | 1; |