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);
12 require MIME::Head; # inherits Mail::Header
13 require Mail::Address;
14 require File::Basename;
17 my $sendmail_orig=(-x ($_="/usr/sbin/sendmail-orig") ? $_ : "/usr/sbin/sendmail");
18 my $HOME="/home/short";
19 # Mail-Alias-1.12 defaults to "/etc/mail/aliases" which does not exist on RedHat sendmail-8.12.5-7
20 # Mail-Alias-1.12 will clutter $_ !
21 my @addr_addon=(Mail::Alias->new("/etc/aliases")->exists("sentout") ? ("sentout") : ());
26 my($rcpt,$iserror)=@_;
28 my $phrase=(defined $opt_F ? $opt_F : "Jan Kratochvil");
34 my $filename="$HOME/.gnupg/options";
35 open F,$filename or do { warn "Open \"$filename\": $!"; last; };
37 my @keys=map((/^\s*default-key\s+(\S+)\s*$/),<F>);
38 @keys==1 or do { warn "Found ".scalar(@keys)." 'default-key's in your \"$filename\", ignoring"; last; };
39 close F or warn "Close \"$filename\": $!";
40 my $default_key=$keys[0];
41 $default_key=~/^[[:xdigit:]]{8}$/ or do { warn "Invalid 'default-key', ignoring: $default_key"; last; };
42 return Mail::Address->new(
44 'pgp-'.uc($default_key).'@jankratochvil.net',
47 # !$is_pgp or fallback
48 return Mail::Address->new(
50 (!$iserror ? 'rcpt' : 'rcpterr')
52 .(defined($rcpt->user()) ? $rcpt->user() : "NOUSER")
54 .(defined($rcpt->host()) ? $rcpt->host() : "LOCAL")
55 .'@jankratochvil.net',
59 # RedHat sendmail-8.9.3-20/src/conf.c/HdrInfo[]/\Q/* destination fields */\E
60 # FIXME: Recognize "Resent-$_" headers for -t but when we are in 'resent' mode?
61 my @h_rcpt=( # case in-sensitive!
68 # ordering matters; first header found is substituted
69 # last header is subsituted if no one is found
76 # FIXME: modularized unification with 'lacemail-accept'
77 # BEGIN lacemail-accept
78 our %muttrc_pending=();
83 $muttrc||="$HOME/.muttrc";
84 $muttrc=~s/^\~/$HOME/;
85 do { warn "Looping muttrc, ignoring: $muttrc"; return (); } if $muttrc_pending{$muttrc};
86 local $muttrc_pending{$muttrc}=1;
88 open MUTTRC,$muttrc or do { warn "open \"$muttrc\": $!"; return (); };
92 # far emulation mutt/init.c/mutt_parse_rc_line()
97 next if !/^(\S+)\s*/s;
100 do { warn "Wrong 'source' parameters at $muttrc:$.: $_"; next; } if !/^\S+$/;
106 close MUTTRC or warn "close \"$muttrc\": $!";
107 return wantarray() ? @r : join("",map("$_\n",@r));
110 my %mutteval_charmap=( # WARNING: Don't use "" or "0" here, see below for "|| warn"!
118 # mutt/init.c/mutt_extract_token()
122 return $_ if !s/^"//;
123 do { warn "Missing trailing quote in: $_"; return $_; } if !s/"$//;
124 s/\\(.)/$mutteval_charmap{$1} || warn "Undefined '\\$1' sequence in: $_";/ges;
132 my @r=map({ (ref $_ ? $_ : qr/^\s*set\s+\Q$_\E\s*=\s*(.*?)\s*$/si); } @headers);
133 my %r=map(($_=>undef()),@r);
137 $r{$ritem}=mutteval $1;
140 for my $var (grep { !defined($r{$_}) } @r) {
141 warn "Variable '$var' not found in muttrc";
144 return wantarray() ? %r : $r{$r[0]};
146 # END lacemail-accept
149 sub sendmail_show { return "\"$sendmail_orig\" ".join(",",map("\"$_\"",@ARGV)); }
151 sub sendmail_orig_exec
153 exec {$sendmail_orig} $0,@ARGV or die "exec(".sendmail_show()."): $!";
157 Getopt::Long::Configure(
161 # FIXME: workaround: 'unknown options' are considered the same as 'arguments'
162 # None of ($REQUIRE_ORDER, $PERMUTE, $RETURN_IN_ORDER) can help us.
163 # No preprocessing possible as it is hard to find option arguments.
173 #my $opt_F; # declared before &FromAddress already
174 my $opt_lacemail_dry_run;
175 my @ARGV_save=@ARGV; # for non-bm mode
176 die if !Getopt::Long::GetOptions(
183 "lacemail-dry-run+",\$opt_lacemail_dry_run,
186 # RedHat sendmail-8.12.5-7/sendmail/main.c/\QDo a quick prescan of the argument list.\E
187 || grep({ File::Basename::basename($0) eq $_; } "newaliases","mailq","smtpd","hoststat","purgestat")
188 # -bm: Deliver mail in the usual way (default).
189 || (defined($opt_b) && $opt_b ne "m")
190 || defined $opt_q # MD_QUEUERUN
191 || defined $opt_Q # MD_QUEUERUN
194 sendmail_orig_exec();
198 # RedHat sendmail-8.9.3-20/src/main.c/main()/\Qif (FullName != NULL)\E
199 # for $opt_F is implemented by Mail::Address in our &FromAddress
201 my $head=MIME::Head->new(\*STDIN);
202 # We may (=will) change the contents and send it multiple times
203 if (defined(my $msgid=$head->get("Message-ID"))) {
204 $head->delete("Message-ID");
205 $head->replace("X-LaceMail-sendmail-Message-ID",$msgid);
207 # options leave in @ARGV, addresses to @addr:
208 my @args=@ARGV; # temporary
210 my @addr=(); # addresses
211 push @{(/^-./ ? \@ARGV : \@addr)},$_ for (@args);
213 for my $addrobj (map({ Mail::Address->parse($_); } map({ ($head->get($_)); } @h_rcpt))) {
214 if (!$addrobj->address()) {
215 # bogus, shouldn't happen
216 warn "->address() not found in \"".$addrobj->format()."\"";
223 # return: Mail::Address instance or undef()
228 return undef() if !defined $line;
229 my @r=Mail::Address->parse($line);
230 warn "Got ".scalar(@r)." addresses while wanting just one; when parsing: $line" if 1!=@r;
241 my $muttrc_From=parseone(scalar muttrc_get("from")); # may get undef()!; parseone() may be redundant
242 $muttrc_From=$muttrc_From->address() if $muttrc_From;
243 $opt_f=undef() if defined($opt_f) && $muttrc_From && lc($opt_f) eq lc($muttrc_From);
245 $from_headername=$_; # leave last item in $from_headername
246 next if !(my @from_val=$head->get($from_headername));
247 @from_val=map({ ($_->address()); } map({ (Mail::Address->parse($_)); } @from_val));
248 $from_headername=undef() if !(1==@from_val && $muttrc_From && lc($from_val[0]) eq lc($muttrc_From));
250 } # fallthru with $from_headername remaining set if last headername did not exist
251 # now $from_headername contains the header name to be replaced w/substituted value
254 # to be utilized later by &FromAddress
256 && do { local $_=$head->mime_attr("Content-Type"); $_ && ~m#^multipart/(?:signed|encrypted)$#; }
257 && do { local $_=$head->mime_attr("Content-Type.protocol"); $_ && ~m#^application/pgp\b#; }
261 # !defined($rcpt) if we have no recipients
262 # make the list unique to prevent dupes being normally filtered by sendmail(8)
263 # one '{' is block-wrapper, another '{' is hash-indirection!
264 # hash keys are just strings, never refs!
265 # unify the list as Mail::Address instances
266 my @rcpts=(!@addr ? (undef()) : values(%{{ map({
268 $obj=parseone $obj if !ref $obj;
269 (!defined $obj ? () : (lc($obj->address())=>$obj));
272 my $stdin_body=(@rcpts<=1 ? undef() : do { # store input data only if it will be used multiple times
276 for my $rcpt (@rcpts) {
280 if (defined $rcpt) { # !defined($rcpt) if we have no recipients
282 $opt_f=FromAddress($rcpt,1)->address() if !defined $opt_f;
283 $head->replace($from_headername,FromAddress($rcpt,0)->format()) if $from_headername;
286 1; # drop '-bm' if present as it is default anyway
287 1; # drop '-t' if present as we are looping now for it
288 push @ARGV,"-f",$opt_f if defined $opt_f;
289 # we don't handle "Full-Name" header thus pass "-F"
290 # "From/Resent-From" should be handled by our &FromAddress
291 push @ARGV,"-F",$opt_F if defined $opt_F;
292 push @ARGV,$rcpt->address() if defined $rcpt;
293 push @ARGV,@addr_addon;
295 local $SIG{"PIPE"}=sub { die "Got SIGPIPE from ".sendmail_show(); };
297 if ($opt_lacemail_dry_run) {
298 print sendmail_show()."\n";
302 defined (my $pid=open SENDMAIL,"|-") or die "Cannot fork to spawn ".sendmail_show().": $!";
303 sendmail_orig_exec() if !$pid; # child
305 $head->print(\*SENDMAIL);
306 print SENDMAIL "\n"; # MIME::Head->print() eats the empty line but it doesn't print it
307 if (defined($stdin_body)) {
308 print SENDMAIL $stdin_body;
317 next if $opt_lacemail_dry_run; # don't close our STDOUT as it is aliased to *SENDMAIL
318 close SENDMAIL or warn "close(".sendmail_show()."): $?=".join(",",
319 (!WIFEXITED($?) ? () : ("EXITSTATUS(".WEXITSTATUS($?).")")),
320 (!WIFSIGNALED($?) ? () : ("TERMSIG(" .WTERMSIG($?) .")")),
321 (!WIFSTOPPED($?) ? () : ("STOPSIG(" .WSTOPSIG($?) .")")),
323 my $gotcode=(!WIFEXITED($?) ? 99 : WEXITSTATUS($?));
324 $exitcode=$gotcode if $gotcode>$exitcode;