| 1 | # Mail::MboxParser - object-oriented access to UNIX-mailboxes | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # Copyright (C) 2001 Tassilo v. Parseval | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # This program is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | # Version: $Id: Mail.pm,v 1.53 2005/11/23 09:30:12 parkerpine Exp $ | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | package Mail::MboxParser::Mail; | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 20 | require 5.004; | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 20 | ▶ | use base qw(Exporter Mail::MboxParser::Base); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | Mail::MboxParser::Mail - Provide mail-objects and methods upon | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | See L<Mail::MboxParser> for an outline on usage. Examples however are also | ||||||||||||||||||||||||||||||||||||||||||
| 24 | provided in this manpage further below. | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | Mail::MboxParser::Mail objects are usually not created directly though, in | ||||||||||||||||||||||||||||||||||||||||||
| 29 | theory, they could be. A description of the provided methods can be found in | ||||||||||||||||||||||||||||||||||||||||||
| 30 | L<Mail::MboxParser>. | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | However, go on reading if you want to use methods from MIME::Entity and learn | ||||||||||||||||||||||||||||||||||||||||||
| 33 | about overloading. | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 20 | ▶ | use Mail::MboxParser::Mail::Body; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | 20 | ▶ | use Mail::MboxParser::Mail::Convertable; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 20 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 20 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 20 | ▶ | use vars qw($VERSION @EXPORT $AUTOLOAD $NL); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 20 | $VERSION = "0.45"; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 20 | @EXPORT = qw(); | |||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | # we'll use it to store the MIME::Parser | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 20 | my $Parser; | |||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 20 | ▶ | use overload '""' => \&as_string, fallback => 1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 20 | ▶ | BEGIN { $Mail::MboxParser::Mail::NL = "\n" } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | use constant | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 20 | ▶ | HAVE_ENCODE => eval { require Encode; 1 } || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.0
Condition50%1/2
eval {
do {
require Encode;
1
}
} || 0
MC/DC0%0/1
eval {
do {
require Encode;
1
}
} || 0
eval {
do {
require Encode;
1
}
}
Truth table50%1/2
A
A= eval {
do {
require Encode;
1
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | use constant | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 20 | ▶ | HAVE_MIMEWORDS => eval { require MIME::Words; 1 } || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.0
Condition50%1/2
eval {
do {
require MIME::Words;
1
}
} || 0
MC/DC0%0/1
eval {
do {
require MIME::Words;
1
}
} || 0
eval {
do {
require MIME::Words;
1
}
}
Truth table50%1/2
A
A= eval {
do {
require MIME::Words;
1
}
}
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | =item B<new(header, body)> | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | This is usually not called directly but instead by C<get_messages()>. You could | ||||||||||||||||||||||||||||||||||||||||||
| 67 | however create a mail-object manually providing the header and body each as | ||||||||||||||||||||||||||||||||||||||||||
| 68 | either one string or as an array-ref representing the lines. | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | Here is a common scenario: Retrieving mails from a remote POP-server using | ||||||||||||||||||||||||||||||||||||||||||
| 71 | Mail::POP3Client and directly feeding each mail to | ||||||||||||||||||||||||||||||||||||||||||
| 72 | C<Mail::MboxParser::Mail-E<gt>new>: | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | use Mail::POP3Client; | ||||||||||||||||||||||||||||||||||||||||||
| 75 | use Mail::MboxParser::Mail; | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | my $pop = new Mail::POP3Client (...); | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | for my $i (1 .. $pop->Count) { | ||||||||||||||||||||||||||||||||||||||||||
| 80 | my $msg = Mail::MboxParser::Mail->new( [ $pop->Head($i) ], | ||||||||||||||||||||||||||||||||||||||||||
| 81 | [ $pop->Body($i) ] ); | ||||||||||||||||||||||||||||||||||||||||||
| 82 | $msg->store_all_attachments( path => '/home/user/dump' ); | ||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | The above effectively behaves like an attachment-only retriever. | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 151 | ▶ | sub init (@) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=8 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 151 | my ($self, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 151 | my ($header, $body, $conf) = @args; | |||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 151 | ▶ | $self->{HEADER} = ref $header ? $header : [ split /$NL/, $header ]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 151 | $self->{HEADER_HASH} = \&_split_header; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 151 | ▶ | $self->{BODY} = ref $body ? $body : [ split /$NL/, $body ]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 151 | $self->{TOP_ENTITY} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 151 | $self->{ARGS} = $conf; | |||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 151 | ▶ | if (! $self->{ARGS}->{uudecode} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | # set default for 'uudecode' option | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 151 | $self->{ARGS}->{uudecode} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | # make sure line-endings are ok if called directly | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 151 | ▶ | if (caller(1) ne 'Mail::MboxParser') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 4 | $self->{ARGS}->{join_string} = ''; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 4 | for (@{ $self->{HEADER} }, @{ $self->{BODY} }) { | |||||||||||||||||||||||||||||||||||||||||
| 110 | 260 | ▶ | $_ .= "\n" unless /.*\n$/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 4 | ▶ | push @{ $self->{HEADER} }, "\n" if $self->{HEADER}->[-1] ne "\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 151 | $self; | |||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | =item B<header> | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | Returns the mail-header as a hash-ref with header-fields as keys. All keys are | ||||||||||||||||||||||||||||||||||||||||||
| 124 | turned to lower-case, so C<$header{Subject}> has to be written as | ||||||||||||||||||||||||||||||||||||||||||
| 125 | C<$header{subject}>. | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | If a header-field occurs more than once in the header, the value of the key is | ||||||||||||||||||||||||||||||||||||||||||
| 128 | an array_ref. Example: | ||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | my $field = $msg->header->{field}; | ||||||||||||||||||||||||||||||||||||||||||
| 131 | print $field->[0]; # first occurance of 'field' | ||||||||||||||||||||||||||||||||||||||||||
| 132 | print $field->[1]; # second one | ||||||||||||||||||||||||||||||||||||||||||
| 133 | ... | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 56 | ▶ | sub header() { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
header: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 140 | 56 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 141 | 56 | ▶ | my $decode = $self->{ARGS}->{decode} || 'NEVER'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'ARGS'}{'decode'} || 'NEVER'
MC/DC100%1/1
$self->{'ARGS'}{'decode'} || 'NEVER'
$self->{'ARGS'}{'decode'}
Truth table100%2/2
A
A= $self->{'ARGS'}{'decode'}
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 56 | $self->reset_last; | |||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 56 | return $self->{HEADER_HASH}->($self, $self->{HEADER}, $decode); | |||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | =item B<from_line> | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | Returns the "From "-line of the message. | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | 6 | ▶ | sub from_line() { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from_line: called CC=3 SCAR=12.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 161 | 6 | $self->reset_last; | |||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | $self->{HEADER_HASH}->($self, $self->{HEADER}, 'NEVER') | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 6 | ▶ | if !exists $self->{FROM}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 6 | ▶ | if (! exists $self->{FROM}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | $self->{LAST_ERR} = "Message did not contain a From-line"; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | 6 | $self->{FROM}; | |||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | =item B<trace> | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | This method returns the "Received: "-lines of the message as a list. | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | 6 | ▶ | sub trace () { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
trace: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 187 | 6 | $self->reset_last; | |||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | $self->{HEADER_HASH}->($self, $self->{HEADER}, 'NEVER') | ||||||||||||||||||||||||||||||||||||||||||
| 190 | 6 | ▶ | if ! exists $self->{TRACE}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | 6 | ▶ | if (! exists $self->{TRACE}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 0 | $self->{LAST_ERR} = "Message did not contain any Received-lines"; | |||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | 6 | @{ $self->{TRACE} }; | |||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | =item B<body> | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | =item B<body(n)> | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | Returns a Mail::MboxParser::Mail::Body object. For methods upon that see | ||||||||||||||||||||||||||||||||||||||||||
| 209 | further below. When called with the argument n, the n-th body of the message is | ||||||||||||||||||||||||||||||||||||||||||
| 210 | retrieved. That is, the body of the n-th entity. | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | Sets C<$mail-E<gt>error> if something went wrong. | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | 22 | ▶ | sub body(;$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
body: called CC=7 SCAR=20.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 22 | my ($self, $num) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 220 | 22 | $self->reset_last; | |||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | 22 | ▶ | if (defined $num && $num >= $self->num_entities) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $num and $num >= $self->num_entities
MC/DC0%0/2
defined $num and $num >= $self->num_entities
defined $num
$num >= $self->num_entities
Truth table66%2/3
A and B
A= defined $numB= $num >= $self->num_entities
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | $self->{LAST_ERR} = "No such body"; | |||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | # body needs the "Content-type: ... boundary=" stuff | ||||||||||||||||||||||||||||||||||||||||||
| 228 | # in order to decide which lines are part of signature and | ||||||||||||||||||||||||||||||||||||||||||
| 229 | # which lines are not (ie denote a MIME-part) | ||||||||||||||||||||||||||||||||||||||||||
| 230 | 22 | my $bound; | |||||||||||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||||||||||||
| 232 | # particular entity desired? | ||||||||||||||||||||||||||||||||||||||||||
| 233 | # we need to read the header of this entity then :-( | ||||||||||||||||||||||||||||||||||||||||||
| 234 | 22 | ▶ | if (defined $num) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | 18 | my $ent = $self->get_entities($num); | |||||||||||||||||||||||||||||||||||||||||
| 236 | 18 | ▶ | if ($bound = $ent->head->get('content-type')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 237 | 6 | $bound =~ /boundary="(.*)"/; $bound = $1; | |||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||
| 239 | 18 | return Mail::MboxParser::Mail::Body->new($ent, $bound, $self->{ARGS}); | |||||||||||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | # else | ||||||||||||||||||||||||||||||||||||||||||
| 243 | 4 | ▶ | if ($bound = $self->header->{'content-type'}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 4 | $bound =~ /boundary="(.*)"/; $bound = $1; | |||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | return ref $self->{TOP_ENTITY} eq 'MIME::Entity' | ||||||||||||||||||||||||||||||||||||||||||
| 247 | ? Mail::MboxParser::Mail::Body->new($self->{TOP_ENTITY}, $bound, $self->{ARGS}) | ||||||||||||||||||||||||||||||||||||||||||
| 248 | 4 | ▶ | : Mail::MboxParser::Mail::Body->new(scalar $self->get_entities(0), $bound, $self->{ARGS}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | =item B<find_body> | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | This will return an index number that represents what Mail::MboxParser::Mail | ||||||||||||||||||||||||||||||||||||||||||
| 258 | considers to be the actual (main)-body of an email. This is useful if you don't | ||||||||||||||||||||||||||||||||||||||||||
| 259 | know about the structure of a message but want to retrieve the message's | ||||||||||||||||||||||||||||||||||||||||||
| 260 | signature for instance: | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | $signature = $msg->body($msg->find_body)->signature; | ||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||
| 264 | Changes are good that find_body does what it is supposed to do. | ||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||
| 270 | 36 | ▶ | sub find_body() { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
find_body: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | 36 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 272 | 36 | $self->{LAST_ERR} = "Could not find a suitable body at all"; | |||||||||||||||||||||||||||||||||||||||||
| 273 | 36 | my $num = -1; | |||||||||||||||||||||||||||||||||||||||||
| 274 | 36 | for my $part ($self->parts_DFS) { | |||||||||||||||||||||||||||||||||||||||||
| 275 | 40 | $num++; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 40 | ▶ | if ($part->effective_type eq 'text/plain') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 36 | $self->reset_last; last; | |||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||
| 280 | 36 | return $num; | |||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||
| 287 | =item B<make_convertable> | ||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | Returns a Mail::MboxParser::Mail::Convertable object. For details on what you | ||||||||||||||||||||||||||||||||||||||||||
| 290 | can do with it, read L<Mail::MboxParser::Mail::Convertable>. | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | ▶ | sub make_convertable(@) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
make_convertable: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 298 | return ref $self->{TOP_ENTITY} eq 'MIME::Entity' | ||||||||||||||||||||||||||||||||||||||||||
| 299 | ? Mail::MboxParser::Mail::Convertable->new($self->{TOP_ENTITY}) | ||||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | ▶ | : Mail::MboxParser::Mail::Convertable->new($self->get_entities(0)); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | =item B<get_field(headerfield)> | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | Returns the specified raw field from the message header, that is: the fieldname | ||||||||||||||||||||||||||||||||||||||||||
| 310 | is not stripped off nor is any decoding done. Returns multiple lines as needed | ||||||||||||||||||||||||||||||||||||||||||
| 311 | if the field is "Received" or another multi-line field. Not case sensitive. | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | C<get_field()> always returns one string regardless of how many times the field | ||||||||||||||||||||||||||||||||||||||||||
| 314 | occured in the header. Multiple occurances are separated by a newline and | ||||||||||||||||||||||||||||||||||||||||||
| 315 | multiple whitespaces squeezed to one. That means you can process each occurance | ||||||||||||||||||||||||||||||||||||||||||
| 316 | of the field thusly: | ||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||
| 318 | for my $field ( split /\n/, $msg->get_field('received') ) { | ||||||||||||||||||||||||||||||||||||||||||
| 319 | # do something with $field | ||||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | Sets C<$mail-E<gt>error> if the field was not found in which case | ||||||||||||||||||||||||||||||||||||||||||
| 323 | C<get_field()> returns C<undef>. | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | 4 | ▶ | sub get_field($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_field: called CC=8 SCAR=21.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | 4 | my ($self, $fieldname) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 331 | 4 | $self->reset_last; | |||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | my @headerlines = ref $self->{HEADER} | ||||||||||||||||||||||||||||||||||||||||||
| 334 | 4 | ? @{$self->{HEADER}} | |||||||||||||||||||||||||||||||||||||||||
| 335 | 4 | ▶ | : split /$NL/, $self->{HEADER}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 336 | 4 | chomp @headerlines; | |||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | 4 | my ($ret, $inretfield); | |||||||||||||||||||||||||||||||||||||||||
| 339 | 4 | foreach my $bit (@headerlines) { | |||||||||||||||||||||||||||||||||||||||||
| 340 | 100 | ▶ | if ($bit =~ /^\s/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | 42 | ▶ | if ($inretfield) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 342 | 2 | $bit =~ s/\s+/ /g; | |||||||||||||||||||||||||||||||||||||||||
| 343 | 2 | $ret .= $bit; | |||||||||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||
| 346 | elsif ($bit =~ /^$fieldname/i) { | ||||||||||||||||||||||||||||||||||||||||||
| 347 | 4 | $bit =~ s/\s+/ /g; | |||||||||||||||||||||||||||||||||||||||||
| 348 | 4 | $inretfield++; | |||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | ▶ | if (defined $ret) { $ret .= "\n" . $bit } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 350 | 4 | else { $ret .= $bit } | |||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||
| 352 | 54 | else { $inretfield = 0; } | |||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | 4 | ▶ | $self->{LAST_ERR} = "No such field" if not $ret; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 356 | 4 | return $ret; | |||||||||||||||||||||||||||||||||||||||||
| 357 | } | ||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||
| 363 | =item B<from> | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | Returns a hash-ref with the two fields 'name' and 'email'. Returns C<undef> if | ||||||||||||||||||||||||||||||||||||||||||
| 366 | empty. The name-field does not necessarily contain a value either. Example: | ||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | print $mail->from->{email}; | ||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | On behalf of suggestions I received from users, from() tries to be smart when | ||||||||||||||||||||||||||||||||||||||||||
| 371 | 'name'is empty and 'email' has the form 'first.name@host.com'. In this case, | ||||||||||||||||||||||||||||||||||||||||||
| 372 | 'name' is set to "First Name". | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | ▶ | sub from() { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
from: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | $self->reset_last; | |||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||
| 382 | 0 | my $from = $self->header->{from}; | |||||||||||||||||||||||||||||||||||||||||
| 383 | 0 | my ($name, $email) = split /\s\</, $from; | |||||||||||||||||||||||||||||||||||||||||
| 384 | 0 | ▶ | $email =~ s/\>$//g unless not $email; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 385 | 0 | ▶ | if ($name && ! $email) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$name and not $email
MC/DC0%0/2
$name and not $email
$name
not $email
Truth table0%0/3
A and not(B)
A= $nameB= not $email
| |||||||||||||||||||||||||||||||||||||||||||
| 386 | 0 | $email = $name; | |||||||||||||||||||||||||||||||||||||||||
| 387 | 0 | $name = ""; | |||||||||||||||||||||||||||||||||||||||||
| 388 | 0 | ▶ | $name = ucfirst($1) . " " . ucfirst($2) if $email =~ /^(.*?)\.(.*)@/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||||||||||||
| 390 | 0 | return {(name => $name, email => $email)}; | |||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||
| 397 | =item B<to> | ||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | Returns an array of hash-references of all to-fields in the mail-header. Fields | ||||||||||||||||||||||||||||||||||||||||||
| 400 | are the same as those of C<$mail-E<gt>from>. Example: | ||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||
| 402 | for my $recipient ($mail->to) { | ||||||||||||||||||||||||||||||||||||||||||
| 403 | print $recipient->{name} || "<no name>", "\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 404 | print $recipient->{email}; | ||||||||||||||||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||
| 407 | The same 'name'-smartness applies here as described under C<from()>. | ||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||
| 409 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||
| 411 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | ▶ | sub to() { shift->_recipients("to") } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
to: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||
| 415 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||
| 417 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | =item B<cc> | ||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||
| 421 | Identical with to() but returning the hash-refed "Cc: "-line. | ||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||
| 423 | The same 'name'-smartness applies here as described under C<from()>. | ||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||
| 427 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | 0 | ▶ | sub cc() { shift->_recipients("cc") } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cc: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||
| 431 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | =item B<id> | ||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | Returns the message-id of a message cutting off the leading and trailing '<' | ||||||||||||||||||||||||||||||||||||||||||
| 438 | and '>' respectively. | ||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||
| 440 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||
| 444 | 0 | ▶ | sub id() { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 445 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 446 | 0 | $self->reset_last; | |||||||||||||||||||||||||||||||||||||||||
| 447 | 0 | $self->header->{'message-id'} =~ /\<(.*)\>/; | |||||||||||||||||||||||||||||||||||||||||
| 448 | 0 | $1; | |||||||||||||||||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||
| 451 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | # -------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 454 | # MIME-related methods | ||||||||||||||||||||||||||||||||||||||||||
| 455 | #--------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||
| 457 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||
| 459 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||
| 461 | =item B<num_entities> | ||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||
| 463 | Returns the number of MIME-entities. That is, the number of sub-entitities | ||||||||||||||||||||||||||||||||||||||||||
| 464 | actually. If 0 is returned and you think this is wrong, check | ||||||||||||||||||||||||||||||||||||||||||
| 465 | C<$mail-E<gt>log>. | ||||||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||||||
| 467 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||
| 469 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||
| 471 | 502 | ▶ | sub num_entities() { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
num_entities: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 472 | 502 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 473 | 502 | $self->reset_last; | |||||||||||||||||||||||||||||||||||||||||
| 474 | # force list contest becaus of wantarray in get_entities | ||||||||||||||||||||||||||||||||||||||||||
| 475 | 502 | ▶ | $self->{NUM_ENT} = () = $self->get_entities unless defined $self->{NUM_ENT}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 476 | 502 | return $self->{NUM_ENT}; | |||||||||||||||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||
| 481 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||
| 483 | =item B<get_entities> | ||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||
| 485 | =item B<get_entities(n)> | ||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | Either returns an array of all MIME::Entity objects or one particular if called | ||||||||||||||||||||||||||||||||||||||||||
| 488 | with a number. If no entity whatsoever could be found, an empty list is | ||||||||||||||||||||||||||||||||||||||||||
| 489 | returned. | ||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||
| 491 | C<$mail-E<gt>log> instantly called after get_entities will give you some | ||||||||||||||||||||||||||||||||||||||||||
| 492 | information of what internally may have failed. If set, this will be an error | ||||||||||||||||||||||||||||||||||||||||||
| 493 | raised by MIME::Entity but you don't need to worry about it at all. It's just | ||||||||||||||||||||||||||||||||||||||||||
| 494 | for the record. | ||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||
| 498 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||
| 500 | 445 | ▶ | sub get_entities(@) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_entities: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 501 | 445 | my ($self, $num) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 502 | 445 | $self->reset_last; | |||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||
| 504 | 445 | ▶ | if (defined $num && $num >= $self->num_entities) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $num and $num >= $self->num_entities
MC/DC0%0/2
defined $num and $num >= $self->num_entities
defined $num
$num >= $self->num_entities
Truth table66%2/3
A and B
A= defined $numB= $num >= $self->num_entities
| |||||||||||||||||||||||||||||||||||||||||||
| 505 | 0 | $self->{LAST_ERR} = "No such entity"; | |||||||||||||||||||||||||||||||||||||||||
| 506 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||
| 509 | 445 | ▶ | if (ref $self->{TOP_ENTITY} ne 'MIME::Entity') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||
| 511 | 97 | ▶ | if (! defined $Parser) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 512 | 11 | eval { require MIME::Parser; }; | |||||||||||||||||||||||||||||||||||||||||
| 513 | 11 | $Parser = new MIME::Parser; $Parser->output_to_core(1); | |||||||||||||||||||||||||||||||||||||||||
| 514 | 11 | $Parser->extract_uuencode($self->{ARGS}->{uudecode}); | |||||||||||||||||||||||||||||||||||||||||
| 515 | } | ||||||||||||||||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||||||||||||||||
| 517 | 97 | my $data = $self->as_string; | |||||||||||||||||||||||||||||||||||||||||
| 518 | 97 | $self->{TOP_ENTITY} = $Parser->parse_data($data); | |||||||||||||||||||||||||||||||||||||||||
| 519 | } | ||||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||
| 521 | 445 | my @parts = eval { $self->{TOP_ENTITY}->parts_DFS; }; | |||||||||||||||||||||||||||||||||||||||||
| 522 | 445 | ▶ | $self->{LAST_LOG} = $@ if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 523 | 445 | ▶ | return wantarray ? @parts : $parts[$num]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 524 | } | ||||||||||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||||||||||
| 526 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||
| 528 | # just overriding MIME::Entity::parts() | ||||||||||||||||||||||||||||||||||||||||||
| 529 | # to work around its strange behaviour | ||||||||||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||||||||||
| 531 | 2 | ▶ | sub parts(@) { shift->get_entities(@_) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parts: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||||||||||
| 533 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||||||
| 535 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||||||||||
| 537 | =item B<get_entity_body(n)> | ||||||||||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||||||||||
| 539 | Returns the body of the n-th MIME::Entity as a single string, undef otherwise | ||||||||||||||||||||||||||||||||||||||||||
| 540 | in which case you could check C<$mail-E<gt>error>. | ||||||||||||||||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||||||||||||||||
| 542 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||
| 544 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||||||||||||||||
| 546 | 0 | ▶ | sub get_entity_body($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_entity_body: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 547 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 548 | 0 | my $num = shift; | |||||||||||||||||||||||||||||||||||||||||
| 549 | 0 | $self->reset_last; | |||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||
| 551 | 0 | ▶ | if ($num < $self->num_entities && | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$num < $self->num_entities and $self->get_entities($num)->bodyhandle
MC/DC0%0/2
$num < $self->num_entities and $self->get_entities($num)->bodyhandle
$num < $self->num_entities
$self->get_entities($num)->bodyhandle
Truth table0%0/3
A and B
A= $num < $self->num_entitiesB= $self->get_entities($num)->bodyhandle
| |||||||||||||||||||||||||||||||||||||||||||
| 552 | $self->get_entities($num)->bodyhandle) { | ||||||||||||||||||||||||||||||||||||||||||
| 553 | 0 | return $self->get_entities($num)->bodyhandle->as_string; | |||||||||||||||||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||||||||||||||||
| 555 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 556 | 0 | $self->{LAST_ERR} = "$num: No such entity"; | |||||||||||||||||||||||||||||||||||||||||
| 557 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||||||||||||||||
| 559 | } | ||||||||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||||||
| 561 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||||||||||||
| 563 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||||||||||||
| 565 | =item B<store_entity_body(n, handle =E<gt> FILEHANDLE)> | ||||||||||||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||||||||||||
| 567 | Stores the stringified body of n-th entity to the specified filehandle. That's | ||||||||||||||||||||||||||||||||||||||||||
| 568 | basically the same as: | ||||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||
| 570 | my $body = $mail->get_entity_body(0); | ||||||||||||||||||||||||||||||||||||||||||
| 571 | print FILEHANDLE $body; | ||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||
| 573 | and could be shortened to this: | ||||||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||||||
| 575 | $mail->store_entity_body(0, handle => \*FILEHANDLE); | ||||||||||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||||||||||
| 577 | It returns a true value on success and undef on failure. In this case, examine | ||||||||||||||||||||||||||||||||||||||||||
| 578 | the value of $mail->error since the entity you specified with 'n' might not | ||||||||||||||||||||||||||||||||||||||||||
| 579 | exist. | ||||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||
| 581 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||
| 583 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||||||||||
| 585 | 0 | ▶ | sub store_entity_body($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
store_entity_body: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 586 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 587 | 0 | my ($num, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 588 | 0 | $self->reset_last; | |||||||||||||||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||||||||||||||||
| 590 | 0 | ▶ | if (not $num || (not exists $args{handle} && | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$num or not exists $args{"handle"} && ref $args{"handle"} ne "GLOB"
exists $args{'handle'} && ref $args{'handle'} ne 'GLOB'
MC/DC0%0/3
$num or not exists $args{"handle"} && ref $args{"handle"} ne "GLOB"
$num
exists $args{'handle'}
ref $args{'handle'} ne 'GLOB'
Truth table0%0/4
A or not(B && C)
A= $numB= exists $args{'handle'}C= ref $args{'handle'} ne 'GLOB'
| |||||||||||||||||||||||||||||||||||||||||||
| 591 | ref $args{handle} ne 'GLOB')) { | ||||||||||||||||||||||||||||||||||||||||||
| 592 | 0 | croak <<EOC; | |||||||||||||||||||||||||||||||||||||||||
| 593 | Wrong number or type of arguments for store_entity_body. Second argument must | ||||||||||||||||||||||||||||||||||||||||||
| 594 | have the form handle => \*FILEHANDLE. | ||||||||||||||||||||||||||||||||||||||||||
| 595 | EOC | ||||||||||||||||||||||||||||||||||||||||||
| 596 | } | ||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||
| 598 | 0 | binmode $args{handle}; | |||||||||||||||||||||||||||||||||||||||||
| 599 | 0 | my $b = $self->get_entity_body($num); | |||||||||||||||||||||||||||||||||||||||||
| 600 | 0 | ▶ | print { $args{handle} } $b if defined $b; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 601 | 0 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 602 | } | ||||||||||||||||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||||||||||||||||
| 604 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||||||||||||
| 606 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||||||||||||
| 608 | =item B<store_attachment(n)> | ||||||||||||||||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||||||||||||||||
| 610 | =item B<store_attachment(n, options)> | ||||||||||||||||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||||||||||||||||
| 612 | It is really just a call to store_entity_body but it will take care that the | ||||||||||||||||||||||||||||||||||||||||||
| 613 | n-th entity really is a saveable attachment. That is, it wont save anything | ||||||||||||||||||||||||||||||||||||||||||
| 614 | with a MIME-type of, say, text/html or so. | ||||||||||||||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||||||||||||
| 616 | Unless further 'options' have been given, an attachment (if found) is stored | ||||||||||||||||||||||||||||||||||||||||||
| 617 | into the current directory under the recommended filename given in the | ||||||||||||||||||||||||||||||||||||||||||
| 618 | MIME-header. 'options' are specified in key/value pairs: | ||||||||||||||||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||||||||||||||||
| 620 | key: | value: | description: | ||||||||||||||||||||||||||||||||||||||||||
| 621 | ===========|================|=============================== | ||||||||||||||||||||||||||||||||||||||||||
| 622 | path | relative or | directory to store attachment | ||||||||||||||||||||||||||||||||||||||||||
| 623 | (".") | absolute | | ||||||||||||||||||||||||||||||||||||||||||
| 624 | | path | | ||||||||||||||||||||||||||||||||||||||||||
| 625 | -----------|----------------|------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 626 | encode | encoding | Some platforms store files | ||||||||||||||||||||||||||||||||||||||||||
| 627 | | suitable for | in e.g. UTF-8. Specify the | ||||||||||||||||||||||||||||||||||||||||||
| 628 | | Encode::encode | appropriate encoding here and | ||||||||||||||||||||||||||||||||||||||||||
| 629 | | | and the filename will be en- | ||||||||||||||||||||||||||||||||||||||||||
| 630 | | | coded accordingly. | ||||||||||||||||||||||||||||||||||||||||||
| 631 | -----------|----------------|------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 632 | store_only | a compiled | store only files whose file | ||||||||||||||||||||||||||||||||||||||||||
| 633 | | regex-pattern | names match this pattern | ||||||||||||||||||||||||||||||||||||||||||
| 634 | -----------|----------------|------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 635 | code | an anonym | first argument will be the | ||||||||||||||||||||||||||||||||||||||||||
| 636 | | subroutine | $msg-object, second one the | ||||||||||||||||||||||||||||||||||||||||||
| 637 | | | index-number of the current | ||||||||||||||||||||||||||||||||||||||||||
| 638 | | | MIME-part | ||||||||||||||||||||||||||||||||||||||||||
| 639 | | | should return a filename for | ||||||||||||||||||||||||||||||||||||||||||
| 640 | | | the attachment | ||||||||||||||||||||||||||||||||||||||||||
| 641 | -----------|----------------|------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 642 | prefix | prefix for | all filenames are prefixed | ||||||||||||||||||||||||||||||||||||||||||
| 643 | | filenames | with this value | ||||||||||||||||||||||||||||||||||||||||||
| 644 | -----------|----------------|------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 645 | args | additional | this array-ref will be passed | ||||||||||||||||||||||||||||||||||||||||||
| 646 | | arguments as | on to the 'code' subroutine | ||||||||||||||||||||||||||||||||||||||||||
| 647 | | array-ref | as a dereferenced array | ||||||||||||||||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||||||||||||
| 650 | Example: | ||||||||||||||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||||||||||||||
| 652 | $msg->store_attachment(1, | ||||||||||||||||||||||||||||||||||||||||||
| 653 | path => "/home/ethan/", | ||||||||||||||||||||||||||||||||||||||||||
| 654 | code => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 655 | my ($msg, $n, @args) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 656 | return $msg->id."+$n"; | ||||||||||||||||||||||||||||||||||||||||||
| 657 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 658 | args => [ "Foo", "Bar" ]); | ||||||||||||||||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||||||||||||||||
| 660 | This will save the attachment found in the second entity under the name that | ||||||||||||||||||||||||||||||||||||||||||
| 661 | consists of the message-ID and the appendix "+1" since the above code works on | ||||||||||||||||||||||||||||||||||||||||||
| 662 | the second entity (that is, with index = 1). 'args' isn't used in this example | ||||||||||||||||||||||||||||||||||||||||||
| 663 | but should demonstrate how to pass additional arguments. Inside the 'code' sub, | ||||||||||||||||||||||||||||||||||||||||||
| 664 | @args equals ("Foo", "Bar"). | ||||||||||||||||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||||||||||||||||
| 666 | If 'path' does not exist, it will try to create the directory for you. | ||||||||||||||||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||||||||||||||||
| 668 | You can specify to save only files matching a certain pattern. To do that, use | ||||||||||||||||||||||||||||||||||||||||||
| 669 | the store-only switch: | ||||||||||||||||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||||||||||||||||
| 671 | $msg->store_attachment(1, path => "/home/ethan/", | ||||||||||||||||||||||||||||||||||||||||||
| 672 | store_only => qr/\.jpg$/i); | ||||||||||||||||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||||||||||||||||
| 674 | The above will only save files that end on '.jpg', not case-sensitive. You | ||||||||||||||||||||||||||||||||||||||||||
| 675 | could also use a non-compiled pattern if you want, but that would make for | ||||||||||||||||||||||||||||||||||||||||||
| 676 | instance case-insensitive matching a little cumbersome: | ||||||||||||||||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||||||||||||||||
| 678 | store_only => '(?i)\.jpg$' | ||||||||||||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||||||||||||
| 680 | If you are working on a platform that requires a certain encoding for filenames | ||||||||||||||||||||||||||||||||||||||||||
| 681 | on disk, you can use the 'encode' option. This becomes necessary for instance on | ||||||||||||||||||||||||||||||||||||||||||
| 682 | Mac OS X which internally is UTF-8 based. If the filename contains 8bit characters | ||||||||||||||||||||||||||||||||||||||||||
| 683 | (like the German umlauts or French accented characters as in 'é'), storing the | ||||||||||||||||||||||||||||||||||||||||||
| 684 | attachment under a non-encoded name will most likely fail. In this case, use something | ||||||||||||||||||||||||||||||||||||||||||
| 685 | like this: | ||||||||||||||||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||||||||||||||||
| 687 | $msg->store_attachment(1, path => '/tmp', encode => 'utf-8'); | ||||||||||||||||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||||||||||||||||
| 689 | See L<Encode::Supported> for a list of encodings that you may use. | ||||||||||||||||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||||||||||||||||
| 691 | Returns the filename under which the attachment has been saved. undef is | ||||||||||||||||||||||||||||||||||||||||||
| 692 | returned in case the entity did not contain a saveable attachement, there was | ||||||||||||||||||||||||||||||||||||||||||
| 693 | no such entity at all or there was something wrong with the 'path' you | ||||||||||||||||||||||||||||||||||||||||||
| 694 | specified. Check C<$mail-E<gt>error> to find out which of these possibilities | ||||||||||||||||||||||||||||||||||||||||||
| 695 | apply. | ||||||||||||||||||||||||||||||||||||||||||
| 696 | |||||||||||||||||||||||||||||||||||||||||||
| 697 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||||||||||||||||
| 699 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||||||||||||||||
| 701 | 0 | ▶ | sub store_attachment($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
store_attachment: not called CC=17 SCAR=57.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 702 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 703 | 0 | my ($num, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 704 | 0 | $self->reset_last; | |||||||||||||||||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||||||||||||||||
| 706 | 0 | ▶ | my $path = $args{path} || "."; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{'path'} || '.'
MC/DC0%0/1
$args{'path'} || '.'
$args{'path'}
Truth table0%0/2
A
A= $args{'path'}
| |||||||||||||||||||||||||||||||||||||||||||
| 707 | 0 | $path =~ s/\/$//; | |||||||||||||||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||||||||||||||||
| 709 | 0 | ▶ | my $prefix = $args{prefix} || ""; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$args{'prefix'} || ''
MC/DC0%0/1
$args{'prefix'} || ''
$args{'prefix'}
Truth table0%0/2
A
A= $args{'prefix'}
| |||||||||||||||||||||||||||||||||||||||||||
| 710 | |||||||||||||||||||||||||||||||||||||||||||
| 711 | 0 | ▶ | if (defined $args{code} && ref $args{code} ne 'CODE') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args{"code"} and ref $args{"code"} ne "CODE"
MC/DC0%0/2
defined $args{"code"} and ref $args{"code"} ne "CODE"
defined $args{"code"}
ref $args{"code"} ne "CODE"
Truth table0%0/3
A and B
A= defined $args{"code"}B= ref $args{"code"} ne "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 712 | 0 | carp <<EOW; | |||||||||||||||||||||||||||||||||||||||||
| 713 | Warning: Second argument for store_attachment must be | ||||||||||||||||||||||||||||||||||||||||||
| 714 | a coderef. Using filename from header instead | ||||||||||||||||||||||||||||||||||||||||||
| 715 | EOW | ||||||||||||||||||||||||||||||||||||||||||
| 716 | 0 | delete @args{ qw(code args) }; | |||||||||||||||||||||||||||||||||||||||||
| 717 | } | ||||||||||||||||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||||||||||||||||
| 719 | 0 | ▶ | if ($num < $self->num_entities) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 720 | 0 | my $file = $self->_get_attachment( $num ); | |||||||||||||||||||||||||||||||||||||||||
| 721 | 0 | ▶ | return if ! defined $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||||||||||||||||
| 723 | 0 | ▶ | if (-e $path && not -d _) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-e $path and not -d _
MC/DC0%0/2
-e $path and not -d _
-e $path
not -d _
Truth table0%0/3
A and not(B)
A= -e $pathB= not -d _
| |||||||||||||||||||||||||||||||||||||||||||
| 724 | 0 | $self->{LAST_ERR} = "$path is a file"; | |||||||||||||||||||||||||||||||||||||||||
| 725 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 726 | } | ||||||||||||||||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||||||||||||||||
| 728 | 0 | ▶ | if (not -e _) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 729 | 0 | ▶ | if (not mkdir $path, 0755) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 730 | 0 | $self->{LAST_ERR} = "Could not create directory $path: $!"; | |||||||||||||||||||||||||||||||||||||||||
| 731 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 732 | } | ||||||||||||||||||||||||||||||||||||||||||
| 733 | } | ||||||||||||||||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||||||||||||||||
| 735 | 0 | ▶ | if (defined $args{code}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 736 | 0 | $file = $args{code}->($self, $num, @{$args{args}}) | |||||||||||||||||||||||||||||||||||||||||
| 737 | } | ||||||||||||||||||||||||||||||||||||||||||
| 738 | |||||||||||||||||||||||||||||||||||||||||||
| 739 | #if ($file =~ /=\?.*\?=/ and HAVE_MIMEWORDS) { # decode qp if possible | ||||||||||||||||||||||||||||||||||||||||||
| 740 | # $file = MIME::Words::decode_mimewords($file); | ||||||||||||||||||||||||||||||||||||||||||
| 741 | #} | ||||||||||||||||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||||||||||||||||
| 743 | 0 | ▶ | return if defined $args{store_only} and $file !~ /$args{store_only}/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args{"store_only"} and not $file =~ /$args{"store_only"}/
MC/DC0%0/2
defined $args{"store_only"} and not $file =~ /$args{"store_only"}/
defined $args{"store_only"}
not $file =~ /$args{"store_only"}/
Truth table0%0/3
A and not(B)
A= defined $args{"store_only"}B= not $file =~ /$args{"store_only"}/
| |||||||||||||||||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||||||||||||||||
| 745 | 0 | ▶ | if ($args{encode} and HAVE_ENCODE) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$args{"encode"} and 1
MC/DC0%0/1
$args{"encode"} and 1
$args{"encode"}
Truth table0%0/2
A
A= $args{"encode"}
| |||||||||||||||||||||||||||||||||||||||||||
| 746 | 0 | $file = Encode::encode($args{encode}, $file); | |||||||||||||||||||||||||||||||||||||||||
| 747 | } | ||||||||||||||||||||||||||||||||||||||||||
| 748 | |||||||||||||||||||||||||||||||||||||||||||
| 749 | 0 | local *ATT; | |||||||||||||||||||||||||||||||||||||||||
| 750 | 0 | ▶ | if (open ATT, ">$path/$prefix$file") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 751 | 0 | $self->store_entity_body($num, handle => \*ATT); | |||||||||||||||||||||||||||||||||||||||||
| 752 | 0 | close ATT ; | |||||||||||||||||||||||||||||||||||||||||
| 753 | 0 | return "$prefix$file"; | |||||||||||||||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||||||||||||||||
| 755 | } | ||||||||||||||||||||||||||||||||||||||||||
| 756 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 757 | 0 | $self->{LAST_ERR} = "Could not create $path/$prefix$file: $!"; | |||||||||||||||||||||||||||||||||||||||||
| 758 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 759 | } | ||||||||||||||||||||||||||||||||||||||||||
| 760 | } | ||||||||||||||||||||||||||||||||||||||||||
| 761 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 762 | 0 | $self->{LAST_ERR} = "$num: No such entity"; | |||||||||||||||||||||||||||||||||||||||||
| 763 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 764 | } | ||||||||||||||||||||||||||||||||||||||||||
| 765 | } | ||||||||||||||||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||||||||||||||||
| 767 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||||||||||||
| 769 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||||||||||||||||
| 771 | =item B<store_all_attachments> | ||||||||||||||||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||||||||||||||||
| 773 | =item B<store_all_attachments(options)> | ||||||||||||||||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||||||||||||||||
| 775 | Walks through an entire mail and stores all apparent attachments. 'options' are | ||||||||||||||||||||||||||||||||||||||||||
| 776 | exactly the same as in C<store_attachement()> with the same behaviour if no | ||||||||||||||||||||||||||||||||||||||||||
| 777 | options are given. | ||||||||||||||||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||||||||||||||||
| 779 | Returns a list of files that have been succesfully saved and an empty list if | ||||||||||||||||||||||||||||||||||||||||||
| 780 | no attachment could be extracted. | ||||||||||||||||||||||||||||||||||||||||||
| 781 | |||||||||||||||||||||||||||||||||||||||||||
| 782 | C<$mail-E<gt>error> will tell you possible failures and a possible explanation | ||||||||||||||||||||||||||||||||||||||||||
| 783 | for that. | ||||||||||||||||||||||||||||||||||||||||||
| 784 | |||||||||||||||||||||||||||||||||||||||||||
| 785 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||||||||||||||||
| 787 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 788 | |||||||||||||||||||||||||||||||||||||||||||
| 789 | 0 | ▶ | sub store_all_attachments(@) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
store_all_attachments: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 790 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 791 | 0 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 792 | 0 | $self->reset_last; | |||||||||||||||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||||||||||||||||
| 794 | 0 | ▶ | if (defined $args{code} and ref $args{code} ne 'CODE') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $args{"code"} and ref $args{"code"} ne "CODE"
MC/DC0%0/2
defined $args{"code"} and ref $args{"code"} ne "CODE"
defined $args{"code"}
ref $args{"code"} ne "CODE"
Truth table0%0/3
A and B
A= defined $args{"code"}B= ref $args{"code"} ne "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 795 | 0 | carp <<EOW; | |||||||||||||||||||||||||||||||||||||||||
| 796 | Warning: Second argument for store_all_attachments must be a coderef. | ||||||||||||||||||||||||||||||||||||||||||
| 797 | Using filename from header instead | ||||||||||||||||||||||||||||||||||||||||||
| 798 | EOW | ||||||||||||||||||||||||||||||||||||||||||
| 799 | 0 | delete @args{ qw(code args) }; | |||||||||||||||||||||||||||||||||||||||||
| 800 | } | ||||||||||||||||||||||||||||||||||||||||||
| 801 | 0 | my @files; | |||||||||||||||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||||||||||||||||
| 803 | 0 | ▶ | if (! exists $args{path} || $args{path} eq '') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not exists $args{"path"} or $args{"path"} eq ""
MC/DC0%0/2
not exists $args{"path"} or $args{"path"} eq ""
not exists $args{"path"}
$args{"path"} eq ""
Truth table0%0/3
not(A) or B
A= not exists $args{"path"}B= $args{"path"} eq ""
| |||||||||||||||||||||||||||||||||||||||||||
| 804 | 0 | $args{path} = '.'; | |||||||||||||||||||||||||||||||||||||||||
| 805 | } | ||||||||||||||||||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||||||||||||||||
| 807 | 0 | for (0 .. $self->num_entities - 1) { | |||||||||||||||||||||||||||||||||||||||||
| 808 | 0 | push @files, $self->store_attachment($_, %args); | |||||||||||||||||||||||||||||||||||||||||
| 809 | } | ||||||||||||||||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||||||||||||||||
| 811 | 0 | ▶ | $self->{LAST_ERR} = "Found no attachment at all" if ! @files; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 812 | 0 | return @files; | |||||||||||||||||||||||||||||||||||||||||
| 813 | } | ||||||||||||||||||||||||||||||||||||||||||
| 814 | |||||||||||||||||||||||||||||||||||||||||||
| 815 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||||||||||||||||
| 817 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 818 | |||||||||||||||||||||||||||||||||||||||||||
| 819 | =item B<get_attachments> | ||||||||||||||||||||||||||||||||||||||||||
| 820 | |||||||||||||||||||||||||||||||||||||||||||
| 821 | =item B<get_attachments(file)> | ||||||||||||||||||||||||||||||||||||||||||
| 822 | |||||||||||||||||||||||||||||||||||||||||||
| 823 | This method returns a mapping from attachment-names (if those are savable) to | ||||||||||||||||||||||||||||||||||||||||||
| 824 | index-numbers of the MIME-part that represents this attachment. It returns a | ||||||||||||||||||||||||||||||||||||||||||
| 825 | hash-reference, the file-names being the key and the index the value: | ||||||||||||||||||||||||||||||||||||||||||
| 826 | |||||||||||||||||||||||||||||||||||||||||||
| 827 | my $mapping = $msg->get_attachments; | ||||||||||||||||||||||||||||||||||||||||||
| 828 | for my $filename (keys %$mapping) { | ||||||||||||||||||||||||||||||||||||||||||
| 829 | print "$filename => $mapping->{$filename}\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 830 | } | ||||||||||||||||||||||||||||||||||||||||||
| 831 | |||||||||||||||||||||||||||||||||||||||||||
| 832 | If called with a string as argument, it tries to look up this filename. If it | ||||||||||||||||||||||||||||||||||||||||||
| 833 | can't be found, undef is returned. In this case you also should have an | ||||||||||||||||||||||||||||||||||||||||||
| 834 | error-message patiently awaiting you in the return value of | ||||||||||||||||||||||||||||||||||||||||||
| 835 | C<$mail-E<gt>error>. | ||||||||||||||||||||||||||||||||||||||||||
| 836 | |||||||||||||||||||||||||||||||||||||||||||
| 837 | Even though it looks tempting, don't do the following: | ||||||||||||||||||||||||||||||||||||||||||
| 838 | |||||||||||||||||||||||||||||||||||||||||||
| 839 | # BAD! | ||||||||||||||||||||||||||||||||||||||||||
| 840 | |||||||||||||||||||||||||||||||||||||||||||
| 841 | for my $file (qw/file1.ext file2.ext file3.ext file4.ext/) { | ||||||||||||||||||||||||||||||||||||||||||
| 842 | print "$file is in message ", $msg->id, "\n" | ||||||||||||||||||||||||||||||||||||||||||
| 843 | if defined $msg->get_attachments($file); | ||||||||||||||||||||||||||||||||||||||||||
| 844 | } | ||||||||||||||||||||||||||||||||||||||||||
| 845 | |||||||||||||||||||||||||||||||||||||||||||
| 846 | The reason is that C<get_attachments()> is currently B<not> optimized to cache | ||||||||||||||||||||||||||||||||||||||||||
| 847 | the filename mapping. So, each time you call it on (even the same) message, it | ||||||||||||||||||||||||||||||||||||||||||
| 848 | will scan it from beginning to end. Better would be: | ||||||||||||||||||||||||||||||||||||||||||
| 849 | |||||||||||||||||||||||||||||||||||||||||||
| 850 | # GOOD! | ||||||||||||||||||||||||||||||||||||||||||
| 851 | |||||||||||||||||||||||||||||||||||||||||||
| 852 | my $mapping = $msg->get_attachments; | ||||||||||||||||||||||||||||||||||||||||||
| 853 | for my $file (qw/file1.ext file2.ext file3.ext file4.ext/) { | ||||||||||||||||||||||||||||||||||||||||||
| 854 | print "$file is in message ", $msg->id, "\n" | ||||||||||||||||||||||||||||||||||||||||||
| 855 | if exists $mapping->{$file}; | ||||||||||||||||||||||||||||||||||||||||||
| 856 | } | ||||||||||||||||||||||||||||||||||||||||||
| 857 | |||||||||||||||||||||||||||||||||||||||||||
| 858 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 859 | |||||||||||||||||||||||||||||||||||||||||||
| 860 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 861 | |||||||||||||||||||||||||||||||||||||||||||
| 862 | 100 | ▶ | sub get_attachments(;$) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_attachments: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 863 | 100 | my ($self, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 864 | 100 | $self->reset_last; | |||||||||||||||||||||||||||||||||||||||||
| 865 | 100 | my %mapping; | |||||||||||||||||||||||||||||||||||||||||
| 866 | |||||||||||||||||||||||||||||||||||||||||||
| 867 | 100 | for my $num (0 .. $self->num_entities - 1) { | |||||||||||||||||||||||||||||||||||||||||
| 868 | 144 | my $file = $self->_get_attachment($num); | |||||||||||||||||||||||||||||||||||||||||
| 869 | 144 | ▶ | $mapping{ $file } = $num if defined $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 870 | } | ||||||||||||||||||||||||||||||||||||||||||
| 871 | |||||||||||||||||||||||||||||||||||||||||||
| 872 | 100 | ▶ | if ($name) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 873 | 13 | ▶ | if (! exists $mapping{$name}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 874 | 1 | $self->{LAST_ERR} = "$name: No such attachment"; | |||||||||||||||||||||||||||||||||||||||||
| 875 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||
| 876 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 877 | 12 | return $mapping{$name} | |||||||||||||||||||||||||||||||||||||||||
| 878 | } | ||||||||||||||||||||||||||||||||||||||||||
| 879 | } | ||||||||||||||||||||||||||||||||||||||||||
| 880 | |||||||||||||||||||||||||||||||||||||||||||
| 881 | 87 | ▶ | if (keys %mapping == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 882 | 80 | $self->{LAST_ERR} = "No attachments at all"; | |||||||||||||||||||||||||||||||||||||||||
| 883 | 80 | return; | |||||||||||||||||||||||||||||||||||||||||
| 884 | } | ||||||||||||||||||||||||||||||||||||||||||
| 885 | |||||||||||||||||||||||||||||||||||||||||||
| 886 | 7 | return \%mapping; | |||||||||||||||||||||||||||||||||||||||||
| 887 | } | ||||||||||||||||||||||||||||||||||||||||||
| 888 | |||||||||||||||||||||||||||||||||||||||||||
| 889 | 144 | ▶ | sub _get_attachment { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_attachment: called CC=8 SCAR=22.8
| |||||||||||||||||||||||||||||||||||||||||||
| 890 | 144 | my ($self, $num) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 891 | 144 | my $file = eval { $self->get_entities($num)->head->recommended_filename }; | |||||||||||||||||||||||||||||||||||||||||
| 892 | 144 | $self->{LAST_LOG} = $@; | |||||||||||||||||||||||||||||||||||||||||
| 893 | 144 | ▶ | if (! $file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 894 | # test for Content-Disposition | ||||||||||||||||||||||||||||||||||||||||||
| 895 | 124 | ▶ | if (! $self->get_entities($num)->head->get('content-disposition')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 896 | 88 | return; | |||||||||||||||||||||||||||||||||||||||||
| 897 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 898 | 36 | my ($type, $filename) = split /;\s*/, | |||||||||||||||||||||||||||||||||||||||||
| 899 | $self->get_entities($num)->head->get('content-disposition'); | ||||||||||||||||||||||||||||||||||||||||||
| 900 | 36 | ▶ | if ($type eq 'attachment') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 901 | 0 | ▶ | if ($filename =~ /filename\*?=(.*?''?)?(.*)$/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 902 | 0 | ($file = $2) =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; | |||||||||||||||||||||||||||||||||||||||||
| 903 | } | ||||||||||||||||||||||||||||||||||||||||||
| 904 | } | ||||||||||||||||||||||||||||||||||||||||||
| 905 | } | ||||||||||||||||||||||||||||||||||||||||||
| 906 | } | ||||||||||||||||||||||||||||||||||||||||||
| 907 | |||||||||||||||||||||||||||||||||||||||||||
| 908 | 56 | ▶ | return if not $file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 909 | |||||||||||||||||||||||||||||||||||||||||||
| 910 | 20 | ▶ | if ($file =~ /=\?.*\?=/ and HAVE_MIMEWORDS) { # decode qp if possible | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$file =~ /=\?.*\?=/ and 1
MC/DC0%0/1
$file =~ /=\?.*\?=/ and 1
$file =~ /=\?.*\?=/
Truth table50%1/2
A
A= $file =~ /=\?.*\?=/
| |||||||||||||||||||||||||||||||||||||||||||
| 911 | 0 | $file = MIME::Words::decode_mimewords($file); | |||||||||||||||||||||||||||||||||||||||||
| 912 | } | ||||||||||||||||||||||||||||||||||||||||||
| 913 | |||||||||||||||||||||||||||||||||||||||||||
| 914 | 20 | return $file; | |||||||||||||||||||||||||||||||||||||||||
| 915 | } | ||||||||||||||||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||||||||||||||||
| 917 | # ---------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 918 | |||||||||||||||||||||||||||||||||||||||||||
| 919 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 920 | |||||||||||||||||||||||||||||||||||||||||||
| 921 | =item B<as_string> | ||||||||||||||||||||||||||||||||||||||||||
| 922 | |||||||||||||||||||||||||||||||||||||||||||
| 923 | Returns the message as one string. This is the method that string overloading | ||||||||||||||||||||||||||||||||||||||||||
| 924 | depends on, so these two are the same: | ||||||||||||||||||||||||||||||||||||||||||
| 925 | |||||||||||||||||||||||||||||||||||||||||||
| 926 | print $msg; | ||||||||||||||||||||||||||||||||||||||||||
| 927 | |||||||||||||||||||||||||||||||||||||||||||
| 928 | print $msg->as_string; | ||||||||||||||||||||||||||||||||||||||||||
| 929 | |||||||||||||||||||||||||||||||||||||||||||
| 930 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 931 | |||||||||||||||||||||||||||||||||||||||||||
| 932 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 933 | |||||||||||||||||||||||||||||||||||||||||||
| 934 | 145 | ▶ | sub as_string { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 935 | 145 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 936 | 145 | my $js = $self->{ARGS}->{join_string}; | |||||||||||||||||||||||||||||||||||||||||
| 937 | 145 | return join $js, @{ $self->{HEADER} }, @{ $self->{BODY} }; | |||||||||||||||||||||||||||||||||||||||||
| 938 | } | ||||||||||||||||||||||||||||||||||||||||||
| 939 | |||||||||||||||||||||||||||||||||||||||||||
| 940 | 0 | ▶ | sub _recipients($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_recipients: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||
| 941 | 0 | my ($self, $field) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 942 | 0 | $self->reset_last; | |||||||||||||||||||||||||||||||||||||||||
| 943 | |||||||||||||||||||||||||||||||||||||||||||
| 944 | 0 | my $rec = $self->header->{$field}; | |||||||||||||||||||||||||||||||||||||||||
| 945 | 0 | ▶ | if (! $rec) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 946 | 0 | $self->{LAST_ERR} = "'$field' not in header"; | |||||||||||||||||||||||||||||||||||||||||
| 947 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 948 | } | ||||||||||||||||||||||||||||||||||||||||||
| 949 | |||||||||||||||||||||||||||||||||||||||||||
| 950 | 0 | $rec =~ s/(?<=\@)(.*?),/$1\n/g; | |||||||||||||||||||||||||||||||||||||||||
| 951 | 0 | my @recs = split /\n/, $rec; | |||||||||||||||||||||||||||||||||||||||||
| 952 | 0 | s/^\s+//, s/\s+$// for @recs; # remove leading or trailing whitespaces | |||||||||||||||||||||||||||||||||||||||||
| 953 | 0 | my @rec_line; | |||||||||||||||||||||||||||||||||||||||||
| 954 | 0 | for my $pair (@recs) { | |||||||||||||||||||||||||||||||||||||||||
| 955 | 0 | my ($name, $email) = split /\s</, $pair; | |||||||||||||||||||||||||||||||||||||||||
| 956 | 0 | ▶ | $email =~ s/\>$//g if $email; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 957 | 0 | ▶ | if ($name && ! $email) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$name and not $email
MC/DC0%0/2
$name and not $email
$name
not $email
Truth table0%0/3
A and not(B)
A= $nameB= not $email
| |||||||||||||||||||||||||||||||||||||||||||
| 958 | 0 | $email = $name; | |||||||||||||||||||||||||||||||||||||||||
| 959 | 0 | $name = ""; | |||||||||||||||||||||||||||||||||||||||||
| 960 | 0 | ▶ | $name = ucfirst($1) . " " . ucfirst($2) if $email =~ /^(.*?)\.(.*)@/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 961 | } | ||||||||||||||||||||||||||||||||||||||||||
| 962 | 0 | push @rec_line, {(name => $name, email => $email)}; | |||||||||||||||||||||||||||||||||||||||||
| 963 | } | ||||||||||||||||||||||||||||||||||||||||||
| 964 | |||||||||||||||||||||||||||||||||||||||||||
| 965 | 0 | return @rec_line; | |||||||||||||||||||||||||||||||||||||||||
| 966 | } | ||||||||||||||||||||||||||||||||||||||||||
| 967 | |||||||||||||||||||||||||||||||||||||||||||
| 968 | # patch provided by Kenn Frankel | ||||||||||||||||||||||||||||||||||||||||||
| 969 | # additional corrections by Nathan Uno | ||||||||||||||||||||||||||||||||||||||||||
| 970 | 62 | ▶ | sub _split_header { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_split_header: called CC=12 SCAR=26.2
| |||||||||||||||||||||||||||||||||||||||||||
| 971 | 62 | local $/ = $NL; | |||||||||||||||||||||||||||||||||||||||||
| 972 | 62 | my ($self, $header, $decode) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 973 | 62 | my @headerlines = @{ $header }; | |||||||||||||||||||||||||||||||||||||||||
| 974 | |||||||||||||||||||||||||||||||||||||||||||
| 975 | 62 | my @header; | |||||||||||||||||||||||||||||||||||||||||
| 976 | 62 | ▶ | chomp @headerlines if ref $header; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 977 | 62 | foreach my $bit (@headerlines) { | |||||||||||||||||||||||||||||||||||||||||
| 978 | 2254 | $bit =~ s/\s+$//; # discard trailing whitespace | |||||||||||||||||||||||||||||||||||||||||
| 979 | 2254 | ▶ | if ($bit =~ s/^\s+/ /) { $header[-1] .= $bit } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 980 | 1158 | else { push @header, $bit } | |||||||||||||||||||||||||||||||||||||||||
| 981 | } | ||||||||||||||||||||||||||||||||||||||||||
| 982 | |||||||||||||||||||||||||||||||||||||||||||
| 983 | 62 | my ($key, $value); | |||||||||||||||||||||||||||||||||||||||||
| 984 | 62 | my %header; | |||||||||||||||||||||||||||||||||||||||||
| 985 | 62 | for (@header) { | |||||||||||||||||||||||||||||||||||||||||
| 986 | 1158 | ▶ | if (/^Received:\s/) { push @{$self->{TRACE}}, substr($_, 10) } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 987 | 54 | elsif (/^From /) { $self->{FROM} = $_ } | |||||||||||||||||||||||||||||||||||||||||
| 988 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 989 | 690 | my $idx = index $_, ": "; | |||||||||||||||||||||||||||||||||||||||||
| 990 | 690 | $key = substr $_, 0, $idx; | |||||||||||||||||||||||||||||||||||||||||
| 991 | 690 | ▶ | $value = $idx != -1 ? substr $_, $idx + 2 : ""; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 992 | 690 | ▶ | if ($decode eq 'ALL' || $decode eq 'HEADER') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$decode eq "ALL" or $decode eq "HEADER"
MC/DC0%0/2
$decode eq "ALL" or $decode eq "HEADER"
$decode eq "ALL"
$decode eq "HEADER"
Truth table33%1/3
A or B
A= $decode eq "ALL"B= $decode eq "HEADER"
| |||||||||||||||||||||||||||||||||||||||||||
| 993 | 20 | ▶ | use MIME::Words qw(:all); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 994 | 0 | $value = decode_mimewords($value); | |||||||||||||||||||||||||||||||||||||||||
| 995 | } | ||||||||||||||||||||||||||||||||||||||||||
| 996 | |||||||||||||||||||||||||||||||||||||||||||
| 997 | # if such a field is already there => make array-ref | ||||||||||||||||||||||||||||||||||||||||||
| 998 | 690 | ▶ | if (exists $header{lc($key)}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 999 | 0 | my $elem = $header{lc($key)}; | |||||||||||||||||||||||||||||||||||||||||
| 1000 | 0 | ▶ | my @data = ref $elem ? @$elem : $elem; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1001 | 0 | push @data, $value; | |||||||||||||||||||||||||||||||||||||||||
| 1002 | 0 | $header{lc($key)} = [ @data ]; | |||||||||||||||||||||||||||||||||||||||||
| 1003 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1004 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 1005 | 690 | $header{lc($key)} = $value; | |||||||||||||||||||||||||||||||||||||||||
| 1006 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1007 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1008 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1009 | 62 | return \%header; | |||||||||||||||||||||||||||||||||||||||||
| 1010 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1011 | |||||||||||||||||||||||||||||||||||||||||||
| 1012 | 40 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=10 SCAR=25.3
| |||||||||||||||||||||||||||||||||||||||||||
| 1013 | 40 | my ($self, @args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1014 | 40 | (my $call = $AUTOLOAD) =~ s/.*:://; | |||||||||||||||||||||||||||||||||||||||||
| 1015 | |||||||||||||||||||||||||||||||||||||||||||
| 1016 | # for backward-compatibility | ||||||||||||||||||||||||||||||||||||||||||
| 1017 | 40 | ▶ | if ($call eq 'store_attachement') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1018 | 0 | return $self->store_attachment(@args); | |||||||||||||||||||||||||||||||||||||||||
| 1019 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1020 | 40 | ▶ | if ($call eq 'store_all_attachements') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1021 | 0 | return $self->store_all_attachments(@args); | |||||||||||||||||||||||||||||||||||||||||
| 1022 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1023 | |||||||||||||||||||||||||||||||||||||||||||
| 1024 | # test some potential classes that might implement $call | ||||||||||||||||||||||||||||||||||||||||||
| 1025 | 20 | ▶ | { no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1026 | 40 | for my $class (qw/MIME::Entity Mail::Internet/) { | |||||||||||||||||||||||||||||||||||||||||
| 1027 | 40 | eval "require $class"; | |||||||||||||||||||||||||||||||||||||||||
| 1028 | # we found a Class that implements $call | ||||||||||||||||||||||||||||||||||||||||||
| 1029 | 40 | ▶ | if ($class->can($call)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1030 | |||||||||||||||||||||||||||||||||||||||||||
| 1031 | # MIME::Entity needed | ||||||||||||||||||||||||||||||||||||||||||
| 1032 | 40 | ▶ | if ($class eq 'MIME::Entity') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1033 | |||||||||||||||||||||||||||||||||||||||||||
| 1034 | 40 | ▶ | if (! defined $Parser) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1035 | 4 | eval { require MIME::Parser }; | |||||||||||||||||||||||||||||||||||||||||
| 1036 | 4 | $Parser = new MIME::Parser; | |||||||||||||||||||||||||||||||||||||||||
| 1037 | 4 | $Parser->output_to_core(1); | |||||||||||||||||||||||||||||||||||||||||
| 1038 | 4 | $Parser->extract_uuencode($self->{ARGS}->{uudecode}); | |||||||||||||||||||||||||||||||||||||||||
| 1039 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1040 | 40 | my $js = $self->{ARGS}->{join_string}; | |||||||||||||||||||||||||||||||||||||||||
| 1041 | 20 | $self->{TOP_ENTITY} = $Parser->parse_data(join $js, @{$self->{HEADER}}, @{$self->{BODY}}) | |||||||||||||||||||||||||||||||||||||||||
| 1042 | 40 | ▶ | if ref $self->{TOP_ENTITY} ne 'MIME::Entity'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1043 | 40 | return $self->{TOP_ENTITY}->$call(@args); | |||||||||||||||||||||||||||||||||||||||||
| 1044 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1045 | |||||||||||||||||||||||||||||||||||||||||||
| 1046 | # Mail::Internet needed | ||||||||||||||||||||||||||||||||||||||||||
| 1047 | 0 | ▶ | if ($class eq 'Mail::Internet') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1048 | return Mail::Internet->new([ split /\n/, join "", ref $self->{HEADER} | ||||||||||||||||||||||||||||||||||||||||||
| 1049 | 0 | ? @{$self->{HEADER}} | |||||||||||||||||||||||||||||||||||||||||
| 1050 | 0 | ▶ | : $self->{HEADER} . $self->{BODY} ]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1051 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1052 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1053 | } # end 'for' | ||||||||||||||||||||||||||||||||||||||||||
| 1054 | } # end 'no strict refs' block | ||||||||||||||||||||||||||||||||||||||||||
| 1055 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1056 | |||||||||||||||||||||||||||||||||||||||||||
| 1057 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 1058 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1059 | |||||||||||||||||||||||||||||||||||||||||||
| 1060 | |||||||||||||||||||||||||||||||||||||||||||
| 1061 | 20 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 1062 | |||||||||||||||||||||||||||||||||||||||||||
| 1063 | __END__ | ||||||||||||||||||||||||||||||||||||||||||