6 $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
13 use POSIX qw(WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG WIFSTOPPED WSTOPSIG);
14 require MIME::Head; # inherits Mail::Header
15 require Mail::Address;
16 require File::Basename;
20 # FIXME: modularized unification with 'perlmail-accept'
21 # BEGIN perlmail-accept
22 our %muttrc_pending=();
27 $muttrc||="$HOME/.muttrc";
28 $muttrc=~s/^\~/$HOME/;
29 do { warn "Looping muttrc, ignoring: $muttrc"; return (); } if $muttrc_pending{$muttrc};
30 local $muttrc_pending{$muttrc}=1;
32 open MUTTRC,$muttrc or do { warn "open \"$muttrc\": $!"; return (); };
36 # far emulation mutt/init.c/mutt_parse_rc_line()
41 next if !/^(\S+)\s*/s;
44 do { warn "Wrong 'source' parameters at $muttrc:$.: $_"; next; } if !/^\S+$/;
50 close MUTTRC or warn "close \"$muttrc\": $!";
51 return wantarray() ? @r : join("",map("$_\n",@r));
54 my %mutteval_charmap=( # WARNING: Don't use "" or "0" here, see below for "|| warn"!
62 # mutt/init.c/mutt_extract_token()
67 do { warn "Missing trailing quote in: $_"; return $_; } if !s/"$//;
68 s/\\(.)/$mutteval_charmap{$1} || warn "Undefined '\\$1' sequence in: $_";/ges;
76 my @r=map({ (ref $_ ? $_ : qr/^\s*set\s+\Q$_\E\s*=\s*(.*?)\s*$/si); } @headers);
77 my %r=map(($_=>undef()),@r);
81 $r{$ritem}=mutteval $1;
84 for my $var (grep { !defined($r{$_}) } @r) {
85 warn "Variable '$var' not found in muttrc";
88 return wantarray() ? %r : $r{$r[0]};
93 sub sendmail_show { return "\"$sendmail_orig\" ".join(",",map("\"$_\"",@ARGV)); }
95 sub sendmail_orig_exec
97 exec {$sendmail_orig} $0,@ARGV or die "exec(".sendmail_show()."): $!";
101 Getopt::Long::Configure(
105 # FIXME: workaround: 'unknown options' are considered the same as 'arguments'
106 # None of ($REQUIRE_ORDER, $PERMUTE, $RETURN_IN_ORDER) can help us.
107 # No preprocessing possible as it is hard to find option arguments.
117 our $opt_F; # from PerlMail::Config;
118 my $opt_perlmail_dry_run;
119 my @ARGV_save=@ARGV; # for non-bm mode
120 die if !Getopt::Long::GetOptions(
127 "perlmail-dry-run+",\$opt_perlmail_dry_run,
130 # RedHat sendmail-8.12.5-7/sendmail/main.c/\QDo a quick prescan of the argument list.\E
131 || grep({ File::Basename::basename($0) eq $_; } "newaliases","mailq","smtpd","hoststat","purgestat")
132 # -bm: Deliver mail in the usual way (default).
133 || (defined($opt_b) && $opt_b ne "m")
134 || defined $opt_q # MD_QUEUERUN
135 || defined $opt_Q # MD_QUEUERUN
138 sendmail_orig_exec();
142 # RedHat sendmail-8.9.3-20/src/main.c/main()/\Qif (FullName != NULL)\E
143 # for $opt_F is implemented by Mail::Address in our &FromAddress
145 my $head=MIME::Head->new(\*STDIN);
146 # options leave in @ARGV, addresses to @addr:
147 my @args=@ARGV; # temporary
149 my @addr=(); # addresses
150 push @{(/^-./ ? \@ARGV : \@addr)},$_ for (@args);
152 for my $addrobj (map({ Mail::Address->parse($_); } map({ ($head->get($_)); } @h_rcpt))) {
153 if (!$addrobj->address()) {
154 # bogus, shouldn't happen
155 warn "->address() not found in \"".$addrobj->format()."\"";
162 # return: Mail::Address instance or undef()
167 return undef() if !defined $line;
168 my @r=Mail::Address->parse($line);
169 warn "Got ".scalar(@r)." addresses while wanting just one; when parsing: $line" if 1!=@r;
180 my $muttrc_From=parseone(scalar muttrc_get("from")); # may get undef()!; parseone() may be redundant
181 $muttrc_From=$muttrc_From->address() if $muttrc_From;
182 $opt_f=undef() if defined($opt_f) && $muttrc_From && lc($opt_f) eq lc($muttrc_From);
184 $from_headername=$_; # leave last item in $from_headername
185 next if !(my @from_val=$head->get($from_headername));
186 @from_val=map({ ($_->address()); } map({ (Mail::Address->parse($_)); } @from_val));
187 $from_headername=undef() if !(1==@from_val && $muttrc_From && lc($from_val[0]) eq lc($muttrc_From));
189 } # fallthru with $from_headername remaining set if last headername did not exist
190 # now $from_headername contains the header name to be replaced w/substituted value
193 # to be utilized later by &FromAddress
194 our $is_pgp; # from PerlMail::Config;
196 && do { local $_=$head->mime_attr("Content-Type"); $_ && ~m#^multipart/(?:signed|encrypted)$#; }
197 && do { local $_=$head->mime_attr("Content-Type.protocol"); $_ && ~m#^application/pgp\b#; }
201 # !defined($rcpt) if we have no recipients
202 # make the list unique to prevent dupes being normally filtered by sendmail(8)
203 # one '{' is block-wrapper, another '{' is hash-indirection!
204 # hash keys are just strings, never refs!
205 # unify the list as Mail::Address instances
206 my @rcpts=(!@addr ? (undef()) : values(%{{ map({
208 $obj=parseone $obj if !ref $obj;
209 (!defined $obj ? () : (lc($obj->address())=>$obj));
212 my $stdin_body=(@rcpts<=1 ? undef() : do { # store input data only if it will be used multiple times
216 for my $rcpt (@rcpts) {
220 if (defined $rcpt) { # !defined($rcpt) if we have no recipients
222 $opt_f=FromAddress($rcpt,1)->address() if !defined $opt_f;
223 $head->replace($from_headername,FromAddress($rcpt,0)->format()) if $from_headername;
226 1; # drop '-bm' if present as it is default anyway
227 1; # drop '-t' if present as we are looping now for it
228 push @ARGV,"-f",$opt_f if defined $opt_f;
229 # we don't handle "Full-Name" header thus pass "-F"
230 # "From/Resent-From" should be handled by our &FromAddress
231 push @ARGV,"-F",$opt_F if defined $opt_F;
232 push @ARGV,$rcpt->address() if defined $rcpt;
233 push @ARGV,@addr_addon;
235 local $SIG{"PIPE"}=sub { die "Got SIGPIPE from ".sendmail_show(); };
237 if ($opt_perlmail_dry_run) {
238 print sendmail_show()."\n";
242 defined (my $pid=open SENDMAIL,"|-") or die "Cannot fork to spawn ".sendmail_show().": $!";
243 sendmail_orig_exec() if !$pid; # child
245 $head->print(\*SENDMAIL);
246 print SENDMAIL "\n"; # MIME::Head->print() eats the empty line but it doesn't print it
247 if (defined($stdin_body)) {
248 print SENDMAIL $stdin_body;
257 next if $opt_perlmail_dry_run; # don't close our STDOUT as it is aliased to *SENDMAIL
258 close SENDMAIL or warn "close(".sendmail_show()."): $?=".join(",",
259 (!WIFEXITED($?) ? () : ("EXITSTATUS(".WEXITSTATUS($?).")")),
260 (!WIFSIGNALED($?) ? () : ("TERMSIG(" .WTERMSIG($?) .")")),
261 (!WIFSTOPPED($?) ? () : ("STOPSIG(" .WSTOPSIG($?) .")")),
263 my $gotcode=(!WIFEXITED($?) ? 99 : WEXITSTATUS($?));
264 $exitcode=$gotcode if $gotcode>$exitcode;