File Coverage

blib/lib/Mail/Message/Body/Construct.pm
Criterion Covered Total %
statement 56 63 88.8
branch 31 46 67.3
condition 1 6 16.6
subroutine 9 10 90.0
pod 4 4 100.0
total 101 129 78.2


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::Body;{
13             our $VERSION = '4.04';
14             }
15              
16             # Mail::Message::Body::Construct adds functionality to Mail::Message::Body
17              
18 5     5   4218 use strict;
  5         11  
  5         239  
19 5     5   24 use warnings;
  5         9  
  5         382  
20              
21 5     5   30 use Log::Report 'mail-message', import => [ qw/__x error/ ];
  5         10  
  5         46  
22              
23 5     5   1045 use Scalar::Util qw/blessed/;
  5         11  
  5         294  
24              
25 5     5   2846 use Mail::Message::Body::String ();
  5         12  
  5         230  
26 5     5   36 use Mail::Message::Body::Lines ();
  5         8  
  5         4453  
27              
28             #--------------------
29              
30             sub foreachLine($)
31 4     4 1 16 { my ($self, $code) = @_;
32 4         7 my $changes = 0;
33 4         8 my @result;
34              
35 4         16 foreach ($self->lines)
36 8         21 { my $becomes = $code->();
37 8 50       36 if(defined $becomes)
38 8         16 { push @result, $becomes;
39 8 50       25 $changes++ if $becomes ne $_;
40             }
41 0         0 else { $changes++ }
42             }
43              
44 4 50       23 $changes ? (ref $self)->new(based_on => $self, data => \@result) : $self;
45             }
46              
47              
48             sub concatenate(@)
49 9     9 1 21 { my $self = shift;
50 9 50       35 return $self if @_==1;
51              
52 9         20 my @unified;
53 9         42 foreach (grep defined, @_)
54 21 50       228 { push @unified,
    50          
    100          
    100          
55             ! ref $_ ? $_
56             : ref $_ eq 'ARRAY' ? @$_
57             : $_->isa('Mail::Message') ? $_->body->decoded
58             : $_->isa('Mail::Message::Body') ? $_->decoded
59             : error(__x"cannot concatenate element {which}", which => $_);
60             }
61              
62 9         54 (ref $self)->new(
63             based_on => $self,
64             mime_type => 'text/plain',
65             data => join('', @unified),
66             );
67             }
68              
69              
70             sub attach(@)
71 0     0 1 0 { my $self = shift;
72              
73 0         0 my @parts;
74 0   0     0 push @parts, shift while @_ && blessed $_[0];
75 0 0       0 @parts or return $self;
76              
77 0 0       0 unshift @parts, $self->isNested ? $self->nested : $self->isMultipart ? $self->parts : $self;
    0          
78              
79 0 0       0 @parts==1 ? $parts[0] : Mail::Message::Body::Multipart->new(parts => \@parts, @_);
80             }
81              
82              
83             # tests in t/51stripsig.t
84              
85             sub stripSignature($@)
86 14     14 1 119 { my ($self, %args) = @_;
87              
88 14 50       63 return $self if $self->mimeType->isBinary;
89              
90 14         658 my $p = $args{pattern};
91 14 100       117 my $pattern = ! defined $p ? qr/^--\s?$/
    100          
92             : ! ref $p ? qr/^\Q$p/
93             : $p;
94              
95 14         91 my $lines = $self->lines; # no copy!
96             my $stop = defined $args{max_lines} ? @$lines - $args{max_lines}
97 14 100       64 : exists $args{max_lines} ? 0
    100          
98             : @$lines-10;
99              
100 14 100       45 $stop = 0 if $stop < 0;
101 14         32 my ($sigstart, $found);
102              
103 14 100       79 if(ref $pattern eq 'CODE')
104 1         7 { for($sigstart = $#$lines; $sigstart >= $stop; $sigstart--)
105 4 100       20 { $pattern->($lines->[$sigstart]) or next;
106 1         6 $found = 1;
107 1         2 last;
108             }
109             }
110             else
111 13         70 { for($sigstart = $#$lines; $sigstart >= $stop; $sigstart--)
112 76 100       391 { $lines->[$sigstart] =~ $pattern or next;
113 9         20 $found = 1;
114 9         18 last;
115             }
116             }
117              
118 14 100       74 $found or return $self;
119              
120 10   33     84 my $bodytype = $args{result_type} || ref $self;
121 10         80 my $stripped = $bodytype->new(based_on => $self, data => [ @$lines[0..$sigstart-1] ]);
122              
123 10 100       51 wantarray or return $stripped;
124              
125 6         43 my $sig = $bodytype->new(based_on => $self, data => [ @$lines[$sigstart..$#$lines] ]);
126 6         69 ($stripped, $sig);
127             }
128              
129             1;