Fix case sensitivity in the POP3 sever.
[mailpopbox.git] / pop3 / conn.go
1 package pop3
2
3 import (
4 "fmt"
5 "io"
6 "net"
7 "net/textproto"
8 "strings"
9 )
10
11 type state int
12
13 const (
14 stateAuth state = iota
15 stateTxn
16 stateUpdate
17 )
18
19 const (
20 errStateAuth = "not in AUTHORIZATION"
21 errStateTxn = "not in TRANSACTION"
22 errSyntax = "syntax error"
23 errDeletedMsg = "no such message - deleted"
24 )
25
26 type connection struct {
27 po PostOffice
28 mb Mailbox
29
30 tp *textproto.Conn
31 remoteAddr net.Addr
32
33 state
34 line string
35
36 user string
37 }
38
39 func AcceptConnection(netConn net.Conn, po PostOffice) {
40 conn := connection{
41 po: po,
42 tp: textproto.NewConn(netConn),
43 state: stateAuth,
44 }
45
46 var err error
47 conn.ok(fmt.Sprintf("POP3 (mailpopbox) server %s", po.Name()))
48
49 for {
50 conn.line, err = conn.tp.ReadLine()
51 if err != nil {
52 conn.err("did't catch that")
53 continue
54 }
55
56 var cmd string
57 if _, err := fmt.Sscanf(conn.line, "%s", &cmd); err != nil {
58 conn.err("invalid command")
59 continue
60 }
61
62 switch strings.ToUpper(cmd) {
63 case "QUIT":
64 conn.doQUIT()
65 break
66 case "USER":
67 conn.doUSER()
68 case "PASS":
69 conn.doPASS()
70 case "STAT":
71 conn.doSTAT()
72 case "LIST":
73 conn.doLIST()
74 case "RETR":
75 conn.doRETR()
76 case "DELE":
77 conn.doDELE()
78 case "NOOP":
79 conn.ok("")
80 case "RSET":
81 conn.doRSET()
82 default:
83 conn.err("unknown command")
84 }
85 }
86 }
87
88 func (conn *connection) ok(msg string) {
89 if len(msg) > 0 {
90 msg = " " + msg
91 }
92 conn.tp.PrintfLine("+OK%s", msg)
93 }
94
95 func (conn *connection) err(msg string) {
96 if len(msg) > 0 {
97 msg = " " + msg
98 conn.tp.PrintfLine("-ERR%s", msg)
99 }
100 }
101
102 func (conn *connection) doQUIT() {
103 defer conn.tp.Close()
104
105 if conn.mb != nil {
106 err := conn.mb.Close()
107 if err != nil {
108 conn.err("failed to remove some messages")
109 return
110 }
111 }
112 conn.ok("goodbye")
113 }
114
115 func (conn *connection) doUSER() {
116 if conn.state != stateAuth {
117 conn.err(errStateAuth)
118 return
119 }
120
121 conn.user = conn.line[len("USER "):]
122 conn.ok("")
123 }
124
125 func (conn *connection) doPASS() {
126 if conn.state != stateAuth {
127 conn.err(errStateAuth)
128 return
129 }
130
131 if len(conn.user) == 0 {
132 conn.err("no USER")
133 return
134 }
135
136 pass := conn.line[len("PASS "):]
137 if mbox, err := conn.po.OpenMailbox(conn.user, pass); err == nil {
138 conn.state = stateTxn
139 conn.mb = mbox
140 conn.ok("")
141 } else {
142 conn.err(err.Error())
143 }
144 }
145
146 func (conn *connection) doSTAT() {
147 if conn.state != stateTxn {
148 conn.err(errStateTxn)
149 return
150 }
151
152 msgs, err := conn.mb.ListMessages()
153 if err != nil {
154 conn.err(err.Error())
155 return
156 }
157
158 size := 0
159 num := 0
160 for _, msg := range msgs {
161 if msg.Deleted() {
162 continue
163 }
164 size += msg.Size()
165 num++
166 }
167
168 conn.ok(fmt.Sprintf("%d %d", num, size))
169 }
170
171 func (conn *connection) doLIST() {
172 if conn.state != stateTxn {
173 conn.err(errStateTxn)
174 return
175 }
176
177 msgs, err := conn.mb.ListMessages()
178 if err != nil {
179 conn.err(err.Error())
180 return
181 }
182
183 conn.ok("scan listing")
184 for _, msg := range msgs {
185 conn.tp.PrintfLine("%d %d", msg.ID(), msg.Size())
186 }
187 conn.tp.PrintfLine(".")
188 }
189
190 func (conn *connection) doRETR() {
191 if conn.state != stateTxn {
192 conn.err(errStateTxn)
193 return
194 }
195
196 msg := conn.getRequestedMessage()
197 if msg == nil {
198 return
199 }
200
201 if msg.Deleted() {
202 conn.err(errDeletedMsg)
203 return
204 }
205
206 rc, err := conn.mb.Retrieve(msg)
207 if err != nil {
208 conn.err(err.Error())
209 return
210 }
211
212 w := conn.tp.DotWriter()
213 io.Copy(w, rc)
214 w.Close()
215 }
216
217 func (conn *connection) doDELE() {
218 if conn.state != stateTxn {
219 conn.err(errStateTxn)
220 return
221 }
222
223 msg := conn.getRequestedMessage()
224 if msg == nil {
225 return
226 }
227
228 if msg.Deleted() {
229 conn.err(errDeletedMsg)
230 return
231 }
232
233 if err := conn.mb.Delete(msg); err != nil {
234 conn.err(err.Error())
235 } else {
236 conn.ok("")
237 }
238 }
239
240 func (conn *connection) doRSET() {
241 if conn.state != stateTxn {
242 conn.err(errStateTxn)
243 return
244 }
245 conn.mb.Reset()
246 conn.ok("")
247 }
248
249 func (conn *connection) getRequestedMessage() Message {
250 var cmd string
251 var idx int
252 if _, err := fmt.Sscanf(conn.line, "%s %d", &cmd, &idx); err != nil {
253 conn.err(errSyntax)
254 return nil
255 }
256
257 if idx < 1 {
258 conn.err("invalid message-number")
259 return nil
260 }
261
262 msg := conn.mb.GetMessage(idx)
263 if msg == nil {
264 conn.err("no such message")
265 return nil
266 }
267 return msg
268 }