]>
iEval git - app-edwardng.git/blob - lib/App/EdwardNG.pm
256cf7d670d7031697918d079edeca7cc0805f8c
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
:: Slurp qw
/read_file/ ;
20 sub debug
{ say STDERR
@_ if $ENV { EDWARDNG_DEBUG
} }
21 sub stringify
($) { join '' , map {; '>' , $_ } @
{ $_ [ 0 ]} }
24 key
=> $ENV { EDWARDNG_KEY
},
25 maybe always_trust
=> $ENV { EDWARDNG_ALWAYS_TRUST
},
26 maybe keydir
=> $ENV { EDWARDNG_KEYDIR
},
27 maybe passphrase
=> $ENV { EDWARDNG_PASSPHRASE
},
28 maybe use_agent
=> $ENV { EDWARDNG_USE_AGENT
},
34 return first_part
$ent -> parts ( 0 ) if $ent -> parts ;
35 $ent -> bodyhandle -> as_string
40 my $parser = MIME
:: Parser
-> new ;
41 $parser -> decode_bodies ( 0 );
42 $parser -> output_to_core ( 1 );
44 if ( ref $msg eq 'MIME::Entity' ) {
45 debug
'Got MIME::Entity' ;
46 } elsif ( ref $msg eq 'IO' ) {
47 debug
'Parsing from filehandle' ;
48 $msg = $parser -> parse ( $msg )
49 } elsif ( ref $msg eq 'SCALAR' ) {
50 debug
'Parsing from string' ;
51 $msg = $parser -> parse_data ($ $msg )
53 debug
"Parsing from file $msg " ;
54 $msg = $parser -> parse_open ( $msg )
56 die "Don't know how to parse $msg "
59 if ( $msg -> mime_type ne 'multipart/signed' && $msg -> mime_type ne 'multipart/encrypted' ) {
60 # PGP/Inline requires decoding
61 $parser -> decode_bodies ( 1 );
62 $msg = $parser -> parse_data ( $msg -> stringify )
66 if ( $gpg -> is_signed ( $msg )) {
67 debug
'This mail looks signed' ;
68 my ( $code , $keyid , $email ) = $gpg -> verify ( $msg );
69 return sign_error
=> (
70 message
=> stringify
$gpg ->{ last_message
}) if $code ;
74 message
=> stringify
$gpg ->{ last_message
});
77 if ( $gpg -> is_encrypted ( $msg )) {
78 debug
'This mail looks encrypted' ;
79 my ( $code , $keyid , $email ) = $gpg -> decrypt ( $msg );
80 return encrypt_error
=> (
81 message
=> stringify
$gpg ->{ last_message
}) if $code ;
83 plaintext
=> stringify
$gpg ->{ plaintext
},
84 decrypted
=> $gpg ->{ decrypted
},
85 message
=> stringify
$gpg ->{ last_message
}) unless defined $keyid ;
86 return signencrypt
=> (
89 plaintext
=> stringify
$gpg ->{ plaintext
},
90 decrypted
=> $gpg ->{ decrypted
},
91 message
=> stringify
$gpg ->{ last_message
});
94 debug
'This mail doesn \' t seem to be signed or encrypted' ;
100 'always-trust!' => \
$ENV { EDWARDNG_ALWAYS_TRUST
},
101 'debug!' => \
$ENV { EDWARDNG_DEBUG
},
102 'from=s' => \
$ENV { EDWARDNG_FROM
},
103 'key=s' => \
$ENV { EDWARDNG_KEY
},
104 'keydir=s' => \
$ENV { EDWARDNG_KEYDIR
},
105 'passphrase=s' => \
$ENV { EDWARDNG_PASSPHRASE
},
106 'use-agent!' => \
$ENV { EDWARDNG_USE_AGENT
},
109 my $parser = MIME
:: Parser
-> new ;
110 $parser -> decode_bodies ( 0 );
111 $parser -> output_to_core ( 1 );
112 my $in = $parser -> parse ( \
* STDIN
);
116 ( $tmpl , %params ) = process_message
$in
118 ( $tmpl , %params ) = ( error
=> message
=> $_ )
121 $params { plaintext
} = first_part
$params { decrypted
} if $params { decrypted
};
123 my $tt = Template
-> new ( INCLUDE_PATH
=> 'tmpl/en' );
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 $mg = mg always_trust
=> 1 ;
133 $mg -> mime_signencrypt ( $email , $in -> get ( 'From' ) =~ /<(.*)>/ ) and debug
'Could not encrypt message. GnuPG said ' , stringify
$mg ->{ last_message
};
144 App::EdwardNG - GnuPG email sign/encrypt testing bot
149 my ($status, %params) = process_message '/path/to/message';
150 if ($status eq 'signencrypt') {
151 say 'This message is encrypted and signed with key ', $params{keyid}, ' from ', $params{email};
152 say 'Its contents are: ', $params{plaintext};
153 } elsif ($status eq 'encrypt') {
154 say 'This message is encrypted but not signed';
155 say 'Its contents are: ', $params{plaintext};
156 } elsif ($status eq 'encrypt_error') {
157 say 'This message is encrypted but I was unable to decrypt it. GnuPG output: ', $params{message};
158 } elsif ($status eq 'sign') {
159 say 'This message is signed with key ', $params{keyid}, ' from ', $params{email};
160 } elsif ($status eq 'sign_error') {
161 say 'This message is signed but I was unable to verify the signature. GnuPG output: ', $params{message};
162 } elsif ($status eq 'plain') {
163 say 'This message is neither signed nor encrypted';
164 } elsif ($status eq 'error') {
165 say 'There was an error processing the message: ', $params{message};
170 EdwardNG is a reimplementation of the Edward reply bot referenced in L<https://emailselfdefense.fsf.org/>.
172 It takes mail messages, checks them for PGP signatures and encryption, then replies appropriately.
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.061199 seconds and 3 git commands to generate.