| 1 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | package Email::MessageID 1.408; | ||||||||||||||||||||
| 4 | # ABSTRACT: Generate world unique message-ids. | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 2 | ▶ | use overload '""' => 'as_string', fallback => 1; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | #pod =head1 SYNOPSIS | ||||||||||||||||||||
| 9 | #pod | ||||||||||||||||||||
| 10 | #pod use Email::MessageID; | ||||||||||||||||||||
| 11 | #pod | ||||||||||||||||||||
| 12 | #pod my $mid = Email::MessageID->new->in_brackets; | ||||||||||||||||||||
| 13 | #pod | ||||||||||||||||||||
| 14 | #pod print "Message-ID: $mid\x0D\x0A"; | ||||||||||||||||||||
| 15 | #pod | ||||||||||||||||||||
| 16 | #pod =head1 DESCRIPTION | ||||||||||||||||||||
| 17 | #pod | ||||||||||||||||||||
| 18 | #pod Message-ids are optional, but highly recommended, headers that identify a | ||||||||||||||||||||
| 19 | #pod message uniquely. This software generates a unique message-id. | ||||||||||||||||||||
| 20 | #pod | ||||||||||||||||||||
| 21 | #pod =method new | ||||||||||||||||||||
| 22 | #pod | ||||||||||||||||||||
| 23 | #pod my $mid = Email::MessageID->new; | ||||||||||||||||||||
| 24 | #pod | ||||||||||||||||||||
| 25 | #pod my $new_mid = Email::MessageID->new( host => $myhost ); | ||||||||||||||||||||
| 26 | #pod | ||||||||||||||||||||
| 27 | #pod This class method constructs an Email::MessageID object | ||||||||||||||||||||
| 28 | #pod containing a unique message-id. You may specify custom C<host> and C<user> | ||||||||||||||||||||
| 29 | #pod parameters. | ||||||||||||||||||||
| 30 | #pod | ||||||||||||||||||||
| 31 | #pod By default, the C<host> is generated from C<Sys::Hostname::hostname>. | ||||||||||||||||||||
| 32 | #pod | ||||||||||||||||||||
| 33 | #pod By default, the C<user> is generated using C<Time::HiRes>'s C<gettimeofday> | ||||||||||||||||||||
| 34 | #pod and the process ID. | ||||||||||||||||||||
| 35 | #pod | ||||||||||||||||||||
| 36 | #pod Using these values we have the ability to ensure world uniqueness down to | ||||||||||||||||||||
| 37 | #pod a specific process running on a specific host, and the exact time down to | ||||||||||||||||||||
| 38 | #pod six digits of microsecond precision. | ||||||||||||||||||||
| 39 | #pod | ||||||||||||||||||||
| 40 | #pod =cut | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 1003 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||
| 43 | 1003 | my ($class, %args) = @_; | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 1003 | ▶ | $args{user} ||= $class->create_user; | ||||||||||||||||||
|
Condition66%2/3
$args{"user"} ||= $class->create_user
MC/DC0%0/2
$args{"user"} ||= $class->create_user
$args{"user"}
$class->create_user
Truth table66%2/3
A ||= B
A= $args{"user"}B= $class->create_user
| |||||||||||||||||||||
| 46 | 1003 | ▶ | $args{host} ||= $class->create_host; | ||||||||||||||||||
|
Condition66%2/3
$args{"host"} ||= $class->create_host
MC/DC0%0/2
$args{"host"} ||= $class->create_host
$args{"host"}
$class->create_host
Truth table66%2/3
A ||= B
A= $args{"host"}B= $class->create_host
| |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 1003 | my $str = "$args{user}\@$args{host}"; | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 1003 | bless \$str => $class; | |||||||||||||||||||
| 51 | } | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | #pod =method create_host | ||||||||||||||||||||
| 54 | #pod | ||||||||||||||||||||
| 55 | #pod my $domain_part = Email::MessageID->create_host; | ||||||||||||||||||||
| 56 | #pod | ||||||||||||||||||||
| 57 | #pod This method returns the domain part of the message-id. | ||||||||||||||||||||
| 58 | #pod | ||||||||||||||||||||
| 59 | #pod =cut | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 2 | my $_SYS_HOSTNAME_LONG; | |||||||||||||||||||
| 62 | 1001 | ▶ | sub create_host { | ||||||||||||||||||
|
Subroutine
create_host: called CC=5 SCAR=17.2
Pod
documented
| |||||||||||||||||||||
| 63 | 1001 | ▶ | unless (defined $_SYS_HOSTNAME_LONG) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 0 | ▶ | $_SYS_HOSTNAME_LONG = (eval { require Sys::Hostname::Long; 1 }) || 0; | ||||||||||||||||||
|
Condition50%1/2
eval {
do {
require Sys::Hostname::Long;
1
}
} || 0
MC/DC0%0/1
eval {
do {
require Sys::Hostname::Long;
1
}
} || 0
eval {
do {
require Sys::Hostname::Long;
1
}
}
Truth table50%1/2
A
A= eval {
do {
require Sys::Hostname::Long;
1
}
}
| |||||||||||||||||||||
| 65 | 2 | ▶ | require Sys::Hostname unless $_SYS_HOSTNAME_LONG; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 1001 | ▶ | return $_SYS_HOSTNAME_LONG ? Sys::Hostname::Long::hostname_long() | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | : Sys::Hostname::hostname(); | ||||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | #pod =method create_user | ||||||||||||||||||||
| 73 | #pod | ||||||||||||||||||||
| 74 | #pod my $local_part = Email::MessageID->create_user; | ||||||||||||||||||||
| 75 | #pod | ||||||||||||||||||||
| 76 | #pod This method returns a unique local part for the message-id. It includes some | ||||||||||||||||||||
| 77 | #pod random data and some predictable data. | ||||||||||||||||||||
| 78 | #pod | ||||||||||||||||||||
| 79 | #pod =cut | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 2 | my @CHARS = ('A'..'F','a'..'f',0..9); | |||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | 2 | my %uniq; | |||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 1001 | ▶ | sub create_user { | ||||||||||||||||||
|
Subroutine
create_user: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 86 | my $noise = join '', | ||||||||||||||||||||
| 87 | 1001 | map {; $CHARS[rand @CHARS] } (0 .. (3 + int rand 6)); | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 1001 | my $t = time; | |||||||||||||||||||
| 90 | 1001 | ▶ | my $u = exists $uniq{$t} ? ++$uniq{$t} : (%uniq = ($t => 0))[1]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 1001 | my $user = join '.', $t . $u, $noise, $$; | |||||||||||||||||||
| 93 | 1001 | return $user; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | #pod =method in_brackets | ||||||||||||||||||||
| 97 | #pod | ||||||||||||||||||||
| 98 | #pod When using Email::MessageID directly to populate the C<Message-ID> field, be | ||||||||||||||||||||
| 99 | #pod sure to use C<in_brackets> to get the string inside angle brackets: | ||||||||||||||||||||
| 100 | #pod | ||||||||||||||||||||
| 101 | #pod header => [ | ||||||||||||||||||||
| 102 | #pod ... | ||||||||||||||||||||
| 103 | #pod 'Message-Id' => Email::MessageID->new->in_brackets, | ||||||||||||||||||||
| 104 | #pod ], | ||||||||||||||||||||
| 105 | #pod | ||||||||||||||||||||
| 106 | #pod Don't make this common mistake: | ||||||||||||||||||||
| 107 | #pod | ||||||||||||||||||||
| 108 | #pod header => [ | ||||||||||||||||||||
| 109 | #pod ... | ||||||||||||||||||||
| 110 | #pod 'Message-Id' => Email::MessageID->new->as_string, # WRONG! | ||||||||||||||||||||
| 111 | #pod ], | ||||||||||||||||||||
| 112 | #pod | ||||||||||||||||||||
| 113 | #pod =for Pod::Coverage address as_string host user | ||||||||||||||||||||
| 114 | #pod | ||||||||||||||||||||
| 115 | #pod =cut | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 2 | ▶ | sub user { (split /@/, ${ $_[0] }, 2)[0] } | ||||||||||||||||||
|
Subroutine
user: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 118 | 2 | ▶ | sub host { (split /@/, ${ $_[0] }, 2)[1] } | ||||||||||||||||||
|
Subroutine
host: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 0 | ▶ | sub in_brackets { | ||||||||||||||||||
|
Subroutine
in_brackets: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 121 | 0 | my ($self) = @_; | |||||||||||||||||||
| 122 | 0 | return "<$$self>"; | |||||||||||||||||||
| 123 | } | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 1000 | ▶ | sub address { | ||||||||||||||||||
|
Subroutine
address: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 126 | 1000 | my ($self) = @_; | |||||||||||||||||||
| 127 | 1000 | return "$$self"; | |||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 0 | ▶ | sub as_string { | ||||||||||||||||||
|
Subroutine
as_string: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 131 | 0 | my ($self) = @_; | |||||||||||||||||||
| 132 | 0 | return "$$self"; | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 2 | 1; | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | __END__ | ||||||||||||||||||||