-
Notifications
You must be signed in to change notification settings - Fork 2
/
filter.go
179 lines (174 loc) · 5.35 KB
/
filter.go
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
// 2019, Georg Sauthoff <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
package main
import (
"bytes"
"io"
"regexp"
)
type header_filter_writer struct {
out io.WriteCloser
state int
}
func new_header_filter_writer(out io.WriteCloser) *header_filter_writer {
w := new(header_filter_writer)
w.out = out
return w
}
// expects full words - i.e. to be chained after the word_split_writer
func (w *header_filter_writer) Write(word []byte) (int, error) {
const ( AT_NAME = iota
IGNORE_VALUE
WRITE_LINE
FILTER_RECEIVED
FILTER_CT
)
nl := []byte("\n")
ignore_hdrs := [][]byte {
[]byte("date:"), []byte("message-id:"), []byte("references:"),
[]byte("x-bogosity:"), []byte("message-id-hash:"),
[]byte("x-message-id-hash:"),
[]byte("x-gonzo:"), []byte("x-old-gonzo"),
}
n := len(word)
switch w.state {
case AT_NAME:
if iequal_any(word, ignore_hdrs) {
w.state = IGNORE_VALUE
} else if ihas_prefix(word, []byte("x-spam-")) {
w.state = IGNORE_VALUE
} else if ihas_suffix(word, []byte("dkim-signature:")) {
w.state = IGNORE_VALUE
} else if iequal(word, []byte("received:")) {
w.state = FILTER_RECEIVED
if _, err := w.out.Write(word); err != nil {
return 0, err
}
} else if iequal(word, []byte("content-type:")) {
w.state = FILTER_CT
if _, err := w.out.Write(word); err != nil {
return 0, err
}
} else {
w.state = WRITE_LINE
if _, err := w.out.Write(word); err != nil {
return 0, err
}
}
case IGNORE_VALUE:
if n == 1 && word[0] == byte('\n') {
w.state = AT_NAME
}
case WRITE_LINE:
if n == 1 && word[0] == byte('\n') {
w.state = AT_NAME
}
if _, err := w.out.Write(word); err != nil {
return 0, err
}
case FILTER_RECEIVED:
if _, err := w.out.Write(word); err != nil {
return 0, err
}
if bytes.HasSuffix(word, []byte("id")) {
w.state = IGNORE_VALUE
if _, err := w.out.Write(nl); err != nil {
return 0, err
}
} else if n == 1 && word[0] == byte('\n') {
w.state = AT_NAME
}
case FILTER_CT:
if _, err := w.out.Write(word); err != nil {
return 0, err
}
if ihas_prefix(word, []byte("boundary")) {
w.state = IGNORE_VALUE
if _, err := w.out.Write(nl); err != nil {
return 0, err
}
} else if n == 1 && word[0] == byte('\n') {
w.state = AT_NAME
}
}
return n, nil
}
func (w *header_filter_writer) Close() error {
return w.out.Close()
}
type xgonzo_filter_writer struct {
out io.WriteCloser
state int
partial []byte
off int
xgonzo_re *regexp.Regexp
}
func new_xgonzo_filter_writer(out io.WriteCloser) *xgonzo_filter_writer {
w := new(xgonzo_filter_writer)
w.out = out
w.partial = make([]byte, 0, 9)
w.off = 1
w.xgonzo_re = regexp.MustCompile("(?i)\nx-gonzo: ")
return w
}
func (w *xgonzo_filter_writer) Write(block []byte) (int, error) {
const ( IN_GONZO = iota
AFTER_GONZO
)
n := len(block)
xgonzo := []byte("\nx-gonzo: ")
old_xgonzo := []byte("X-old-gonzo: ")
for len(block) != 0 {
switch w.state {
case IN_GONZO:
i := imatch_prefix(block, xgonzo[w.off:])
switch {
case i + w.off == len(xgonzo):
w.out.Write(old_xgonzo)
block = block[i:]
w.state = AFTER_GONZO
case i > 0 && i == len(block):
w.partial = append(w.partial, block[:i]...)
w.off += i
block = block[i:]
default:
if len(w.partial) > 0 {
w.out.Write(w.partial)
}
w.state = AFTER_GONZO
}
case AFTER_GONZO:
i := -1
if loc := w.xgonzo_re.FindIndex(block); loc != nil {
i = loc[0] // til we have a case-insensitive bytes.Index version
}
if i == -1 {
i := bytes.LastIndexByte(block, byte('\n'))
if i == -1 {
w.out.Write(block)
block = block[:0]
} else {
l := len(block) - i
if l < len(xgonzo) && imatch_prefix(block[i:], xgonzo) == l {
w.partial = w.partial[:0]
w.partial = append(w.partial, block[i+1:]...)
w.out.Write(block[:i+1])
w.off = l
w.state = IN_GONZO
} else {
w.out.Write(block)
}
block = block[:0]
}
} else {
w.out.Write(block[:i+1])
block = block[i+len(xgonzo):]
w.out.Write(old_xgonzo)
}
}
}
return n, nil
}
func (w *xgonzo_filter_writer) Close() error {
return w.out.Close()
}