| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## MIME Email Builder - ~/lib/Mail/Make/Headers/Subject.pm | ||
| 3 | ## Version v0.1.1 | ||
| 4 | ## Copyright(c) 2026 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2026/03/03 | ||
| 7 | ## Modified 2026/03/05 | ||
| 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::Subject; | ||
| 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 ); | ||
| 21 | use utf8; | ||
| 22 | use Encode (); | ||
| 23 | use Mail::Make::Exception; | ||
| 24 | use MIME::Base64 (); | ||
| 25 | use overload( | ||
| 26 | '""' => 'as_string', | ||
| 27 | 'bool' => sub { 1 }, | ||
| 28 | ); | ||
| 29 | our $EXCEPTION_CLASS = 'Mail::Make::Exception'; | ||
| 30 | our $VERSION = 'v0.1.1'; | ||
| 31 | # RFC 2047 §2: an encoded-word must not exceed 75 characters total. | ||
| 32 | # =?UTF-8?B? ... ?= - the wrapper is 12 chars, leaving 63 for base64 text. | ||
| 33 | # 63 base64 chars encode floor(63 * 3/4) = 47 raw bytes. | ||
| 34 | # We use 45 bytes per chunk (multiple of 3) to keep base64 clean. | ||
| 35 | use constant | ||
| 36 | { | ||
| 37 | CHARSET => 'UTF-8', | ||
| 38 | EW_MAX_BYTES => 45, | ||
| 39 | FOLD_SEP => "\015\012 ", | ||
| 40 | }; | ||
| 41 | }; | ||
| 42 | |||
| 43 | use strict; | ||
| 44 | use warnings; | ||
| 45 | |||
| 46 | sub init | ||
| 47 | { | ||
| 48 | my $self = shift( @_ ); | ||
| 49 | $self->{_raw} = undef; # original Perl string, not yet encoded | ||
| 50 | $self->{_encoded} = undef; # RFC 2047 form, cached after first encode | ||
| 51 | $self->{_exception_class} = $EXCEPTION_CLASS; | ||
| 52 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 53 | return( $self ); | ||
| 54 | } | ||
| 55 | |||
| 56 | # as_string() - returns the RFC 2047 encoded form suitable for the wire. | ||
| 57 | # Pure ASCII values are returned unchanged. | ||
| 58 | sub as_string | ||
| 59 | { | ||
| 60 | my $self = shift( @_ ); | ||
| 61 | return( '' ) if( !defined( $self->{_raw} ) ); | ||
| 62 | return( $self->{_encoded} ) if( defined( $self->{_encoded} ) ); | ||
| 63 | $self->{_encoded} = _encode_subject( $self->{_raw} ); | ||
| 64 | return( $self->{_encoded} ); | ||
| 65 | } | ||
| 66 | |||
| 67 | # decode( $encoded_string ) - class or instance method. | ||
| 68 | # Decodes an RFC 2047 encoded Subject value back to a Perl string. | ||
| 69 | sub decode | ||
| 70 | { | ||
| 71 | my $self = shift( @_ ); | ||
| 72 | my $str = shift( @_ ) // return( $self->error( "No value to decode." ) ); | ||
| 73 | return( _decode_subject( $str ) ); | ||
| 74 | } | ||
| 75 | |||
| 76 | # field_name() - always returns 'Subject' | ||
| 77 | sub field_name { return( 'Subject' ); } | ||
| 78 | |||
| 79 | # raw() - returns the decoded Perl string (the original value before encoding) | ||
| 80 | sub raw { return( shift->{_raw} ); } | ||
| 81 | |||
| 82 | # value( [$text] ) - sets or gets the subject text. | ||
| 83 | # On assignment: stores the raw Perl string; clears the encoded cache. | ||
| 84 | # On retrieval: returns the decoded Perl string (i.e. human-readable). | ||
| 85 | sub value | ||
| 86 | { | ||
| 87 | my $self = shift( @_ ); | ||
| 88 | if( @_ ) | ||
| 89 | { | ||
| 90 | my $text = shift( @_ ); | ||
| 91 | unless( defined( $text ) ) | ||
| 92 | { | ||
| 93 | return( $self->error( "Subject value must be a defined scalar." ) ); | ||
| 94 | } | ||
| 95 | $self->{_raw} = $text; | ||
| 96 | $self->{_encoded} = undef; # invalidate cache | ||
| 97 | return( $self ); | ||
| 98 | } | ||
| 99 | return( $self->{_raw} ); | ||
| 100 | } | ||
| 101 | |||
| 102 | # _decode_ew( $charset, $encoding, $text ) → decoded Perl string fragment | ||
| 103 | sub _decode_ew | ||
| 104 | { | ||
| 105 | my( $charset, $enc, $text ) = @_; | ||
| 106 | my $bytes; | ||
| 107 | if( uc( $enc ) eq 'B' ) | ||
| 108 | { | ||
| 109 | $bytes = MIME::Base64::decode_base64( $text ); | ||
| 110 | } | ||
| 111 | else | ||
| 112 | { | ||
| 113 | # Q encoding: _ → space, =XX → byte | ||
| 114 | $text =~ s/_/ /g; | ||
| 115 | $text =~ s/=([0-9A-Fa-f]{2})/chr( hex( $1 ) )/ge; | ||
| 116 | $bytes = $text; | ||
| 117 | } | ||
| 118 | local $@; | ||
| 119 | my $decoded = eval{ Encode::decode( $charset, $bytes ) }; | ||
| 120 | return( $@ ? $bytes : $decoded ); | ||
| 121 | } | ||
| 122 | |||
| 123 | # _decode_subject( $wire_string ) → $perl_string | ||
| 124 | # Decodes all RFC 2047 encoded-words in $wire_string. | ||
| 125 | # Handles both ?B? (Base64) and ?Q? (Quoted-Printable) forms. | ||
| 126 | sub _decode_subject | ||
| 127 | { | ||
| 128 | my $str = shift( @_ ); | ||
| 129 | return( $str ) unless( defined( $str ) && $str =~ /=\?/ ); | ||
| 130 | |||
| 131 | # Collapse folding whitespace between consecutive encoded-words: | ||
| 132 | # RFC 2047 §6.2: whitespace between two encoded-words is ignored. | ||
| 133 | $str =~ s/\?=[ \t]*(?:\015\012)?[ \t]*=\?/?==?/g; | ||
| 134 | |||
| 135 | $str =~ s/=\?([A-Za-z0-9_-]+)\?([BbQq])\?([^?]*)\?=/_decode_ew( $1, $2, $3 )/ge; | ||
| 136 | return( $str ); | ||
| 137 | } | ||
| 138 | |||
| 139 | # _encode_subject( $perl_string ) → $wire_string | ||
| 140 | # Returns the string as-is if it is pure printable ASCII (RFC 2822 §2.2). | ||
| 141 | # Otherwise encodes the UTF-8 bytes in one or more RFC 2047 Base64 | ||
| 142 | # encoded-words, folded with CRLF SP between them. | ||
| 143 | sub _encode_subject | ||
| 144 | { | ||
| 145 | my $text = shift( @_ ); | ||
| 146 | |||
| 147 | # Pure printable ASCII + tab/space: no encoding needed. | ||
| 148 | # We also reject bare CRs and LFs here; they are illegal in a header. | ||
| 149 | return( $text ) unless( $text =~ /[^\x09\x20-\x7E]/ ); | ||
| 150 | |||
| 151 | # Ensure the string carries Perl's UTF-8 flag before encoding to bytes. | ||
| 152 | # If the flag is off (raw octets from a context without Encode::decode), | ||
| 153 | # Encode::encode treats each octet as Latin-1, producing double-encoding: | ||
| 154 | # e.g. U+2014 (E2 80 94) becomes C3 A2 C2 80 C2 94 instead of E2 80 94. | ||
| 155 | utf8::decode( $text ) unless( utf8::is_utf8( $text ) ); | ||
| 156 | |||
| 157 | # Encode the entire string to UTF-8 bytes, then split into safe chunks. | ||
| 158 | # Crucially: we split on byte boundaries that do NOT break a multi-byte UTF-8 sequence. | ||
| 159 | # Because we encode the whole string first we work purely in bytes and the split is | ||
| 160 | # safe. | ||
| 161 | my $bytes = Encode::encode( CHARSET, $text ); | ||
| 162 | |||
| 163 | my @words; | ||
| 164 | my $offset = 0; | ||
| 165 | my $total = length( $bytes ); | ||
| 166 | while( $offset < $total ) | ||
| 167 | { | ||
| 168 | # Grab up to EW_MAX_BYTES bytes, but do not cut inside a multi-byte sequence. | ||
| 169 | # UTF-8 continuation bytes are 0x80..0xBF; a leading byte of a multi-byte sequence | ||
| 170 | # is 0xC0..0xFF. We back up until we sit at a leading byte boundary (or the very end). | ||
| 171 | my $len = EW_MAX_BYTES; | ||
| 172 | $len = $total - $offset if( $offset + $len > $total ); | ||
| 173 | my $chunk = substr( $bytes, $offset, $len ); | ||
| 174 | |||
| 175 | # If the byte immediately after our chunk is a UTF-8 continuation byte (0x80–0xBF), | ||
| 176 | # the last character of our chunk is split.c | ||
| 177 | # Walk backwards to a safe cut point. | ||
| 178 | if( $offset + $len < $total ) | ||
| 179 | { | ||
| 180 | my $next = ord( substr( $bytes, $offset + $len, 1 ) ); | ||
| 181 | if( ( $next & 0xC0 ) == 0x80 ) | ||
| 182 | { | ||
| 183 | # Find last leading byte in chunk | ||
| 184 | while( $len > 0 && ( ord( substr( $chunk, $len - 1, 1 ) ) & 0xC0 ) == 0x80 ) | ||
| 185 | { | ||
| 186 | $len--; | ||
| 187 | } | ||
| 188 | $chunk = substr( $bytes, $offset, $len ); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | my $b64 = MIME::Base64::encode_base64( $chunk, '' ); | ||
| 193 | push( @words, '=?' . CHARSET . '?B?' . $b64 . '?=' ); | ||
| 194 | $offset += $len; | ||
| 195 | } | ||
| 196 | |||
| 197 | return( join( FOLD_SEP, @words ) ); | ||
| 198 | } | ||
| 199 | |||
| 200 | # NOTE: STORABLE support | ||
| 201 | sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); } | ||
| 202 | |||
| 203 | sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); } | ||
| 204 | |||
| 205 | 1; | ||
| 206 | # NOTE: POD | ||
| 207 | __END__ |