-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransaction_io.go
80 lines (71 loc) · 1.53 KB
/
transaction_io.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
package msmtpd
import (
"bufio"
"fmt"
"strings"
"time"
)
func (t *Transaction) serve() {
defer func() {
t.server.runCloseHandlers(t)
t.close()
if t.Span != nil {
t.Span.End()
}
t.cancel()
}()
if !t.server.EnableProxyProtocol {
t.welcome()
}
for {
for t.scanner.Scan() {
line := t.scanner.Text()
t.LogTrace("Received: %s", strings.TrimSpace(line))
t.handle(line)
}
err := t.scanner.Err()
if err == bufio.ErrTooLong {
t.reply(500, "Line too long")
// Advance reader to the next newline
t.reader.ReadString('\n')
t.scanner = bufio.NewScanner(t.reader)
// Reset and have the client start over.
t.reset()
continue
}
break
}
}
func (t *Transaction) reject() {
t.reply(421, "I'm tired. Take a break, please.")
t.close()
}
func (t *Transaction) reset() {
t.Body = nil
t.Parsed = nil
}
func (t *Transaction) welcome() {
t.reply(220, t.server.WelcomeMessage)
}
func (t *Transaction) reply(code int, message string) {
t.LogTrace("Sending: %d %s", code, message)
fmt.Fprintf(t.writer, "%d %s\r\n", code, message)
t.flush()
}
func (t *Transaction) flush() {
t.conn.SetWriteDeadline(time.Now().Add(t.server.WriteTimeout))
t.writer.Flush()
t.conn.SetReadDeadline(time.Now().Add(t.server.ReadTimeout))
}
func (t *Transaction) error(err error) {
if smtpdError, ok := err.(ErrorSMTP); ok {
t.reply(smtpdError.Code, smtpdError.Message)
} else {
t.reply(502, fmt.Sprintf("%s", err))
}
}
func (t *Transaction) close() {
t.writer.Flush()
time.Sleep(200 * time.Millisecond)
t.conn.Close()
}