| 1 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 2 | ## MIME Email Builder - ~/lib/Mail/Make/Headers/ContentType.pm | ||||||||||||||||||||||||||||||||||||||||||
| 3 | ## Version v0.1.0 | ||||||||||||||||||||||||||||||||||||||||||
| 4 | ## Copyright(c) 2026 DEGUEST Pte. Ltd. | ||||||||||||||||||||||||||||||||||||||||||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||||||||||||||||||||||||||||||||||||||||||
| 6 | ## Created 2026/03/02 | ||||||||||||||||||||||||||||||||||||||||||
| 7 | ## Modified 2026/03/02 | ||||||||||||||||||||||||||||||||||||||||||
| 8 | ## All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 9 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 10 | ## This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||
| 11 | ## under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 12 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 13 | package Mail::Make::Headers::ContentType; | ||||||||||||||||||||||||||||||||||||||||||
| 14 | BEGIN | ||||||||||||||||||||||||||||||||||||||||||
| 15 | 10 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 10 | warnings::register_categories( 'Mail::Make' ); | |||||||||||||||||||||||||||||||||||||||||
| 19 | 10 | ▶ | use parent qw( Mail::Make::Headers::Generic ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 10 | ▶ | use vars qw( $VERSION $EXCEPTION_CLASS $VALID_CHARSETS ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 10 | ▶ | use Mail::Make::Exception; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | 10 | our $EXCEPTION_CLASS = 'Mail::Make::Exception'; | |||||||||||||||||||||||||||||||||||||||||
| 23 | # Common charsets accepted as valid; not exhaustive but covers typical usage | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 10 | our $VALID_CHARSETS = qr/^(?: | |||||||||||||||||||||||||||||||||||||||||
| 25 | us-ascii | ascii | | ||||||||||||||||||||||||||||||||||||||||||
| 26 | utf-8 | utf8 | | ||||||||||||||||||||||||||||||||||||||||||
| 27 | iso-8859-[\d]+ | | ||||||||||||||||||||||||||||||||||||||||||
| 28 | windows-\d+ | | ||||||||||||||||||||||||||||||||||||||||||
| 29 | shift[_-]jis | | ||||||||||||||||||||||||||||||||||||||||||
| 30 | euc-jp | | ||||||||||||||||||||||||||||||||||||||||||
| 31 | iso-2022-jp | ||||||||||||||||||||||||||||||||||||||||||
| 32 | )$/xi; | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 10 | our $VERSION = 'v0.1.0'; | |||||||||||||||||||||||||||||||||||||||||
| 34 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | sub init | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 203 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=9 SCAR=23.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 203 | my $self = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 42 | 203 | $self->{_exception_class} = $EXCEPTION_CLASS; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 203 | ▶ | @_ = () if( @_ == 1 && $self->_is_a( $_[0] => 'Module::Generic::Null' ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and $self->_is_a($_[0], "Module::Generic::Null")
MC/DC0%0/2
@_ == 1 and $self->_is_a($_[0], "Module::Generic::Null")
@_ == 1
$self->_is_a($_[0], "Module::Generic::Null")
Truth table66%2/3
A and B
A= @_ == 1B= $self->_is_a($_[0], "Module::Generic::Null")
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 203 | ▶ | if( @_ ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | { | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 201 | my $str = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 47 | 201 | ▶ | if( !defined( $str ) || !length( "$str" ) ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $str and length "$str"
MC/DC0%0/2
defined $str and length "$str"
defined $str
length "$str"
Truth table33%1/3
A and B
A= defined $strB= length "$str"
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | { | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | return( $self->error( "No value was provided for Content-Type field." ) ); | |||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 201 | my $params = $self->_get_args_as_hash( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 52 | 201 | ▶ | my $hv = $self->_parse_header_value( $str ) || return( $self->pass_error ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->_parse_header_value($str) || (return $self->pass_error)
MC/DC0%0/1
$self->_parse_header_value($str) || (return $self->pass_error)
$self->_parse_header_value($str)
Truth table50%1/2
A
A= $self->_parse_header_value($str)
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 201 | $hv->param( $_ => $params->{ $_ } ) for( keys( %$params ) ); | |||||||||||||||||||||||||||||||||||||||||
| 54 | 201 | $self->_hv( $hv ); | |||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 203 | ▶ | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 203 | $self->_field_name( 'Content-Type' ); | |||||||||||||||||||||||||||||||||||||||||
| 58 | 203 | return( $self ); | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 184 | ▶ | sub as_string { return( shift->_hv_as_string( @_ ) ); } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | sub boundary | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 8 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
boundary: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 8 | my $self = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 66 | 8 | ▶ | if( @_ ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | { | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 6 | my $b = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 69 | 6 | ▶ | if( defined( $b ) && length( $b ) ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $b and length $b
MC/DC0%0/2
defined $b and length $b
defined $b
length $b
Truth table33%1/3
A and B
A= defined $bB= length $b
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | { | ||||||||||||||||||||||||||||||||||||||||||
| 71 | # RFC 2046 boundary: 1-70 chars from a limited set, no trailing space | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 6 | ▶ | return( $self->error( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
length $b > 70 or $b =~ m[[^0-9a-zA-Z'()+_,\-./:=? ]] or $b =~ / $/
length $b > 70 or $b =~ m[[^0-9a-zA-Z'()+_,\-./:=? ]]
MC/DC100%3/3
length $b > 70 or $b =~ m[[^0-9a-zA-Z'()+_,\-./:=? ]] or $b =~ / $/
length $b > 70
$b =~ m[[^0-9a-zA-Z'()+_,\-./:=? ]]
$b =~ / $/
Truth table100%4/4
A or B or C
A= length $b > 70B= $b =~ m[[^0-9a-zA-Z'()+_,\-./:=? ]]C= $b =~ / $/
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | "Invalid boundary '$b': only 0-9 a-z A-Z and '()+_,-./:=? ' are allowed, max 70 chars, no trailing space" | ||||||||||||||||||||||||||||||||||||||||||
| 74 | ) ) if( length( $b ) > 70 || $b =~ m{[^0-9a-zA-Z'()+_,\-./:=? ]} || $b =~ / $/ ); | ||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 3 | return( $self->_set_get_param( boundary => $b ) ); | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | return( $self->_set_get_param( 'boundary' ) ); | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | sub charset | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 10 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
charset: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 10 | my $self = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 84 | 10 | ▶ | if( @_ ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 6 | my $cs = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 87 | 6 | ▶ | if( defined( $cs ) && length( $cs ) ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $cs and length $cs
MC/DC0%0/2
defined $cs and length $cs
defined $cs
length $cs
Truth table33%1/3
A and B
A= defined $csB= length $cs
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | { | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 6 | ▶ | unless( $cs =~ $VALID_CHARSETS ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | { | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 1 | return( $self->error( "Unknown or unsupported charset '$cs'." ) ); | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | # Normalise to lowercase | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 5 | $cs = lc( $cs ); | |||||||||||||||||||||||||||||||||||||||||
| 95 | # Normalise utf8 -> utf-8 | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 5 | ▶ | $cs = 'utf-8' if( $cs eq 'utf8' ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 5 | return( $self->_set_get_param( charset => $cs ) ); | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 4 | return( $self->_set_get_param( 'charset' ) ); | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | # Returns a fresh RFC 2046 compliant boundary using Data::UUID | ||||||||||||||||||||||||||||||||||||||||||
| 104 | sub make_boundary | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
make_boundary: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | my $self = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | ▶ | $self->_load_class( 'Data::UUID' ) || return( $self->pass_error ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | return( Data::UUID->new->create_str ); | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub name { return( shift->_set_get_param( name => @_ ) ); } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | sub type | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 8 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
type: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 8 | my $self = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||||
| 116 | 8 | ▶ | if( @_ ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | { | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 3 | ▶ | my $mime = shift( @_ ) || return( $self->error( "No MIME type was provided." ) ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift @_ || (return $self->error('No MIME type was provided.'))
MC/DC100%1/1
shift @_ || (return $self->error('No MIME type was provided.'))
shift @_
Truth table100%2/2
A
A= shift @_
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | # Basic structural validation: type/subtype | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 2 | ▶ | unless( $mime =~ m{^[A-Za-z0-9][A-Za-z0-9!\#\$&\-^_]* | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | /[A-Za-z0-9][A-Za-z0-9!\#\$&\-^_.+]*$}x ) | ||||||||||||||||||||||||||||||||||||||||||
| 122 | { | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 1 | return( $self->error( "Invalid MIME type '$mime': expected 'type/subtype' format." ) ); | |||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | ▶ | my $hv = $self->_new_hv( $mime ) || return( $self->pass_error ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->_new_hv($mime) || (return $self->pass_error)
MC/DC0%0/1
$self->_new_hv($mime) || (return $self->pass_error)
$self->_new_hv($mime)
Truth table50%1/2
A
A= $self->_new_hv($mime)
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 1 | $self->_hv( $hv ); | |||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | return( $mime ); | |||||||||||||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||||||||||||
| 129 | else | ||||||||||||||||||||||||||||||||||||||||||
| 130 | { | ||||||||||||||||||||||||||||||||||||||||||
| 131 | 5 | ▶ | my $hv = $self->_hv || return( '' ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->_hv || (return '')
MC/DC100%1/1
$self->_hv || (return '')
$self->_hv
Truth table100%2/2
A
A= $self->_hv
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 4 | return( $hv->value_data ); | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | # Alias | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | ▶ | sub value { return( shift->type( @_ ) ); } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
value: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 140 | # NOTE: POD | ||||||||||||||||||||||||||||||||||||||||||
| 141 | __END__ | ||||||||||||||||||||||||||||||||||||||||||