| 1 | package Log::Dispatch::Email::DKIM; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use autodie qw(:all); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use base 'Log::Dispatch::Email'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use Carp qw(carp croak); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use Email::Address::XS; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 10 | ▶ | use File::Basename qw(basename); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 10 | ▶ | use File::HomeDir; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 10 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 10 | ▶ | use Params::Get qw(get_params); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 10 | ▶ | use Params::Validate::Strict qw(validate_strict); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 10 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 10 | ▶ | use Scalar::Util qw(looks_like_number); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # Private helpers are really private (a call from outside this package croaks, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # which also works for $self->_helper() dispatch); the one protected method, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # i18n(), may be called by a subclass that wants to reword the messages. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 10 | ▶ | use Sub::Private { 'mode' => 'enforce' }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 10 | ▶ | use Sub::Protected; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | Log::Dispatch::Email::DKIM - Log::Dispatch appender that DKIM-signs its mail | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | Version 0.001.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 10 | ▶ | use version; our $VERSION = version->declare('0.001.0'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # Everything that was once a literal in the code lives here, so that it is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # named once and can be changed (or, for the messages, translated) in one place. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 10 | Readonly my $DEFAULT_SENDMAIL => '/usr/sbin/sendmail'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 10 | Readonly my $KEY_FILE_SUFFIX => '.private'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 10 | Readonly my $KEY_DIR_NAME => 'dkim'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 10 | Readonly my $KEY_FILE_RE => qr/\A(.+)\Q$KEY_FILE_SUFFIX\E\z/; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 10 | Readonly my $LINE_BREAKS_RE => qr/[\r\n]+/; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # The environment variable that overrides each setting | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 10 | Readonly my %ENV_VAR => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 'domain' => 'DKIM_DOMAIN', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 'selector' => 'DKIM_SELECTOR', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 'keyfile' => 'DKIM_KEYFILE', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 'keydir' => 'DKIM_KEYDIR', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 'sendmail' => 'DKIM_SENDMAIL', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | # Input schema for send_email(). Log::Dispatch hands over more than the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | # message (level, name and so on) when the appender is unbuffered, so unknown | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # parameters are ignored rather than rejected. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 10 | Readonly my $SEND_EMAIL_SCHEMA => { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 'message' => { 'type' => 'string', 'min' => 0 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | # The message catalogue. An entry is [ $sprintf_format, @names_of_arguments ], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # or a hash of such entries for the variants of a message: 'one' and 'other' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # are chosen by the 'count' argument, and any other key by the 'gender' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # argument (see i18n()). It is a package variable, not a Readonly, so that a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # site can translate it with local() or by assigning to a key. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 10 | our %MESSAGES = ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 'no_recipient' => ['To whom should I be sending this e-mail?'], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 'no_sender' => ['DKIM: no valid From address to send as; message not sent'], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 'no_key' => ['DKIM: no DKIM key for %s; sending unsigned', 'domain'], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 'unknown_domain' => ['unknown domain'], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 'send_failed' => ['DKIM: sendmail failed for %s', 'sender'], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 'unknown_setting' => ['DKIM: unknown setting %s', 'setting'], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 'no_message' => ['message is required'], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 'bad_arguments' => ['DKIM: invalid arguments to %s: %s', 'routine', 'reason'], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | Pattern 1: from a log4perl configuration file. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | log4perl.appender.A2=Log::Dispatch::Email::DKIM | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | log4perl.appender.A2.from=webmaster@example.com | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | log4perl.appender.A2.to=admin@example.com | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | log4perl.appender.A2.subject=Error in shop/index.fcgi | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | Pattern 2: directly from Log::Dispatch, sending each message at once. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | use Log::Dispatch; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | my $log = Log::Dispatch->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | outputs => [ [ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 'Email::DKIM', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 'min_level' => 'error', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 'from' => 'webmaster@example.com', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 'to' => ['admin@example.com'], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 'subject' => 'Error in shop/index.fcgi', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 'buffered' => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | ] ], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | $log->error("Something went wrong\n"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | Pattern 3: choose the key and the selector with environment variables (for | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | example in the web server settings). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | SetEnv DKIM_KEYFILE /home/site/dkim/202611.private | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | SetEnv DKIM_SELECTOR 202611 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | Pattern 4: in a test, send to a script that saves the message instead of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | to sendmail. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | local $ENV{'DKIM_SENDMAIL'} = '/tmp/capture-script'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | $log->error("Test message\n"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | Sends a log message the way L<Log::Dispatch::Email::Sendmail> does, but signs | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | it with L<Mail::Send::DKIM> first, so the message carries a C<DKIM-Signature> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | for the domain it claims to come from. An unsigned C<webmaster@> message and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | a forgery are indistinguishable in a DMARC report; signing is what tells them | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | apart, and what lets the copy a mailing list forwards still authenticate. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | The signing key is found rather than configured. C<Log::Dispatch> validates | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | each appender's arguments and passes on only C<name>, C<min_level>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | C<max_level>, C<callbacks> and C<newline>, so a C<keyfile=> line in the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | log4perl configuration never reaches this object. Instead the domain is taken | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | from the C<from> address and the selector from the key's file name. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | The defaults suit this host: the key in C<~/dkim/*.private> with the highest | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | name is used, and its name is the selector. Each can be overridden in the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | environment, which is also how a test points it at a capture script. An | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | empty variable counts as unset. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | =item C<DKIM_DOMAIN> - the domain put in the signature (its C<d=> tag), instead of the one in C<from> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | =item C<DKIM_SELECTOR> - the DKIM selector, instead of the key's file name | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | =item C<DKIM_KEYFILE> - the private key to sign with | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | =item C<DKIM_KEYDIR> - where to look for C<*.private> (default C<~/dkim>) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | =item C<DKIM_SENDMAIL> - the sendmail to hand the message to (then C<SENDMAIL>) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | C<DKIM_DOMAIN> changes only the domain in the signature. Whether a message is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | signed at all is still decided by the domain in C<from>, so a message from an | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | address this host has no key for is sent unsigned whatever C<DKIM_DOMAIN> says. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | C<DKIM_SENDMAIL> wins over C<SENDMAIL>, the variable L<Mail::Send::DKIM> and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | its C<mail-dkim> programme read, so a host that sets only C<SENDMAIL> gets the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | same sendmail from both. With neither set, F</usr/sbin/sendmail> is used. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | The home directory comes from L<File::HomeDir>, so it is found even when the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | process (a CGI script, say) has no C<HOME> in its environment. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | =head1 COMMON PITFALLS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | B<Buffering is on by default.> Log::Dispatch::Email collects messages and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | sends ONE e-mail when the program ends (or when C<flush> is called). Set | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | C<< buffered => 0 >> to send one e-mail for each message. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | B<You cannot give the appender a key file in its settings.> Log::Dispatch | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | throws away settings it does not know. Use the environment variables in | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | L</CONFIGURATION> instead. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | B<An empty environment variable means "not set".> C<DKIM_DOMAIN=""> does | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | not turn anything off. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | B<The signing domain comes from C<from>.> If C<from> is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | C<webmaster@example.com> the message is signed for C<example.com>. Your DKIM | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | key must belong to that domain, or the receiver will not accept the signature. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | B<The highest file name wins, not the newest file.> Of C<202611.private> and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | C<202701.private>, the second is used. If you keep an old key with a larger | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | name, it will be used. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | B<A missing key is not an error.> The e-mail is still sent, without a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | signature, and you only get a warning. Watch for the warning. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | B<C<undef> is not a message.> C<send_email(message =E<gt> undef)> stops with | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | an error. An empty string is accepted. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | B<A missing C<to> or an unusable C<from> loses the message.> Nothing is sent | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | and one line is written to STDERR. It is not a warning, so a warning handler | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | will not see it. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | B<Private routines cannot be called from outside.> Names that start with an | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | underscore croak if you call them. Under C<prove> the check is switched off | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | (C<HARNESS_ACTIVE>), so a test can pass where real code would fail. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | B<The message catalogue is shared by the whole program.> Changing | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | C<%Log::Dispatch::Email::DKIM::MESSAGES> changes the words for every | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | appender. Use C<local> to limit the change. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | =head1 ENCODING | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | This module does not convert text. It passes the bytes it is given to | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | sendmail and it adds no C<Content-Type> or C<charset> header. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | =item B<Message body> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | Give it UTF-8 B<bytes> (text that is already encoded, for example with | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | C<Encode::encode('UTF-8', $text)>). Non-ASCII letters and emojis then reach | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | sendmail unchanged. Because no charset is declared, a mail program may show | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | them wrongly. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | If you give it a Perl character string that holds a character above code | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | point 255 (an emoji, for example), Perl warns C<Wide character in print> and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | sends UTF-8. If all the characters are below 256 (an accented letter, for | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | example), they are sent as single Latin-1 bytes. Do not mix the two kinds. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | =item B<Subject> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | The same rule applies. The subject is NOT converted to the C<=?UTF-8?...?=> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | form that older mail systems need. Use ASCII for a safe subject. A non-ASCII | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | subject is sent as raw UTF-8, which only works with receivers that accept it. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | =item B<From and To addresses> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | Use ASCII. A display name such as C<"Web Master"> is allowed, but it is not | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | encoded either. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | =item B<Environment variables and file names> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | These are byte strings from the operating system. Use ASCII for the selector | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | and domain. A key directory with non-ASCII letters in its name works, because | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | it is read as bytes. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | =item B<Line breaks> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | In C<to>, C<from> and C<subject>, any line break is replaced by one space. In | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | the body, every line break is changed to a carriage return and line feed, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | which DKIM needs. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | =head2 send_email | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | =head3 Purpose | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | Builds the message, signs it and hands it to sendmail(1). Log::Dispatch | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | calls this itself, once per message when unbuffered and once per flush when | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | buffered, so applications do not normally call it. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | =head3 Args | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | A hash or hash reference with C<message>, the text of the message body. Any | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | other key (C<level>, C<name>, ...) is ignored. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | =head3 Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | C<$self>, so calls can be chained. The outcome of the delivery is reported | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | by warning, not by the return value, because Log::Dispatch ignores it. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | =head3 Side Effects | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | Runs sendmail(1). While it runs, STDERR is pointed at the null device | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | because some sendmail implementations write a warning there that would corrupt | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | CGI output. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | A missing or unreadable key is not an error: it warns and sends the message | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | unsigned, which is what L<Mail::Send::DKIM> does for a signing problem of any | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | kind. A line break in the subject, From or To is replaced by a space, so a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | logged value cannot add a header of its own. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | When there is no recipient, or the C<from> address cannot be parsed, nothing | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | is sent and the problem is written straight to STDERR, not warned: a warning | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | could be routed back into this appender and loop. C<from> may carry a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | display name (C<"Web Master" E<lt>webmaster@example.comE<gt>>); the bare | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | address is used as the sendmail envelope sender and as the signing domain. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | =head3 Usage | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | $appender->send_email(message => "Something went wrong\n"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | use Log::Dispatch; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | my $log = Log::Dispatch->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | outputs => [ [ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 'Email::DKIM', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 'min_level' => 'error', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 'from' => 'webmaster@example.com', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 'to' => ['admin@example.com'], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 'subject' => 'Error in shop/index.fcgi', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | ] ], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | # Sent, signed with the highest ~/dkim/*.private, when $log goes away | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | $log->error("Something went wrong\n"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | # Test a different key and a capture script instead of sendmail | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | local $ENV{'DKIM_KEYFILE'} = '/tmp/keys/202611.private'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | local $ENV{'DKIM_SENDMAIL'} = '/tmp/capture'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | =head4 INPUT | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | message => { type => 'string', min => 0 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | Unknown keys are ignored. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | =head4 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | { type => 'object', isa => 'Log::Dispatch::Email::DKIM' } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | =item C<DKIM: no DKIM key for DOMAIN; sending unsigned> (warning) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | No readable key, domain or selector could be found. The message is sent | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | without a signature. Put a C<SELECTOR.private> key in C<~/dkim>, or set | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | C<DKIM_KEYFILE> or C<DKIM_KEYDIR>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | =item C<DKIM: sendmail failed for SENDER> (warning) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | sendmail(1) could not be run or exited with a failure (a non-zero exit | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | status). Check C<DKIM_SENDMAIL> and the mail queue. When sendmail could not | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | be started at all, L<Mail::Send::DKIM> also warns C<cannot run PROGRAM: REASON>, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | where REASON is the operating system's own error text, in the current locale. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | =item C<To whom should I be sending this e-mail?> (STDERR) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | The appender has no recipient, so nothing was sent. Give it a C<to>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | =item C<DKIM: no valid From address to send as; message not sent> (STDERR) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | C<from> is missing or is not an address. Give it a C<from> such as | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | C<webmaster@example.com>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | =item C<DKIM: invalid arguments to send_email: REASON> (fatal) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | The arguments were not a message. Pass C<< message => $text >>; REASON is the validator's complaint, or | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | C<message is required>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | =head3 PSEUDOCODE | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | validate the arguments (croak if there is no message) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | parse From into an address (undef if it is not one) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | if there is no recipient or no address | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | write the reason to STDERR and stop | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | find domain, selector and keyfile (environment, then From, then ~/dkim) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | warn if any is missing or the keyfile is unreadable | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | build the message with a sanitised To, From and Subject | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | run sendmail with STDERR on the null device | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | warn if it failed | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | return self | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | sub send_email | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 37 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
send_email: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 37 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 37 | my $args = $self->_validated_arguments('send_email', \@_); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 33 | my $sender = $self->_parse_address($self->{'from'}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | # Neither case can be reported with carp: it could come back here. An | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | # empty list is as good as no recipient: sendmail -t would be given "To: ". | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 32 | my $recipients = $self->{'to'}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 32 | ▶ | if(ref($recipients) ne 'ARRAY' || !@{$recipients}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
ref $recipients ne "ARRAY" or not @{$recipients;}
MC/DC100%2/2
ref $recipients ne "ARRAY" or not @{$recipients;}
ref $recipients ne "ARRAY"
not @{$recipients;}
Truth table100%3/3
A or not(B)
A= ref $recipients ne "ARRAY"B= not @{$recipients;}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | 5 | $self->_say_to_stderr('no_recipient'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | } elsif(!$sender) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 2 | $self->_say_to_stderr('no_sender'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 25 | $self->_deliver($sender, $args->{'message'}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 31 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | =head2 i18n | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | =head3 Purpose | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | Turns a message key into text for the user, so that every warning and error | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | this class raises can be translated or reworded by a subclass or by assigning | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | to C<%Log::Dispatch::Email::DKIM::MESSAGES>. Protected: callable from | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | this class and its subclasses only. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | =head3 Args | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | The message key, then an optional hash reference of arguments. C<count> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | selects the C<one> or C<other> variant of a message, and C<gender> selects the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | variant of that name, with C<other> as the fallback. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | =head3 Returns | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | The message text. An unknown key returns the key itself, so a missing | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | translation is visible rather than fatal. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | =head3 Side Effects | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | None. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | =head3 Usage | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | my $text = $self->i18n('no_key', { 'domain' => 'example.com' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | # A catalogue entry is [ $sprintf_format, @argument_names ], or a hash | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | # of variants of one | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | local $Log::Dispatch::Email::DKIM::MESSAGES{'files'} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | 'one' => ['%s file', 'count'], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | 'other' => ['%s files', 'count'], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | $self->i18n('files', { 'count' => 2 }); # "2 files" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | =head4 INPUT | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | key => { type => 'string', min => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | args => { type => 'hashref', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | =head4 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | { type => 'string' } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | None of its own. An unknown key is returned as the text. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | =head3 PSEUDOCODE | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | look the key up; if absent, return the key | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | if the entry is a hash of variants, choose one by gender, then count | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | format the entry with its named arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | 76 | ▶ | sub i18n :Protected | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
i18n: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | 76 | my ($self, $key, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | 76 | ▶ | $args ||= {}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args ||= {}
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | 76 | ▶ | my $entry = defined($MESSAGES{$key}) ? $self->_choose_variant($MESSAGES{$key}, $args) : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | # No such key, or a message with no usable variant: show the key, so the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | # gap is visible rather than fatal | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | 76 | ▶ | return defined($entry) ? sprintf($entry->[0], map { $args->{$_} // q{} } @{$entry}[1 .. $#{$entry}]) : $key; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$args->{$_} // ""
MC/DC100%1/1
$args->{$_} // ""
$args->{$_}
Truth table100%2/2
A
A= $args->{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | 10 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | # Purpose: pick the wording for a message that has variants. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | # Entry: $entry is an array reference (returned as it is) or a hash reference | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | # of variants; $args is a hash reference. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | # Exit: an array reference [ $format, @names ], or undef if a hash of variants | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | # has none that applies. The variant named by $args->{gender} wins, then | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | # 'one' when $args->{count} is the number 1, then 'other'. A count that is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | # not a number is not 1. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | # Side effects: none. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | 87 | ▶ | sub _choose_variant :Private | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_choose_variant: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | 87 | my ($self, $entry, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | 87 | ▶ | if(ref($entry) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | 26 | my $count = $args->{'count'}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | 26 | ▶ | my $is_one = defined($count) && looks_like_number($count) && $count == 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
defined $count && &looks_like_number($count) && $count == 1
defined $count and &looks_like_number($count)
MC/DC100%3/3
defined $count && &looks_like_number($count) && $count == 1
defined $count
&looks_like_number($count)
$count == 1
Truth table100%4/4
A and B && C
A= defined $countB= &looks_like_number($count)C= $count == 1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | 26 | ▶ | my @wanted = grep { defined } ($args->{'gender'}, $is_one ? 'one' : undef, 'other'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | 26 | my ($found) = grep { exists $entry->{$_} } @wanted; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | 26 | ▶ | $entry = defined($found) ? $entry->{$found} : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | 87 | return $entry; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | 10 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | # Purpose: raise a warning in the user's words. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | # Entry: $key and $args as for i18n(). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | # Exit: $self, for chaining. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | # Side effects: a carp(), reported from the caller's point of view. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | 21 | ▶ | sub _warn_user :Private | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_warn_user: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | 21 | my ($self, $key, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | 21 | carp($self->i18n($key, $args)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | 20 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | 10 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | # Purpose: report a problem that must not go through the warn() machinery. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | # Entry: $key and $args as for i18n(). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | # Exit: $self, for chaining. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | # Side effects: a line on STDERR. Not a warning, because a warn handler may | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | # log it, which would send it back through this appender. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | 4 | ▶ | sub _say_to_stderr :Private | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_say_to_stderr: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | 4 | my ($self, $key, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | 4 | print STDERR $self->i18n($key, $args), "\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | 4 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | 10 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | # Purpose: normalise and check the arguments of a public routine. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | # Entry: $routine is its name (for the message); $arguments is an array | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | # reference to its @_, in any form Params::Get understands. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | # Exit: a hash reference of the validated arguments. Croaks, in the user's | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | # words, if they do not match $SEND_EMAIL_SCHEMA. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | # Side effects: none. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | 39 | ▶ | sub _validated_arguments :Private | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validated_arguments: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | 39 | my ($self, $routine, $arguments) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | 39 | my $validated = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | 39 | validate_strict( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | 'schema' => $SEND_EMAIL_SCHEMA, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | 'input' => get_params(undef, $arguments), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | 'unknown_parameter_handler' => 'ignore', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | # validate_strict() lets an undef through a required parameter, by design, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | # so a missing message is checked for as well | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | 39 | ▶ | my $reason = $@ || (defined($validated->{'message'}) ? q{} : $self->i18n('no_message')); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$@ || (defined $validated->{'message'} ? '' : $self->i18n('no_message'))
MC/DC100%2/2
$@ || (defined $validated->{'message'} ? '' : $self->i18n('no_message'))
$@
(defined $validated->{'message'} ? '' : $self->i18n('no_message'))
Truth table100%3/3
A || B
A= $@B= (defined $validated->{'message'} ? '' : $self->i18n('no_message'))
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | 39 | ▶ | if(length($reason)) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | # The reason ends in a newline when it came from a die(); drop it so the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | # message stays on one line, with croak's own location after it | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | 13 | chomp($reason); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | 13 | croak($self->i18n('bad_arguments', { 'routine' => $routine, 'reason' => $reason })); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | 26 | return $validated; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | 10 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | # Purpose: read an address into its parts. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | # Entry: $text is a From-style address, with or without a display name. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | # Exit: an Email::Address::XS object, or undef if $text holds no valid | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | # address. Email::Address::XS, not a regex, because a display name may | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | # itself contain an '@'. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | # Side effects: none. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | 42 | ▶ | sub _parse_address :Private | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_address: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | 42 | my ($self, $text) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | # Warnings are silenced here (Email::Address::XS carps about junk), for the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | # same reason _say_to_stderr() avoids warn(): one could loop back to us | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | 42 | ▶ | local $SIG{'__WARN__'} = sub { }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | 42 | ▶ | my ($address) = (defined($text) && length($text)) ? Email::Address::XS->parse($text) : (); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $text and length $text
MC/DC100%2/2
defined $text and length $text
defined $text
length $text
Truth table100%3/3
A and B
A= defined $textB= length $text
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | 42 | ▶ | return ($address && $address->is_valid()) ? $address : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$address and $address->is_valid
MC/DC100%2/2
$address and $address->is_valid
$address
$address->is_valid
Truth table100%3/3
A and B
A= $addressB= $address->is_valid
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | 10 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | # Purpose: make a value safe to use as the text of one header line. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | # Entry: $value is a string. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | # Exit: the string with each run of line breaks replaced by one space, so it | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | # cannot end its own header and begin another (header injection). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | # Side effects: none. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | 115 | ▶ | sub _one_line :Private | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_one_line: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | 115 | my ($self, $value) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | 115 | $value =~ s/$LINE_BREAKS_RE/ /g; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | 115 | return $value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | 10 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | # Purpose: read an override from the environment. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | # Entry: $setting is a key of %ENV_VAR; anything else croaks. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | # Exit: the variable's value, or undef if it is unset or empty. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | # Side effects: none. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | 143 | ▶ | sub _from_environment :Private | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_from_environment: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | 143 | my ($self, $setting) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | # A misspelt setting is a bug in this module; do not read $ENV{undef} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | 143 | ▶ | croak($self->i18n('unknown_setting', { 'setting' => $setting })) unless(exists($ENV_VAR{$setting})); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | 137 | my $value = $ENV{$ENV_VAR{$setting}}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | 137 | ▶ | return (defined($value) && length($value)) ? $value : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value and length $value
MC/DC100%2/2
defined $value and length $value
defined $value
length $value
Truth table100%3/3
A and B
A= defined $valueB= length $value
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | 10 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | # Purpose: the sendmail named by the SENDMAIL variable that Mail::Send::DKIM | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | # also reads. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | # Exit: its value, or undef if it is unset or empty. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | # Side effects: none. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | 14 | ▶ | sub _plain_sendmail :Private | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_plain_sendmail: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | 14 | my $value = $ENV{'SENDMAIL'}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | 14 | ▶ | return (defined($value) && length($value)) ? $value : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value and length $value
MC/DC100%2/2
defined $value and length $value
defined $value
length $value
Truth table100%3/3
A and B
A= defined $valueB= length $value
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | 10 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | # Purpose: find the private key to sign with. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | # Entry: none; consults DKIM_KEYFILE, DKIM_KEYDIR and the home | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | # directory. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | # Exit: the path of DKIM_KEYFILE, or else of the highest-named | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | # *.private in the key directory, or undef if there is none. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | # Side effects: reads a directory. A directory listing is used instead of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | # glob() because glob() splits on spaces and expands '[' and '*'. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | 34 | ▶ | sub _find_key_file :Private | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_find_key_file: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | 34 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | 34 | my $keyfile = $self->_from_environment('keyfile'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | 34 | ▶ | return $keyfile if(defined($keyfile)); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | 32 | my $dir = $self->_from_environment('keydir'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | 32 | ▶ | if(!defined($dir)) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | 5 | my $home = File::HomeDir->my_home(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | 5 | ▶ | $dir = File::Spec->catdir($home, $KEY_DIR_NAME) if(defined($home)); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | 32 | ▶ | if(defined($dir) && -d $dir) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $dir and -d $dir
MC/DC100%2/2
defined $dir and -d $dir
defined $dir
-d $dir
Truth table100%3/3
A and B
A= defined $dirB= -d $dir
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | # Only regular files count (a directory called x.private is no key). A | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | # directory that cannot be read, or that vanishes now, means "no key", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | # which sends the mail unsigned, not an exception from the logger. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | 28 | my @names = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | 28 | opendir(my $handle, $dir); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | 27 | ▶ | my @found = sort grep { /\Q$KEY_FILE_SUFFIX\E\z/ && -f File::Spec->catfile($dir, $_) } readdir($handle); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
/\Q$KEY_FILE_SUFFIX\E\z/ && -f 'File::Spec'->catfile($dir, $_)
MC/DC100%2/2
/\Q$KEY_FILE_SUFFIX\E\z/ && -f 'File::Spec'->catfile($dir, $_)
/\Q$KEY_FILE_SUFFIX\E\z/
-f 'File::Spec'->catfile($dir, $_)
Truth table100%3/3
A && B
A= /\Q$KEY_FILE_SUFFIX\E\z/B= -f 'File::Spec'->catfile($dir, $_)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | 27 | closedir($handle); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | 27 | @found; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | 28 | ▶ | $keyfile = File::Spec->catfile($dir, $names[-1]) if(@names); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | 32 | return $keyfile; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | 10 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | # Purpose: work out how to sign this message. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | # Entry: $sender is the Email::Address::XS of the From address. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | # Exit: a hash reference { domain, selector, keyfile }; any may be undef. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | # The domain is the one From: claims, because a signature for any other | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | # domain would not align with the message; the selector is the key's file | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | # name. The environment overrides each. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | # Side effects: reads a directory (see _find_key_file). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | 31 | ▶ | sub _signing_key :Private | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_signing_key: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | 31 | my ($self, $sender) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | 31 | my $keyfile = $self->_find_key_file(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | 30 | my $selector = $self->_from_environment('selector'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | 30 | ▶ | if(!defined($selector) && defined($keyfile) && basename($keyfile) =~ $KEY_FILE_RE) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $selector and defined $keyfile and basename($keyfile) =~ /$KEY_FILE_RE/
not defined $selector and defined $keyfile
MC/DC100%3/3
not defined $selector and defined $keyfile and basename($keyfile) =~ /$KEY_FILE_RE/
not defined $selector
defined $keyfile
basename($keyfile) =~ /$KEY_FILE_RE/
Truth table100%4/4
not(A) and B and C
A= not defined $selectorB= defined $keyfileC= basename($keyfile) =~ /$KEY_FILE_RE/
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | 6 | $selector = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | 30 | ▶ | 'domain' => $self->_from_environment('domain') // $sender->host(), | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$self->_from_environment("domain") // $sender->host
MC/DC0%0/2
$self->_from_environment("domain") // $sender->host
$self->_from_environment("domain")
$sender->host
Truth table66%2/3
A // B
A= $self->_from_environment("domain")B= $sender->host
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | 'selector' => $selector, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | 'keyfile' => $keyfile, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | 10 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | # Purpose: run a piece of code with STDERR discarded. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | # Entry: $code is a code reference. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | # Exit: whatever $code returns (in scalar context). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | # Side effects: some sendmail implementations write a warning to STDERR that | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 693 | # breaks CGI output, so STDERR is the null device while $code runs and is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 694 | # restored after, as Log::Dispatch::Email::Sendmail does. If the null | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 695 | # device cannot be opened $code runs anyway: a noisy message beats a lost one. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 696 | 30 | ▶ | sub _without_stderr :Private | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_without_stderr: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 697 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 698 | 30 | my ($self, $code) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | 30 | my $result; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | 30 | local *STDERR; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | 30 | eval { open(STDERR, '>', File::Spec->devnull()); 1 }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | 30 | $result = $code->(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | 27 | return $result; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | 10 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 710 | # Purpose: sign and send one message. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 711 | # Entry: $sender is an Email::Address::XS; $message is the body text; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 712 | # $self->{to} is a non-empty array reference. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 713 | # Exit: $self, for chaining. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 714 | # Side effects: runs sendmail(1); warns if the message is unsigned or the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 715 | # send failed. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 716 | 36 | ▶ | sub _deliver :Private | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_deliver: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 717 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 718 | 36 | my ($self, $sender, $message) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 720 | 36 | my $key = $self->_signing_key($sender); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 722 | # Unsigned mail is still sent, but whoever reads the log should hear of it | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 723 | 35 | ▶ | my $signed = (defined($key->{'domain'}) && defined($key->{'selector'}) && defined($key->{'keyfile'}) && -r $key->{'keyfile'}); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%9/9
defined $key->{'domain'} && defined $key->{'selector'} && defined $key->{'keyfile'} && -r $key->{'keyfile'}
defined $key->{"domain"} and defined $key->{"selector"} and defined $key->{"keyfile"}
defined $key->{"domain"} and defined $key->{"selector"}
MC/DC100%4/4
defined $key->{'domain'} && defined $key->{'selector'} && defined $key->{'keyfile'} && -r $key->{'keyfile'}
defined $key->{"domain"}
defined $key->{"selector"}
defined $key->{"keyfile"}
-r $key->{'keyfile'}
Truth table100%5/5
A and B and C && D
A= defined $key->{"domain"}B= defined $key->{"selector"}C= defined $key->{"keyfile"}D= -r $key->{'keyfile'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 724 | 35 | ▶ | if(!$signed) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 725 | 20 | ▶ | $self->_warn_user('no_key', { 'domain' => $key->{'domain'} // $self->i18n('unknown_domain') }); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$key->{"domain"} // $self->i18n("unknown_domain")
MC/DC0%0/2
$key->{"domain"} // $self->i18n("unknown_domain")
$key->{"domain"}
$self->i18n("unknown_domain")
Truth table66%2/3
A // B
A= $key->{"domain"}B= $self->i18n("unknown_domain")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 726 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 728 | 35 | require Mail::Send::DKIM; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 730 | my $dkim = Mail::Send::DKIM->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 731 | # Which messages to sign is decided by the From: domain; the signature | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 732 | # is for the signing domain, which DKIM_DOMAIN may change | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 733 | 'domains' => [ $sender->host() ], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 734 | 'signing_domain' => $key->{'domain'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 735 | 'selector' => $key->{'selector'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 736 | 35 | ▶ | 'keyfile' => $key->{'keyfile'}, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
($self->_from_environment("sendmail") // _plain_sendmail()) // $DEFAULT_SENDMAIL
$self->_from_environment("sendmail") // _plain_sendmail()
MC/DC100%3/3
($self->_from_environment("sendmail") // _plain_sendmail()) // $DEFAULT_SENDMAIL
$self->_from_environment("sendmail")
_plain_sendmail()
$DEFAULT_SENDMAIL
Truth table100%4/4
A // B // C
A= $self->_from_environment("sendmail")B= _plain_sendmail()C= $DEFAULT_SENDMAIL
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 737 | 'sendmail' => $self->_from_environment('sendmail') // _plain_sendmail() // $DEFAULT_SENDMAIL, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 738 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 740 | my $text = join("\n", | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 741 | 34 | 'To: ' . $self->_one_line(join(',', @{$self->{'to'}})), | |||||||||||||||||||||||||||||||||||||||||||||||||
| 742 | 'From: ' . $self->_one_line($self->{'from'}), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 743 | 34 | 'Subject: ' . $self->_one_line($self->{'subject'}), | |||||||||||||||||||||||||||||||||||||||||||||||||
| 744 | ) . "\n\n" . $message; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 746 | # sign() returns undef when it cannot sign, and the message goes unsigned | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 747 | 33 | ▶ | if($signed) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 748 | 13 | my $signed_text = $dkim->sign($text); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 749 | 13 | ▶ | $text = $signed_text if(defined($signed_text)); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 750 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 752 | # The envelope sender is the bare address: a display name would be | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 753 | # refused as an invalid sendmail -f argument | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 754 | 33 | ▶ | my $status = $self->_without_stderr(sub { $dkim->send($text, '-t', '-f', $sender->address()) }); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 755 | 32 | ▶ | $self->_warn_user('send_failed', { 'sender' => $sender->address() }) if($status); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 757 | 32 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 758 | 10 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 760 | =head1 EXAMPLES | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 761 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 762 | A complete log4perl configuration file, which logs everything to a file and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 763 | emails anything of level C<WARN> or above. Save it as, say, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 764 | F<myapp.l4pconf> and load it with C<Log::Log4perl-E<gt>init('myapp.l4pconf')>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 766 | log4perl.logger.myapp=TRACE, A1, A2 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 767 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 768 | log4perl.appender.A1=Log::Dispatch::File | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 769 | log4perl.appender.A1.Filter=RangeAll | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 770 | log4perl.appender.A1.filename=/tmp/myapp.log | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 771 | log4perl.appender.A1.mode=append | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 772 | log4perl.appender.A1.layout=Log::Log4perl::Layout::PatternLayout | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 773 | log4perl.appender.A1.layout.ConversionPattern=%d %p> %F{1}:%L %M - %m%n | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 774 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 775 | log4perl.appender.A2=Log::Dispatch::Email::DKIM | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 776 | log4perl.appender.A2.Filter=RangeError | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 777 | log4perl.appender.A2.from=webmaster@example.com | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 778 | log4perl.appender.A2.to=admin@example.com | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 779 | log4perl.appender.A2.subject=Error in myapp | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 780 | log4perl.appender.A2.layout=Log::Log4perl::Layout::PatternLayout | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 781 | log4perl.appender.A2.layout.ConversionPattern=%d %p> %F{1}:%L %M - %m%n | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 783 | log4perl.filter.RangeAll=Log::Log4perl::Filter::LevelRange | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 784 | log4perl.filter.RangeAll.LevelMin=TRACE | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 785 | log4perl.filter.RangeAll.LevelMax=FATAL | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 786 | log4perl.filter.RangeAll.AcceptOnMatch=true | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 787 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 788 | log4perl.filter.RangeError=Log::Log4perl::Filter::LevelRange | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 789 | log4perl.filter.RangeError.LevelMin=WARN | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 790 | log4perl.filter.RangeError.LevelMax=FATAL | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 791 | log4perl.filter.RangeError.AcceptOnMatch=true | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 792 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 793 | =head1 LIMITATIONS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 794 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 795 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 796 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 797 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 798 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 799 | The message catalogue is a package variable and C<i18n> is protected, because | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 800 | C<Log::Dispatch> constructs the appender and does not pass on extra | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 801 | arguments, so there is no object configuration to carry a locale. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 802 | L<Object::Configure> is therefore not used: there is nowhere for it to hook | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 803 | in. A site changes the wording by assigning to | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 804 | C<%Log::Dispatch::Email::DKIM::MESSAGES>, which is global to the process. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 805 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 806 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 808 | The key is found by convention (C<~/dkim/*.private>, highest name wins) and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 809 | the environment, not configured per appender, for the same reason. Two | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 810 | appenders in one process cannot sign with different keys. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 811 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 812 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 813 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 814 | The key with the highest I<name> is used, not the newest or the one that | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 815 | matches the published DNS record. Leave a retired key in the directory and, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 816 | if it sorts last, it is the one that signs. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 817 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 818 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 819 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 820 | The message goes out only once: if sendmail(1) fails the message is lost, not | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 821 | queued for retry, and the only record is the warning. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 822 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 823 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 824 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 825 | A C<from> with no valid address, or a missing recipient, loses the message. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 826 | This used to be a fatal error from the underlying mailer, which a logger should | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 827 | not raise. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 828 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 829 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 830 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 831 | No MIME headers are added: the body has no declared charset and the subject | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 832 | is not encoded. See L</ENCODING>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 833 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 834 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 835 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 836 | Redirecting STDERR is process-wide for the duration of the send, so output | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 837 | written by another thread meanwhile is lost. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 838 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 839 | =item * | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 840 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 841 | The plural and gender support in C<i18n> selects a variant by key; it does | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 842 | not know the plural rules of any language beyond one against other. Use | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 843 | L<Locale::Maketext> if you need more. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 844 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 845 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 846 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 847 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 848 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 849 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 850 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 851 | =item * L<Test Dashboard|https://nigelhorne.github.io/Log-Dispatch-Email-DKIM/coverage/> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 852 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 853 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 854 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 855 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 856 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 857 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 858 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 859 | =head1 FORMAL SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 860 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 861 | The notation is Z. C<Config> is what an appender holds after C<new>. A | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 862 | message is signed only when a readable key, a domain and a selector are all | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 863 | found. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 864 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 865 | [STRING, ADDRESS, PATH] | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 866 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 867 | ┌─ Config ───────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 868 | │ from : STRING | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 869 | │ to : seq STRING | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 870 | │ subject : STRING | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 871 | │ buffered : 𝔹 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 872 | │ buffer : seq STRING | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 873 | ├────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 874 | │ buffered = false ⇒ buffer = ⟨ ⟩ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 875 | └────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 876 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 877 | =head2 send_email | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 878 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 879 | ┌─ SendEmail ────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 880 | │ Ξ Config | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 881 | │ message? : STRING | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 882 | │ sender : ADDRESS ⇸ ADDRESS (the parse of from) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 883 | │ key : PATH ⇸ PATH (the key file, if any) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 884 | │ sent! : 𝔹 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 885 | │ signed! : 𝔹 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 886 | ├────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 887 | │ message? is defined | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 888 | │ sent! = (to ≠ ⟨ ⟩ ∧ sender ≠ ∅ ∧ sendmail succeeds) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 889 | │ signed! = (sent! ∧ key ≠ ∅ ∧ readable(key) ∧ domain defined | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 890 | │ ∧ selector defined) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 891 | │ sent! ∧ ¬ signed! ⇒ one warning "no DKIM key" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 892 | │ to ≠ ⟨ ⟩ ∧ sender ≠ ∅ ∧ ¬ sent! ⇒ one warning "sendmail failed" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 893 | │ (to = ⟨ ⟩ ∨ sender = ∅) ⇒ one line on STDERR and no warning | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 894 | └────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 895 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 896 | =head2 i18n | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 897 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 898 | ┌─ I18n ─────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 899 | │ Ξ Config | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 900 | │ catalogue : STRING ⇸ (ENTRY ∪ (STRING ⇸ ENTRY)) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 901 | │ key? : STRING | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 902 | │ args? : STRING ⇸ STRING | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 903 | │ text! : STRING | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 904 | ├────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 905 | │ key? ∉ dom catalogue ⇒ text! = key? | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 906 | │ key? ∈ dom catalogue ⇒ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 907 | │ text! = sprintf(format(entry), args?(names(entry))) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 908 | │ a name missing from args? is replaced by the empty string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 909 | └────────────────────────────────────────────────────────── | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 910 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 911 | =head1 STATE DIAGRAM | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 912 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 913 | An appender is always in one of these states: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 914 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 915 | EMPTY no message is waiting | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 916 | PENDING one or more messages wait in the buffer | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 917 | SENDING send_email is running | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 918 | GONE the object has been destroyed | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 919 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 920 | new | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 921 | | | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 922 | v | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 923 | +-----------+ log, buffered=1 +-----------+ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 924 | | |------------------->| |--+ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 925 | | EMPTY | | PENDING | | log, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 926 | | | | |<-+ buffered=1 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 927 | +-----------+ +-----------+ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 928 | | ^ | | | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 929 | log, | | | destroy | flush, or | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 930 | buffered=0 | | v | destroy | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 931 | | | +------+ | | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 932 | | | | GONE | | | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 933 | v | +------+ | | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 934 | +-----------+ | | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 935 | | |<------------------------+ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 936 | | SENDING | | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 937 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 938 | +-----------+ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 939 | send_email ends: back to EMPTY (the buffer is cleared) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 940 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 941 | The same changes in a table: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 942 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 943 | From Trigger To Action or side effect | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 944 | ------- ----------------------- ------- ----------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 945 | (none) new EMPTY Checks the settings. Makes an | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 946 | empty buffer if buffered = 1. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 947 | EMPTY log, buffered = 1 PENDING Adds the text to the buffer. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 948 | PENDING log, buffered = 1 PENDING Adds the text to the buffer. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 949 | EMPTY log, buffered = 0 SENDING Calls send_email at once. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 950 | PENDING flush SENDING Joins the buffer into one text | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 951 | and calls send_email. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 952 | PENDING object destroyed SENDING Same as flush. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 953 | SENDING send_email ends EMPTY The buffer is cleared. The | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 954 | result is one of: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 955 | - signed and sent: no output | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 956 | - no key: sent, one warning | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 957 | - sendmail failed: one warning | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 958 | - no to, or bad from: nothing | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 959 | sent, one line on STDERR | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 960 | - no message: croaks and the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 961 | buffer is NOT cleared | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 962 | EMPTY object destroyed GONE Nothing is sent. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 963 | PENDING program is ending GONE Log::Dispatch::Email warns | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 964 | (global destruction) that messages were not sent. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 965 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 966 | =head1 LICENCE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 967 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 968 | Copyright 2026 Nigel Horne. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 969 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 970 | Usage is subject to the GPL2 licence terms. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 971 | If you use it, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 972 | please let me know. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 973 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 974 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 975 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 976 | 10 | 1; # End of Log-Dispatch-Email-DKIM | |||||||||||||||||||||||||||||||||||||||||||||||||