10 "github.com/uber-go/zap"
16 stateAuth state = iota
22 errStateAuth = "not in AUTHORIZATION"
23 errStateTxn = "not in TRANSACTION"
24 errSyntax = "syntax error"
25 errDeletedMsg = "no such message - deleted"
28 type connection struct {
43 func AcceptConnection(netConn net.Conn, po PostOffice, log zap.Logger) {
44 log = log.With(zap.Stringer("client", netConn.RemoteAddr()))
47 tp: textproto.NewConn(netConn),
52 conn.log.Info("accepted connection")
53 conn.ok(fmt.Sprintf("POP3 (mailpopbox) server %s", po.Name()))
58 conn.line, err = conn.tp.ReadLine()
60 conn.log.Error("ReadLine()", zap.Error(err))
66 if _, err := fmt.Sscanf(conn.line, "%s", &cmd); err != nil {
67 conn.err("invalid command")
71 conn.log = log.With(zap.String("command", cmd))
73 switch strings.ToUpper(cmd) {
96 conn.err("unknown command")
101 func (conn *connection) ok(msg string) {
105 conn.tp.PrintfLine("+OK%s", msg)
108 func (conn *connection) err(msg string) {
109 conn.log.Error("error", zap.String("message", msg))
112 conn.tp.PrintfLine("-ERR%s", msg)
116 func (conn *connection) doQUIT() {
117 defer conn.tp.Close()
120 err := conn.mb.Close()
122 conn.err("failed to remove some messages")
129 func (conn *connection) doUSER() {
130 if conn.state != stateAuth {
131 conn.err(errStateAuth)
136 if len(conn.line) < cmd {
137 conn.err("invalid user")
141 conn.user = conn.line[cmd:]
145 func (conn *connection) doPASS() {
146 if conn.state != stateAuth {
147 conn.err(errStateAuth)
151 if len(conn.user) == 0 {
157 if len(conn.line) < cmd {
158 conn.err("invalid pass")
162 pass := conn.line[cmd:]
163 if mbox, err := conn.po.OpenMailbox(conn.user, pass); err == nil {
164 conn.log.Info("authenticated", zap.String("user", conn.user))
165 conn.state = stateTxn
169 conn.log.Error("failed to open mailbox", zap.Error(err))
170 conn.err(err.Error())
174 func (conn *connection) doSTAT() {
175 if conn.state != stateTxn {
176 conn.err(errStateTxn)
180 msgs, err := conn.mb.ListMessages()
182 conn.log.Error("failed to list messages", zap.Error(err))
183 conn.err(err.Error())
189 for _, msg := range msgs {
197 conn.ok(fmt.Sprintf("%d %d", num, size))
200 func (conn *connection) doLIST() {
201 if conn.state != stateTxn {
202 conn.err(errStateTxn)
206 msgs, err := conn.mb.ListMessages()
208 conn.log.Error("failed to list messages", zap.Error(err))
209 conn.err(err.Error())
213 conn.ok("scan listing")
214 for _, msg := range msgs {
215 conn.tp.PrintfLine("%d %d", msg.ID(), msg.Size())
217 conn.tp.PrintfLine(".")
220 func (conn *connection) doRETR() {
221 if conn.state != stateTxn {
222 conn.err(errStateTxn)
226 msg := conn.getRequestedMessage()
232 conn.err(errDeletedMsg)
236 rc, err := conn.mb.Retrieve(msg)
238 conn.log.Error("failed to retrieve messages", zap.Error(err))
239 conn.err(err.Error())
243 conn.log.Info("retreive message", zap.String("unique-id", msg.UniqueID()))
244 conn.ok(fmt.Sprintf("%d", msg.Size()))
246 w := conn.tp.DotWriter()
251 func (conn *connection) doDELE() {
252 if conn.state != stateTxn {
253 conn.err(errStateTxn)
257 msg := conn.getRequestedMessage()
263 conn.err(errDeletedMsg)
267 if err := conn.mb.Delete(msg); err != nil {
268 conn.log.Error("failed to delete message", zap.Error(err))
269 conn.err(err.Error())
271 conn.log.Info("delete message", zap.String("unique-id", msg.UniqueID()))
276 func (conn *connection) doRSET() {
277 if conn.state != stateTxn {
278 conn.err(errStateTxn)
282 conn.log.Info("reset")
286 func (conn *connection) doUIDL() {
287 if conn.state != stateTxn {
288 conn.err(errStateTxn)
292 msgs, err := conn.mb.ListMessages()
294 conn.log.Error("failed to list messages", zap.Error(err))
295 conn.err(err.Error())
299 conn.ok("unique-id listing")
300 for _, msg := range msgs {
304 conn.tp.PrintfLine("%d %s", msg.ID(), msg.UniqueID())
306 conn.tp.PrintfLine(".")
309 func (conn *connection) getRequestedMessage() Message {
312 if _, err := fmt.Sscanf(conn.line, "%s %d", &cmd, &idx); err != nil {
318 conn.err("invalid message-number")
322 msg := conn.mb.GetMessage(idx)
324 conn.err("no such message")