-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
81 lines (63 loc) · 1.28 KB
/
conn.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
package imap
import (
"fmt"
"io"
)
type Writer interface {
io.Writer
Splat(msg string)
Continuation(msg string)
Ok(r *Request)
OkWithCode(r *Request, responseCode string)
No(r *Request, err error)
Bad(r *Request, err error)
}
type Reader interface {
io.Reader
}
type Conn struct {
rwc io.ReadWriteCloser
parser *Parser
}
func NewConn(rwc io.ReadWriteCloser) *Conn {
conn := &Conn{
rwc: rwc,
}
conn.parser = NewParser(conn)
return conn
}
func (c *Conn) Write(b []byte) (int, error) {
return c.rwc.Write(b)
}
func (c *Conn) Close() error {
return c.rwc.Close()
}
func (c *Conn) Splat(msg string) {
fmt.Fprintf(c, "* %s\r\n", msg)
}
func (c *Conn) Continuation(msg string) {
fmt.Fprintf(c, "+ %s\r\n", msg)
}
func (c *Conn) Ok(r *Request) {
fmt.Fprintf(c, "%s OK %s completed\r\n", r.Tag, r.Command)
}
func (c *Conn) OkWithCode(r *Request, responseCode string) {
fmt.Fprintf(c, "%s OK [%s] %s completed\r\n", r.Tag, responseCode, r.Command)
}
func (c *Conn) No(r *Request, err error) {
tag := "*"
if r != nil {
tag = r.Tag
}
fmt.Fprintf(c, "%s NO %s\r\n", tag, err)
}
func (c *Conn) Bad(r *Request, err error) {
tag := "*"
if r != nil {
tag = r.Tag
}
fmt.Fprintf(c, "%s BAD %s\r\n", tag, err)
}
func (c *Conn) DiscardLine() {
c.parser.DiscardLine()
}