]>
iEval git - app-edwardng.git/blob - lib/App/EdwardNG.pm
6 use parent qw
/Exporter/ ;
7 our $VERSION = '0.001' ;
8 our @EXPORT = qw
/process_message/ ;
10 use Email
:: Sender
:: Simple qw
/sendmail/ ;
11 use File
:: Share qw
/dist_file/ ;
12 use File
:: Slurp qw
/read_file/ ;
13 use File
:: Spec
:: Functions qw
/rel2abs/ ;
22 sub debug
{ say STDERR
@_ if $ENV { EDWARDNG_DEBUG
} }
23 sub stringify
($) { join '' , map {; '>' , $_ } @
{ $_ [ 0 ]} }
26 key
=> $ENV { EDWARDNG_KEY
},
27 maybe always_trust
=> $ENV { EDWARDNG_ALWAYS_TRUST
},
28 maybe keydir
=> $ENV { EDWARDNG_KEYDIR
},
29 maybe passphrase
=> $ENV { EDWARDNG_PASSPHRASE
},
30 maybe use_agent
=> $ENV { EDWARDNG_USE_AGENT
},
36 return first_part
( $ent -> parts ( 0 )) if $ent -> parts ;
37 stringify
[ $ent -> bodyhandle -> as_lines ]
43 my $parser = MIME
:: Parser
-> new ;
44 $parser -> decode_bodies ( 0 );
45 $parser -> output_to_core ( 1 );
47 $msg = $in if ref $in eq 'MIME::Entity' ;
48 $msg = $parser -> parse ( $in ) if ref $in eq 'IO' ;
49 $msg = $parser -> parse_data ( $in ) if ref $in eq 'SCALAR' ;
50 $msg = $parser -> parse_open ( $in ) unless ref $in ;
51 die "Don't know how to parse $in " unless $msg ;
53 if ( $msg -> mime_type ne 'multipart/signed' && $msg -> mime_type ne 'multipart/encrypted' ) {
54 # PGP/Inline requires decoding
55 $parser -> decode_bodies ( 1 );
56 $msg = $parser -> parse_data ( $msg -> stringify )
60 if ( $gpg -> is_signed ( $msg )) {
61 debug
'This mail looks signed' ;
62 my ( $code , $keyid , $email ) = $gpg -> verify ( $msg );
63 return sign_error
=> (
64 message
=> stringify
$gpg ->{ last_message
}) if $code ;
68 message
=> stringify
$gpg ->{ last_message
});
71 if ( $gpg -> is_encrypted ( $msg )) {
72 debug
'This mail looks encrypted' ;
73 my ( $code , $keyid , $email ) = $gpg -> decrypt ( $msg );
74 return encrypt_error
=> (
75 message
=> stringify
$gpg ->{ last_message
}) if $code ;
77 plaintext
=> stringify
$gpg ->{ plaintext
},
78 decrypted
=> $gpg ->{ decrypted
},
79 message
=> stringify
$gpg ->{ last_message
}) unless defined $keyid ;
80 return signencrypt
=> (
83 plaintext
=> stringify
$gpg ->{ plaintext
},
84 decrypted
=> $gpg ->{ decrypted
},
85 message
=> stringify
$gpg ->{ last_message
});
88 debug
'This mail doesn \' t seem to be signed or encrypted' ;
94 'always-trust!' => \
$ENV { EDWARDNG_ALWAYS_TRUST
},
95 'debug!' => \
$ENV { EDWARDNG_DEBUG
},
96 'from=s' => \
$ENV { EDWARDNG_FROM
},
97 'key=s' => \
$ENV { EDWARDNG_KEY
},
98 'keydir=s' => \
$ENV { EDWARDNG_KEYDIR
},
99 'logfile=s' => \
$ENV { EDWARDNG_LOGFILE
},
100 'passphrase=s' => \
$ENV { EDWARDNG_PASSPHRASE
},
101 'tmpl-path=s' => \
$ENV { EDWARDNG_TMPL_PATH
},
102 'use-agent!' => \
$ENV { EDWARDNG_USE_AGENT
},
104 my $tmpl_path = $ENV { EDWARDNG_TMPL_PATH
} // 'en' ;
105 open STDERR
, '>>' , $ENV { EDWARDNG_LOGFILE
} if $ENV { EDWARDNG_LOGFILE
};
107 my $parser = MIME
:: Parser
-> new ;
108 $parser -> decode_bodies ( 0 );
109 $parser -> output_to_core ( 1 );
110 my $in = $parser -> parse ( \
* STDIN
);
111 debug
'Received mail from ' , $in -> get ( 'From' );
115 ( $tmpl , %params ) = process_message
$in
117 ( $tmpl , %params ) = ( error
=> message
=> $_ )
119 debug
"Result is $tmpl , GnuPG said: \n " , $params { message
};
121 $params { plaintext
} = first_part
$params { decrypted
} if $params { decrypted
};
123 my $tt = Template
-> new ( INCLUDE_PATH
=> rel2abs
$tmpl_path , dist_file
'App-EdwardNG' , 'tmpl' );
125 $tt -> process ( $tmpl , \
%params , \
$data );
126 my $email = MIME
:: Entity
-> build (
127 From
=> $ENV { EDWARDNG_FROM
},
128 To
=> $in -> get ( 'From' ),
129 Subject
=> 'Re: ' . $in -> get ( 'Subject' ),
132 my $email_unencrypted = $email -> dup ;
133 my $mg = mg always_trust
=> 1 ;
134 my $encrypt_failed = $mg -> mime_signencrypt ( $email , $in -> get ( 'From' ) =~ /<(.*)>/ );
135 debug
'Could not encrypt message, sending unencrypted. GnuPG said:' , " \n " , stringify
$mg ->{ last_message
} if $encrypt_failed ;
136 sendmail
$encrypt_failed ?
$email_unencrypted : $email
146 App::EdwardNG - GnuPG email sign/encrypt testing bot
151 my ($status, %params) = process_message '/path/to/message';
152 if ($status eq 'signencrypt') {
153 say 'This message is encrypted and signed with key ', $params{keyid}, ' from ', $params{email};
154 say 'Its contents are: ', $params{plaintext};
155 } elsif ($status eq 'encrypt') {
156 say 'This message is encrypted but not signed';
157 say 'Its contents are: ', $params{plaintext};
158 } elsif ($status eq 'encrypt_error') {
159 say 'This message is encrypted but I was unable to decrypt it. GnuPG output: ', $params{message};
160 } elsif ($status eq 'sign') {
161 say 'This message is signed with key ', $params{keyid}, ' from ', $params{email};
162 } elsif ($status eq 'sign_error') {
163 say 'This message is signed but I was unable to verify the signature. GnuPG output: ', $params{message};
164 } elsif ($status eq 'plain') {
165 say 'This message is neither signed nor encrypted';
166 } elsif ($status eq 'error') {
167 say 'There was an error processing the message: ', $params{message};
172 EdwardNG is a reimplementation of the Edward reply bot referenced in L<https://emailselfdefense.fsf.org/>.
174 This module exports a single function, B<process_message>, which takes a single parameter representing the message. This parameter can be:
178 =item A filehandle reference, e.g. C<\*STDIN>.
180 =item A reference to a scalar which holds the message contents.
182 =item A scalar which represents a path to a message.
184 =item A L<MIME::Entity> object
188 The function returns a status followed by a hash. Possible results:
194 The message is neither signed nor encrypted.
196 =item sign_error, message => $message
198 The message is signed but the signature could not be verified. GnuPG output is $message.
200 =item sign, keyid => $keyid, email => $email, message => $message
202 The message is signed with key $keyid from $email. GnuPG output is $message.
204 =item encrypt_error, message => $message
206 The message is encrypted and unable to be decrypted. GnuPG output is $message.
208 =item encrypt, plaintext => $plaintext, decrypted => $decrypted, message => $message
210 The message is encrypted and unsigned. $plaintext is the decrypted message as plain text, while $decrypted is a MIME::Entity representing the decrypted message. GnuPG output is $message.
212 =item signencrypt, plaintext => $plaintext, decrypted => $decrypted, keyid => $keyid, email => $email, message => $message
214 The message is encrypted and signed with key $keyid from $email. $plaintext is the decrypted message as plain text, while $decrypted is a MIME::Entity representing the decrypted message. GnuPG output is $message.
216 =item error, message => $message
218 There was an error while processing the message. The error can be found in $message.
224 This module is configured via the %ENV hash. See the L<edwardng(1)> manpage for more information.
232 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
234 =head1 COPYRIGHT AND LICENSE
236 Copyright (C) 2014 by Fundația Ceata
238 This library is free software; you can redistribute it and/or modify
239 it under the same terms as Perl itself, either Perl version 5.18.2 or,
240 at your option, any later version of Perl 5 you may have available.
This page took 0.077272 seconds and 4 git commands to generate.