forked from TheCreeper/go-pop3
/
pop3.go
340 lines (300 loc) · 7.2 KB
/
pop3.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Package pop3 provides an implementation of the Post Office
// Protocol - Version 3.
package pop3
import (
"bufio"
"crypto/tls"
"errors"
"fmt"
"io"
"net"
"net/mail"
"strconv"
"strings"
)
// Dial connects to the address on the named network.
func Dial(address string) (c *Client, err error) {
conn, err := net.Dial("tcp", address)
if err != nil {
return
}
return NewClient(conn)
}
// DialTLS connects to the address on the named network using tls.
func DialTLS(address string) (c *Client, err error) {
conn, err := tls.Dial("tcp", address, nil)
if err != nil {
return
}
return NewClient(conn)
}
// NewClient returns a new client object using an existing connection.
func NewClient(conn net.Conn) (c *Client, err error) {
c = &Client{
conn: conn,
r: bufio.NewReader(conn),
w: bufio.NewWriter(conn),
}
// Make sure we receive the server greeting
line, err := c.ReadLine()
if err != nil {
return
}
if !IsOK(line) {
return nil, errors.New("pop3: Server did not respond with +OK")
}
return
}
// ReadLine reads a single line from the buffer.
func (c *Client) ReadLine() (line string, err error) {
b, _, err := c.r.ReadLine()
if err == io.EOF {
return
}
if err != nil {
return
}
line = string(b)
return
}
// ReadLines reads from the buffer until it hits the message end dot (".").
func (c *Client) ReadLines() (lines []string, err error) {
for {
line, err := c.ReadLine()
if err != nil {
return nil, err
}
// Look for a dot to indicate the end of a message
// from the server.
if line == "." {
break
}
lines = append(lines, line)
}
return
}
// Send writes a command to the buffer and flushes it. Does not return any
// lines from the buffer.
func (c *Client) Send(format string, args ...interface{}) (err error) {
if _, err = c.w.WriteString(fmt.Sprintf(format, args...)); err != nil {
return
}
return c.w.Flush()
}
// Cmd sends a command to the server and returns a single line from the buffer.
func (c *Client) Cmd(format string,
args ...interface{}) (line string, err error) {
if err = c.Send(format, args...); err != nil {
return
}
line, err = c.ReadLine()
if err != nil {
return
}
if !IsOK(line) {
return "", errors.New("pop3: Server did not respond with +OK")
}
return
}
// User sends the username to the server.
func (c *Client) User(u string) (err error) {
if _, err = c.Cmd("%s %s\r\n", USER, u); err != nil {
return
}
return
}
// Pass sends the password to the server.
func (c *Client) Pass(p string) (err error) {
if _, err = c.Cmd("%s %s\r\n", PASS, p); err != nil {
return
}
return
}
// Quit sends the quit command to the server and closes the socket.
func (c *Client) Quit() (err error) {
if err = c.Send("%s\r\n", QUIT); err != nil {
return
}
return c.conn.Close()
}
// Auth sends the username and password to the server using the User and
// Pass methods. Noop is also called incase the server does not respond
// with invalid auth.
func (c *Client) Auth(u, p string) (err error) {
if err = c.User(u); err != nil {
return
}
if err = c.Pass(p); err != nil {
return
}
// issue a dud command. Server might not respond with invalid auth
// unless a cmd is issued
return c.Noop()
}
// Stat retreives a listing for the current maildrop, consisting of the number
// of messages and the total size of the maildrop.
func (c *Client) Stat() (count, size int, err error) {
line, err := c.Cmd("%s\r\n", STAT)
if err != nil {
return
}
// Number of messages in maildrop
count, err = strconv.Atoi(strings.Fields(line)[1])
if err != nil {
return
}
if count == 0 {
return
}
// Total size of messages in bytes
size, err = strconv.Atoi(strings.Fields(line)[2])
if err != nil {
return
}
if size == 0 {
return
}
return
}
// List returns the MessageList object which contains the message non unique
// id and its size.
func (c *Client) List(msg int) (list MessageList, err error) {
line, err := c.Cmd("%s %s\r\n", LIST, msg)
if err != nil {
return
}
id, err := strconv.Atoi(strings.Fields(line)[0])
if err != nil {
return
}
size, err := strconv.Atoi(strings.Fields(line)[1])
if err != nil {
return
}
return MessageList{id, size}, nil
}
// ListAll returns a MessageList object which contains all messages in the
// maildrop.
func (c *Client) ListAll() (list []MessageList, err error) {
if _, err = c.Cmd("%s\r\n", LIST); err != nil {
return
}
lines, err := c.ReadLines()
if err != nil {
return
}
for _, v := range lines {
id, err := strconv.Atoi(strings.Fields(v)[0])
if err != nil {
return nil, err
}
size, err := strconv.Atoi(strings.Fields(v)[1])
if err != nil {
return nil, err
}
list = append(list, MessageList{id, size})
}
return
}
// Retr downloads the given message and returns it as a mail.Message object.
func (c *Client) Retr(msg int) (m string, err error) {
if _, err = c.Cmd("%s %s\r\n", RETR, msg); err != nil {
return
}
m, err = c.r
if err != nil {
return
}
// mail.ReadMessage does not consume the message end dot in the buffer
// so we must move the buffer along. Need to find a better way of
// doing this.
line, err := c.ReadLine()
if err != nil {
return
}
if line != "." {
if err = c.r.UnreadByte(); err != nil {
return
}
}
return
}
// Dele will delete the given message from the maildrop.
// Changes will only take affect after the Quit command is issued.
func (c *Client) Dele(msg int) (err error) {
if _, err = c.Cmd("%s %s\r\n", DELE, msg); err != nil {
return
}
return
}
// Noop will do nothing however can prolong the end of a connection.
func (c *Client) Noop() (err error) {
if _, err = c.Cmd("%s\r\n", NOOP); err != nil {
return
}
return
}
// Rset will unmark any messages that have being marked for deletion in
// the current session.
func (c *Client) Rset() (err error) {
if _, err = c.Cmd("%s\r\n", RSET); err != nil {
return
}
return
}
// Top will return a varible number of lines for a given message as a
// mail.Message object.
func (c *Client) Top(msg int, n int) (m *mail.Message, err error) {
if _, err = c.Cmd("%s %d %d\r\n", TOP, msg, n); err != nil {
return
}
m, err = mail.ReadMessage(c.r)
if err != nil {
return
}
// mail.ReadMessage does not consume the message end dot in the buffer
// so we must move the buffer along. Need to find a better way of
// doing this.
line, err := c.ReadLine()
if err != nil {
return
}
if line != "." {
if err = c.r.UnreadByte(); err != nil {
return
}
}
return
}
// Uidl will return a MessageUidl object which contains the message non
// unique id and a unique id.
func (c *Client) Uidl(msg int) (list MessageUidl, err error) {
line, err := c.Cmd("%s %s\r\n", UIDL, msg)
if err != nil {
return
}
id, err := strconv.Atoi(strings.Fields(line)[1])
if err != nil {
return
}
return MessageUidl{id, strings.Fields(line)[2]}, nil
}
// UidlAll will return a MessageUidl object which contains all messages in
// the maildrop.
func (c *Client) UidlAll() (list []MessageUidl, err error) {
if _, err = c.Cmd("%s\r\n", UIDL); err != nil {
return
}
lines, err := c.ReadLines()
if err != nil {
return
}
for _, v := range lines {
id, err := strconv.Atoi(strings.Fields(v)[0])
if err != nil {
return nil, err
}
list = append(list, MessageUidl{id, strings.Fields(v)[1]})
}
return
}