-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutt-trim
More file actions
executable file
·215 lines (188 loc) · 5.49 KB
/
Copy pathmutt-trim
File metadata and controls
executable file
·215 lines (188 loc) · 5.49 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/perl
#
# Remove any clutter from the quoted message.
# Originally named autotrim and written by Michael Velten.
use utf8;
# keep nested quotes up to $ind_max-th level
my $ind_max = 5;
# insert $gap many blank lines between different quote levels
my $gap = 0;
# Precompile regex patterns
my $name_regex = qr/[[:alpha:]]+(?:[\'`-][[:alpha:]]+|[.])*/;
my $fullname_regex = qr/\b(?:$name_regex[, ]?\s+)*$name_regex\b/;
my $word_regex = qr/[[:alpha:]]+(?:[\'`-][[:alpha:]]+)*/;
my @greetings_regexes = map { qr/$_/ } (
'Dear',
'[Hh](ello|i|ey)',
'[Gg]ood ([Mm]orning|[Aa]fternoon|[Ee]vening)',
'Sehr geehrter?',
'Lieber?',
'Guten (Tag|Morgen|Nachmittag|Abend)',
'[Hh]allo',
'[Mm]oin',
'[Mm]esdames(,| et) [Mm]essieurs\s*[,.!]?',
'M(adame)',
'M(onsieur)',
'[Cc]her',
'[Cc]h[eè]re',
'[Bb]onjour',
'[Ss]alut',
'Senhor(ita|a)?',
'Sra?\.?',
'Car([ií]ssim)?[ao]s?',
'Prezad([ií]ssim)?[ao]s?',
'Estimad([ií]ssim)?[ao]s?',
'[Bb]om [Dd]ia',
'[Bb]oa ([Tt]arde|[Nn]oite)',
'[Oo](i|l[aá])',
'[Aa]l[ôo]',
'[Hh]ola',
'Se[nñ]or(ita|a)?',
);
my @greetouts_regexes = map { qr/$_/ } (
'([Ww]ith )?(([Kk]ind|[Bb]est|[Ww]arm) )?([Rr]egards|[Ww]ishes)',
'[Bb]est',
'[Cc]heers',
'([Mm]any\s+)?[Tt]hanks',
'[Mm]it ([Vv]iel|[Bb]est|[Ll]ieb|[Ff]reundlich|[Hh]erzlich)en [Gg]r([ü]|ue)([ß]|ss)en',
'(([Vv]iel|[Bb]est|[Ll]ieb|[Ff]reundlich|[Hh]erzlich)e )?[Gg]r([ü]|ue)([ß]|ss)e',
'(([Bb]est|[Ll]ieb|[Ff]reundlich|[Hh]erzlich)e[rn] )?[Gg]ru([ß]|ss)',
'[Mm]it (([Bb]est|[Ll]ieb|[Ff]reundlich|[Hh]erzlich)em )?[Gg]ru([ß]|ss)',
'([LV]|MF)G',
'(([Tt]r[eè]s|[Bb]ien) )?([Cc]ordi|[Aa]mic)alement',
'[Aa]miti[eé]s?',
'[Aa]tenciosamente',
'[Aa]tt',
'[Aa]bra[cç]os?',
'[Aa]tentamente',
'[Cc]ordialmente',
);
my @greeting_line_regexes = map {
my $re = $_;
qr/^>+\s*$re(?:\s+$fullname_regex)?\s*[,.!]*\s*$/
} @greetings_regexes;
my @greetout_line_regexes = map {
my $re = $_;
qr/^>+\s*$re(?:\s*[,.!]+)?\s*$/
} @greetouts_regexes;
my $saw_greeting = 0;
my $saw_greetout = 0;
my $saw_blank_line = 0;
my $prev_inds = 0;
my $saw_own_sig = 0;
my $inds_other_sig = 0;
my $quote_header = 0;
my $extra_pref = '';
my (@mail, @purged_mail);
my $msg = shift;
die "Usage: $0 MAIL" unless defined $msg && length $msg;
open(my $MAIL, "+<:encoding(UTF-8)", $msg) or die "$0: Can't open $msg: $!";
push(@mail, $_) while <$MAIL>;
# Precompile regex patterns outside the loop
my $quote_header_regex = qr/^>+\s*[-_=━]{3,}\s*(?:$word_regex(?:\s+$word_regex)*)?\s*[-_=━]{3,}$/;
my $outlook_header_kv_regex = qr/^>+\s*(?:[-*]\s*)?$word_regex(?:\s+$word_regex)*\s*:\s+/;
my $signature_regex = qr/^--\s?$/;
my $other_signature_regex = qr/^>+\s*--\s?$/;
my $filler_line_regex = qr/^>+\s*[-_=+#*]+$/;
my $blank_line_regex = qr/^>+\s*$/;
# Process whole mail
LINE: foreach my $line (@mail) {
my $inds = 0;
# count indent level
if ($line =~ /^>/) {
# $line =~ tr/\xA0/ /;
# tighten "> > " to ">> "
my ($pref, $suff) = $line =~ /^([>[:space:]]+)(.*)$/;
$pref =~ s/(>\s*(?!$))/>/g;
# reduce multiple pre- and post-blanks to one post-blank
$pref =~ s/^\s*(>+)\s*/$1 /;
$line = $pref . $suff . "\n";
# prepend additional '>' for each Outlook quote header
if ($line =~ $quote_header_regex) {
$quote_header = 1;
next LINE;
}
# first line after Outlook quote header that does not start with ...:
if ($quote_header == 1 && $line !~ $outlook_header_kv_regex) {
$extra_pref = '>' . $extra_pref;
$quote_header = 0;
}
$pref = $extra_pref . $pref;
$line = $pref . $suff . "\n";
$inds = $pref =~ tr/>//;
}
# remove last line if blank and last in citation level
if ($inds == 0 && $inds < $prev_inds && $saw_blank_line) {
$prev_inds = $inds;
pop @purged_mail;
}
# Treat non-quoted lines as is
if ($inds == 0) {
push(@purged_mail, $line);
next LINE;
}
# Keep all lines after my own signature unmodified
if ($line =~ $signature_regex || $saw_own_sig) {
$saw_own_sig = 1;
push(@purged_mail, $line);
next LINE;
}
# skip line if number of '>'s is greater than $ind_max
if ($inds > $ind_max) {
next LINE;
}
# Remove other signatures
if ($line =~ $other_signature_regex) {
$inds_other_sig = $inds;
next LINE;
}
if ($inds == $inds_other_sig) {
next LINE;
} else {
$inds_other_sig = 0;
}
# Remove quoted greeting
unless ($saw_greeting) {
foreach my $greeting_re (@greeting_line_regexes) {
if ($line =~ $greeting_re) {
$saw_greeting = 1;
next LINE;
}
}
}
# Remove quoted "greetout"
unless ($saw_greetout) {
foreach my $greetout_re (@greetout_line_regexes) {
if ($line =~ $greetout_re) {
$saw_greetout = 1;
next LINE;
}
}
}
# Remove quoted filler lines
if ($line =~ $filler_line_regex) {
next LINE;
}
# Squeeze multiple blank lines
if ($line =~ $blank_line_regex) {
if ($saw_blank_line) {
next LINE;
} else {
$saw_blank_line = 1;
# remove trailing blanks
$line =~ tr/ //ds;
}
} else {
$saw_blank_line = 0;
}
# Insert $gap many empty lines between different quote levels
$line = "\n" x $gap . $line if $prev_inds != $inds;
$prev_inds = $inds;
# Save purged line
push(@purged_mail, $line);
}
# Overwrite original mail with purged mail
truncate($MAIL, 0) or die "$0: truncate failed for $msg: $!";
seek($MAIL, 0, 0) or die "$0: seek failed for $msg: $!";
print {$MAIL} @purged_mail;
close($MAIL) or die "$0: close failed for $msg: $!";