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.log.Error("unknown command")
97 conn.err("unknown command")
102 func (conn *connection) ok(msg string) {
106 conn.tp.PrintfLine("+OK%s", msg)
109 func (conn *connection) err(msg string) {
110 conn.log.Error("error", zap.String("message", msg))
113 conn.tp.PrintfLine("-ERR%s", msg)
117 func (conn *connection) doQUIT() {
118 defer conn.tp.Close()
121 err := conn.mb.Close()
123 conn.err("failed to remove some messages")
130 func (conn *connection) doUSER() {
131 if conn.state != stateAuth {
132 conn.err(errStateAuth)
136 conn.user = conn.line[len("USER "):]
140 func (conn *connection) doPASS() {
141 if conn.state != stateAuth {
142 conn.err(errStateAuth)
146 if len(conn.user) == 0 {
151 pass := conn.line[len("PASS "):]
152 if mbox, err := conn.po.OpenMailbox(conn.user, pass); err == nil {
153 conn.log.Info("authenticated", zap.String("user", conn.user))
154 conn.state = stateTxn
158 conn.log.Error("failed to open mailbox", zap.Error(err))
159 conn.err(err.Error())
163 func (conn *connection) doSTAT() {
164 if conn.state != stateTxn {
165 conn.err(errStateTxn)
169 msgs, err := conn.mb.ListMessages()
171 conn.log.Error("failed to list messages", zap.Error(err))
172 conn.err(err.Error())
178 for _, msg := range msgs {
186 conn.ok(fmt.Sprintf("%d %d", num, size))
189 func (conn *connection) doLIST() {
190 if conn.state != stateTxn {
191 conn.err(errStateTxn)
195 msgs, err := conn.mb.ListMessages()
197 conn.log.Error("failed to list messages", zap.Error(err))
198 conn.err(err.Error())
202 conn.ok("scan listing")
203 for _, msg := range msgs {
204 conn.tp.PrintfLine("%d %d", msg.ID(), msg.Size())
206 conn.tp.PrintfLine(".")
209 func (conn *connection) doRETR() {
210 if conn.state != stateTxn {
211 conn.err(errStateTxn)
215 msg := conn.getRequestedMessage()
221 conn.err(errDeletedMsg)
225 rc, err := conn.mb.Retrieve(msg)
227 conn.log.Error("failed to retrieve messages", zap.Error(err))
228 conn.err(err.Error())
232 conn.ok(fmt.Sprintf("%d", msg.Size()))
234 w := conn.tp.DotWriter()
239 func (conn *connection) doDELE() {
240 if conn.state != stateTxn {
241 conn.err(errStateTxn)
245 msg := conn.getRequestedMessage()
251 conn.err(errDeletedMsg)
255 if err := conn.mb.Delete(msg); err != nil {
256 conn.log.Error("failed to delete message", zap.Error(err))
257 conn.err(err.Error())
263 func (conn *connection) doRSET() {
264 if conn.state != stateTxn {
265 conn.err(errStateTxn)
272 func (conn *connection) doUIDL() {
273 if conn.state != stateTxn {
274 conn.err(errStateTxn)
278 msgs, err := conn.mb.ListMessages()
280 conn.log.Error("failed to list messages", zap.Error(err))
281 conn.err(err.Error())
285 conn.ok("unique-id listing")
286 for _, msg := range msgs {
290 conn.tp.PrintfLine("%d %s", msg.ID(), msg.UniqueID())
292 conn.tp.PrintfLine(".")
295 func (conn *connection) getRequestedMessage() Message {
298 if _, err := fmt.Sscanf(conn.line, "%s %d", &cmd, &idx); err != nil {
304 conn.err("invalid message-number")
308 msg := conn.mb.GetMessage(idx)
310 conn.err("no such message")