6 $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
11 use POSIX qw(WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG WIFSTOPPED WSTOPSIG);
13 require Mail::Address;
14 require File::Basename;
16 my $sendmail_orig=(-x ($_="/usr/sbin/sendmail-orig") ? $_ : "/usr/sbin/sendmail");
17 my $HOME="/home/lace";
21 my($rcpt,$iserror)=@_;
23 return Mail::Address->new(
24 (defined $opt_F ? $opt_F : "Jan Kratochvil"),
25 (!$iserror ? 'rcpt' : 'rcpterr')
27 .(defined($rcpt->user()) ? $rcpt->user() : "NOUSER")
29 .(defined($rcpt->host()) ? $rcpt->host() : "LOCAL")
30 .'@jankratochvil.net',
34 # RedHat sendmail-8.9.3-20/src/conf.c/HdrInfo[]/\Q/* destination fields */\E
35 # FIXME: Recognize "Resent-$_" headers for -t but when we are in 'resent' mode?
36 my @h_rcpt=( # case in-sensitive!
43 # ordering matters; first header found is substituted
44 # last header is subsituted if no one is found
51 # FIXME: modularized unification with 'lacemail-accept'
52 # BEGIN lacemail-accept
53 our %muttrc_pending=();
58 $muttrc||="$HOME/.muttrc";
59 $muttrc=~s/^\~/$HOME/;
60 do { warn "Looping muttrc, ignoring: $muttrc"; return (); } if $muttrc_pending{$muttrc};
61 local $muttrc_pending{$muttrc}=1;
63 open MUTTRC,$muttrc or do { warn "open \"$muttrc\": $!"; return (); };
67 # far emulation mutt/init.c/mutt_parse_rc_line()
72 next if !/^(\S+)\s*/s;
75 do { warn "Wrong 'source' parameters at $muttrc:$.: $_"; next; } if !/^\S+$/;
81 close MUTTRC or warn "close \"$muttrc\": $!";
82 return wantarray() ? @r : join("",map("$_\n",@r));
85 my %mutteval_charmap=( # WARNING: Don't use "" or "0" here, see below for "|| warn"!
93 # mutt/init.c/mutt_extract_token()
98 do { warn "Missing trailing quote in: $_"; return $_; } if !s/"$//;
99 s/\\(.)/$mutteval_charmap{$1} || warn "Undefined '\\$1' sequence in: $_";/ges;
107 my @r=map({ (ref $_ ? $_ : qr/^\s*set\s+\Q$_\E\s*=\s*(.*?)\s*$/si); } @headers);
108 my %r=map(($_=>undef()),@r);
112 $r{$ritem}=mutteval $1;
115 for my $var (grep { !defined($r{$_}) } @r) {
116 warn "Variable '$var' not found in muttrc";
119 return wantarray() ? %r : $r{$r[0]};
121 # END lacemail-accept
124 sub sendmail_show { return "\"$sendmail_orig\" ".join(",",map("\"$_\"",@ARGV)); }
126 sub sendmail_orig_exec
128 exec {$sendmail_orig} $0,@ARGV or die "exec(".sendmail_show()."): $!";
132 Getopt::Long::Configure(
136 # FIXME: workaround: 'unknown options' are considered the same as 'arguments'
137 # None of ($REQUIRE_ORDER, $PERMUTE, $RETURN_IN_ORDER) can help us.
138 # No preprocessing possible as it is hard to find option arguments.
146 #my $opt_F; # declared before &FromAddress already
147 my $opt_lacemail_dry_run;
148 my @ARGV_save=@ARGV; # for non-bm mode
149 die if !Getopt::Long::GetOptions(
154 "lacemail-dry-run+",\$opt_lacemail_dry_run,
157 # RedHat sendmail-8.12.5-7/sendmail/main.c/\QDo a quick prescan of the argument list.\E
158 || grep({ File::Basename::basename($0) eq $_; } "newaliases","mailq","smtpd","hoststat","purgestat")
159 # -bm: Deliver mail in the usual way (default).
160 || (defined($opt_b) && $opt_b ne "m")
163 sendmail_orig_exec();
167 # RedHat sendmail-8.9.3-20/src/main.c/main()/\Qif (FullName != NULL)\E
168 # for $opt_F is implemented by Mail::Address in our &FromAddress
170 my $head=Mail::Header->new(\*STDIN);
171 # We may (=will) change the contents and send it multiple times
172 if (defined(my $msgid=$head->get("Message-ID"))) {
173 $head->delete("Message-ID");
174 $head->replace("X-LaceMail-sendmail-Message-ID",$msgid);
176 # options leave in @ARGV, addresses to @addr:
177 my @args=@ARGV; # temporary
179 my @addr=(); # addresses
180 push @{(/^-./ ? \@ARGV : \@addr)},$_ for (@args);
182 for my $addrobj (map({ Mail::Address->parse($_); } map({ ($head->get($_)); } @h_rcpt))) {
183 if (!$addrobj->address()) {
184 # bogus, shouldn't happen
185 warn "->address() not found in \"".$addrobj->format()."\"";
192 # return: Mail::Address instance or undef()
197 return undef() if !defined $line;
198 my @r=Mail::Address->parse($line);
199 warn "Got ".scalar(@r)." addresses while wanting just one; when parsing: $line" if 1!=@r;
210 my $muttrc_From=parseone(scalar muttrc_get("from")); # may get undef()!; parseone() may be redundant
211 $muttrc_From=$muttrc_From->address() if $muttrc_From;
212 $opt_f=undef() if defined($opt_f) && $muttrc_From && lc($opt_f) eq lc($muttrc_From);
215 $from_headername=$_; # leave last item in $from_headername
216 last if @from_val=$head->get($from_headername);
218 @from_val=map({ ($_->address()); } map({ (Mail::Address->parse($_)); } @from_val));
219 $from_headername=undef() if !(1==@from_val && $muttrc_From && lc($from_val[0]) eq lc($muttrc_From));
220 # now $from_headername contains the header name to be replaced w/substituted value
224 my @rcpts=(@addr ? @addr : (undef())); # !defined($rcpt) if we have no recipients
225 my $stdin_body=(@rcpts<=1 ? undef() : do { # store input data only if it will be used multiple times
229 for my $rcpt (@rcpts) {
233 if (defined $rcpt) { # !defined($rcpt) if we have no recipients
236 $rcpt=parseone $rcpt;
237 next if !defined $rcpt;
239 $opt_f=FromAddress($rcpt,1)->address() if !defined $opt_f;
240 $head->replace($from_headername,FromAddress($rcpt,0)->format()) if $from_headername;
243 1; # drop '-bm' if present as it is default anyway
244 1; # drop '-t' if present as we are looping now for it
245 push @ARGV,"-f",$opt_f if defined $opt_f;
246 # we don't handle "Full-Name" header thus pass "-F"
247 # "From/Resent-From" should be handled by our &FromAddress
248 push @ARGV,"-F",$opt_F if defined $opt_F;
249 push @ARGV,$rcpt->address() if defined $rcpt;
251 local $SIG{"PIPE"}=sub { die "Got SIGPIPE from ".sendmail_show(); };
253 if ($opt_lacemail_dry_run) {
254 print sendmail_show()."\n";
258 defined (my $pid=open SENDMAIL,"|-") or die "Cannot fork to spawn ".sendmail_show().": $!";
259 sendmail_orig_exec() if !$pid; # child
261 $head->print(\*SENDMAIL);
262 print "\n"; # Mail::Header->print() eats the empty line but it doesn't print it
263 if (defined($stdin_body)) {
264 print SENDMAIL $stdin_body;
273 next if $opt_lacemail_dry_run; # don't close our STDOUT as it is aliased to *SENDMAIL
274 close SENDMAIL or warn "close(".sendmail_show()."): $?=".join(",",
275 (!WIFEXITED($?) ? () : ("EXITSTATUS(".WEXITSTATUS($?).")")),
276 (!WIFSIGNALED($?) ? () : ("TERMSIG(" .WTERMSIG($?) .")")),
277 (!WIFSTOPPED($?) ? () : ("STOPSIG(" .WSTOPSIG($?) .")")),
279 my $gotcode=(!WIFEXITED($?) ? 99 : WEXITSTATUS($?));
280 $exitcode=$gotcode if $gotcode>$exitcode;