Mail/Make/GPG.pm

filter: Statementstmt 9.6% 25 / 259 Branchbran 0.0% 0 / 90 Conditioncond 0.0% 0 / 93 MC/DCmcdc 0.0% 0 / 49 Subroutinesub 23.5% 8 / 34 Podpod 83.3% 10 / 12 totaltotal 8.0% 43 / 537 CC 93 SCAR 44.0
CC
93
Cov
6%
sign_encrypt
182.0
_entity_from_canonical
182.0
_serialise_for_gpg
132.0
CRAP 81.1
« Mail/Make/SMIME.pm ↑ Summary Mail/Make/Entity.pm »
1 ##----------------------------------------------------------------------------
2 ## MIME Email Builder - ~/lib/Mail/Make/GPG.pm
3 ## Version v0.1.5
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2026/03/05
7 ## Modified 2026/05/11
8 ## All rights reserved
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package Mail::Make::GPG;
15 BEGIN
16 1 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 warnings::register_categories( 'Mail::Make' );
20 1 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶ use vars qw( $VERSION $EXCEPTION_CLASS );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶ use Mail::Make::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 our $EXCEPTION_CLASS = 'Mail::Make::Exception';
24 1 our $VERSION = 'v0.1.5';
25 };
26  
27 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 sub init
31 0 ▶{
Subroutine init: not called CC=2 SCAR=17.9
Pod documented
32 0 my $self = shift( @_ );
33 0 $self->{auto_fetch} = 0; # bool: fetch missing recipient keys from keyserver
34 0 $self->{digest} = 'SHA256';
35 0 $self->{gpg_bin} = undef; # explicit path to gpg binary; undef = search PATH
36 0 $self->{key_id} = undef; # default signing key fingerprint or ID
37 0 $self->{keyserver} = undef; # keyserver URL for auto-fetch
38 0 $self->{passphrase} = undef; # string or CODE ref; undef = use gpg-agent
39 0 $self->{_exception_class} = $EXCEPTION_CLASS;
40 0 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 0 0
41 0 return( $self );
42 }
43  
44 0 ▶sub auto_fetch { return( shift->_set_get_boolean( 'auto_fetch', @_ ) ); }
Subroutine auto_fetch: not called CC=1 SCAR=6.9
Pod documented
45  
46 0 ▶sub digest { return( shift->_set_get_scalar( 'digest', @_ ) ); }
Subroutine digest: not called CC=1 SCAR=6.9
Pod documented
47  
48 # encrypt( entity => $entity, recipients => \@addrs [, %opts] )
49 # Signs $entity and returns a new Mail::Make object whose top-level MIME type is
50 # multipart/encrypted per RFC 3156 §4.
51 #
52 # The caller is responsible for supplying recipient public keys in the GnuPG keyring.
53 # When AutoFetch + KeyServer are set, we attempt key retrieval first.
54 sub encrypt
55 0 ▶{
Subroutine encrypt: not called CC=9 SCAR=45.0
Pod documented
56 0 my $self = shift( @_ );
57 0 my $opts = $self->_get_args_as_hash( @_ );
58 my $entity = $opts->{entity} ||
59 0 ▶ return( $self->error( "The option 'entity' is required." ) );
Condition0%0/2
$opts->{'entity'} || (return $self->error(q[The option 'entity' is required.]))
l!l
00
MC/DC0%0/1
$opts->{'entity'} || (return $self->error(q[The option 'entity' is required.]))
$opts->{'entity'}
Truth table0%0/2
A
A= $opts->{'entity'}
Aresult
11
00
60 my $recipients = $opts->{recipients} ||
61 0 ▶ return( $self->error( "The option 'recipients' is required." ) );
Condition0%0/2
$opts->{'recipients'} || (return $self->error(q[The option 'recipients' is required.]))
l!l
00
MC/DC0%0/1
$opts->{'recipients'} || (return $self->error(q[The option 'recipients' is required.]))
$opts->{'recipients'}
Truth table0%0/2
A
A= $opts->{'recipients'}
Aresult
11
00
62 0 ▶ $recipients = [ $recipients ] unless( ref( $recipients ) eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $recipients eq "ARRAY" 0 0 0
63 0 ▶ unless( scalar( @$recipients ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @$recipients) 0 0 0
64 {
65 0 return( $self->error( "The value for the 'recipients' must not be empty." ) );
66 }
67  
68 0 ▶ $self->_maybe_fetch_keys( $recipients ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_maybe_fetch_keys($recipients) or return $self->pass_error 0 0 0
69  
70 # Serialise the original message body for gpg input
71 0 ▶ my $plaintext = $self->_serialise_for_gpg( $entity ) || return( $self->pass_error );
Condition0%0/2
$self->_serialise_for_gpg($entity) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_serialise_for_gpg($entity) || (return $self->pass_error)
$self->_serialise_for_gpg($entity)
Truth table0%0/2
A
A= $self->_serialise_for_gpg($entity)
Aresult
11
00
72  
73 0 my @args = ( $self->_base_gpg_args, '--encrypt', '--armor' );
74 0 push( @args, '--recipient', $_ ) for( @$recipients );
75  
76 0 ▶ my $ciphertext = $self->_run_gpg( \@args, \$plaintext ) || return( $self->pass_error );
Condition0%0/2
$self->_run_gpg(\@args, \$plaintext) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_run_gpg(\@args, \$plaintext) || (return $self->pass_error)
$self->_run_gpg(\@args, \$plaintext)
Truth table0%0/2
A
A= $self->_run_gpg(\@args, \$plaintext)
Aresult
11
00
77  
78 0 return( $self->_build_encrypted_mail( $entity, \$ciphertext ) );
79 }
80  
81 0 ▶sub gpg_bin { return( shift->_set_get_scalar( 'gpg_bin', @_ ) ); }
Subroutine gpg_bin: not called CC=1 SCAR=6.9
Pod documented
82  
83 0 ▶sub key_id { return( shift->_set_get_scalar( 'key_id', @_ ) ); }
Subroutine key_id: not called CC=1 SCAR=6.9
Pod documented
84  
85 0 ▶sub keyserver { return( shift->_set_get_scalar( 'keyserver', @_ ) ); }
Subroutine keyserver: not called CC=1 SCAR=6.9
Pod documented
86  
87 0 ▶sub passphrase { return( shift->_set_get_scalar( 'passphrase', @_ ) ); }
Subroutine passphrase: not called CC=1 SCAR=6.9
Pod documented
88  
89 # sign( entity => $entity [, %opts] )
90 # Signs $entity and returns a new Mail::Make object whose top-level MIME type is
91 # multipart/signed per RFC 3156 §5.
92 sub sign
93 0 ▶{
Subroutine sign: not called CC=9 SCAR=45.0
Pod documented
94 0 my $self = shift( @_ );
95 0 my $opts = $self->_get_args_as_hash( @_ );
96 my $entity = $opts->{entity} ||
97 0 ▶ return( $self->error( "The option ¶entity' is required." ) );
Condition0%0/2
$opts->{'entity'} || (return $self->error("The option \302\266entity' is required."))
l!l
00
MC/DC0%0/1
$opts->{'entity'} || (return $self->error("The option \302\266entity' is required."))
$opts->{'entity'}
Truth table0%0/2
A
A= $opts->{'entity'}
Aresult
11
00
98  
99 0 ▶ my $key_id = $self->_resolve_key_id( $opts ) ||
Condition0%0/2
$self->_resolve_key_id($opts) || (return $self->error('KeyId is required (set via option or gpg_sign() default).'))
l!l
00
MC/DC0%0/1
$self->_resolve_key_id($opts) || (return $self->error('KeyId is required (set via option or gpg_sign() default).'))
$self->_resolve_key_id($opts)
Truth table0%0/2
A
A= $self->_resolve_key_id($opts)
Aresult
11
00
100 return( $self->error( 'KeyId is required (set via option or gpg_sign() default).' ) );
101 0 ▶ my $digest = uc( $opts->{digest} // $self->{digest} );
Condition0%0/3
$opts->{"digest"} // $self->{"digest"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"digest"} // $self->{"digest"}
$opts->{"digest"} $self->{"digest"}
Truth table0%0/3
A // B
A= $opts->{"digest"}B= $self->{"digest"}
ABresult
1X1
011
000
102  
103 # Ensure Date and Message-ID are committed to the Mail::Make object's own _headers
104 # BEFORE serialising. This must happen without calling as_entity(), which would merge
105 # RFC 2822 headers onto $self->{_parts}[0].
106 0 ▶ $self->_ensure_envelope_headers( $entity ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_ensure_envelope_headers($entity) or return $self->pass_error 0 0 0
107  
108 # Serialise the MIME body that will be signed: Part 1 of multipart/signed.
109 # Per RFC 3156 §5.1 this is the entity with CRLF line endings, exactly as it will
110 # appear on the wire.
111 0 ▶ my $canonical = $self->_serialise_for_gpg( $entity ) || return( $self->pass_error );
Condition0%0/2
$self->_serialise_for_gpg($entity) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_serialise_for_gpg($entity) || (return $self->pass_error)
$self->_serialise_for_gpg($entity)
Truth table0%0/2
A
A= $self->_serialise_for_gpg($entity)
Aresult
11
00
112  
113 0 my $passphrase = $self->_resolve_passphrase( $opts );
114 0 ▶ return( $self->pass_error ) if( $self->error ); # CODE ref may have thrown
Branch: 1 branch
BranchTrueFalseTotal
if $self->error 0 0 0
115  
116 0 my @args = (
117 $self->_base_gpg_args,
118 '--detach-sign',
119 '--armor',
120 '--digest-algo', $digest,
121 '--local-user', $key_id,
122 );
123 0 ▶ if( defined( $passphrase ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $passphrase) 0 0 0
124 {
125 0 push( @args, '--passphrase-fd', '0', '--pinentry-mode', 'loopback' );
126 }
127  
128 0 ▶ my $signature = $self->_run_gpg( \@args, \$canonical, passphrase => $passphrase ) || return( $self->pass_error );
Condition0%0/2
$self->_run_gpg(\@args, \$canonical, 'passphrase', $passphrase) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_run_gpg(\@args, \$canonical, 'passphrase', $passphrase) || (return $self->pass_error)
$self->_run_gpg(\@args, \$canonical, 'passphrase', $passphrase)
Truth table0%0/2
A
A= $self->_run_gpg(\@args, \$canonical, 'passphrase', $passphrase)
Aresult
11
00
129  
130 0 return( $self->_build_signed_mail( $entity, \$signature, $canonical, digest => $digest ) );
131 }
132  
133 # sign_encrypt( entity => $entity, recipients => \@addrs [, %opts] )
134 # Signs then encrypts $entity. The result is a multipart/encrypted message whose payload
135 # is a signed+encrypted OpenPGP message.
136 sub sign_encrypt
137 0 ▶{
Subroutine sign_encrypt: not called CC=13 SCAR=52.0
Pod documented
138 0 my $self = shift( @_ );
139 0 my $opts = $self->_get_args_as_hash( @_ );
140 0 ▶ my $entity = $opts->{entity} || return( $self->error( "The option 'entity' is required." ) );
Condition0%0/2
$opts->{'entity'} || (return $self->error(q[The option 'entity' is required.]))
l!l
00
MC/DC0%0/1
$opts->{'entity'} || (return $self->error(q[The option 'entity' is required.]))
$opts->{'entity'}
Truth table0%0/2
A
A= $opts->{'entity'}
Aresult
11
00
141 0 ▶ my $recipients = $opts->{recipients} || return( $self->error( "The option recipients is required." ) );
Condition0%0/2
$opts->{'recipients'} || (return $self->error('The option recipients is required.'))
l!l
00
MC/DC0%0/1
$opts->{'recipients'} || (return $self->error('The option recipients is required.'))
$opts->{'recipients'}
Truth table0%0/2
A
A= $opts->{'recipients'}
Aresult
11
00
142 0 ▶ $recipients = [ $recipients ] unless( ref( $recipients ) eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $recipients eq "ARRAY" 0 0 0
143 0 ▶ unless( scalar( @$recipients ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @$recipients) 0 0 0
144 {
145 0 return( $self->error( "The value for the option 'recipients' must not be empty." ) );
146 }
147  
148 0 ▶ my $key_id = $self->_resolve_key_id( $opts ) || return( $self->error( 'KeyId is required.' ) );
Condition0%0/2
$self->_resolve_key_id($opts) || (return $self->error('KeyId is required.'))
l!l
00
MC/DC0%0/1
$self->_resolve_key_id($opts) || (return $self->error('KeyId is required.'))
$self->_resolve_key_id($opts)
Truth table0%0/2
A
A= $self->_resolve_key_id($opts)
Aresult
11
00
149 0 ▶ my $digest = uc( $opts->{digest} // $self->{digest} );
Condition0%0/3
$opts->{"digest"} // $self->{"digest"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"digest"} // $self->{"digest"}
$opts->{"digest"} $self->{"digest"}
Truth table0%0/3
A // B
A= $opts->{"digest"}B= $self->{"digest"}
ABresult
1X1
011
000
150  
151 0 ▶ $self->_maybe_fetch_keys( $recipients ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_maybe_fetch_keys($recipients) or return $self->pass_error 0 0 0
152  
153 0 ▶ my $plaintext = $self->_serialise_for_gpg( $entity ) || return( $self->pass_error );
Condition0%0/2
$self->_serialise_for_gpg($entity) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_serialise_for_gpg($entity) || (return $self->pass_error)
$self->_serialise_for_gpg($entity)
Truth table0%0/2
A
A= $self->_serialise_for_gpg($entity)
Aresult
11
00
154  
155 0 my $passphrase = $self->_resolve_passphrase( $opts );
156 0 ▶ return( $self->pass_error ) if( $self->error );
Branch: 1 branch
BranchTrueFalseTotal
if $self->error 0 0 0
157  
158 0 my @args = (
159 $self->_base_gpg_args,
160 '--sign',
161 '--encrypt',
162 '--armor',
163 '--digest-algo', $digest,
164 '--local-user', $key_id,
165 );
166 0 push( @args, '--recipient', $_ ) for( @$recipients );
167 0 ▶ if( defined( $passphrase ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $passphrase) 0 0 0
168 {
169 0 push( @args, '--passphrase-fd', '0', '--pinentry-mode', 'loopback' );
170 }
171  
172 0 ▶ my $ciphertext = $self->_run_gpg( \@args, \$plaintext, passphrase => $passphrase ) || return( $self->pass_error );
Condition0%0/2
$self->_run_gpg(\@args, \$plaintext, 'passphrase', $passphrase) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_run_gpg(\@args, \$plaintext, 'passphrase', $passphrase) || (return $self->pass_error)
$self->_run_gpg(\@args, \$plaintext, 'passphrase', $passphrase)
Truth table0%0/2
A
A= $self->_run_gpg(\@args, \$plaintext, 'passphrase', $passphrase)
Aresult
11
00
173  
174 0 return( $self->_build_encrypted_mail( $entity, \$ciphertext ) );
175 }
176  
177 # _base_gpg_args() → list
178 # Returns args common to every gpg invocation.
179 sub _base_gpg_args
180 0 ▶{
Subroutine _base_gpg_args: not called CC=2 SCAR=17.9
181 0 my $self = shift( @_ );
182 0 ▶ my $bin = $self->_find_gpg_bin || return( $self->pass_error );
Condition0%0/2
$self->_find_gpg_bin || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_find_gpg_bin || (return $self->pass_error)
$self->_find_gpg_bin
Truth table0%0/2
A
A= $self->_find_gpg_bin
Aresult
11
00
183 return(
184 0 $bin,
185 '--batch',
186 '--no-tty',
187 '--status-fd', '2',
188 );
189 }
190  
191 # _build_encrypted_mail( $original_mail, \$ciphertext ) → Mail::Make object
192 # Constructs a new Mail::Make object whose body is a RFC 3156 §4
193 # multipart/encrypted structure.
194 #
195 # Structure:
196 # multipart/encrypted; protocol="application/pgp-encrypted"
197 # ├── application/pgp-encrypted ("Version: 1")
198 # └── application/octet-stream (ASCII-armoured ciphertext)
199 sub _build_encrypted_mail
200 0 ▶{
Subroutine _build_encrypted_mail: not called CC=4 SCAR=30.0
201 0 my( $self, $original, $ciphertext_ref ) = @_;
202 0 require Mail::Make;
203 0 require Mail::Make::Entity;
204  
205 0 my $boundary = _random_boundary();
206  
207 # Build the two MIME parts
208 0 ▶ my $ver_part = Mail::Make::Entity->build(
Condition0%0/2
'Mail::Make::Entity'->build('type', 'application/pgp-encrypted', 'encoding', '7bit', 'data', "Version: 1\r\n") || (return $self->pass_error('Mail::Make::Entity'->error))
l!l
00
MC/DC0%0/1
'Mail::Make::Entity'->build('type', 'application/pgp-encrypted', 'encoding', '7bit', 'data', "Version: 1\r\n") || (return $self->pass_error('Mail::Make::Entity'->error))
'Mail::Make::Entity'->build('type', 'application/pgp-encrypted', 'encoding', '7bit', 'data', "Version: 1\r\n")
Truth table0%0/2
A
A= 'Mail::Make::Entity'->build('type', 'application/pgp-encrypted', 'encoding', '7bit', 'data', "Version: 1\r\n")
Aresult
11
00
209 type => 'application/pgp-encrypted',
210 encoding => '7bit',
211 data => "Version: 1\r\n",
212 ) || return( $self->pass_error( Mail::Make::Entity->error ) );
213 0 $ver_part->headers->set( 'Content-Disposition' => 'inline' );
214  
215 my $ct_part = Mail::Make::Entity->build(
216 type => 'application/octet-stream',
217 encoding => '7bit',
218 0 ▶ data => ${ $ciphertext_ref },
Condition0%0/2
'Mail::Make::Entity'->build('type', 'application/octet-stream', 'encoding', '7bit', 'data', ${$ciphertext_ref;}) || (return $self->pass_error('Mail::Make::Entity'->error))
l!l
00
MC/DC0%0/1
'Mail::Make::Entity'->build('type', 'application/octet-stream', 'encoding', '7bit', 'data', ${$ciphertext_ref;}) || (return $self->pass_error('Mail::Make::Entity'->error))
'Mail::Make::Entity'->build('type', 'application/octet-stream', 'encoding', '7bit', 'data', ${$ciphertext_ref;})
Truth table0%0/2
A
A= 'Mail::Make::Entity'->build('type', 'application/octet-stream', 'encoding', '7bit', 'data', ${$ciphertext_ref;})
Aresult
11
00
219 ) || return( $self->pass_error( Mail::Make::Entity->error ) );
220 0 $ct_part->headers->set( 'Content-Disposition' => 'inline; filename="encrypted.asc"' );
221  
222 # Assemble the multipart/encrypted container
223 0 ▶ my $top = Mail::Make::Entity->build(
Condition0%0/2
'Mail::Make::Entity'->build('type', sprintf('multipart/encrypted; protocol="application/pgp-encrypted"; boundary="%s"', $boundary)) || (return $self->pass_error('Mail::Make::Entity'->error))
l!l
00
MC/DC0%0/1
'Mail::Make::Entity'->build('type', sprintf('multipart/encrypted; protocol="application/pgp-encrypted"; boundary="%s"', $boundary)) || (return $self->pass_error('Mail::Make::Entity'->error))
'Mail::Make::Entity'->build('type', sprintf('multipart/encrypted; protocol="application/pgp-encrypted"; boundary="%s"', $boundary))
Truth table0%0/2
A
A= 'Mail::Make::Entity'->build('type', sprintf('multipart/encrypted; protocol="application/pgp-encrypted"; boundary="%s"', $boundary))
Aresult
11
00
224 type => sprintf(
225 'multipart/encrypted; protocol="application/pgp-encrypted"; boundary="%s"',
226 $boundary
227 ),
228 ) || return( $self->pass_error( Mail::Make::Entity->error ) );
229 0 $top->add_part( $ver_part );
230 0 $top->add_part( $ct_part );
231  
232 0 return( $self->_wrap_in_mail( $original, $top ) );
233 }
234  
235 # _build_signed_mail( $original_mail, \$signature, digest => $algo ) → Mail::Make object
236 # Constructs a new Mail::Make object whose body is a RFC 3156 §5
237 # multipart/signed structure.
238 #
239 # Structure:
240 # multipart/signed; protocol="application/pgp-signature"; micalg="pgp-sha256"
241 # ├── <original MIME body: the part that was signed>
242 # └── application/pgp-signature (ASCII-armoured detached signature)
243 sub _build_signed_mail
244 0 ▶{
Subroutine _build_signed_mail: not called CC=5 SCAR=34.0
245 0 my $self = shift( @_ );
246 0 my $original = shift( @_ );
247 0 my $signature_ref = shift( @_ );
248 0 my $canonical = shift( @_ );
249 0 my $opts = $self->_get_args_as_hash( @_ );
250 0 ▶ my $digest = lc( $opts->{digest} // $self->{digest} );
Condition0%0/3
$opts->{"digest"} // $self->{"digest"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"digest"} // $self->{"digest"}
$opts->{"digest"} $self->{"digest"}
Truth table0%0/3
A // B
A= $opts->{"digest"}B= $self->{"digest"}
ABresult
1X1
011
000
251 0 require Mail::Make;
252 0 require Mail::Make::Entity;
253  
254 0 my $boundary = _random_boundary();
255  
256 # Part 1: a fresh entity whose content is exactly $canonical (the MIME-only bytes
257 # that gpg signed). Built via _entity_from_canonical() which parses the Content-* headers
258 # from $canonical and wraps the body in a Body::InCore.
259 # We never call as_entity() on $original here: for simple text/plain messages
260 # as_entity() would re-add RFC 2822 headers onto $self->{_parts}[0], corrupting the
261 # MIME-only Part 1.
262 0 ▶ my $body_entity = $self->_entity_from_canonical( $canonical ) || return( $self->pass_error );
Condition0%0/2
$self->_entity_from_canonical($canonical) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_entity_from_canonical($canonical) || (return $self->pass_error)
$self->_entity_from_canonical($canonical)
Truth table0%0/2
A
A= $self->_entity_from_canonical($canonical)
Aresult
11
00
263  
264 # Part 2: the detached signature
265 my $sig_part = Mail::Make::Entity->build(
266 type => 'application/pgp-signature',
267 encoding => '7bit',
268 0 ▶ data => ${ $signature_ref },
Condition0%0/2
'Mail::Make::Entity'->build('type', 'application/pgp-signature', 'encoding', '7bit', 'data', ${$signature_ref;}) || (return $self->pass_error('Mail::Make::Entity'->error))
l!l
00
MC/DC0%0/1
'Mail::Make::Entity'->build('type', 'application/pgp-signature', 'encoding', '7bit', 'data', ${$signature_ref;}) || (return $self->pass_error('Mail::Make::Entity'->error))
'Mail::Make::Entity'->build('type', 'application/pgp-signature', 'encoding', '7bit', 'data', ${$signature_ref;})
Truth table0%0/2
A
A= 'Mail::Make::Entity'->build('type', 'application/pgp-signature', 'encoding', '7bit', 'data', ${$signature_ref;})
Aresult
11
00
269 ) || return( $self->pass_error( Mail::Make::Entity->error ) );
270 0 $sig_part->headers->set( 'Content-Disposition' => 'inline; filename="signature.asc"' );
271  
272 # Multipart/signed container
273 0 ▶ my $top = Mail::Make::Entity->build(
Condition0%0/2
'Mail::Make::Entity'->build('type', sprintf('multipart/signed; protocol="application/pgp-signature"; micalg="pgp-%s"; boundary="%s"', $digest, $boundary)) || (return $self->pass_error('Mail::Make::Entity'->error))
l!l
00
MC/DC0%0/1
'Mail::Make::Entity'->build('type', sprintf('multipart/signed; protocol="application/pgp-signature"; micalg="pgp-%s"; boundary="%s"', $digest, $boundary)) || (return $self->pass_error('Mail::Make::Entity'->error))
'Mail::Make::Entity'->build('type', sprintf('multipart/signed; protocol="application/pgp-signature"; micalg="pgp-%s"; boundary="%s"', $digest, $boundary))
Truth table0%0/2
A
A= 'Mail::Make::Entity'->build('type', sprintf('multipart/signed; protocol="application/pgp-signature"; micalg="pgp-%s"; boundary="%s"', $digest, $boundary))
Aresult
11
00
274 type => sprintf(
275 'multipart/signed; protocol="application/pgp-signature"; micalg="pgp-%s"; boundary="%s"',
276 $digest, $boundary
277 ),
278 ) || return( $self->pass_error( Mail::Make::Entity->error ) );
279 0 $top->add_part( $body_entity );
280 0 $top->add_part( $sig_part );
281  
282 0 return( $self->_wrap_in_mail( $original, $top ) );
283 }
284  
285 # _ensure_envelope_headers( $mail_make_obj )
286 # Generates Date and Message-ID on $mail directly into its _headers object WITHOUT calling
287 # as_entity(). Called by sign() and sign_encrypt() before _serialise_for_gpg() so that
288 # those values exist when _wrap_in_mail() later copies _headers onto the outer multipart wrapper.
289 sub _ensure_envelope_headers
290 0 ▶{
Subroutine _ensure_envelope_headers: not called CC=8 SCAR=42.8
291 0 my $self = shift( @_ );
292 0 ▶ my $mail = shift( @_ ) ||
Condition0%0/2
shift @_ || (return $self->error('No Make::Mail instance was provided.'))
l!l
00
MC/DC0%0/1
shift @_ || (return $self->error('No Make::Mail instance was provided.'))
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
293 return( $self->error( "No Make::Mail instance was provided." ) );
294 0 ▶ if( !$self->_is_a( $mail => 'Mail::Make' ) )
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->_is_a($mail, 'Mail::Make')) { } 0 0 0
elsif (not $self->_is_a($mail->{'_headers'}, 'Mail::Make::Headers')) { } 0 0 0
295 {
296 0 return( $self->error( "Value provided is not a Mail::Make instance." ) );
297 }
298 elsif( !$self->_is_a( $mail->{_headers} => 'Mail::Make::Headers' ) )
299 {
300 0 return( $self->error( "No Mail::Make::Headers instance could be found on Mail::Make object!" ) );
301 }
302  
303 # Date
304 0 ▶ unless( $mail->{_headers}->exists( 'Date' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($mail->{"_headers"}->exists("Date")) 0 0 0
305 {
306 $mail->{_headers}->init_header( Date => $mail->_format_date ) ||
307 0 ▶ return( $self->pass_error( $mail->{_headers}->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$mail->{"_headers"}->init_header("Date", $mail->_format_date) or return $self->pass_error($mail->{"_headers"}->error) 0 0 0
308 }
309  
310 # Message-ID
311 0 ▶ unless( $mail->{_headers}->exists( 'Message-ID' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($mail->{"_headers"}->exists("Message-ID")) 0 0 0
312 {
313 $mail->{_headers}->message_id(
314 { generate => 1, domain => $mail->_default_domain }
315 0 ▶ ) || return( $self->pass_error( $mail->{_headers}->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$mail->{"_headers"}->message_id({"generate", 1, "domain", $mail->_default_domain}) or return $self->pass_error($mail->{"_headers"}->error) 0 0 0
316 }
317  
318 0 return(1);
319 }
320  
321 # _entity_from_canonical( $canonical ) → Mail::Make::Entity
322 # Builds a fresh Mail::Make::Entity whose headers and body match $canonical exactly (the
323 # MIME-only string returned by _serialise_for_gpg). Used as Part 1 of the multipart/signed
324 # wrapper so that what Thunderbird verifies is byte-for-byte identical to what gpg signed.
325 sub _entity_from_canonical
326 0 ▶{
Subroutine _entity_from_canonical: not called CC=13 SCAR=52.0
327 0 my( $self, $canonical ) = @_;
328 0 require Mail::Make::Entity;
329 0 require Mail::Make::Headers;
330 0 require Mail::Make::Body::InCore;
331  
332 # Split on the first CRLF+CRLF blank-line separator.
333 0 my $pos = index( $canonical, "\015\012\015\012" );
334 0 ▶ if( $pos < 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 0) 0 0 0
335 {
336 0 return( $self->error( 'No header/body separator.' ) );
337 }
338  
339 0 my $hdr_block = substr( $canonical, 0, $pos );
340 0 my $body = substr( $canonical, $pos + 4 ); # skip CRLFCRLF
341  
342 # Build a fresh entity with a fresh Headers object.
343 0 ▶ my $entity = Mail::Make::Entity->new || return( $self->pass_error( Mail::Make::Entity->error ) );
Condition0%0/2
'Mail::Make::Entity'->new || (return $self->pass_error('Mail::Make::Entity'->error))
l!l
00
MC/DC0%0/1
'Mail::Make::Entity'->new || (return $self->pass_error('Mail::Make::Entity'->error))
'Mail::Make::Entity'->new
Truth table0%0/2
A
A= 'Mail::Make::Entity'->new
Aresult
11
00
344 0 ▶ my $headers = Mail::Make::Headers->new || return( $self->pass_error( Mail::Make::Headers->error ) );
Condition0%0/2
'Mail::Make::Headers'->new || (return $self->pass_error('Mail::Make::Headers'->error))
l!l
00
MC/DC0%0/1
'Mail::Make::Headers'->new || (return $self->pass_error('Mail::Make::Headers'->error))
'Mail::Make::Headers'->new
Truth table0%0/2
A
A= 'Mail::Make::Headers'->new
Aresult
11
00
345 0 $entity->headers( $headers );
346  
347 # Parse MIME header lines from $hdr_block.
348 # Continuation lines (starting with whitespace) are folded onto the preceding field value.
349 0 my $cur_name = '';
350 0 my $cur_value = '';
351 0 for my $line ( split( /\015\012/, $hdr_block ) )
352 {
353 0 ▶ if( $line =~ /^[ \t]/ )
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^[ \t]/) { } 0 0 0
elsif ($line =~ /^([\x21-\x39\x3B-\x7E]+):\s*(.*?)\s*$/) { } 0 0 0
354 {
355 # Continuation: append stripped content to current value.
356 0 ( my $cont = $line ) =~ s/^[ \t]+//;
357 0 $cur_value .= ' ' . $cont;
358 }
359 elsif( $line =~ /^([\x21-\x39\x3B-\x7E]+):\s*(.*?)\s*$/ )
360 {
361 # New field: flush the previous one first.
362 0 ▶ if( CORE::length( $cur_name ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $cur_name) 0 0 0
363 {
364 0 ▶ $headers->push_header( $cur_name => $cur_value ) ||
Branch: 1 branch
BranchTrueFalseTotal
$headers->push_header($cur_name, $cur_value) or return $self->pass_error($headers->error) 0 0 0
365 return( $self->pass_error( $headers->error ) );
366 }
367 0 ( $cur_name, $cur_value ) = ( $1, $2 );
368 }
369 }
370 # Flush the last header.
371 0 ▶ if( CORE::length( $cur_name ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $cur_name) 0 0 0
372 {
373 0 ▶ $headers->push_header( $cur_name => $cur_value ) ||
Branch: 1 branch
BranchTrueFalseTotal
$headers->push_header($cur_name, $cur_value) or return $self->pass_error($headers->error) 0 0 0
374 return( $self->pass_error( $headers->error ) );
375 }
376  
377 # Attach the body verbatim; mark is_encoded so print_body skips re-encoding (the body
378 # in $canonical is already encoded).
379 0 ▶ my $body_obj = Mail::Make::Body::InCore->new( $body ) ||
Condition0%0/2
'Mail::Make::Body::InCore'->new($body) || (return $self->pass_error('Mail::Make::Body::InCore'->error))
l!l
00
MC/DC0%0/1
'Mail::Make::Body::InCore'->new($body) || (return $self->pass_error('Mail::Make::Body::InCore'->error))
'Mail::Make::Body::InCore'->new($body)
Truth table0%0/2
A
A= 'Mail::Make::Body::InCore'->new($body)
Aresult
11
00
380 return( $self->pass_error( Mail::Make::Body::InCore->error ) );
381 0 $entity->body( $body_obj );
382 0 $entity->{is_encoded} = 1;
383  
384 # Cache effective_type so is_multipart() and similar checks work.
385 0 ▶ my $ct = $headers->get( 'Content-Type' ) // '';
Condition0%0/2
$headers->get("Content-Type") // ""
l!l
00
MC/DC0%0/1
$headers->get("Content-Type") // ""
$headers->get("Content-Type")
Truth table0%0/2
A
A= $headers->get("Content-Type")
Aresult
11
00
386 0 ( my $type = $ct ) =~ s/;.*//s;
387 0 $type =~ s/\s+$//;
388 0 $entity->effective_type( $type );
389  
390 0 return( $entity );
391 }
392  
393 # _find_gpg_bin() → $path
394 # Locates the gpg binary: explicit gpg_bin attribute wins; otherwise we search for gpg2
395 # then gpg in PATH via File::Which.
396 sub _find_gpg_bin
397 0 ▶{
Subroutine _find_gpg_bin: not called CC=7 SCAR=40.3
398 0 my $self = shift( @_ );
399 0 ▶ if( defined( $self->{gpg_bin} ) && length( $self->{gpg_bin} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"gpg_bin"} and length $self->{"gpg_bin"}) 0 0 0
Condition0%0/3
defined $self->{"gpg_bin"} and length $self->{"gpg_bin"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"gpg_bin"} and length $self->{"gpg_bin"}
defined $self->{"gpg_bin"} length $self->{"gpg_bin"}
Truth table0%0/3
A and B
A= defined $self->{"gpg_bin"}B= length $self->{"gpg_bin"}
ABresult
0X0
100
111
400 {
401 0 return( $self->{gpg_bin} );
402 }
403  
404 0 ▶ $self->_load_class( 'File::Which' ) ||
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("File::Which") or return $self->error("File::Which is required to locate gpg. Install it with: cpan File::Which") 0 0 0
405 return( $self->error( 'File::Which is required to locate gpg. Install it with: cpan File::Which' ) );
406  
407 0 for my $candidate ( qw( gpg2 gpg ) )
408 {
409 0 my $path = File::Which::which( $candidate );
410 0 ▶ if( defined( $path ) && length( $path ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path and length $path) 0 0 0
Condition0%0/3
defined $path and length $path
!ll&&!rl&&r
000
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table0%0/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
411 {
412 0 $self->{gpg_bin} = $path;
413 0 return( $path );
414 }
415 }
416 0 return( $self->error( 'The gpg binary is not found in PATH. Install GnuPG or set the GpgBin option.' ) );
417 }
418  
419 # _maybe_fetch_keys( \@recipients )
420 # When auto_fetch is enabled and a keyserver is configured, attempts to retrieve missing
421 # public keys for each recipient. Failures are silently ignored and the key may already be
422 # in the local keyring.
423 sub _maybe_fetch_keys
424 0 ▶{
Subroutine _maybe_fetch_keys: not called CC=7 SCAR=40.3
425 0 my( $self, $recipients ) = @_;
426 0 ▶ return(1) unless( $self->{auto_fetch} && defined( $self->{keyserver} ) && length( $self->{keyserver} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"auto_fetch"} and defined $self->{"keyserver"} and length $self->{"keyserver"} 0 0 0
Condition0%0/6
$self->{"auto_fetch"} and defined $self->{"keyserver"} and length $self->{"keyserver"}
!ll&&!rl&&r
000
$self->{"auto_fetch"} and defined $self->{"keyserver"}
!ll&&!rl&&r
000
MC/DC0%0/3
$self->{"auto_fetch"} and defined $self->{"keyserver"} and length $self->{"keyserver"}
$self->{"auto_fetch"} defined $self->{"keyserver"} length $self->{"keyserver"}
Truth table0%0/4
A and B and C
A= $self->{"auto_fetch"}B= defined $self->{"keyserver"}C= length $self->{"keyserver"}
ABCresult
0XX0
10X0
1100
1111
427  
428 0 ▶ $self->_load_class( 'IPC::Run' ) ||
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("IPC::Run") or return $self->error("IPC::Run is required for GPG operations. Install it with: cpan IPC::Run") 0 0 0
429 return( $self->error( 'IPC::Run is required for GPG operations. Install it with: cpan IPC::Run' ) );
430  
431 0 ▶ my $bin = $self->_find_gpg_bin || return( $self->pass_error );
Condition0%0/2
$self->_find_gpg_bin || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_find_gpg_bin || (return $self->pass_error)
$self->_find_gpg_bin
Truth table0%0/2
A
A= $self->_find_gpg_bin
Aresult
11
00
432 0 local $@;
433 0 foreach my $r ( @$recipients )
434 {
435 0 my( $out, $err ) = ( '', '' );
436 eval
437 0 {
438 IPC::Run::run(
439 [ $bin, '--batch', '--no-tty',
440 '--keyserver', $self->{keyserver},
441 0 '--locate-keys', $r,
442 ],
443 \undef, \$out, \$err,
444 );
445 };
446 # Best-effort: do not propagate errors from key fetch
447 }
448 0 return(1);
449 }
450  
451 # _random_boundary() → $string
452 # Generates a random MIME boundary string.
453 sub _random_boundary
454 0 ▶{
Subroutine _random_boundary: not called CC=1 SCAR=6.9
455 0 return( sprintf( '----=_NextPart_GPG_%08X%08X', int( rand(0xFFFFFFFF) ), int( rand(0xFFFFFFFF) ) ) );
456 }
457  
458 # _resolve_key_id( \%opts ) → $string
459 sub _resolve_key_id
460 0 ▶{
Subroutine _resolve_key_id: not called CC=3 SCAR=24.8
461 0 my $self = shift( @_ );
462 0 my $opts = $self->_get_args_as_hash( @_ );
463 0 ▶ my $kid = $opts->{key_id} // $self->{key_id} // '';
Condition0%0/5
($opts->{"key_id"} // $self->{"key_id"}) // ""
l!l
00
$opts->{"key_id"} // $self->{"key_id"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($opts->{"key_id"} // $self->{"key_id"}) // ""
$opts->{"key_id"} $self->{"key_id"}
Truth table0%0/3
A // B
A= $opts->{"key_id"}B= $self->{"key_id"}
ABresult
1X1
011
000
464 0 return( $kid );
465 }
466  
467 # _resolve_passphrase( \%opts ) → $string | undef
468 # Resolves the passphrase from per-call option or instance default.
469 # CODE refs are called once here with no arguments.
470 # Returns undef when no passphrase is configured (gpg-agent will be used).
471 sub _resolve_passphrase
472 0 ▶{
Subroutine _resolve_passphrase: not called CC=5 SCAR=34.0
473 0 my $self = shift( @_ );
474 0 my $opts = $self->_get_args_as_hash( @_ );
475 0 ▶ my $pp = $opts->{passphrase} // $self->{passphrase};
Condition0%0/3
$opts->{"passphrase"} // $self->{"passphrase"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"passphrase"} // $self->{"passphrase"}
$opts->{"passphrase"} $self->{"passphrase"}
Truth table0%0/3
A // B
A= $opts->{"passphrase"}B= $self->{"passphrase"}
ABresult
1X1
011
000
476 0 ▶ return unless( defined( $pp ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pp 0 0 0
477 0 ▶ if( ref( $pp ) eq 'CODE' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pp eq "CODE") 0 0 0
478 {
479 0 local $@;
480 0 $pp = eval{ $pp->() };
481 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
482 {
483 0 return( $self->error( "Passphrase callback failed: $@" ) );
484 }
485 }
486 0 return( $pp );
487 }
488  
489 # _run_gpg( \@args, \$input, passphrase => $pp ) → $stdout_string | undef
490 #
491 # Executes gpg via IPC::Run. IPC::Run handles multiplexed I/O internally, avoiding the
492 # select()-loop complexity of a raw fork/pipe approach.
493 #
494 # Passphrase handling (--passphrase-fd 0 + --pinentry-mode loopback):
495 # We prepend the passphrase (followed by a newline) to the stdin payload.
496 # gpg reads exactly one line from fd 0 as the passphrase, then continues reading the same
497 # fd for the message data. This avoids opening a second file descriptor and is the standard
498 # approach for batch use of GnuPG 2.1+.
499 sub _run_gpg
500 0 ▶{
Subroutine _run_gpg: not called CC=8 SCAR=42.8
501 0 my $self = shift( @_ );
502 0 my $args = shift( @_ );
503 0 my $input_ref = shift( @_ );
504 0 my $opts = $self->_get_args_as_hash( @_ );
505 0 my $passphrase = $opts->{passphrase};
506  
507 0 ▶ $self->_load_class( 'IPC::Run' ) ||
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("IPC::Run") or return $self->error("IPC::Run is required for GPG operations. Install it with: cpan IPC::Run") 0 0 0
508 return( $self->error( 'IPC::Run is required for GPG operations. Install it with: cpan IPC::Run' ) );
509  
510 # Build the complete stdin blob
511 0 my $stdin = '';
512 0 ▶ $stdin .= $passphrase . "\n" if( defined( $passphrase ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $passphrase 0 0 0
513 0 ▶ $stdin .= ( ref( $input_ref ) ? ${ $input_ref } : $input_ref );
Branch: 1 branch
BranchTrueFalseTotal
ref $input_ref ? : 0 0 0
514  
515 0 my( $stdout, $stderr ) = ( '', '' );
516  
517 0 local $@;
518 0 local $SIG{PIPE} = 'IGNORE';
519 my $ok = eval
520 0 {
521 0 IPC::Run::run( $args, \$stdin, \$stdout, \$stderr );
522 };
523 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
524 {
525 0 return( $self->error( "gpg execution error: $@" ) );
526 }
527 0 ▶ unless( $ok )
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
528 {
529 # Extract the most informative line from gpg's stderr output
530 0 my @lines = split( /\n/, $stderr );
531 0 my ($msg) = grep { /\bERROR\b|\berror\b|failed|No secret key|No public key|bad passphrase/i } @lines;
532 0 ▶ $msg //= $lines[-1] // $stderr;
Condition0%0/6
$msg //= $lines[-1] // $stderr
l!l&&r!l&&!r
000
$lines[-1] // $stderr
l!l&&r!l&&!r
000
MC/DC0%0/3
$msg //= $lines[-1] // $stderr
$msg $lines[-1] $stderr
Truth table0%0/4
A //= B // C
A= $msgB= $lines[-1]C= $stderr
ABCresult
1XX1
01X1
0011
0000
533 0 $msg =~ s/^\s+|\s+$//g;
534 0 return( $self->error( "gpg failed: $msg" ) );
535 }
536  
537 0 return( $stdout );
538 }
539  
540 # _serialise_for_gpg( $mail_make_obj ) → $string
541 # Returns the MIME body of the Mail::Make object with CRLF line endings, suitable for
542 # feeding to gpg (signing) or for encrypting.
543 #
544 # For multipart/signed (RFC 3156 §5.1) the data fed to gpg must be identical to Part 1 as
545 # it will appear on the wire, i.e. with CRLF.
546 sub _serialise_for_gpg
547 0 ▶{
Subroutine _serialise_for_gpg: not called CC=11 SCAR=48.8
548 0 my( $self, $mail ) = @_;
549 0 ▶ unless( defined( $mail ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mail) 0 0 0
550 {
551 0 return( $self->error( 'No Mail::Make object supplied.' ) );
552 }
553  
554 0 ▶ unless( $mail->can( 'as_entity' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($mail->can("as_entity")) 0 0 0
555 {
556 0 return( $self->error( 'Argument must be a Mail::Make object.' ) );
557 }
558  
559 # RFC 3156 §5.1: Part 1 of multipart/signed must carry only MIME
560 # Content-* headers; RFC 2822 envelope fields belong on the outer wrapper.
561 #
562 # Root-cause: Mail::Make::as_entity() reuses $self->{_parts}[0] as $top_entity for
563 # simple text/plain messages and merges RFC 2822 headers directly onto it. Any call to
564 # as_entity() re-adds those headers to the same object. We therefore serialise to a
565 # string and filter the RFC 2822 header lines at string level, never mutating the entity.
566 0 ▶ my $entity = $mail->as_entity || return( $self->pass_error( $mail->error ) );
Condition0%0/2
$mail->as_entity || (return $self->pass_error($mail->error))
l!l
00
MC/DC0%0/1
$mail->as_entity || (return $self->pass_error($mail->error))
$mail->as_entity
Truth table0%0/2
A
A= $mail->as_entity
Aresult
11
00
567  
568 0 ▶ my $full = $entity->as_string || return( $self->pass_error( $entity->error ) );
Condition0%0/2
$entity->as_string || (return $self->pass_error($entity->error))
l!l
00
MC/DC0%0/1
$entity->as_string || (return $self->pass_error($entity->error))
$entity->as_string
Truth table0%0/2
A
A= $entity->as_string
Aresult
11
00
569  
570 # Canonicalise line endings to CRLF FIRST (RFC 3156 §5.1).
571 # Doing this before the separator search ensures we always find \015\012\015\012
572 # regardless of whether Entity::as_string used LF or CRLF.
573 0 $full =~ s/\015?\012/\015\012/g;
574  
575 # Locate the header / body separator (first blank line).
576 # After canonicalisation this is always \r\n\r\n.
577 0 my $pos = index( $full, "\015\012\015\012" );
578 0 ▶ if( $pos < 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < 0) 0 0 0
579 {
580 0 return( $self->error( 'No header/body separator found.' ) );
581 }
582  
583 # Include the \r\n that terminates the last header in hdr_block,
584 # so that every kept line already carries its own EOL.
585 0 my $hdr_block = substr( $full, 0, $pos + 2 ); # up to and including last header \r\n
586 0 my $body_block = substr( $full, $pos + 4 ); # skip \r\n\r\n
587  
588 # Walk header lines and keep only Content-* headers.
589 # RFC 3156 §5.1: Part 1 carries Content-* headers only.
590 # MIME-Version belongs on the outer wrapper, not inside Part 1.
591 # Continuation lines (starting with whitespace) follow their field.
592 0 my $mime_hdr = '';
593 0 my $keep = 0;
594 0 for my $line ( split( /(?<=\015\012)/, $hdr_block ) )
595 {
596 0 ▶ if( $line =~ /^[ \t]/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^[ \t]/) { } 0 0 0
597 {
598 0 ▶ $mime_hdr .= $line if( $keep );
Branch: 1 branch
BranchTrueFalseTotal
if $keep 0 0 0
599 }
600 else
601 {
602 0 ▶ $keep = ( $line =~ /^Content-/i ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^Content-/i ? : 0 0 0
603 0 ▶ $mime_hdr .= $line if( $keep );
Branch: 1 branch
BranchTrueFalseTotal
if $keep 0 0 0
604 }
605 }
606  
607 # Reassemble: kept MIME headers (each already ends with \r\n)
608 # + one \r\n blank line + body.
609 0 my $raw = $mime_hdr . "\015\012" . $body_block;
610  
611 # RFC 2046 §5.1.1: the \r\n immediately before a boundary delimiter belongs to the
612 # boundary, not to the body. Strip exactly one trailing \r\n.
613 0 $raw =~ s/\015\012$//;
614  
615 0 return( $raw );
616 }
617  
618 # _wrap_in_mail( $original_mail, $top_entity ) → Mail::Make object
619 # Creates a new Mail::Make object that carries $top_entity as its pre-built entity,
620 # copying envelope headers (From, To, Cc, Subject, etc.) from $original_mail.
621 sub _wrap_in_mail
622 0 ▶{
Subroutine _wrap_in_mail: not called CC=2 SCAR=17.9
623 0 my( $self, $original, $top_entity ) = @_;
624 0 require Mail::Make;
625  
626 # Ok, the check for error here is really semantic, because there is virtually zero chance of that happening.
627 0 ▶ my $new = Mail::Make->new || return( $self->pass_error( Mail::Make->error ) );
Condition0%0/2
'Mail::Make'->new || (return $self->pass_error('Mail::Make'->error))
l!l
00
MC/DC0%0/1
'Mail::Make'->new || (return $self->pass_error('Mail::Make'->error))
'Mail::Make'->new
Truth table0%0/2
A
A= 'Mail::Make'->new
Aresult
11
00
628  
629 # Date and Message-ID were generated by _ensure_envelope_headers() in
630 # sign() / sign_encrypt() before _serialise_for_gpg() was called, so
631 # $original->headers already has them. Do NOT call as_entity() here:
632 # for simple text/plain messages as_entity() reuses $self->{_parts}[0] as $top_entity
633 # and would merge RFC 2822 headers back onto it, which would corrupt Part 1 of the
634 # multipart/signed structure.
635  
636 # Merge envelope headers into BOTH the new Mail::Make object AND directly into
637 # $top_entity's headers. The hook in as_entity() returns _gpg_entity verbatim, so the
638 # standard header-merge logic never runs.
639 # We must therefore inject the RFC 2822 headers here.
640 0 my $ent_headers = $top_entity->headers;
641 0 $ent_headers->init_header( 'MIME-Version' => '1.0' );
642  
643 $original->headers->scan(sub
644 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
645 0 my( $name, $value ) = @_;
646 # Inject into top entity so the wire message carries all headers
647 0 $ent_headers->init_header( $name => $value );
648 # Also keep in the new Mail::Make object for introspection
649 0 $new->headers->set( $name => $value );
650 0 return(1);
651 0 });
652  
653 # Store the pre-assembled top entity so as_entity() returns it directly.
654 0 $new->{_gpg_entity} = $top_entity;
655  
656 0 return( $new );
657 }
658  
659 # NOTE: STORABLE support
660 0 ▶sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: not called CC=1 SCAR=6.9
Pod undocumented
661  
662 0 ▶sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: not called CC=1 SCAR=6.9
Pod undocumented
663  
664 1 1;
665 # NOTE: POD
666 __END__
« Mail/Make/SMIME.pm ↑ Summary Mail/Make/Entity.pm »