1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
package Email::MessageID;
use strict;
use vars qw[$VERSION];
$VERSION = '1.35';
use Email::Address;
=head1 NAME
Email::MessageID - Generate world unique message-ids.
=head1 SYNOPSIS
use Email::MessageID;
my $mid = Email::MessageID->new;
print "Message-ID: $mid\x0A\x0D";
=head1 DESCRIPTION
Message-ids are optional, but highly recommended, headers that identify a
message uniquely. This software generates a unique message-id.
=head2 Methods
=over 4
=item new
my $mid = Email::MessageID->new;
my $new_mid = Email::MessageID->new( host => $myhost );
This class method constructs an L<Email::Address|Email::Address> object
containing a unique message-id. You may specify custom C<host> and C<user>
parameters.
By default, the C<host> is generated from C<Sys::Hostname::hostname>.
By default, the C<user> is generated using C<Time::HiRes>'s C<gettimeofday>
and the process ID.
Using these values we have the ability to ensure world uniqueness down to
a specific process running on a specific host, and the exact time down to
six digits of microsecond precision.
=cut
sub new {
my ($class, %args) = @_;
$args{user} ||= $class->create_user;
$args{host} ||= $class->create_host;
my $mid = join '@', @args{qw[user host]};
return Email::Address->new(undef, $mid);
}
=item create_host
my $domain_part = Email::Address->create_host;
This method returns the domain part of the message-id.
=cut
sub create_host {
require Sys::Hostname;
return Sys::Hostname::hostname();
}
=item create_user
my $local_part = Email::Address->create_user;
This method returns a unique local part for the message-id. It includes some
random data and some predictable data.
=cut
my @CHARS = ('A'..'F','a'..'f',0..9);
my $unique_value = 0;
sub _generate_string {
my $length = 3;
$length = rand(8) until $length > 3;
join '', (map $CHARS[rand $#CHARS], 0 .. $length), $unique_value++;
}
sub create_user {
my $pseudo_random = $_[0]->_generate_string;
my $user = join '.', time, $pseudo_random, $$;
return $user;
}
1;
__END__
=pod
=back
=head1 SEE ALSO
L<Email::Address>, L<Time::HiRes>, L<Sys::Hostname>, L<perl>.
=head1 AUTHOR
Casey West, <F<casey@geeknest.com>>.
=head1 COPYRIGHT
Copyright (c) 2004 Casey West. All rights reserved.
This module is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
|