File Coverage

blib/lib/Mail/Message/Part.pm
Criterion Covered Total %
statement 49 60 81.6
branch 11 20 55.0
condition 4 9 44.4
subroutine 11 14 78.5
pod 8 9 88.8
total 83 112 74.1


line stmt bran cond sub pod time code
1             # This code is part of Perl distribution Mail-Message version 4.04.
2             # The POD got stripped from this file by OODoc version 3.06.
3             # For contributors see file ChangeLog.
4              
5             # This software is copyright (c) 2001-2026 by Mark Overmeer.
6              
7             # This is free software; you can redistribute it and/or modify it under
8             # the same terms as the Perl 5 programming language system itself.
9             # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10              
11              
12             package Mail::Message::Part;{
13             our $VERSION = '4.04';
14             }
15              
16 37     37   465 use parent 'Mail::Message';
  37         106  
  37         278  
17              
18 37     37   3558 use strict;
  37         111  
  37         1160  
19 37     37   201 use warnings;
  37         85  
  37         2387  
20              
21 37     37   232 use Log::Report 'mail-message', import => [ qw/__x error panic/ ];
  37         700  
  37         532  
22              
23 37     37   7366 use Scalar::Util qw/weaken/;
  37         101  
  37         36098  
24              
25             #--------------------
26              
27             sub init($)
28 75     75 0 200 { my ($self, $args) = @_;
29 75   66     464 $args->{head} ||= Mail::Message::Head::Complete->new;
30              
31 75         380 $self->SUPER::init($args);
32              
33             exists $args->{container}
34 75 50       272 or error __x"no container specified for part.";
35              
36             weaken($self->{MMP_container})
37 75 100       406 if $self->{MMP_container} = $args->{container};
38              
39 75         285 $self;
40             }
41              
42              
43             sub coerce($@)
44 65     65 1 164 { my ($class, $thing, $container) = (shift, shift, shift);
45 65 100       362 if($thing->isa($class))
46 34         121 { $thing->container($container);
47 34         107 return $thing;
48             }
49              
50 31 100       147 return $class->buildFromBody($thing, $container, @_)
51             if $thing->isa('Mail::Message::Body');
52              
53             # Although cloning is a Bad Thing(tm), we must avoid modifying
54             # header fields of messages which reside in a folder.
55 13 50       84 my $message = $thing->isa('Mail::Box::Message') ? $thing->clone : $thing;
56              
57 13         64 my $part = $class->SUPER::coerce($message);
58 13         45 $part->container($container);
59 13         65 $part;
60             }
61              
62              
63             sub buildFromBody($$;@)
64 18     18 1 39 { my ($class, $body, $container) = (shift, shift, shift);
65              
66 18         111 my $head = Mail::Message::Head::Complete->new;
67 18         51 while(@_)
68 0 0       0 { if(ref $_[0]) {$head->add(shift)}
  0         0  
69 0         0 else {$head->add(shift, shift)}
70             }
71              
72 18         62 my $part = $class->new(head => $head, container => $container);
73              
74 18         95 $part->body($body);
75 18         52 $part;
76             }
77              
78             sub container(;$)
79 83     83 1 126 { my $self = shift;
80 83 100       201 @_ or return $self->{MMP_container};
81              
82 70         148 $self->{MMP_container} = shift;
83 70         234 weaken($self->{MMP_container});
84             }
85              
86             sub toplevel()
87 0 0   0 1 0 { my $body = shift->container or return;
88 0 0       0 my $msg = $body->message or return;
89 0         0 $msg->toplevel;
90             }
91              
92             sub isPart() { 1 }
93              
94             sub partNumber()
95 11     11 1 24 { my $self = shift;
96 11 50       27 my $body = $self->container or panic 'no container';
97 11         39 $body->partNumberOf($self);
98             }
99              
100             sub readFromParser($;$)
101 29     29 1 102 { my ($self, $parser, $bodytype) = @_;
102              
103             my $head = $self->readHead($parser) //
104 29   33     164 Mail::Message::Head::Complete->new(message => $self, field_type => $self->{MM_field_type});
105              
106 29   33     254 my $body = $self->readBody($parser, $head, $bodytype) //
107             Mail::Message::Body::Lines->new(data => []);
108              
109 29         190 $self->head($head);
110 29         103 $self->storeBody($body->contentInfoFrom($head));
111 29         140 $self;
112             }
113              
114             #--------------------
115              
116             sub printEscapedFrom($)
117 0     0 1   { my ($self, $out) = @_;
118 0           $self->head->print($out);
119 0           $self->body->printEscapedFrom($out);
120             }
121              
122             #--------------------
123              
124             sub destruct()
125 0     0 1   { my $self = shift;
126 0           error __x"you cannot destruct message parts, only whole messages.";
127             }
128              
129             1;