blib/lib/Mail/Run/Crypt.pm

filter: Statementstmt 91.3% 21 / 23 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 93.5% 29 / 31 SCAR 0.0
CC
1
Cov
91%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package Mail::Run::Crypt;
2  
3 # Force me to write this properly
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # Require this version of Perl
9 6 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # Import required modules
12 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use English '-no_match_vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use IPC::Run3;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Mail::GnuPG;
Subroutine BEGIN: called CC=1 SCAR=2.6
16 use MIME::Entity;
17  
18 # Specify package verson
19 our $VERSION = '0.11';
20  
21 # Default exit value
22 our $DEFAULT_EXIT = 127; ## no critic (ProhibitMagicNumbers)
23  
24 # Oldschool constructor
25 sub new {
26 my ( $class, %opts ) = @_;
27  
28 # Blindly slurp in all the options given
29 my $self = {%opts};
30  
31 # We must have a recipient
32 defined $self->{mailto}
33 or croak 'MAILTO required';
34  
35 # Default the instance name to the package name if it wasn't given;
36 # runcrypt(1) will pass it in
37 defined $self->{name} or $self->{name} = $class;
38  
39 # We default to encrypting but not signing
40 defined $self->{encrypt} or $self->{encrypt} = 1;
41 defined $self->{sign} or $self->{sign} = 0;
42  
43 # If signing, we need a key ID and a passphrase
44 if ( $self->{sign} ) {
45 defined $self->{keyid}
46 or croak 'Key ID required for signing';
47 defined $self->{passphrase}
48 or croak 'Passphrase required for signing';
49 }
50  
51 # Return objectified self
52 return bless $self, $class;
53 }
54  
55 # Run a given command
56 sub run {
57 my ( $self, @command ) = @_;
58  
59 # Run the command and wait for it to finish; keep its exit value for later
60 my ( @out, @err );
61 eval { run3 \@command, undef, \@out, \@err }
62 or warn "Command failed: $EVAL_ERROR\n";
63 $self->{exit} = $CHILD_ERROR >> 8;
64  
65 # If there was output, mail it
66 if (@out) {
67 my $command = join q{ }, @command;
68 my $subject = "$self->{name} output: $command";
69 $self->_mail( $subject, \@out );
70 }
71  
72 # If there were errors, mail them
73 if (@err) {
74 my $command = join q{ }, @command;
75 my $subject = "$self->{name} errors: $command";
76 $self->_mail( $subject, \@err );
77 }
78  
79 # Return status reflecting the command exit value
80 return $self->{exit} == 0;
81 }
82  
83 # Return the value of the most recently run command, or 1 otherwise
84 sub bail {
85 my $self = shift;
86 my $exit =
87 defined $self->{exit}
88 ? $self->{exit}
89 : $DEFAULT_EXIT;
90 return $exit;
91 }
92  
93 # Send the message to the address in $ENV{MAILTO}
94 sub _mail {
95 my ( $self, $subject, $content ) = @_;
96  
97 # Build MIME object with plaintext message
98 my $mime = MIME::Entity->build(
99 To => $self->{mailto},
100 Subject => $subject,
101 Data => $content,
102 );
103  
104 # Encrypt the MIME object
105 my $mgpg = Mail::GnuPG->new(
106 key => $self->{keyid},
107 passphrase => $self->{passphrase},
108 );
109  
110 # Sign and/or encrypt as appropriate
111 if ( $self->{sign} and $self->{encrypt} ) {
112 $mgpg->mime_signencrypt( $mime, $self->{mailto} );
113 }
114 elsif ( $self->{sign} ) {
115 $mgpg->mime_sign( $mime, $self->{mailto} );
116 }
117 elsif ( $self->{encrypt} ) {
118 $mgpg->mime_encrypt( $mime, $self->{mailto} );
119 }
120  
121 # Send it
122 return $mime->send();
123 }
124  
125 1;
126  
127 __END__
↑ Summary