summaryrefslogtreecommitdiffstats
path: root/scripts/edmail.in
blob: 24834c99cd0c6089d46e30d533ea5df6521f18f5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#! @PERL@ -w

use Getopt::Long;
use POSIX qw(setlocale);
use Locale::gettext;
use strict;

setlocale(LC_MESSAGES, "");
bindtextdomain("quilt", "@LOCALEDIR@");
textdomain("quilt");

sub _($) {
    return gettext(shift);
}

my (%append_name, %append_value, $remove_empty_headers, %remove_header,
    %extract_recipients, %replace_name, %replace_value, $charset);
GetOptions('add-recipient:s%' =>
    sub {
	$append_name{lc $_[1]} = $_[1];
	$append_value{lc $_[1]} .= ",\n " . $_[2];
    },
    'remove-header:s' => sub { $remove_header{lc $_[1]}++ },
    'remove-empty-headers' => \$remove_empty_headers,
    'replace-header:s%' =>
    sub {
	$replace_name{lc $_[1]} = $_[1];
	$replace_value{lc $_[1]} = $_[2];
    },
    'extract-recipients:s' => sub { $extract_recipients{lc $_[1]} = 1 },
    'charset' => \$charset)
    or exit 1;
my %recipient_headers = map {lc $_ => 1} (@ARGV, keys %append_name);

# Email address formats understood:
#    Andreas Gruenbacher <agruen@suse.de>
#    "Andreas G." <agruen@suse.de>
#    agruen@suse.de (Andreas Gruenbacher)
#    agruen@suse.de
#    agruen@[suse.de]
#
# Not understood (needs proper encoding):
#    Andreas Grünbacher <agruen@suse.de>

sub check_recipient($) {
    my ($recipient) = @_;
    my ($display, $deliver);
    local $_ = $recipient;
    my $spl = '()<>\[\]:;@\\,"';  # special characters
    my $spldot = "$spl.";  # special characters + dot

    # FIXME: Take a character set option and if set, encode invalid
    #	     characters in atoms: =?iso-8859-1?q?Gr=FCnbacher?=

    if (($display, $deliver) = /^(.*?)\s*<(.+)>$/ or
	($deliver, $display) = /^(\S*)(\s*\(.*\))$/) {
	$_ = $display;
	if (/^"((?:[^"\\]|\\[^\n\r])*)"/) {
	    $display = $1;
	} else {
	    # The value is not (properly) quoted. Check for invalid characters.
	    while (/\(/ or /\)/) {
		die sprintf(
_("Display name '%s' contains unpaired parentheses\n"), $display)
		    unless s/\(([^()]*)\)/$1/;
	    }
	    die sprintf(
_("Display name '%s' contains invalid characters\n"), $display)
		if /[$spldot]/;
	}
	die sprintf(
_("Display name '%s' contains non-printable or 8-bit characters\n"), $display)
	    if (/[^ \t\40-\176]/);
    } else {
	$deliver = $_;
    }
    # Check for a valid delivery address
    die sprintf(_("Delivery address '%s' is invalid\n"), $display)
	if $deliver =~ /[ \t]/ or $deliver =~ /[^ \t\40-\176]/ or
	   $deliver !~ /^[^$spl]+@(\[?)[^$spldot]+(?:\.[^$spldot]+)*(\]?)$/ or
	   (!$1) != (!$2);
    return $deliver;
}

my %recipients;
sub process_header($) {
    local ($_) = @_;
    my ($name, $value);

    return unless defined $_;
    unless (($name, $value) = /^([\41-\176]+):\s*(.*)\s*/s) {
	print;
	return
    }
    if (%extract_recipients) {
	if (exists $extract_recipients{lc $name}) {
	    #print "(($value))";
	    $value =~ s/^\s*//;  $value =~ s/\s*$//;
	    foreach my $recipient (split /\s*,\s*/s, $value) {
		    next if $recipient =~ /^\s*$/;
		    #print "<<$recipient>>";
		    print check_recipient($recipient), "\n";
	    }
	}
	return;
    }
    return if exists $remove_header{lc $name};
    if (exists $replace_name{lc $name}) {
	    if (exists $replace_value{lc $name}) {
		print "$replace_name{lc $name}: $replace_value{lc $name}\n"; 
		delete $replace_value{lc $name};
	    }
	    return;
    }
    if (exists $recipient_headers{lc $1}) {
	if (exists $append_name{lc $name}) {
	    $value .= $append_value{lc $name};
	    delete $append_name{lc $name};
	}
	my @recipients;
	# This is a recipients field. Split out all the recipients and
	# check the addresses. Suppress duplicate recipients.
	$value =~ s/^\s*//;  $value =~ s/\s*$//;
	foreach my $recipient (split /\s*,\s*/, $value) {
	    next if $recipient =~ /^\s*$/;
	    my $deliver = check_recipient($recipient);
	    push @recipients, $recipient
	    	unless exists $recipients{$deliver};
	    $recipients{$deliver} = $deliver;
	}
	print "$name: ", join(",\n ", @recipients), "\n"
	    if @recipients || !$remove_empty_headers;
    } else {
	    print if $value ne "" || !$remove_empty_headers;
    }
}

my $header;
while (<STDIN>) {
    last if (/^$/);
    if (/^\S/) {
	process_header $header;
	undef $header;
    }
    $header .= $_;
}
process_header $header;
foreach my $name (keys %append_name) {
    process_header $append_name{$name} . ': ' . $append_value{$name};
}
unless (%extract_recipients) {
    # Copy the message body to standard output
    # FIXME check for 7-bit clean, else assume $charset
    # FIXME if UTF-8, check for invalid characters!
    # FIXME must make sure that all messages are written in
    #       either 7-bit or $charset => mbox !!!

    # Content-Transfer-Encoding: 7bit
    # Content-Transfer-Encoding: 8bit
    # Content-Type: text/plain; charset=ISO-8859-15
    # Content-Type: text/plain; charset=UTF-8
    undef $/;
    print "\n", <STDIN>;
}