Fixed passthru: -q|-Q are also valid non-delivering options
[PerlMail.git] / perlmail-sendmail
1 #! /usr/bin/perl
2 #
3 # $Id$
4
5 use vars qw($VERSION);
6 $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
7 use strict;
8 use warnings;
9
10 require Getopt::Long;
11 use POSIX qw(WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG WIFSTOPPED WSTOPSIG);
12 require Mail::Header;
13 require Mail::Address;
14 require File::Basename;
15
16 my $sendmail_orig=(-x ($_="/usr/sbin/sendmail-orig") ? $_ : "/usr/sbin/sendmail");
17 my $HOME="/home/lace";
18 my $opt_F;
19 sub FromAddress
20 {
21 my($rcpt,$iserror)=@_;
22
23         return Mail::Address->new(
24                         (defined $opt_F ? $opt_F : "Jan Kratochvil"),
25                         (!$iserror ? 'rcpt' : 'rcpterr')
26                                         .'-'
27                                         .(defined($rcpt->user()) ? $rcpt->user() : "NOUSER")
28                                         .".AT."
29                                         .(defined($rcpt->host()) ? $rcpt->host() : "LOCAL")
30                                         .'@jankratochvil.net',
31                         );
32 }
33
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!
37                 "To",
38                 "Cc",
39                 "Bcc",
40                 "Apparently-To",
41                 );
42
43 # ordering matters; first header found is substituted
44 # last header is subsituted if no one is found
45 my @h_from=(
46                 "Resent-From",
47                 "From",
48                 );
49
50
51 # FIXME: modularized unification with 'lacemail-accept'
52 # BEGIN lacemail-accept
53 our %muttrc_pending=();
54 sub muttrc
55 {
56 my($muttrc)=@_;
57
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;
62         local *MUTTRC;
63         open MUTTRC,$muttrc or do { warn "open \"$muttrc\": $!"; return (); };
64         local $/="\n";
65         local $_;
66         my @r=();
67         # far emulation mutt/init.c/mutt_parse_rc_line()
68         while (<MUTTRC>) {
69                 s/^[\s;]*//s;
70                 s/[#;].*$//s;
71                 s/\s*$//s;
72                 next if !/^(\S+)\s*/s;
73                 if ($1 eq "source") {
74                         $_=$';
75                         do { warn "Wrong 'source' parameters at $muttrc:$.: $_"; next; } if !/^\S+$/;
76                         push @r,muttrc($_);
77                         next;
78                         }
79                 push @r,$_;
80                 }
81         close MUTTRC or warn "close \"$muttrc\": $!";
82         return wantarray() ? @r : join("",map("$_\n",@r));
83 }
84
85 my %mutteval_charmap=(          # WARNING: Don't use "" or "0" here, see below for "|| warn"!
86                 '\\'=>"\\",
87                 'r'=>"\r",
88                 'n'=>"\n",
89                 't'=>"\t",
90                 'f'=>"\f",
91                 'e'=>"\e",
92                 );
93 # mutt/init.c/mutt_extract_token()
94 sub mutteval
95 {
96         local $_=$_[0];
97         return $_ if !s/^"//;
98         do { warn "Missing trailing quote in: $_"; return $_; } if !s/"$//;
99         s/\\(.)/$mutteval_charmap{$1} || warn "Undefined '\\$1' sequence in: $_";/ges;
100         return $_;
101 }
102
103 sub muttrc_get
104 {
105 my(@headers)=@_;
106
107         my @r=map({ (ref $_ ? $_ : qr/^\s*set\s+\Q$_\E\s*=\s*(.*?)\s*$/si); } @headers);
108         my %r=map(($_=>undef()),@r);
109         for (muttrc()) {
110                 for my $ritem (@r) {
111                         /$ritem/si or next;
112                         $r{$ritem}=mutteval $1;
113                         }
114                 }
115         for my $var (grep { !defined($r{$_}) } @r) {
116                 warn "Variable '$var' not found in muttrc";
117                 return undef();
118                 }
119         return wantarray() ? %r : $r{$r[0]};
120 }
121 # END lacemail-accept
122
123
124 sub sendmail_show { return "\"$sendmail_orig\" ".join(",",map("\"$_\"",@ARGV)); }
125
126 sub sendmail_orig_exec
127 {
128         exec {$sendmail_orig} $0,@ARGV or die "exec(".sendmail_show()."): $!";
129         die "NOTREACHED";
130 }
131
132 Getopt::Long::Configure(
133                 "no_ignorecase",
134                 "no_getopt_compat",
135                 "bundling",
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.
139                 "permute",
140                 "pass_through",
141                 );
142
143 my $opt_b;
144 my $opt_Q;
145 my $opt_q;
146 my $opt_t;
147 our $opt_f;
148 #my $opt_F;     # declared before &FromAddress already
149 my $opt_lacemail_dry_run;
150 my @ARGV_save=@ARGV;    # for non-bm mode
151 die if !Getopt::Long::GetOptions(
152                 "b=s"              ,\$opt_b,
153                 "Q:s"              ,\$opt_Q,
154                 "q:s"              ,\$opt_q,
155                 "t"                ,\$opt_t,
156                 "f=s"              ,\$opt_f,
157                 "F=s"              ,\$opt_F,
158                 "lacemail-dry-run+",\$opt_lacemail_dry_run,
159                 );
160 if (0
161                 # RedHat sendmail-8.12.5-7/sendmail/main.c/\QDo a quick prescan of the argument list.\E
162                 || grep({ File::Basename::basename($0) eq $_; } "newaliases","mailq","smtpd","hoststat","purgestat")
163                 # -bm: Deliver mail in the usual way (default).
164                 || (defined($opt_b) && $opt_b ne "m")
165                 || defined $opt_q       # MD_QUEUERUN
166                 || defined $opt_Q       # MD_QUEUERUN
167                 ) {
168         @ARGV=@ARGV_save;
169         sendmail_orig_exec();
170         die "NOTREACHED";
171         }
172
173 # RedHat sendmail-8.9.3-20/src/main.c/main()/\Qif (FullName != NULL)\E
174 #   for $opt_F is implemented by Mail::Address in our &FromAddress
175
176 my $head=Mail::Header->new(\*STDIN);
177 # We may (=will) change the contents and send it multiple times
178 if (defined(my $msgid=$head->get("Message-ID"))) {
179         $head->delete("Message-ID");
180         $head->replace("X-LaceMail-sendmail-Message-ID",$msgid);
181         }
182 # options leave in @ARGV, addresses to @addr:
183 my @args=@ARGV; # temporary
184 @ARGV=();       # options
185 my @addr=();    # addresses
186 push @{(/^-./ ? \@ARGV : \@addr)},$_ for (@args);
187 if ($opt_t) {
188         for my $addrobj (map({ Mail::Address->parse($_); } map({ ($head->get($_)); } @h_rcpt))) {
189                 if (!$addrobj->address()) {
190                         # bogus, shouldn't happen
191                         warn "->address() not found in \"".$addrobj->format()."\"";
192                         next;
193                 }
194                 push @addr,$addrobj;
195                 }
196         }
197
198 # return: Mail::Address instance or undef()
199 sub parseone
200 {
201 my($line)=@_;
202
203         return undef() if !defined $line;
204         my @r=Mail::Address->parse($line);
205         warn "Got ".scalar(@r)." addresses while wanting just one; when parsing: $line" if 1!=@r;
206         return $r[0];
207 }
208
209 sub matches
210 {
211         return 
212 }
213
214 my $from_headername;
215 {
216         my $muttrc_From=parseone(scalar muttrc_get("from"));    # may get undef()!; parseone() may be redundant
217         $muttrc_From=$muttrc_From->address() if $muttrc_From;
218         $opt_f=undef() if defined($opt_f) && $muttrc_From && lc($opt_f) eq lc($muttrc_From);
219         my @from_val;
220         for (@h_from) {
221                 $from_headername=$_;    # leave last item in $from_headername
222                 last if @from_val=$head->get($from_headername);
223                 }
224         @from_val=map({ ($_->address()); } map({ (Mail::Address->parse($_)); } @from_val));
225         $from_headername=undef() if !(1==@from_val && $muttrc_From && lc($from_val[0]) eq lc($muttrc_From));
226         # now $from_headername contains the header name to be replaced w/substituted value
227         }
228
229 my $exitcode=0;
230 my @rcpts=(@addr ? @addr : (undef()));  # !defined($rcpt) if we have no recipients
231 my $stdin_body=(@rcpts<=1 ? undef() : do {      # store input data only if it will be used multiple times
232                 local $/=undef();
233                 <STDIN>;
234                 });
235 for my $rcpt (@rcpts) {
236         local @ARGV=@ARGV;
237         local $opt_f=$opt_f;
238
239         if (defined $rcpt) {    # !defined($rcpt) if we have no recipients
240                 local $_;
241                 if (!ref $rcpt) {
242                         $rcpt=parseone $rcpt;
243                         next if !defined $rcpt;
244                         }
245                 $opt_f=FromAddress($rcpt,1)->address() if !defined $opt_f;
246                 $head->replace($from_headername,FromAddress($rcpt,0)->format()) if $from_headername;
247                 }
248
249         1;      # drop '-bm' if present as it is default anyway
250         1;      # drop '-t' if present as we are looping now for it
251         push @ARGV,"-f",$opt_f if defined $opt_f;
252         # we don't handle "Full-Name" header thus pass "-F"
253         # "From/Resent-From" should be handled by our &FromAddress
254         push @ARGV,"-F",$opt_F if defined $opt_F;
255         push @ARGV,$rcpt->address() if defined $rcpt;
256
257         local $SIG{"PIPE"}=sub { die "Got SIGPIPE from ".sendmail_show(); };
258         local *SENDMAIL;
259         if ($opt_lacemail_dry_run) {
260                 print sendmail_show()."\n";
261                 *SENDMAIL=\*STDOUT;
262                 }
263         else {
264                 defined (my $pid=open SENDMAIL,"|-") or die "Cannot fork to spawn ".sendmail_show().": $!";
265                 sendmail_orig_exec() if !$pid; # child
266                 }
267         $head->print(\*SENDMAIL);
268         print "\n";     # Mail::Header->print() eats the empty line but it doesn't print it
269         if (defined($stdin_body)) {
270                 print SENDMAIL $stdin_body;
271                 }
272         else {
273                 local $_;
274                 while (<STDIN>) {
275                         print SENDMAIL $_;
276                         }
277                 }
278
279         next if $opt_lacemail_dry_run;  # don't close our STDOUT as it is aliased to *SENDMAIL
280         close SENDMAIL or warn "close(".sendmail_show()."): $?=".join(",",
281                         (!WIFEXITED($?)   ? () : ("EXITSTATUS(".WEXITSTATUS($?).")")),
282                         (!WIFSIGNALED($?) ? () : ("TERMSIG("   .WTERMSIG($?)   .")")),
283                         (!WIFSTOPPED($?)  ? () : ("STOPSIG("   .WSTOPSIG($?)   .")")),
284                         );
285         my $gotcode=(!WIFEXITED($?) ? 99 : WEXITSTATUS($?));
286         $exitcode=$gotcode if $gotcode>$exitcode;
287         }
288 exit $exitcode;