| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## MIME Email Builder - ~/lib/Mail/Make/Stream/QuotedPrint.pm | ||
| 3 | ## Version v0.3.0 | ||
| 4 | ## Copyright(c) 2026 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2026/03/02 | ||
| 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::Stream::QuotedPrint; | ||
| 14 | BEGIN | ||
| 15 | { | ||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | warnings::register_categories( 'Mail::Make' ); | ||
| 19 | use Mail::Make::Stream; | ||
| 20 | use parent -norequire, qw( Mail::Make::Stream::Generic ); | ||
| 21 | use vars qw( @EXPORT_OK $VERSION $EXCEPTION_CLASS $QuotedPrintError $DEBUG ); | ||
| 22 | use Exporter qw( import ); | ||
| 23 | use Mail::Make::Exception; | ||
| 24 | use Encode (); | ||
| 25 | use Module::Generic::File::IO; | ||
| 26 | our @EXPORT_OK = qw( decode_qp encode_qp ); | ||
| 27 | our $EXCEPTION_CLASS = 'Mail::Make::Exception'; | ||
| 28 | our $VERSION = 'v0.3.1'; | ||
| 29 | our $DEBUG = 0; | ||
| 30 | }; | ||
| 31 | |||
| 32 | use strict; | ||
| 33 | use warnings; | ||
| 34 | |||
| 35 | # decode( $from, $to [, %opts] ) | ||
| 36 | # Decodes QP data from $from line-by-line and writes raw bytes to $to. | ||
| 37 | sub decode | ||
| 38 | { | ||
| 39 | my $self = shift( @_ ); | ||
| 40 | my $from = shift( @_ ); | ||
| 41 | my $to = shift( @_ ); | ||
| 42 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 43 | my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from ); | ||
| 44 | my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 ); | ||
| 45 | return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) ); | ||
| 46 | $self->_load_class( 'MIME::QuotedPrint', { no_import => 1 } ) || return( $self->pass_error ); | ||
| 47 | |||
| 48 | # Wrap the filehandle into an object that supports getline(). | ||
| 49 | # Native in-memory handles (fileno = -1) already support readline(); do NOT attempt | ||
| 50 | # fdopen() on them: that call requires a real OS file descriptor. | ||
| 51 | unless( $self->_can( $from_fh => 'getline' ) ) | ||
| 52 | { | ||
| 53 | my $fd = $self->_can( $from_fh => 'fileno' ) ? $from_fh->fileno : fileno( $from_fh ); | ||
| 54 | if( defined( $fd ) && $fd >= 0 ) | ||
| 55 | { | ||
| 56 | my $io = Module::Generic::File::IO->new; | ||
| 57 | $io->fdopen( $fd, 'r' ) || | ||
| 58 | return( $self->pass_error( $io->error ) ); | ||
| 59 | $from_fh = $io; | ||
| 60 | } | ||
| 61 | # else: native in-memory glob - readline() below works as-is | ||
| 62 | } | ||
| 63 | |||
| 64 | my $buff; | ||
| 65 | while( defined( $buff = $self->_can( $from_fh => 'getline' ) ? $from_fh->getline : readline( $from_fh ) ) ) | ||
| 66 | { | ||
| 67 | my $decoded = MIME::QuotedPrint::decode_qp( $buff ); | ||
| 68 | my $rv = $writer->( $decoded ); | ||
| 69 | return( $self->pass_error ) if( !defined( $rv ) ); | ||
| 70 | } | ||
| 71 | return( $self->pass_error( $from_fh->error ) ) | ||
| 72 | if( !defined( $buff ) && $self->_can( $from_fh => 'error' ) && $self->error ); | ||
| 73 | return( $self ); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub decode_qp | ||
| 77 | { | ||
| 78 | my $s = __PACKAGE__->new( debug => $DEBUG ); | ||
| 79 | my $rv = $s->decode( @_ ); | ||
| 80 | if( !defined( $rv ) ) | ||
| 81 | { | ||
| 82 | $QuotedPrintError = $s->error; | ||
| 83 | return( $s->pass_error ); | ||
| 84 | } | ||
| 85 | else | ||
| 86 | { | ||
| 87 | undef( $QuotedPrintError ); | ||
| 88 | return( $rv ); | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | # encode( $from, $to [, %opts] ) | ||
| 93 | # Encodes raw data from $from as Quoted-Printable and writes to $to. | ||
| 94 | # Options: | ||
| 95 | # eol => $str line ending (default "\015\012") | ||
| 96 | sub encode | ||
| 97 | { | ||
| 98 | my $self = shift( @_ ); | ||
| 99 | my $from = shift( @_ ); | ||
| 100 | my $to = shift( @_ ); | ||
| 101 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 102 | my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from ); | ||
| 103 | my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 ); | ||
| 104 | return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) ); | ||
| 105 | $self->_load_class( 'MIME::QuotedPrint', { no_import => 1 } ) || return( $self->pass_error ); | ||
| 106 | |||
| 107 | # Wrap the filehandle into an object that supports getline(). | ||
| 108 | # Native in-memory handles (fileno = -1) already support readline(); do NOT attempt | ||
| 109 | # fdopen() on them: that call requires a real OS file descriptor. | ||
| 110 | unless( $self->_can( $from_fh => 'getline' ) ) | ||
| 111 | { | ||
| 112 | my $fd = $self->_can( $from_fh => 'fileno' ) ? $from_fh->fileno : fileno( $from_fh ); | ||
| 113 | if( defined( $fd ) && $fd >= 0 ) | ||
| 114 | { | ||
| 115 | my $io = Module::Generic::File::IO->new; | ||
| 116 | $io->fdopen( $fd, 'r' ) || | ||
| 117 | return( $self->pass_error( $io->error ) ); | ||
| 118 | $from_fh = $io; | ||
| 119 | } | ||
| 120 | # else: native in-memory glob - readline() below works as-is | ||
| 121 | } | ||
| 122 | my $eol = ( exists( $opts->{eol} ) && defined( $opts->{eol} ) ) ? $opts->{eol} : "\015\012"; | ||
| 123 | my $has_eol = length( $eol ); | ||
| 124 | |||
| 125 | my $buff; | ||
| 126 | while( defined( $buff = $self->_can( $from_fh => 'getline' ) ? $from_fh->getline : readline( $from_fh ) ) ) | ||
| 127 | { | ||
| 128 | # Ensure the chunk is raw UTF-8 bytes, not Perl's internal representation | ||
| 129 | $buff = Encode::encode_utf8( $buff ) if( Encode::is_utf8( $buff ) ); | ||
| 130 | my $encoded = MIME::QuotedPrint::encode_qp( $buff, ( $has_eol ? ( $eol ) : () ) ); | ||
| 131 | my $rv = $writer->( $encoded ); | ||
| 132 | return( $self->pass_error ) if( !defined( $rv ) ); | ||
| 133 | } | ||
| 134 | return( $self->pass_error( $from_fh->error ) ) | ||
| 135 | if( !defined( $buff ) && $self->_can( $from_fh => 'error' ) && $self->error ); | ||
| 136 | return( $self ); | ||
| 137 | } | ||
| 138 | |||
| 139 | sub encode_qp | ||
| 140 | { | ||
| 141 | my $s = __PACKAGE__->new; | ||
| 142 | my $rv = $s->encode( @_ ); | ||
| 143 | if( !defined( $rv ) ) | ||
| 144 | { | ||
| 145 | $QuotedPrintError = $s->error; | ||
| 146 | return( $s->pass_error ); | ||
| 147 | } | ||
| 148 | else | ||
| 149 | { | ||
| 150 | undef( $QuotedPrintError ); | ||
| 151 | return( $rv ); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | sub encode_qp_utf8 { return( shift->encode_qp( Encode::encode_utf8( shift( @_ ) ) ) ); } | ||
| 156 | |||
| 157 | sub is_decoder_installed | ||
| 158 | { | ||
| 159 | local $@; | ||
| 160 | eval | ||
| 161 | { | ||
| 162 | local $SIG{__DIE__} = sub{}; | ||
| 163 | require MIME::QuotedPrint; | ||
| 164 | }; | ||
| 165 | return( $@ ? 0 : 1 ); | ||
| 166 | } | ||
| 167 | |||
| 168 | sub is_encoder_installed | ||
| 169 | { | ||
| 170 | local $@; | ||
| 171 | eval | ||
| 172 | { | ||
| 173 | local $SIG{__DIE__} = sub{}; | ||
| 174 | require MIME::QuotedPrint; | ||
| 175 | }; | ||
| 176 | return( $@ ? 0 : 1 ); | ||
| 177 | } | ||
| 178 | |||
| 179 | # NOTE: sub FREEZE is inherited | ||
| 180 | |||
| 181 | # NOTE: sub STORABLE_freeze is inherited | ||
| 182 | |||
| 183 | # NOTE: sub STORABLE_thaw is inherited | ||
| 184 | |||
| 185 | # NOTE: sub THAW is inherited | ||
| 186 | |||
| 187 | 1; | ||
| 188 | # NOTE: POD | ||
| 189 | __END__ |