| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## MIME Email Builder - ~/lib/Mail/Make/Stream.pm | ||
| 3 | ## Version v0.1.0 | ||
| 4 | ## Copyright(c) 2026 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2026/03/05 | ||
| 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; | ||
| 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 Mail::Make::Exception; | ||
| 22 | our $EXCEPTION_CLASS = 'Mail::Make::Exception'; | ||
| 23 | our $VERSION = 'v0.1.0'; | ||
| 24 | }; | ||
| 25 | |||
| 26 | use strict; | ||
| 27 | use warnings; | ||
| 28 | |||
| 29 | sub init | ||
| 30 | { | ||
| 31 | my $self = shift( @_ ); | ||
| 32 | $self->{_exception_class} = $EXCEPTION_CLASS; | ||
| 33 | $self->{_init_strict_use_sub} = 1; | ||
| 34 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 35 | return( $self ); | ||
| 36 | } | ||
| 37 | |||
| 38 | # NOTE: STORABLE support | ||
| 39 | |||
| 40 | # NOTE: FREEZE is inherited | ||
| 41 | |||
| 42 | sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); } | ||
| 43 | |||
| 44 | sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); } | ||
| 45 | |||
| 46 | # NOTE: THAW is inherited | ||
| 47 | |||
| 48 | 1; | ||
| 49 | |||
| 50 | # NOTE: Mail::Make::Stream::Generic inline class | ||
| 51 | { | ||
| 52 | package | ||
| 53 | Mail::Make::Stream::Generic; | ||
| 54 | BEGIN | ||
| 55 | { | ||
| 56 | use strict; | ||
| 57 | use warnings; | ||
| 58 | use parent qw( Module::Generic ); | ||
| 59 | use vars qw( $VERSION $EXCEPTION_CLASS ); | ||
| 60 | use Mail::Make::Exception; | ||
| 61 | use Module::Generic::File::IO; | ||
| 62 | use Module::Generic::Scalar::IO; | ||
| 63 | our $EXCEPTION_CLASS = 'Mail::Make::Exception'; | ||
| 64 | our $VERSION = $Mail::Make::Stream::VERSION; | ||
| 65 | }; | ||
| 66 | |||
| 67 | use strict; | ||
| 68 | use warnings; | ||
| 69 | |||
| 70 | sub init | ||
| 71 | { | ||
| 72 | my $self = shift( @_ ); | ||
| 73 | my $class = ( ref( $self ) || $self ); | ||
| 74 | $self->{_init_strict_use_sub} = 1; | ||
| 75 | no strict 'refs'; | ||
| 76 | $self->{_exception_class} = defined( ${"${class}::EXCEPTION_CLASS"} ) | ||
| 77 | ? ${"${class}::EXCEPTION_CLASS"} | ||
| 78 | : $EXCEPTION_CLASS; | ||
| 79 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 80 | return( $self ); | ||
| 81 | } | ||
| 82 | |||
| 83 | # _get_glob_from_arg( $source_or_dest [, write => 1] ) | ||
| 84 | # | ||
| 85 | # Normalises any of the following into a ( $fh, $op ) pair: | ||
| 86 | # | ||
| 87 | # - A native Perl glob / IO object → used directly | ||
| 88 | # - A scalar reference (\$scalar) → Module::Generic::Scalar::IO | ||
| 89 | # - A plain string (file path) → Module::Generic::File opened via new_file() | ||
| 90 | # | ||
| 91 | # $op is a closure that: | ||
| 92 | # Read mode: $op->( $buf, $len ) - returns bytes read (0 at EOF, undef on error) | ||
| 93 | # Write mode: $op->( $data ) - returns true on success, undef on error | ||
| 94 | sub _get_glob_from_arg | ||
| 95 | { | ||
| 96 | my $self = shift( @_ ); | ||
| 97 | my $this = shift( @_ ); | ||
| 98 | if( !defined( $this ) || ( !ref( $this ) && !length( $this ) ) ) | ||
| 99 | { | ||
| 100 | return( $self->error( "No argument was provided." ) ); | ||
| 101 | } | ||
| 102 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 103 | $opts->{write} = 0 if( !exists( $opts->{write} ) ); | ||
| 104 | my $mode = $opts->{write} ? '+>' : '<'; | ||
| 105 | my $fh; | ||
| 106 | my $is_native_glob = 0; | ||
| 107 | |||
| 108 | if( $self->_is_glob( $this ) ) | ||
| 109 | { | ||
| 110 | $fh = $this; | ||
| 111 | # Even if this is an in-memory scalar handle, fileno() returns -1, which is true | ||
| 112 | $is_native_glob++ if( fileno( $this ) ); | ||
| 113 | } | ||
| 114 | elsif( $self->_is_scalar( $this ) ) | ||
| 115 | { | ||
| 116 | $fh = Module::Generic::Scalar::IO->new( $this, $mode ) || | ||
| 117 | return( $self->pass_error( Module::Generic::Scalar::IO->error ) ); | ||
| 118 | $is_native_glob++; | ||
| 119 | } | ||
| 120 | else | ||
| 121 | { | ||
| 122 | my $f = $self->new_file( "$this" ) || return( $self->pass_error ); | ||
| 123 | if( !$f->exists && !$opts->{write} ) | ||
| 124 | { | ||
| 125 | return( $self->error( "File '$this' does not exist." ) ); | ||
| 126 | } | ||
| 127 | $fh = $f->open( $mode, { binmode => 'raw', ( $opts->{write} ? ( autoflush => 1 ) : () ) } ) || | ||
| 128 | return( $self->pass_error( $f->error ) ); | ||
| 129 | $is_native_glob++; | ||
| 130 | } | ||
| 131 | |||
| 132 | my $flags; | ||
| 133 | if( $self->_can( $fh => 'fcntl' ) ) | ||
| 134 | { | ||
| 135 | $flags = $fh->fcntl( F_GETFL, 0 ); | ||
| 136 | } | ||
| 137 | else | ||
| 138 | { | ||
| 139 | $flags = fcntl( $fh, F_GETFL, 0 ); | ||
| 140 | } | ||
| 141 | |||
| 142 | if( defined( $flags ) ) | ||
| 143 | { | ||
| 144 | if( $opts->{write} ) | ||
| 145 | { | ||
| 146 | unless( $flags & ( O_RDWR | O_WRONLY | O_APPEND ) ) | ||
| 147 | { | ||
| 148 | return( $self->error( "Filehandle provided does not have write permission enabled." ) ); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | else | ||
| 152 | { | ||
| 153 | unless( ( ( $flags & O_RDONLY ) == O_RDONLY ) || ( $flags & O_RDWR ) ) | ||
| 154 | { | ||
| 155 | return( $self->error( "Filehandle provided does not have read permission enabled. File handle flags value is '$flags'" ) ); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | # We check if the file handle is an object, because calling core read() or print() | ||
| 161 | # on it would not work unless the glob has implemented a tie. See perltie. | ||
| 162 | my $op; | ||
| 163 | my $meth; | ||
| 164 | if( $opts->{write} ) | ||
| 165 | { | ||
| 166 | if( $is_native_glob ) | ||
| 167 | { | ||
| 168 | $op = sub | ||
| 169 | { | ||
| 170 | my $rv = print( $fh @_ ); | ||
| 171 | return( $self->error( "Error writing ", CORE::length( $_[0] ), " bytes of data to output: $!" ) ) | ||
| 172 | if( !defined( $rv ) ); | ||
| 173 | return( $rv ); | ||
| 174 | }; | ||
| 175 | } | ||
| 176 | elsif( ( $meth = ( $self->_can( $fh => 'print' ) || $self->_can( $fh => 'write' ) ) ) ) | ||
| 177 | { | ||
| 178 | $op = sub | ||
| 179 | { | ||
| 180 | local $@; | ||
| 181 | my $rv = eval{ $fh->$meth( @_ ) }; | ||
| 182 | if( $@ ) | ||
| 183 | { | ||
| 184 | return( $self->error( "Error writing ", CORE::length( $_[0] ), " bytes of data to output: $@" ) ); | ||
| 185 | } | ||
| 186 | if( !defined( $rv ) ) | ||
| 187 | { | ||
| 188 | my $err; | ||
| 189 | if( defined( $! ) ) | ||
| 190 | { | ||
| 191 | $err = $!; | ||
| 192 | } | ||
| 193 | elsif( $self->_can( $fh => 'error' ) ) | ||
| 194 | { | ||
| 195 | $err = $fh->error; | ||
| 196 | } | ||
| 197 | elsif( $self->_can( $fh => 'errstr' ) ) | ||
| 198 | { | ||
| 199 | $err = $fh->errstr; | ||
| 200 | } | ||
| 201 | return( $self->error( "Error writing ", CORE::length( $_[0] ), " bytes of data to output: $err" ) ); | ||
| 202 | } | ||
| 203 | return( $rv ); | ||
| 204 | }; | ||
| 205 | } | ||
| 206 | else | ||
| 207 | { | ||
| 208 | return( $self->error( "The file handle provided is not a native opened one and does not support the print() or write() methods." ) ); | ||
| 209 | } | ||
| 210 | } | ||
| 211 | else | ||
| 212 | { | ||
| 213 | if( $is_native_glob ) | ||
| 214 | { | ||
| 215 | $op = sub | ||
| 216 | { | ||
| 217 | my $n = read( $fh, $_[0], $_[1] ); | ||
| 218 | if( !defined( $n ) ) | ||
| 219 | { | ||
| 220 | return( $self->error( "Error reading ", $_[1], " bytes of data from input: $!" ) ); | ||
| 221 | } | ||
| 222 | return( $n ); | ||
| 223 | }; | ||
| 224 | } | ||
| 225 | elsif( $self->_can( $fh => 'read' ) ) | ||
| 226 | { | ||
| 227 | $op = sub | ||
| 228 | { | ||
| 229 | local $@; | ||
| 230 | my $n = eval{ $fh->read( @_ ) }; | ||
| 231 | if( $@ ) | ||
| 232 | { | ||
| 233 | return( $self->error( "Error reading ", $_[1], " bytes of data from input: $@" ) ); | ||
| 234 | } | ||
| 235 | if( !defined( $n ) ) | ||
| 236 | { | ||
| 237 | my $err; | ||
| 238 | if( defined( $! ) ) | ||
| 239 | { | ||
| 240 | $err = $!; | ||
| 241 | } | ||
| 242 | elsif( $self->_can( $fh => 'error' ) ) | ||
| 243 | { | ||
| 244 | $err = $fh->error; | ||
| 245 | } | ||
| 246 | elsif( $self->_can( $fh => 'errstr' ) ) | ||
| 247 | { | ||
| 248 | $err = $fh->errstr; | ||
| 249 | } | ||
| 250 | return( $self->error( "Error reading ", $_[1], " bytes of data from input: $err" ) ); | ||
| 251 | } | ||
| 252 | return( $n ); | ||
| 253 | }; | ||
| 254 | } | ||
| 255 | else | ||
| 256 | { | ||
| 257 | return( $self->error( "The file handle provided is not a native opened one and does not support the read() method." ) ); | ||
| 258 | } | ||
| 259 | } | ||
| 260 | return( $fh, $op ); | ||
| 261 | } | ||
| 262 | |||
| 263 | # NOTE: sub FREEZE is inherited | ||
| 264 | |||
| 265 | sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); } | ||
| 266 | |||
| 267 | sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); } | ||
| 268 | |||
| 269 | # NOTE: sub THAW is inherited | ||
| 270 | } | ||
| 271 | |||
| 272 | 1; | ||
| 273 | # NOTE: POD | ||
| 274 | __END__ |