Mail-Send-DKIM
view release on metacpan or search on metacpan
lib/Mail/Send/DKIM.pm view on Meta::CPAN
otherwise the name of the only F<*.private> file in B<keydir>. C<undef> when it
cannot be decided - no key, or several keys and no name - and the reason is a
warning on standard error. B<sign> asks this before doing anything else.
=head3 PURPOSE
Settles which of the keys is to be used, once, in one place.
A key in B<keydir> is a regular file with a name in front of F<.private>.
A directory with such a name, and a file that is only F<.private>, are not
keys and are not counted. A selector that is an empty string is the same as
none.
=head3 EXAMPLE
my $selector = $dkim->selector();
die 'no usable key' unless(defined($selector));
=head3 API SPECIFICATION
=head4 Input (Params::Validate::Strict)
{}
=head4 Output (Return::Set)
{ type => 'string', optional => 1 }
=head3 MESSAGES
Message key Meaning and resolution
keydir_unreadable keydir cannot be opened: create it, or fix the mode or
the keydir option.
keys_ambiguous keydir holds no key, or more than one, and no selector
was named: add the key, or set DKIM_SELECTOR.
=head3 PSEUDOCODE
if a selector was given: return it
if a key file was given: return its name without .private, .key or .pem
list the *.private files in keydir (a warning and undef if it cannot be read)
if there is not exactly one: warn, return undef
return its name without .private
=cut
sub selector
{
my $self = shift;
my $selector = $self->{'selector'};
return $selector if(defined($selector) && length($selector));
# A named key file carries its selector in its name, which is the layout
# the keys are kept in: <keydir>/<selector>.private.
if(defined($self->{'keyfile'})) {
($selector = basename($self->{'keyfile'})) =~ s/$KEYFILE_NAME_SUFFIX//;
return $selector if(length($selector));
}
my $keys = $self->_private_keys();
# undef, as the POD says: a bare return would be an empty list when the
# result is used in a list, as in a hash of options.
return undef unless(defined($keys)); ## no critic (Subroutines::ProhibitExplicitReturnUndef)
unless(@{$keys} == 1) {
$self->_say('keys_ambiguous', { 'keydir' => $self->{'keydir'}, 'count' => scalar(@{$keys}) });
return undef; ## no critic (Subroutines::ProhibitExplicitReturnUndef)
}
($selector = $keys->[0]) =~ s/\Q$KEY_SUFFIX\E\z//;
return $selector;
}
# Purpose: List the keys in the key directory.
# Entry criteria: $self->{keydir} is set.
# Exit status: An array reference of the names of the files ending .private, sorted;
# undef, after a warning, when the directory cannot be read.
# Side effects: Reads a directory; may warn.
sub _private_keys :Private
{
my $self = shift;
# An unreadable directory is a warning and an unsigned message, so autodie
# stands aside.
no autodie qw(opendir closedir);
my $keys;
if(opendir(my $dh, $self->{'keydir'})) {
# A key is a file with a name before the suffix: a directory called
# x.private, or a file called just .private, is not one.
$keys = [ sort grep { /\A.+\Q$KEY_SUFFIX\E\z/s && -f "$self->{'keydir'}/$_" } readdir($dh) ];
closedir($dh);
} else {
$self->_say('keydir_unreadable', { 'keydir' => $self->{'keydir'}, 'os_error' => "$!" });
}
return $keys;
}
=head2 B<i18n> $key, \%arguments
Returns the text of a message, in the language this object speaks, with the
arguments put in. This is the one place any sentence is made, so a caller can
change every message by giving B<new> a C<messages> option.
A message is a format whose placeholders are named, C<%{name}s> or
C<%{name}d>, and which may carry the usual C<sprintf> width and flags
(C<%{name}-8s>). It may instead be a hash of formats, from which one is chosen
by the arguments: C<count> selects C<zero>, C<one> or C<other> (C<other> when
there is no better one), and C<variant> - a grammatical gender, for instance -
selects the entry of that name, or C<variant.one>, C<variant.other> and so on
when the choice is both. An argument a format asks for and was not given is an
error, not an empty string. An argument that is given as C<undef> is an empty
string (C<0> in a C<%{name}d>). A C<count> that is not a number selects no
plural form. C<%%> is a percent sign, and any other C<%> that is not part of a
placeholder is plain text. An entry that is an empty hash says nothing.
Croaks if C<$key> is not a message that is defined.
=head3 EXAMPLE
my $text = $dkim->i18n('keys_ambiguous', { keydir => '/etc/dkim', count => 2 });
# '... /etc/dkim holds 2 keys; name the selector to choose one'
my $french = Mail::Send::DKIM->new(messages => {
'keys_ambiguous' => {
'one' => '%{keydir}s contient %{count}d cle',
'other' => '%{keydir}s contient %{count}d cles',
},
});
=head3 API SPECIFICATION
=head4 Input (Params::Validate::Strict)
{
key => { type => 'string' },
arguments => { type => 'hashref', optional => 1 },
}
( run in 1.105 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )