-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmessage.go
154 lines (138 loc) · 3.23 KB
/
message.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
package mail
import (
"bytes"
"strconv"
)
const crlf = "\015\012"
type Message struct {
*Part
RFC822Size int `json:"size"`
InternalDate int `json:"-"`
}
func NewMessage() *Message {
return &Message{Part: &Part{}}
}
func ReadMessage(rfc5322 string) (*Message, error) {
m := NewMessage()
err := m.Parse(rfc5322)
return m, err
}
func (m *Message) Parse(rfc5322 string) error {
h, err := ReadHeader(rfc5322, RFC5322Header)
if err != nil {
return err
}
m.Header = h
m.RFC822Size = len(rfc5322)
h.Repair()
h.RepairWithBody(m.Part, rfc5322[h.numBytes:])
ct := h.ContentType()
if ct != nil && ct.Type == "multipart" {
m.parseMultipart(rfc5322, ct.parameter("boundary"), ct.Subtype == "digest")
} else {
bp := m.parseBodypart(rfc5322[h.numBytes:], h)
m.Part = bp
}
//m.fix8BitHeaderFields()
m.Header.Simplify()
return nil
}
// Returns the message formatted in RFC 822 (actually 2822) format. The return
// value is a canonical expression of the message, not whatever was parsed.
//
// If \a avoidUTF8 is true, this function loses information rather than
// including UTF-8 in the result.
func (m *Message) RFC822(avoidUTF8 bool) string {
var buf strings.Builder
if m.RFC822Size > 0 {
buf.Grow(m.RFC822Size)
} else {
buf.Grow(50000)
}
buf.WriteString(m.Header.AsText(avoidUTF8))
buf.WriteString(crlf)
buf.WriteString(m.Body(avoidUTF8))
return buf.String()
}
// Returns the text representation of the body of this message.
func (m *Message) Body(avoidUTF8 bool) string {
buf := new(bytes.Buffer)
ct := m.Header.ContentType()
if ct != nil && ct.Type == "multipart" {
m.appendMultipart(buf, avoidUTF8)
} else {
// FIXME: Is this the right place to restore this linkage?
if len(m.Parts) > 0 {
firstChild := m.Parts[0]
firstChild.Header = m.Header
m.appendAnyPart(buf, firstChild, ct, avoidUTF8)
}
}
return buf.String()
}
// Returns a pointer to the Bodypart whose IMAP part number is \a s and
// possibly create it. Creates Bodypart objects if \a create is true. Returns
// null pointer if \a s is not valid and \a create is false.
func (m *Message) BodyPart(s string, create bool) *Part {
b := 0
var bp *Part
for b < len(s) {
e := b
for e < len(s) && s[e] >= '0' && s[e] <= '9' {
e++
}
if e < len(s) && s[e] != '.' {
return nil
}
n, err := strconv.Atoi(s[b:e])
b = e + 1
if err != nil || n == 0 {
return nil
}
cs := m.Parts
if bp != nil {
cs = bp.Parts
}
i := 0
var c *Part
for i, c = range cs {
if c.Number >= n {
break
}
}
if c != nil && c.Number == n {
if n == 1 && c.Header == nil {
// it's possible that i doesn't have a header of its
// own, and that the parent message's header functions
// as such. link it in if that's the case.
h := &Header{}
if bp != nil && bp.message != nil {
h = bp.message.Header
}
if h != nil && (h.ContentType() == nil ||
h.ContentType().Type != "multipart") {
c.Header = h
}
}
bp = c
} else if create {
var child *Part
if bp != nil {
child = &Part{
Number: n,
parent: bp,
}
} else {
child = &Part{
Number: n,
parent: m.Part,
}
}
cs = append(append(cs[:i], child), cs[i:]...)
bp = child
} else {
return nil
}
}
return bp
}