X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=lib%2FApp%2FEdwardNG.pm;h=6f6b3a0adc0abc8cd3b4fcebb93050bc963a0817;hb=b65d84868bf813c43b91ee58bdf26671e28fa819;hp=88046414814dfe4af44f5cbe016a4bbf50be60f7;hpb=1f08c3525c33b88f5b67e84b573f695ec2d4643e;p=app-edwardng.git diff --git a/lib/App/EdwardNG.pm b/lib/App/EdwardNG.pm index 8804641..6f6b3a0 100644 --- a/lib/App/EdwardNG.pm +++ b/lib/App/EdwardNG.pm @@ -9,6 +9,7 @@ our @EXPORT = qw/process_message/; use Email::Sender::Simple qw/sendmail/; use File::Slurp qw/read_file/; +use File::Spec::Functions qw/rel2abs/; use Getopt::Long; use MIME::Entity; use MIME::Parser; @@ -36,25 +37,17 @@ sub first_part{ } sub process_message { - my ($msg) = @_; + my ($in) = @_; + my $msg; my $parser = MIME::Parser->new; $parser->decode_bodies(0); $parser->output_to_core(1); - if (ref $msg eq 'MIME::Entity') { - debug 'Got MIME::Entity'; - } elsif (ref $msg eq 'IO') { - debug 'Parsing from filehandle'; - $msg = $parser->parse($msg) - } elsif (ref $msg eq 'SCALAR') { - debug 'Parsing from string'; - $msg = $parser->parse_data($$msg) - } elsif (!ref $msg) { - debug "Parsing from file $msg"; - $msg = $parser->parse_open($msg) - } else { - die "Don't know how to parse $msg" - } + $msg = $in if ref $in eq 'MIME::Entity'; + $msg = $parser->parse ($in) if ref $in eq 'IO'; + $msg = $parser->parse_data ($in) if ref $in eq 'SCALAR'; + $msg = $parser->parse_open ($in) unless ref $in; + die "Don't know how to parse $in" unless $msg; if ($msg->mime_type ne 'multipart/signed' && $msg->mime_type ne 'multipart/encrypted') { # PGP/Inline requires decoding @@ -103,8 +96,10 @@ sub run { 'key=s' => \$ENV{EDWARDNG_KEY}, 'keydir=s' => \$ENV{EDWARDNG_KEYDIR}, 'passphrase=s' => \$ENV{EDWARDNG_PASSPHRASE}, + 'tmpl-path=s' => \$ENV{EDWARDNG_TMPL_PATH}, 'use-agent!' => \$ENV{EDWARDNG_USE_AGENT}, ); + my $tmpl_path = $ENV{EDWARDNG_TMPL_PATH} // 'en'; my $parser = MIME::Parser->new; $parser->decode_bodies(0); @@ -120,19 +115,20 @@ sub run { $params{plaintext} = first_part $params{decrypted} if $params{decrypted}; - my $tt = Template->new(INCLUDE_PATH => 'tmpl/en'); - my ($data, $subject); + my $tt = Template->new(INCLUDE_PATH => rel2abs $tmpl_path, 'tmpl'); + my $data; $tt->process($tmpl, \%params, \$data); - $tt->process('subject', undef, \$subject); my $email = MIME::Entity->build( From => $ENV{EDWARDNG_FROM}, To => $in->get('From'), - Subject => $subject, + Subject => 'Re: ' . $in->get('Subject'), Data => $data); + my $email_unencrypted = $email->dup; my $mg = mg always_trust => 1; - $mg->mime_signencrypt($email, $in->get('From') =~ /<(.*)>/) and debug 'Could not encrypt message. GnuPG said ', stringify $mg->{last_message}; - sendmail $email + my $encrypt_failed = $mg->mime_signencrypt($email, $in->get('From') =~ /<(.*)>/); + debug 'Could not encrypt message, sending unencrypted. GnuPG said ', stringify $mg->{last_message} if $encrypt_failed; + sendmail $encrypt_failed ? $email_unencrypted : $email } 1; @@ -170,8 +166,6 @@ App::EdwardNG - GnuPG email sign/encrypt testing bot EdwardNG is a reimplementation of the Edward reply bot referenced in L. -It takes mail messages, checks them for PGP signatures and encryption, then replies appropriately. - This module exports a single function, B, which takes a single parameter representing the message. This parameter can be: =over