~dricottone/digestion

ref: 62eccd92f6655185139d9015022c8b9953a51521 digestion/message/header.go -rw-r--r-- 2.7 KiB
62eccd92Dominic Ricottone Refactored into subdirectories; Added common decoders 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
package message

import (
	"strings"
)

const (
	HeaderSubject         = "HeaderSubject"
	HeaderDate            = "HeaderDate"
	HeaderFrom            = "HeaderFrom"
	HeaderTo              = "HeaderTo"
	HeaderCc              = "HeaderCc"
	HeaderMessageID       = "HeaderMessageID"
	HeaderContentType     = "HeaderContentType"
	HeaderContentEncoding = "HeaderContentEncoding"
)

// Message headers
type MessageHeader struct {
	Subject     string
	Date        string
	From        string
	To          string
	Cc          string
	MessageID   string
	ContentType string
	LastSet     string
}

func NewHeader() *MessageHeader {
	return &MessageHeader{"", "", "", "", "", "", "", ""}
}

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

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

// Message part headers
type MessagePartHeader struct {
	ContentType     string
	ContentEncoding string
	LastSet         string
}

func NewPartHeader() *MessagePartHeader {
	return &MessagePartHeader{"", "", ""}
}

func (m *MessagePartHeader) SetHeader(s string) {
	if strings.HasPrefix(s, "\t") {
		m.append_last_set(s)
	} else if strings.HasPrefix(s, "Content-Type:") {
		m.ContentType = strings.TrimSpace(s[13:])
		m.LastSet = HeaderContentType
	} else if strings.HasPrefix(s, "Content-Transfer-Encoding:") {
		m.ContentEncoding = strings.TrimSpace(s[26:])
		m.LastSet = HeaderContentEncoding
	}
}

func (m *MessagePartHeader) append_last_set(s string) {
	s = strings.TrimSpace(s)
	switch m.LastSet {
	case HeaderContentType:
		m.ContentType += " " + s
	case HeaderContentEncoding:
		m.ContentEncoding += " " + s
	}
}