Debian Codegraph @

api.go

raw symbols SCIP
1 package reply
2
3 import (
4 "io"
5 "io/ioutil"
6 "strings"
7 "time"
8
9 "github.com/dlclark/regexp2"
10 )
11
12 func init() {
13 // The default configuration is set to 'forever'.
14 // I am not expecting any regex to take more than a few milliseconds.
15 // Setting this value to 1s just to be on the safe side.
16 regexp2.DefaultMatchTimeout = 1 * time.Second
17 }
18
19 // FromReader returns the reply text from the e-mail text body.
20 func FromReader(reader io.Reader) (string, error) {
21 bytes, err := ioutil.ReadAll(reader)
22 if err != nil {
23 return "", err
24 }
25
26 return FromText(string(bytes)), nil
27 }
28
29 // FromText returns the reply text from the e-mail text body.
30 func FromText(text string) string {
31 if strings.TrimSpace(text) == "" {
32 return text
33 }
34
35 // do some cleanup
36 text = cleanup(text)
37
38 // from now on, we'll work on a line-by-line basis
39 lines := strings.Split(text, "\n")
40 patternBuilder := strings.Builder{}
41
42 for _, line := range lines {
43 patternBuilder.WriteString(classifyLine(line))
44 }
45
46 pattern := patternBuilder.String()
47
48 // remove everything after the first delimiter
49 {
50 match, err := regexp2.MustCompile(`d`, regexp2.RE2).FindStringMatch(pattern)
51 if err != nil {
52 return ""
53 }
54 if match != nil {
55 index := match.Index
56 pattern = sliceString(pattern, 0, index-1)
57 lines = sliceArray(lines, 0, index-1)
58 }
59 }
60
61 // remove all mobile signatures
62 for {
63 match, err := regexp2.MustCompile(`s`, regexp2.RE2).FindStringMatch(pattern)
64 if err != nil {
65 return ""
66 }
67 if match != nil {
68 index := match.Index
69 pattern = stringSliceBang(pattern, index)
70 lines = sliceSliceBang(lines, index)
71 } else {
72 break
73 }
74 }
75
76 // when the reply is at the end of the email
77 {
78 match, err := regexp2.MustCompile(`^(b[^t]+)*b[bqeh]+t[et]*$`, regexp2.RE2).FindStringMatch(pattern)
79 if err != nil {
80 return ""
81 }
82 if match != nil {
83 submatch, err := regexp2.MustCompile(`t[et]*$`, regexp2.RE2).FindStringMatch(pattern)
84 if err != nil {
85 return ""
86 }
87
88 index := submatch.Index
89 pattern = ""
90 lines = sliceArray(lines, index, len(lines)-1)
91 }
92 }
93
94 // if there is an embedded email marker, not followed by a quote
95 // then take everything up to that marker
96 {
97 match, err := regexp2.MustCompile(`te*b[^q]*$`, regexp2.RE2).FindStringMatch(pattern)
98 if err != nil {
99 return ""
100 }
101 if match != nil {
102 index := match.Index
103 pattern = sliceString(pattern, 0, index)
104 lines = sliceArray(lines, 0, index)
105 }
106 }
107
108 // if there is an embedded email marker, followed by a huge quote
109 // then take everything up to that marker
110 {
111 match, err := regexp2.MustCompile(`te*b[eqbh]*([te]*)$`, regexp2.RE2).FindStringMatch(pattern)
112 if err != nil {
113 return ""
114 }
115 if match != nil && strings.Count(match.GroupByNumber(1).String(), "t") < 7 {
116 submatch, err := regexp2.MustCompile(`te*b[eqbh]*[te]*$`, regexp2.RE2).FindStringMatch(pattern)
117 if err != nil {
118 return ""
119 }
120 index := submatch.Index
121 pattern = sliceString(pattern, 0, index)
122 lines = sliceArray(lines, 0, index)
123 }
124 }
125
126 // if there is some text before a huge quote ending the email,
127 // then remove the quote
128 {
129 match, err := regexp2.MustCompile(`te*[qbe]+$`, regexp2.RE2).FindStringMatch(pattern)
130 if err != nil {
131 return ""
132 }
133 if match != nil {
134 index := match.Index
135 pattern = sliceString(pattern, 0, index)
136 lines = sliceArray(lines, 0, index)
137 }
138 }
139
140 // if there still are some embedded email markers, just remove them
141 for {
142 match, err := regexp2.MustCompile(`b`, regexp2.RE2).FindStringMatch(pattern)
143 if err != nil {
144 return ""
145 }
146 if match != nil {
147 index := match.Index
148 pattern = stringSliceBang(pattern, index)
149 lines = sliceSliceBang(lines, index)
150 } else {
151 break
152 }
153 }
154
155 // fix email headers when they span over multiple lines
156 {
157 match, err := regexp2.MustCompile(`h+[hte]+h+e`, regexp2.RE2).FindStringMatch(pattern)
158 if err != nil {
159 return ""
160 }
161 if match != nil {
162 index := match.Index
163 for i := 0; i < match.Length; i++ {
164 c := []rune(header)[0]
165 pattern = stringReplaceChar(pattern, c, index+i)
166 }
167 }
168 }
169
170 // if there are at least 3 consecutive email headers,
171 // take everything up to these headers
172 {
173 match, err := regexp2.MustCompile(`t[eq]*h{3,}`, regexp2.RE2).FindStringMatch(pattern)
174 if err != nil {
175 return ""
176 }
177 if match != nil {
178 index := match.Index
179 pattern = sliceString(pattern, 0, index)
180 lines = sliceArray(lines, 0, index)
181 }
182 }
183
184 // if there still are some email headers, just remove them
185 for {
186 match, err := regexp2.MustCompile(`h`, regexp2.RE2).FindStringMatch(pattern)
187 if err != nil {
188 return ""
189 }
190 if match != nil {
191 index := match.Index
192 pattern = stringSliceBang(pattern, index)
193 lines = sliceSliceBang(lines, index)
194 } else {
195 break
196 }
197 }
198
199 // remove trailing quotes when there's at least one line of text
200 {
201 match1, err := regexp2.MustCompile(`t`, regexp2.RE2).FindStringMatch(pattern)
202 if err != nil {
203 return ""
204 }
205 match2, err := regexp2.MustCompile(`[eq]+$`, regexp2.RE2).FindStringMatch(pattern)
206 if err != nil {
207 return ""
208 }
209 if match1 != nil && match2 != nil {
210 index := match2.Index
211 pattern = sliceString(pattern, 0, index-1)
212 lines = sliceArray(lines, 0, index-1)
213 }
214 }
215
216 return strings.Join(lines, "\n")
217 }
218
219 func cleanup(text string) string {
220 // normalize line endings
221 replacer := strings.NewReplacer(
222 "\r\n", "\n",
223 "\r", "\n",
224 )
225
226 text = replacer.Replace(text)
227
228 // remove PGP markers
229 for _, r := range patterns["REMOVE_PGP_MARKERS_REGEX"] {
230 text, _ = r.Replace(text, "", 0, -1)
231 }
232
233 // remove unsubscribe links
234 for _, r := range patterns["REMOVE_UNSUBSCRIBE_REGEX"] {
235 text, _ = r.Replace(text, "", 0, -1)
236 }
237
238 // remove alias-style quotes marker
239 for _, r := range patterns["REMOVE_ALIAS_REGEX"] {
240 text, _ = r.Replace(text, "", 0, -1)
241 }
242
243 // change enclosed-style quotes format
244 for _, r := range patterns["CHANGE_ENCLOSED_QUOTE_ONE_REGEX"] {
245 text, _ = r.ReplaceFunc(text, func(m regexp2.Match) string {
246 newText, _ := regexp2.MustCompile(`^`, regexp2.RE2).Replace(m.GroupByNumber(2).String(), "> ", 0, -1)
247 return newText
248 }, 0, -1)
249 }
250
251 for _, r := range patterns["CHANGE_ENCLOSED_QUOTE_TWO_REGEX"] {
252 text, _ = r.ReplaceFunc(text, func(m regexp2.Match) string {
253 newText, _ := regexp2.MustCompile(`^`, regexp2.RE2).Replace(m.GroupByNumber(1).String(), "> ", 0, -1)
254 return newText
255 }, 0, -1)
256 }
257
258 // fix all quotes formats
259 for _, r := range patterns["FIX_QUOTES_FORMAT_REGEX"] {
260 text, _ = r.ReplaceFunc(text, func(m regexp2.Match) string {
261 newText, _ := regexp2.MustCompile(`([[:alpha:]]+>|\|)`, regexp2.RE2).Replace(m.GroupByNumber(1).String(), ">", 0, -1)
262 return newText
263 }, 0, -1)
264 }
265
266 // fix embedded email markers that might span over multiple lines
267 for _, regex := range patterns["FIX_EMBEDDED_REGEX"] {
268 text, _ = regex.ReplaceFunc(text, func(m regexp2.Match) string {
269 if strings.Count(m.String(), "\n") > 4 {
270 return m.String()
271 }
272 newText, _ := regexp2.MustCompile(`\n+[[:space:]]*`, regexp2.RE2).Replace(m.String(), " ", 0, -1)
273 return newText
274 }, 0, -1)
275 }
276
277 // remove leading/trailing whitespaces
278 return strings.TrimSpace(text)
279 }