-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_data.go
61 lines (50 loc) · 1.08 KB
/
command_data.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
package smtpd
import (
"bytes"
"io"
"strings"
)
func newDataCommand() command {
return &dataCommand{
started: false,
}
}
type dataCommand struct {
started bool
buf bytes.Buffer
}
func (c *dataCommand) Next() []string {
return CommandsMail
}
func (c *dataCommand) Process(line string, ex *Exchange) (*reply, bool) {
if !c.started {
c.started = true
r := newReply(ReplyDataStart,
"start mail input; end with <CRLF>.<CRLF>")
return r, false
}
if strings.HasPrefix(line, ".") {
// We remove the first dot since clients will send a preceding
// dot to avoid the EOM sequence
line = line[1:]
if len(line) == 0 {
ex.Body(c.reader())
err := ex.Done()
if err != nil {
c.buf.Reset()
r := newReply(ReplySyntaxError, err.Error())
return r, false
}
return ok(), true
}
}
c.buf.WriteString(line)
c.buf.WriteRune('\n')
return nil, false
}
func (c *dataCommand) reader() io.Reader {
// Transparently remove the last byte since it is an extraneous newline
stream := c.buf.Bytes()
stream = stream[:len(stream)-1]
return bytes.NewReader(stream)
}