-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
95 lines (79 loc) · 1.84 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
package main
import (
"bytes"
"strings"
"time"
)
type Message struct {
EnvelopeSender string
ReceivedField string
Buf []byte
SessionId string
EOL string
}
func (be *Backend) NewMessage(s *Session, b []byte) *Message {
c := be.Config
var received string
if c.C.AddReceived {
var words []string
words = append(words, "from")
words = append(words, s.HelloHost)
rh, err := addrToIP(s.RemoteAddr)
if err == nil {
words = append(words, "("+addressLiteral(rh)+")")
}
words = append(words, "by")
words = append(words, c.S.Domain)
lh, err := addrToIP(s.LocalAddr)
if err == nil {
words = append(words, "("+addressLiteral(lh)+")")
}
words = append(words, "with")
var with string
if c.S.LMTP {
with = "LMTP"
} else {
// FIXME: "SMTP" is possible
with = "ESMTP"
}
words = append(words, with)
words = append(words, "id")
words = append(words, s.Id)
if len(s.RcptTos) == 1 {
words = append(words, "for")
words = append(words, "<"+s.RcptTos[0]+">")
}
words[len(words)-1] += ";"
words = append(words, time.Now().Format(time.RFC1123Z))
lines := []string{"Received:"}
for _, w := range words {
if 78 < len(lines[len(lines)-1]+" "+w) {
lines = append(lines, " "+w)
} else {
lines[len(lines)-1] += " " + w
}
}
received = strings.Join(lines, "\r\n")
}
return &Message{
EnvelopeSender: s.MailFrom,
ReceivedField: received,
Buf: b,
SessionId: s.Id,
EOL: c.C.EOL,
}
}
func (m *Message) Serialized() []byte {
b := []byte("Return-Path: <" + m.EnvelopeSender + ">\r\n")
if m.ReceivedField != "" {
b = append(b, []byte(m.ReceivedField+"\r\n")...)
}
if len(m.Buf) != 0 {
b = append(b, m.Buf...)
}
eol := m.EOL
if eol != "" && eol != "\r\n" {
b = bytes.Replace(b, []byte("\r\n"), []byte(eol), -1)
}
return b
}