| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## MIME Email Builder - ~/lib/Mail/Make/Headers/ContentTransferEncoding.pm | ||
| 3 | ## Version v0.2.0 | ||
| 4 | ## Copyright(c) 2026 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2026/03/02 | ||
| 7 | ## Modified 2026/03/03 | ||
| 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::ContentTransferEncoding; | ||
| 14 | BEGIN | ||
| 15 | { | ||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | warnings::register_categories( 'Mail::Make' ); | ||
| 19 | use parent qw( Module::Generic ); | ||
| 20 | use vars qw( $VERSION $EXCEPTION_CLASS %VALID_ENCODINGS ); | ||
| 21 | use Mail::Make::Exception; | ||
| 22 | use overload | ||
| 23 | ( | ||
| 24 | '""' => 'as_string', | ||
| 25 | bool => sub{1}, | ||
| 26 | ); | ||
| 27 | # RFC 2045 section 6.1 - defined encodings | ||
| 28 | %VALID_ENCODINGS = | ||
| 29 | ( | ||
| 30 | '7bit' => 1, | ||
| 31 | '8bit' => 1, | ||
| 32 | 'binary' => 1, | ||
| 33 | 'base64' => 1, | ||
| 34 | 'quoted-printable' => 1, | ||
| 35 | ); | ||
| 36 | our $EXCEPTION_CLASS = 'Mail::Make::Exception'; | ||
| 37 | our $VERSION = 'v0.2.0'; | ||
| 38 | }; | ||
| 39 | |||
| 40 | use strict; | ||
| 41 | use warnings; | ||
| 42 | |||
| 43 | sub init | ||
| 44 | { | ||
| 45 | my $self = shift( @_ ); | ||
| 46 | $self->{_encoding} = undef; | ||
| 47 | $self->{_exception_class} = $EXCEPTION_CLASS; | ||
| 48 | # Module::Generic passes positional args through init() as-is when | ||
| 49 | # _init_strict_use_sub is not set - accept the encoding as first positional arg. | ||
| 50 | my $encoding = shift( @_ ); | ||
| 51 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 52 | if( defined( $encoding ) && length( $encoding ) ) | ||
| 53 | { | ||
| 54 | $self->encoding( $encoding ) || return( $self->pass_error ); | ||
| 55 | } | ||
| 56 | return( $self ); | ||
| 57 | } | ||
| 58 | |||
| 59 | sub as_string | ||
| 60 | { | ||
| 61 | my $self = shift( @_ ); | ||
| 62 | return( $self->{_encoding} // '' ); | ||
| 63 | } | ||
| 64 | |||
| 65 | # encoding( [$value] ) | ||
| 66 | # Gets or sets the encoding token. Validates against VALID_ENCODINGS. | ||
| 67 | sub encoding | ||
| 68 | { | ||
| 69 | my $self = shift( @_ ); | ||
| 70 | if( @_ ) | ||
| 71 | { | ||
| 72 | my $enc = lc( shift( @_ ) // '' ); | ||
| 73 | unless( exists( $VALID_ENCODINGS{ $enc } ) ) | ||
| 74 | { | ||
| 75 | return( $self->error( "Invalid Content-Transfer-Encoding '$enc'; must be one of: " . | ||
| 76 | join( ', ', sort( keys( %VALID_ENCODINGS ) ) ) ) ); | ||
| 77 | } | ||
| 78 | $self->{_encoding} = $enc; | ||
| 79 | return( $self ); | ||
| 80 | } | ||
| 81 | return( $self->{_encoding} ); | ||
| 82 | } | ||
| 83 | |||
| 84 | # is_binary() | ||
| 85 | # Returns true if the encoding is 'binary' (never valid for mail text parts). | ||
| 86 | sub is_binary { return( ( shift->{_encoding} // '' ) eq 'binary' ? 1 : 0 ); } | ||
| 87 | |||
| 88 | # is_encoded() | ||
| 89 | # Returns true if the encoding is base64 or quoted-printable. | ||
| 90 | sub is_encoded | ||
| 91 | { | ||
| 92 | my $enc = shift->{_encoding} // ''; | ||
| 93 | return( ( $enc eq 'base64' || $enc eq 'quoted-printable' ) ? 1 : 0 ); | ||
| 94 | } | ||
| 95 | |||
| 96 | # value() - alias for as_string | ||
| 97 | sub value { return( shift->as_string ); } | ||
| 98 | |||
| 99 | # NOTE: STORABLE support | ||
| 100 | sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); } | ||
| 101 | |||
| 102 | sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); } | ||
| 103 | |||
| 104 | 1; | ||
| 105 | # NOTE: POD | ||
| 106 | __END__ |