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) {
94 conn.log.Error("unknown command")
95 conn.err("unknown command")
100 func (conn *connection) ok(msg string) {
104 conn.tp.PrintfLine("+OK%s", msg)
107 func (conn *connection) err(msg string) {
108 conn.log.Error("error", zap.String("message", msg))
111 conn.tp.PrintfLine("-ERR%s", msg)
115 func (conn *connection) doQUIT() {
116 defer conn.tp.Close()
119 err := conn.mb.Close()
121 conn.err("failed to remove some messages")
128 func (conn *connection) doUSER() {
129 if conn.state != stateAuth {
130 conn.err(errStateAuth)
134 conn.user = conn.line[len("USER "):]
138 func (conn *connection) doPASS() {
139 if conn.state != stateAuth {
140 conn.err(errStateAuth)
144 if len(conn.user) == 0 {
149 pass := conn.line[len("PASS "):]
150 if mbox, err := conn.po.OpenMailbox(conn.user, pass); err == nil {
151 conn.log.Info("authenticated", zap.String("user", conn.user))
152 conn.state = stateTxn
156 conn.log.Error("failed to open mailbox", zap.Error(err))
157 conn.err(err.Error())
161 func (conn *connection) doSTAT() {
162 if conn.state != stateTxn {
163 conn.err(errStateTxn)
167 msgs, err := conn.mb.ListMessages()
169 conn.log.Error("failed to list messages", zap.Error(err))
170 conn.err(err.Error())
176 for _, msg := range msgs {
184 conn.ok(fmt.Sprintf("%d %d", num, size))
187 func (conn *connection) doLIST() {
188 if conn.state != stateTxn {
189 conn.err(errStateTxn)
193 msgs, err := conn.mb.ListMessages()
195 conn.log.Error("failed to list messages", zap.Error(err))
196 conn.err(err.Error())
200 conn.ok("scan listing")
201 for _, msg := range msgs {
202 conn.tp.PrintfLine("%d %d", msg.ID(), msg.Size())
204 conn.tp.PrintfLine(".")
207 func (conn *connection) doRETR() {
208 if conn.state != stateTxn {
209 conn.err(errStateTxn)
213 msg := conn.getRequestedMessage()
219 conn.err(errDeletedMsg)
223 rc, err := conn.mb.Retrieve(msg)
225 conn.log.Error("failed to retrieve messages", zap.Error(err))
226 conn.err(err.Error())
230 conn.ok(fmt.Sprintf("%d", msg.Size()))
232 w := conn.tp.DotWriter()
237 func (conn *connection) doDELE() {
238 if conn.state != stateTxn {
239 conn.err(errStateTxn)
243 msg := conn.getRequestedMessage()
249 conn.err(errDeletedMsg)
253 if err := conn.mb.Delete(msg); err != nil {
254 conn.log.Error("failed to delete message", zap.Error(err))
255 conn.err(err.Error())
261 func (conn *connection) doRSET() {
262 if conn.state != stateTxn {
263 conn.err(errStateTxn)
270 func (conn *connection) getRequestedMessage() Message {
273 if _, err := fmt.Sscanf(conn.line, "%s %d", &cmd, &idx); err != nil {
279 conn.err("invalid message-number")
283 msg := conn.mb.GetMessage(idx)
285 conn.err("no such message")