« blib/lib/Mail/Make/Headers/ContentTransferEncoding.pm
↑ Summary
lib/Mail/Make/Headers/Subject.pm »
| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## MIME Email Builder - ~/lib/Mail/Make/Headers/MessageID.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::MessageID; | ||
| 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 Data::UUID; | ||
| 22 | use Mail::Make::Exception; | ||
| 23 | use overload | ||
| 24 | ( | ||
| 25 | '""' => 'as_string', | ||
| 26 | bool => sub { 1 }, | ||
| 27 | ); | ||
| 28 | our $EXCEPTION_CLASS = 'Mail::Make::Exception'; | ||
| 29 | our $VERSION = 'v0.2.0'; | ||
| 30 | }; | ||
| 31 | |||
| 32 | use strict; | ||
| 33 | use warnings; | ||
| 34 | |||
| 35 | sub init | ||
| 36 | { | ||
| 37 | my $self = shift( @_ ); | ||
| 38 | $self->{_id} = undef; | ||
| 39 | $self->{_exception_class} = $EXCEPTION_CLASS; | ||
| 40 | my $id = shift( @_ ); | ||
| 41 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 42 | if( defined( $id ) && length( $id ) ) | ||
| 43 | { | ||
| 44 | $self->id( $id ) || return( $self->pass_error ); | ||
| 45 | } | ||
| 46 | else | ||
| 47 | { | ||
| 48 | $self->{_id} = $self->_generate; | ||
| 49 | } | ||
| 50 | return( $self ); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub as_string | ||
| 54 | { | ||
| 55 | my $self = shift( @_ ); | ||
| 56 | return( $self->{_id} // '' ); | ||
| 57 | } | ||
| 58 | |||
| 59 | # generate() | ||
| 60 | # Generates a new unique Message-ID string and sets it. | ||
| 61 | sub generate | ||
| 62 | { | ||
| 63 | my $self = shift( @_ ); | ||
| 64 | $self->{_id} = $self->_generate; | ||
| 65 | return( $self ); | ||
| 66 | } | ||
| 67 | |||
| 68 | # id( [$value] ) | ||
| 69 | # Gets or sets the Message-ID string. Must be in angle-bracket format | ||
| 70 | # <local-part@domain> per RFC 2822. | ||
| 71 | sub id | ||
| 72 | { | ||
| 73 | my $self = shift( @_ ); | ||
| 74 | if( @_ ) | ||
| 75 | { | ||
| 76 | my $val = shift( @_ ); | ||
| 77 | unless( $val =~ /\A<[^>]+\@[^>]+>\z/ ) | ||
| 78 | { | ||
| 79 | return( $self->error( "Message-ID must be in <local\@domain> format, got: '$val'" ) ); | ||
| 80 | } | ||
| 81 | $self->{_id} = $val; | ||
| 82 | return( $self ); | ||
| 83 | } | ||
| 84 | return( $self->{_id} ); | ||
| 85 | } | ||
| 86 | |||
| 87 | # value() - alias for as_string | ||
| 88 | sub value { return( shift->as_string ); } | ||
| 89 | |||
| 90 | # _generate() | ||
| 91 | # Produces a unique <uuid@generated> string. | ||
| 92 | sub _generate | ||
| 93 | { | ||
| 94 | my $self = shift( @_ ); | ||
| 95 | my $uuid = lc( Data::UUID->new->create_str ); | ||
| 96 | $uuid =~ tr/-//d; | ||
| 97 | return( "<${uuid}\@mail.make.generated>" ); | ||
| 98 | } | ||
| 99 | |||
| 100 | # NOTE: STORABLE support | ||
| 101 | sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); } | ||
| 102 | |||
| 103 | sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); } | ||
| 104 | |||
| 105 | 1; | ||
| 106 | # NOTE: POD | ||
| 107 | __END__ |