X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=scripts%2Fmqr-smtp.pl;h=6ba45ee342cdeaf8371057d547617db583cca1dd;hb=cacc5ac4f2617d85bae8bf5d58554ab56494ec97;hp=23d37d9c393634e837b7944edbb6e90956e80f9d;hpb=15970793cd6ac303cbc32d70489f22c5c6bd8a98;p=MQR.git diff --git a/scripts/mqr-smtp.pl b/scripts/mqr-smtp.pl index 23d37d9..6ba45ee 100755 --- a/scripts/mqr-smtp.pl +++ b/scripts/mqr-smtp.pl @@ -4,10 +4,40 @@ use warnings; use strict; use AnyEvent::SMTP::Server; - -my $server = AnyEvent::SMTP::Server->new( port => 2525 ); +use AnyEvent::SMTP::Client qw(sendmail); +use Data::Dump qw(dump); -$server->reg_cb( +use lib 'lib'; +use MQR::Redis; + +my $host = $ENV{SMTP_HOST} || '0.0.0.0'; +my $port = $ENV{SMTP_PORT} || 2525; +my $server = $ENV{SMTP_SERVER}; + +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"; + + 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"; + }, +# debug => 1, # XXX + 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"; @@ -18,9 +48,15 @@ $server->reg_cb( }, mail => sub { my ($s,$mail) = @_; - warn "Received mail from $mail->{from} to $mail->{to}\n$mail->{data}\n"; + my $from = $mail->{from}; + warn "Received mail from $from to ",join(' and ',@{$mail->{to}}), "\n$mail->{data}\n"; + 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 $from $to" => $subject ); + } }, ); -$server->start; +$smtp->start; AnyEvent->condvar->recv;