forked from taknb2nch/go-pop3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pop3proto.go
175 lines (139 loc) · 3.6 KB
/
pop3proto.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package pop3
import (
"bufio"
"fmt"
"io"
_ "log"
"net/textproto"
"strings"
)
// A ResponseError describes a protocol violation such as an invalid response or a hung-up connection.
type ResponseError string
func (r ResponseError) Error() string {
return string(r)
}
// A Conn represents a textual network protocol connection for POP3.
type Conn struct {
Reader
Writer
conn io.ReadWriteCloser
}
// NewConn returns a new Conn using conn for I/O.
func NewConn(conn io.ReadWriteCloser) *Conn {
return &Conn{
Reader: Reader{R: textproto.NewReader(bufio.NewReader(conn))},
Writer: Writer{W: bufio.NewWriter(conn)},
conn: conn,
}
}
// Close closes the connection.
func (c *Conn) Close() error {
return c.conn.Close()
}
// A Reader implements convenience methods
// for reading requests or responses from a text protocol network connection.
type Reader struct {
R *textproto.Reader
}
// NewReader returns a new Reader reading from r.
func NewReader(r *bufio.Reader) *Reader {
return &Reader{R: textproto.NewReader(r)}
}
// ReadLine reads a single line from r,
// eliding the final \n or \r\n from the returned string.
// This calls textproto.Reader.ReadLine simply.
func (r *Reader) ReadLine() (string, error) {
return r.R.ReadLine()
// for debug
// l, err := r.R.ReadLine()
// log.Printf("> %s\n", l)
// return l, err
}
// ReadLines reads a multiline until the last line of the only period,
// and returns a each line at slice.
// it does not contain last period.
func (r *Reader) ReadLines() ([]string, error) {
var lines []string
var line string
var err error
for {
line, err = r.R.ReadLine()
if err != nil {
return nil, err
}
if line == "." {
return lines, nil
}
lines = append(lines, line)
}
}
// ReadToPeriod reads a multiline until the last line of the only period,
// and returns as a string.
// it does not contain last period.
func (r *Reader) ReadToPeriod() (string, error) {
lines, err := r.ReadLines()
if err != nil {
return "", err
}
return strings.Join(lines, "\r\n"), nil
}
// ReadResponse reads a single line from r,
// and parses reponse.
// if the response is -ERR or has some other errors,
// it returns error.
func (r *Reader) ReadResponse() (string, error) {
line, err := r.ReadLine()
if err != nil {
return "", err
}
return r.parseResponse(line)
}
func (r *Reader) parseResponse(line string) (string, error) {
s := strings.ToUpper(line)
if s == "+OK" {
return "", nil
} else if strings.HasPrefix(s, "+OK ") {
return line[4:], nil
} else if s == "-ERR" {
return "", ResponseError("")
} else if strings.HasPrefix(s, "-ERR ") {
return "", ResponseError(line[5:])
} else {
return "", ResponseError(fmt.Sprintf("unknown response: %s", line))
}
}
var crnl = []byte{'\r', '\n'}
// A Writer implements convenience methods
// for writing requests or responses to a text protocol network connection.
type Writer struct {
W *bufio.Writer
}
// NewWriter returns a new Writer writing to w.
func NewWriter(w *bufio.Writer) *Writer {
return &Writer{W: w}
}
// WriteLine writes the formatted output followed by \r\n.
func (w *Writer) WriteLine(format string, args ...interface{}) error {
var err error
if _, err = fmt.Fprintf(w.W, format, args...); err != nil {
return err
}
if _, err = w.W.Write(crnl); err != nil {
return err
}
return w.W.Flush()
// for debug
// var err error
// l := fmt.Sprintf(format, args...)
// if _, err = fmt.Fprint(w.W, l); err != nil {
// return err
// }
// if _, err = w.W.Write(crnl); err != nil {
// return err
// }
// if err = w.W.Flush(); err != nil {
// return err
// }
// log.Printf("< %s\n", l)
// return nil
}