1# $Id$
2#
3#  Copyright (c) 1990-2006, Raphael Manfredi
4#
5#  You may redistribute only under the terms of the Artistic License,
6#  as specified in the README file that comes with the distribution.
7#  You may reuse parts of this distribution only within the terms of
8#  that same Artistic License; a copy of which may be found at the root
9#  of the source tree for mailagent 3.0.
10#
11# $Log
12
13#
14# fakesend		-- new mailagent command
15#
16# Resend message as if it were being sent locally as a brand new message,
17# removing all traces of origin from the one we got.
18#
19# This comannd parses the first few lines of the body as a new header, that
20# should contain at least a To: line, but that can contain also a Cc: and
21# other header information. The only information kept from the original
22# messages are Subject, Date, References and In-Reply-To.
23#
24# A new message is built and resent appropriately by letting sendmail
25# parse the new To: and Cc: fields.
26#
27sub fakesend {
28	my ($cmd_line) = @_;
29	my @body = split(/\n/, $header{'Body'});
30	my $x;
31	my %nhead;						# New header
32	my $last_header;
33	local $_;
34	my $cont = ' ' x 4;
35	while (defined ($_ = shift(@body))) {
36		last if /^\s*$/ || /^-+$/;	# End of new header (blank or --- line)
37		if (/^\s/) {				# Continuation line
38			s/^\s+/ /;
39			$nhead{$last_header} .= "\n$cont$_" if $last_header ne '';
40		} elsif (/^([\w-]+):\s*(.*)/) {
41			my $value = $2;
42			$last_header = header::normalize($1);
43			if ($nhead{$last_header} ne '') {
44				$nhead{$last_header} .= "\n$cont$value";
45			} else {
46				$nhead{$last_header} .= $value;
47			}
48		}
49	}
50	unless ($nhead{'To'} || $nhead{'Cc'}) {
51		&'add_log("FAKESEND found no To nor Cc line in new header");
52		return 1;	# Failed
53	}
54	local *MAILER;
55	unless (open(MAILER, "| /usr/lib/sendmail -t")) {
56		&'add_log("ERROR cannot launch sendmail: $!") if $'loglvl;
57		return 1;	# Failed
58	}
59	# Fake a from from ~/.mailagent, unless there was an extra From already
60	print MAILER "From: $cf'name <$cf'email>\n" unless defined $nhead{'From'};
61
62	# Propage old fields from original message
63	foreach my $field (qw(Subject Date References In-Reply-To)) {
64		next if $header{$field} eq '';
65		print MAILER "$field: ", $header{$field}, "\n";
66	}
67	# Add all fields from new header
68	foreach my $field (keys %nhead) {
69		next if $nhead{$field} eq '';
70		print MAILER "$field: ", $nhead{$field}, "\n";
71	}
72	print MAILER "\n";				# EOH
73	foreach my $body (@body) {
74		print MAILER $body, "\n";
75	}
76	&'add_log("FAKESEND To: $nhead{'To'}") if $'loglvl > 5 && $nhead{'To'};
77	&'add_log("FAKESEND Cc: $nhead{'Cc'}") if $'loglvl > 5 && $nhead{'Cc'};
78	close MAILER;
79	my $status = $?;
80	&'add_log("ERROR while closing sendmail: status = $status")
81		if $status && $'loglvl;
82	return $status != 0;			# Failure status
83}
84
85