~dricottone/digestion

ref: 6664dffbe0572acfb58412a48c25debc609e2f96 digestion/main.go -rw-r--r-- 7.1 KiB
6664dffbDominic Ricottone Rewrote in Go; Split textwrap into external library 4 years ago
                                                                                
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package main

import (
	"fmt"
	"os"
	"strings"
	"io"
	"bufio"
	"regexp"

	"git.dominic-ricottone.com/textwrap/common"
)

// An enumeration of header parts
const(
	HeaderSubject         = "HeaderSubject"
	HeaderDate            = "HeaderDate"
	HeaderFrom            = "HeaderFrom"
	HeaderTo              = "HeaderTo"
	HeaderCc              = "HeaderCc"
	HeaderMessageID       = "HeaderMessageID"
	HeaderContentType     = "HeaderContentType"
	HeaderContentEncoding = "HeaderContentEncoding"
)

// A message header container, used within message containers
type MessageHeader struct {
	Subject     string
	Date        string
	From        string
	To          string
	Cc          string
	MessageID   string
	ContentType string
	LastSet     string
}

// Builder for a message header
func NewHeader() *MessageHeader {
	return &MessageHeader{"", "", "", "", "", "", "", ""}
}

// A message part header container, used within message part containers
type MessagePartHeader struct {
	ContentType     string
	ContentEncoding string
	LastSet         string
}

// Builder for a message part header
func NewPartHeader() *MessagePartHeader {
	return &MessagePartHeader{"", "", ""}
}

// A message part container, used within message containers
type MessagePart struct {
	Header      *MessagePartHeader
	Content     []string
}

// Builder for a message part
func NewPart() *MessagePart {
	return &MessagePart{NewPartHeader(), []string{""}}
}

// A message container
type Message struct {
	Header       *MessageHeader
	Parts        []*MessagePart
	PartBoundary *regexp.Regexp
}

// Builder for a message
func NewMessage() *Message {
	return &Message{NewHeader(), []*MessagePart{NewPart()}, nil}
}

// Message setters
func (m *Message) SetHeader(line string) {
	if strings.HasPrefix(line, "Subject:") {
		m.Header.Subject = line[8:]
		m.Header.LastSet = HeaderSubject
	} else if strings.HasPrefix(line, "Date:") {
		m.Header.Date = line[5:]
		m.Header.LastSet = HeaderDate
	} else if strings.HasPrefix(line, "From:") {
		m.Header.From = line[5:]
		m.Header.LastSet = HeaderFrom
	} else if strings.HasPrefix(line, "To:") {
		m.Header.To = line[3:]
		m.Header.LastSet = HeaderTo
	} else if strings.HasPrefix(line, "Cc:") {
		m.Header.Cc = line[3:]
		m.Header.LastSet = HeaderCc
	} else if strings.HasPrefix(line, "Message-ID:") {
		m.Header.MessageID = line[11:]
		m.Header.LastSet = HeaderMessageID
	} else if strings.HasPrefix(line, "Content-Type:") {
		m.Header.ContentType = line[13:]
		m.Header.LastSet = HeaderContentType
	}
}

func (m *Message) AppendLastHeader(s string) {
	switch m.Header.LastSet {
	case HeaderSubject:
		m.Header.Subject += " " + s
	case HeaderDate:
		m.Header.Date += " " + s
	case HeaderFrom:
		m.Header.From += " " + s
	case HeaderTo:
		m.Header.To += " " + s
	case HeaderCc:
		m.Header.Cc += " " + s
	case HeaderMessageID:
		m.Header.MessageID += " " + s
	case HeaderContentType:
		m.Header.ContentType += " " + s
	}
}

func (m *Message) SetPartHeader(line string) {
	if strings.HasPrefix(line, "Content-Type:") {
		m.Parts[len(m.Parts)-1].Header.ContentType = line[13:]
		m.Parts[len(m.Parts)-1].Header.LastSet = HeaderContentType
	} else if strings.HasPrefix(line, "Content-Transfer-Encoding:") {
		m.Parts[len(m.Parts)-1].Header.ContentEncoding = line[26:]
		m.Parts[len(m.Parts)-1].Header.LastSet = HeaderContentEncoding
	}
}

func (m *Message) AppendLastPartHeader(s string) {
	switch m.Parts[len(m.Parts)-1].Header.LastSet {
	case HeaderContentType:
		m.Parts[len(m.Parts)-1].Header.ContentType += " " + s
	case HeaderContentEncoding:
		m.Parts[len(m.Parts)-1].Header.ContentEncoding += " " + s
	}
}

func (m *Message) AppendPart() {
	m.Parts = append(m.Parts, NewPart())
}

