-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail-to-filter
executable file
·172 lines (160 loc) · 5.83 KB
/
mail-to-filter
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
#!/usr/bin/perl
no warnings 'deprecated';
# changes
# - silence warnings
# - use Return-Path to detect mutt weed
# mail-to-filter - strips long distribution lists from mail
# Copyright (C) 2000-2003 Gary A. Johnson
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# Description:
#
# This program is intended to be used as a filter ahead of a mail
# reader's pager, for example as the display_filter for mutt. It
# shortens To and Cc lists longer than two lines, replacing the
# second and subsequent lines with
#
# [<lines> lines deleted]
#
# indicating the number of lines deleted. It also removes excess
# spaces at the ends of all lines of the message, but preserves
# the trailing space of the signature's "-- " line.
#
# Revision History:
#
# 18 Nov. 2003 - Allow the lower-case header field names "to" and
# "cc" generated by some MUAs. Patch contributed
# by Aseem Asthana.
#
# 27 Mar. 2002 - Truncates lines in To: and Cc: lists longer than
# $COLUMNS.
#
# 25 Jan. 2002 - Detects mutt's "weed" state by checking for
# "^From " on the first line and if not weeding,
# does not filter To and Cc lists.
#
# 27 Nov. 2001 - No longer removes trailing space from
# sigdashes ("^-- $").
#
# The latest version is available at
#
# http://www.spocom.com/users/gjohnson/mutt/mail-to-filter
#
# Please direct any comments to:
#
# Gary Johnson <[email protected]>
eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
if $running_under_some_shell;
# this emulates #! processing on NIH machines.
# (remove #! line above if indigestible)
eval '$'.$1.'$2;' while $ARGV[0] =~ /^([A-Za-z_0-9]+=)(.*)/ && shift;
# process any FOO=bar switches
$[ = 1; # set array base to 1
$, = ' '; # set output field separator
$\ = "\n"; # set output record separator
$weeding = 1;
$in_to_list = 0;
$in_cc_list = 0;
$spaces = sprintf('%80s', '');
$width = $ENV{'COLUMNS'} || 80;
line: while (<>) {
chop; # strip record separator
s/\s+$// unless /^-- $/;
# Remove excess spaces at the ends of lines
# that can cause lines to wrap unnecessarily,
# but don't corrupt sigdashes.
if ($weeding == 1) {
@Fld = split(' ', $_, 9999);
if ($in_to_list == 1) {
if (/^[Tt][Oo]:.*@/) {
$lines += 1;
$last_line = $_;
next line;
}
if (/^[^ \t]/ || /^$/) {
if ($lines == 1) {
# The $last_line was not the first line of the "To:"
# list, so it should begin with a tab. Assume for
# now that it does and adjust the long-line
# truncation procedure accordingly.
#
if (length($last_line) > $width - 7) {
substr($last_line, $width - 7 - 4) = "[...]";
}
print $last_line;
}
elsif ($lines > 1) {
print $indentation . '[' . $lines . ' lines deleted]';
}
$in_to_list = 0;
}
else {
$lines += 1;
$last_line = $_;
next line;
}
}
if ($in_cc_list == 1) {
if (/^[Cc][Cc]:.*@/) {
$lines += 1;
$last_line = $_;
next line;
}
if (/^[^ \t]/ || /^$/) {
if ($lines == 1) {
# The $last_line was not the first line of the "Cc:"
# list, so it should begin with a tab. Assume for
# now that it does and adjust the long-line
# truncation procedure accordingly.
#
if (length($last_line) > $width - 7) {
substr($last_line, $width - 7 - 4) = "[...]";
}
print $last_line;
}
elsif ($lines > 1) {
print $indentation . '[' . $lines . ' lines deleted]';
}
$in_cc_list = 0;
}
else {
$lines += 1;
$last_line = $_;
next line;
}
}
if (/^[Tt][Oo]:.*@/) {
$indentation = substr($spaces, 1, index($_, $Fld[2]) - 1);
$in_to_list = 1;
$lines = 0;
if (length($_) > $width) {
substr($_, $width - 4) = "[...]";
}
}
if (/^[Cc][Cc]:.*@/) {
$indentation = substr($spaces, 1, index($_, $Fld[2]) - 1);
$in_cc_list = 1;
$lines = 0;
if (length($_) > $width) {
substr($_, $width - 4) = "[...]";
}
}
if (($. == 3) && (/^Return-Path: /)) {
$weeding = 0;
}
}
print $_;
}
exit 0;