Remove dependency on 'prog-mutt' From username.
[PerlMail.git] / perlmail-sendmail
1 #! /usr/bin/perl
2
3 #       $Id$
4 # Copyright (C) 2002-2003 Jan Kratochvil <project-PerlMail@jankratochvil.net>
5
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19
20
21 use vars qw($VERSION);
22 $VERSION=do { my @r=(q$Revision$=~/\d+/g); sprintf "%d.".("%03d"x$#r),@r; };
23 use strict;
24 use warnings;
25
26 use File::Basename;
27 BEGIN {
28         use lib $ENV{"PERLMAIL_BASEDIR"} || File::Basename::dirname($0);
29         use PerlMail::Config;
30         use PerlMail::Lib;
31         }
32
33 require Getopt::Long;
34 use POSIX qw(WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG WIFSTOPPED WSTOPSIG);
35 require MIME::Head;     # inherits Mail::Header
36 require Mail::Address;
37
38
39 sub sendmail_show { return "\"$sendmail_orig\" ".join(",",map("\"$_\"",@ARGV)); }
40
41 sub sendmail_orig_exec
42 {
43         exec {$sendmail_orig} $0,@ARGV or die "exec(".sendmail_show()."): $!";
44         die "NOTREACHED";
45 }
46
47 Getopt::Long::Configure(
48                 "no_ignorecase",
49                 "no_getopt_compat",
50                 "bundling",
51                 # FIXME: workaround: 'unknown options' are considered the same as 'arguments'
52                 # None of ($REQUIRE_ORDER, $PERMUTE, $RETURN_IN_ORDER) can help us.
53                 # No preprocessing possible as it is hard to find option arguments.
54                 "permute",
55                 "pass_through",
56                 );
57
58 my $opt_b;
59 my $opt_Q;
60 my $opt_q;
61 my $opt_t;
62 our $opt_f;     # not exported, just for local()
63 my $opt_perlmail_dry_run;
64 my @ARGV_save=@ARGV;    # for non-bm mode
65 die if !Getopt::Long::GetOptions(
66                 "b=s"              ,\$opt_b,
67                 "Q:s"              ,\$opt_Q,
68                 "q:s"              ,\$opt_q,
69                 "t"                ,\$opt_t,
70                 "f=s"              ,\$opt_f,
71                 "F=s"              ,\$opt_F,
72                 "perlmail-dry-run+",\$opt_perlmail_dry_run,
73                 );
74 if (0
75                 # RedHat sendmail-8.12.5-7/sendmail/main.c/\QDo a quick prescan of the argument list.\E
76                 || grep({ File::Basename::basename($0) eq $_; } "newaliases","mailq","smtpd","hoststat","purgestat")
77                 # -bm: Deliver mail in the usual way (default).
78                 || (defined($opt_b) && $opt_b ne "m")
79                 || defined $opt_q       # MD_QUEUERUN
80                 || defined $opt_Q       # MD_QUEUERUN
81                 ) {
82         @ARGV=@ARGV_save;
83         sendmail_orig_exec();
84         die "NOTREACHED";
85         }
86
87 # RedHat sendmail-8.9.3-20/src/main.c/main()/\Qif (FullName != NULL)\E
88 #   for $opt_F is implemented by Mail::Address in our &FromAddress
89
90 my $head=MIME::Head->new(\*STDIN);
91 # options leave in @ARGV, addresses to @addr:
92 my @args=@ARGV; # temporary
93 @ARGV=();       # options
94 my @addr=();    # addresses
95 push @{(/^-./ ? \@ARGV : \@addr)},$_ for (@args);
96 if ($opt_t) {
97         for my $addrobj (map({ Mail::Address->parse($_); } map({ ($head->get($_)); } @h_rcpt))) {
98                 if (!$addrobj->address()) {
99                         # bogus, shouldn't happen
100                         warn "->address() not found in \"".$addrobj->format()."\"";
101                         next;
102                 }
103                 push @addr,$addrobj;
104                 }
105         }
106
107 sub matches
108 {
109         return 
110 }
111
112 my $from_headername;
113 {
114         my $muttrc_From=parseone(scalar muttrc_get("from"));    # may get undef()!; parseone() may be redundant
115         $muttrc_From=$muttrc_From->address() if $muttrc_From;
116         $opt_f=undef() if defined($opt_f) && $muttrc_From && lc($opt_f) eq lc($muttrc_From);
117         for (@h_from) {
118                 $from_headername=$_;    # leave last item in $from_headername
119                 next if !(my @from_val=$head->get($from_headername));
120                 @from_val=map({ ($_->address()); } map({ (Mail::Address->parse($_)); } @from_val));
121                 $from_headername=undef() if !(1==@from_val && $muttrc_From && lc($from_val[0]) eq lc($muttrc_From));
122                 last;
123                 }       # fallthru with $from_headername remaining set if last headername did not exist
124         # now $from_headername contains the header name to be replaced w/substituted value
125         }
126
127 # to be utilized later by &FromAddress
128 $is_pgp=(1
129                 && do { local $_=$head->mime_attr("Content-Type");          $_ && ~m#^multipart/(?:signed|encrypted)$#; }
130                 && do { local $_=$head->mime_attr("Content-Type.protocol"); $_ && ~m#^application/pgp\b#; }
131                 );
132
133 my $exitcode=0;
134 # !defined($rcpt) if we have no recipients
135 # make the list unique to prevent dupes being normally filtered by sendmail(8)
136 # one '{' is block-wrapper, another '{' is hash-indirection!
137 # hash keys are just strings, never refs!
138 # unify the list as Mail::Address instances
139 my @rcpts=(!@addr ? (undef()) : values(%{{ map({
140                 my $obj=$_;
141                 $obj=parseone $obj if !ref $obj;
142                 (!defined $obj ? () : (lc($obj->address())=>$obj));
143                 } @addr) }}));
144
145 my $stdin_body=(@rcpts<=1 ? undef() : do {      # store input data only if it will be used multiple times
146                 local $/=undef();
147                 <STDIN>;
148                 });
149 for my $rcpt (@rcpts) {
150         local @ARGV=@ARGV;
151         local $opt_f=$opt_f;
152
153         if (defined $rcpt) {    # !defined($rcpt) if we have no recipients
154                 local $_;
155                 $opt_f=FromAddress($rcpt,1)->address() if !defined $opt_f;
156                 if ($from_headername) {
157                         if (my $fromaddr=FromAddress($rcpt,0)->format()) {
158                                 $head->replace($from_headername,$fromaddr);
159                                 }
160                         }
161                 }
162
163         1;      # drop '-bm' if present as it is default anyway
164         1;      # drop '-t' if present as we are looping now for it
165         push @ARGV,"-f",$opt_f if defined $opt_f;
166         # we don't handle "Full-Name" header thus pass "-F"
167         # "From/Resent-From" should be handled by our &FromAddress
168         push @ARGV,"-F",$opt_F if defined $opt_F;
169         push @ARGV,$rcpt->address() if defined $rcpt;
170         push @ARGV,@addr_addon;
171
172         local $SIG{"PIPE"}=sub { die "Got SIGPIPE from ".sendmail_show(); };
173         local *SENDMAIL;
174         if ($opt_perlmail_dry_run) {
175                 print sendmail_show()."\n";
176                 *SENDMAIL=\*STDOUT;
177                 }
178         else {
179                 defined (my $pid=open SENDMAIL,"|-") or die "Cannot fork to spawn ".sendmail_show().": $!";
180                 sendmail_orig_exec() if !$pid; # child
181                 }
182         $head->print(\*SENDMAIL);
183         print SENDMAIL "\n";    # MIME::Head->print() eats the empty line but it doesn't print it
184         if (defined($stdin_body)) {
185                 print SENDMAIL $stdin_body;
186                 }
187         else {
188                 local $_;
189                 while (<STDIN>) {
190                         print SENDMAIL $_;
191                         }
192                 }
193
194         next if $opt_perlmail_dry_run;  # don't close our STDOUT as it is aliased to *SENDMAIL
195         close SENDMAIL or warn "close(".sendmail_show()."): $?=".join(",",
196                         (!WIFEXITED($?)   ? () : ("EXITSTATUS(".WEXITSTATUS($?).")")),
197                         (!WIFSIGNALED($?) ? () : ("TERMSIG("   .WTERMSIG($?)   .")")),
198                         (!WIFSTOPPED($?)  ? () : ("STOPSIG("   .WSTOPSIG($?)   .")")),
199                         );
200         my $gotcode=(!WIFEXITED($?) ? 99 : WEXITSTATUS($?));
201         $exitcode=$gotcode if $gotcode>$exitcode;
202         }
203 exit $exitcode;