func (m *Message) AppendContent(s string) {
	i := len(m.Parts)-1
	m.Parts[i].Content = append(m.Parts[i].Content, s)
}

func (m *Message) FindBoundary(re *regexp.Regexp) {
	match := re.FindStringSubmatch(m.Header.ContentType)
	if match != nil {
		boundary := strings.Replace(match[1], " ", "", -1)
		m.PartBoundary, _ = regexp.Compile(".*" + boundary + ".*")
	}
}

// Message logic
func (m *Message) MatchBoundary(line string) bool {
	if m.PartBoundary != nil {
		return m.PartBoundary.MatchString(line)
	} else {
		return false
	}
}

// A message printer
func (m *Message) Dump() {
	fmt.Printf("Subject: %s\n", m.Header.Subject)
	fmt.Printf("Date: %s\n", m.Header.Date)
	fmt.Printf("From: %s\n", m.Header.From)
	fmt.Printf("To: %s\n", m.Header.To)
	fmt.Printf("Cc: %s\n", m.Header.Cc)
	fmt.Printf("MessageID: %s\n", m.Header.MessageID)
	fmt.Printf("ContentType: %s\n", m.Header.ContentType)
	for i := 0; i < len(m.Parts); i++ {
		fmt.Printf("Part %d:\n", i)
		fmt.Printf("ContentType: %s\n", m.Parts[i].Header.ContentType)
		fmt.Printf("ContentEncoding: %s\n", m.Parts[i].Header.ContentEncoding)

		wrapped, _ := common.WrapArray(m.Parts[i].Content, 80)
		for j := 0; j < len(wrapped); j++ {
			fmt.Printf("%s\n", wrapped[j])
		}
		fmt.Println("EOF")
	}
}

// Parser statuses
const (
	ParsingPreHeader  = "ParsingPreHeader"
	ParsingHeader     = "ParsingHeader"
	ParsingPartHeader = "ParsingPartHeader"
	ParsingContent    = "ParsingContent"
)

func parse_stream(reader io.Reader) {
	// Create scanner from reader
	input := bufio.NewScanner(reader)

	// Compile regular expressions
	re_message_break, err := regexp.Compile("^-+$")
	if err != nil {
		fmt.Printf("internal error - %v\n", err)
		os.Exit(1)
	}
	re_header, err := regexp.Compile(
		"^(?:Date|From|Subject|To|Cc|Message-ID|" +
		"Content-(?:Type|Transfer-Encoding)):",
	)
	if err != nil {
		fmt.Printf("internal error - %v\n", err)
		os.Exit(1)
	}
	re_multipart, err := regexp.Compile(".*boundary=\"(.*)\".*")
	if err != nil {
		fmt.Printf("internal error - %v\n", err)
		os.Exit(1)
	}

	parsing := ParsingPreHeader
	message := NewMessage()

	for input.Scan() {
		line := input.Text()
		tline := strings.TrimSpace(line)

		if parsing == ParsingPreHeader {
			if re_header.MatchString(tline) {
				parsing = ParsingHeader
				message.SetHeader(tline)
			}
		} else if parsing == ParsingHeader {
			if tline == "" {
				parsing = ParsingContent
				message.FindBoundary(re_multipart)
			} else if strings.HasPrefix(line, "\t") {
				message.AppendLastHeader(tline)
			} else {
				message.SetHeader(tline)
			}
		} else if parsing == ParsingPartHeader {
			if tline == "" {
				parsing = ParsingContent
			} else if strings.HasPrefix(line, "\t") {
				message.AppendLastPartHeader(tline)
			} else {
				message.SetPartHeader(tline)
			}
		} else if parsing == ParsingContent {
			if re_message_break.MatchString(tline) {
				parsing = ParsingPreHeader
				message.Dump()
				message = NewMessage()
			} else if message.MatchBoundary(tline) {
				parsing = ParsingPartHeader
				message.AppendPart()
			} else {
				message.AppendContent(tline)
			}
		}
	}

	// Check for scanner errors
	if err = input.Err(); err != nil {
		fmt.Printf("internal error - %v\n", err)
		os.Exit(1)
	}
}

func parse_file(filename string) {
	// Check file
	file, err := os.Open(filename)
	if err != nil {
		fmt.Printf("cannot read file '%s'\n", filename)
		os.Exit(1)
	}
	defer file.Close()

	// Parse
	parse_stream(file)
}

func main() {
	// Check STDIN
	_, err := os.Stdin.Stat()
	if err != nil {
		fmt.Println("cannot read input")
		os.Exit(1)
	}

	// Parse
	parse_stream(os.Stdin)
}