added SEND
authorDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 31 Aug 2010 15:54:47 +0000 (17:54 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 31 Aug 2010 15:54:47 +0000 (17:54 +0200)
This allows you to use !track term by sending e-mail to MQR and
receive track notifications via e-mail

scripts/mqr-smtp.pl

index 68c4279..15eac84 100755 (executable)
@@ -4,6 +4,7 @@ use warnings;
 use strict;
 
 use AnyEvent::SMTP::Server;
+use AnyEvent::SMTP::Client qw(sendmail);
 use Data::Dump qw(dump);
 
 use lib 'lib';
@@ -11,10 +12,31 @@ use MQR::Redis;
 
 my $host = $ENV{SMTP_HOST} || '0.0.0.0';
 my $port = $ENV{SMTP_PORT} || 2525;
+my $server = $ENV{SMTP_SERVER} || '127.0.0.1:25';
 
-my $server = AnyEvent::SMTP::Server->new( host => $host, port => $port );
+MQR::Redis->redis->psubscribe( "SEND $host:$port smtp *", sub {
+       my ( $message, $channel ) = @_;
+       my ( undef, undef, undef, $from, $to ) = split(/\s/,$channel);
+       warn "sendmail $from -> $to | $message\n";
 
-$server->reg_cb(
+       my $f = $to;
+       $f =~ s/\@.+//;
+       $f .= '@mqr.rot13.org';
+
+       sendmail
+               from => $f,
+               to => $from,
+               data => "Subject: $message\n\n$message",
+               cb => sub {
+                       my ($ok,$error) = @_;
+                       warn "# sendmail ", $ok && "OK $ok" || "ERROR: $error", "\n";
+               },
+               server => $server,
+});
+
+my $smtp = AnyEvent::SMTP::Server->new( host => $host, port => $port );
+
+$smtp->reg_cb(
        client => sub {
                my ($s,$con) = @_;
                warn "Client from $con->{host}:$con->{port} connected\n";
@@ -30,10 +52,10 @@ $server->reg_cb(
                foreach my $to ( @{ $mail->{to} } ) {
                        my $subject = $1 if $mail->{data} =~ m/^Subject:\s*(.+)$/m;
                        $subject =~ s/[\s\r\n]+$//; # XXX important!
-                       MQR::Redis->publish( "MSG $host:$port smtp $mail->{from} $to" => $subject );
+                       MQR::Redis->publish( "MSG $host:$port smtp $from $to" => $subject );
                }
        },
 );
 
-$server->start;
+$smtp->start;
 AnyEvent->condvar->recv;