#!/usr/bin/perl # License # GPLv2 (http://www.gnu.org/licenses/gpl-2.0.txt) # Authors # DELAPORTE Antoine use strict; use warnings; use Getopt::Std; use Net::SMTP; # Configuration dans /etc/postfix/master.cf # nto unix - n n - - pipe # flags= user=nobody argv=/usr/local/bin/nto.pl -s $sender -n $nexthop -r $recipient -u $user # # Configuration dans la transportable # email/domaine nto:email/domaine|ressend,announce=/etc/postfix/announce.txt # email/domaine email/domaine | parametres # Parametres: # ressend (par defaut, sauf si announce present), envoie le mail retravaillé pour le destinataire reel # annouce=fichier envoie au sender un mail comme quoi l'utilisateur a demenagé # Les entrees __SENDER__ __RECIPIENT__ __NEXTHOP__ et __SUBJECT__ seront automatiquement reecrite # TODO un lock pour ne pas renvoyer 2 fois au meme emetteur l'annonce de demenagement # rewrite (par defaut desactivé) reecris dans le header tout les recipient par des nexthop my %opt; getopts( "s:n:r:u:", \%opt ); my $subject; my $inbody=0; my $body=""; my $header=""; my $smtpserver="localhost"; my $smtpport=25; my %cfg; my ($nexthop,$params)=split(/\|/,$opt{n},2); $opt{n}=$nexthop; my $i=0; if(defined($params)) { foreach (split(/,/,$params)) { my ($clef,$val)=split(/=/); if(!defined($val)) { $val=1; } $cfg{$clef}=$val; $i++ } } if(($i==0) or defined($cfg{rewrite})) { $cfg{ressend}=1; } if(defined($cfg{announce}) and ($cfg{annouce}==1)) { $cfg{announce}="/etc/postfix/announce.txt"; } if(!($opt{n} =~ m/@/)) { $opt{n}=$opt{u}."@".$opt{n}; } while() { if(/^$/) { $inbody++; } if(!$inbody) { $header.=$_; if(/^Subject/) { chomp(); $subject=$_; $subject=~s/^Subject: //g; } } else { $body.=$_; } } if($cfg{ressend}) { if($cfg{rewrite}) { $header=~s/$opt{r}/$opt{n}/g; } my $smtp = Net::SMTP->new($smtpserver, Port=>$smtpport, Timeout => 10, Debug => 1); die "Could not connect to server!\n" unless $smtp; ###$smtp->auth($smtpuser, $smtppassword); $smtp->mail($opt{s}); $smtp->to($opt{n}); $smtp->data(); $smtp->datasend($header."\n".$body); $smtp->quit; } if(defined($cfg{announce}) and (-r $cfg{announce})) { my $mail=""; open(ANN,"<".$cfg{announce}); while () { $mail.=$_; } close(ANN); $mail=~s/__SENDER__/$opt{s}/g; $mail=~s/__RECIPIENT__/$opt{r}/g; $mail=~s/__NEXTHOP__/$opt{n}/g; $mail=~s/__SUBJECT__/$subject/g; my $smtp = Net::SMTP->new($smtpserver, Port=>$smtpport, Timeout => 10, Debug => 1); die "Could not connect to server!\n" unless $smtp; ###$smtp->auth($smtpuser, $smtppassword); $smtp->mail($opt{n}); $smtp->to($opt{s}); $smtp->data(); $smtp->datasend($mail); $smtp->quit; }