-
Notifications
You must be signed in to change notification settings - Fork 6
/
command.go
260 lines (217 loc) · 6.57 KB
/
command.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
package popgun
import (
"fmt"
"strconv"
"strings"
)
type Executable interface {
Run(c *Client, args []string) (int, error)
}
type QuitCommand struct{}
func (cmd QuitCommand) Run(c *Client, args []string) (int, error) {
newState := c.currentState
if c.currentState == STATE_TRANSACTION {
err := c.backend.Update(c.user)
if err != nil {
return 0, fmt.Errorf("Error updating maildrop for user %s: %v", c.user, err)
}
err = c.backend.Unlock(c.user)
if err != nil {
c.printer.Err("Server was unable to unlock maildrop")
return 0, fmt.Errorf("Error unlocking maildrop for user %s: %v", c.user, err)
}
newState = STATE_UPDATE
}
c.isAlive = false
c.printer.Ok("Goodbye")
return newState, nil
}
type UserCommand struct{}
func (cmd UserCommand) Run(c *Client, args []string) (int, error) {
if c.currentState != STATE_AUTHORIZATION {
return 0, ErrInvalidState
}
if len(args) != 1 {
return 0, fmt.Errorf("Invalid arguments count: %d", len(args))
}
c.user = args[0]
c.printer.Ok("")
return STATE_AUTHORIZATION, nil
}
type PassCommand struct{}
func (cmd PassCommand) Run(c *Client, args []string) (int, error) {
if c.currentState != STATE_AUTHORIZATION {
return 0, ErrInvalidState
}
if c.lastCommand != "USER" {
c.printer.Err("PASS can be executed only directly after USER command")
return STATE_AUTHORIZATION, nil
}
if len(args) != 1 {
return 0, fmt.Errorf("Invalid arguments count: %d", len(args))
}
c.pass = args[0]
if !c.authorizator.Authorize(c.user, c.pass) {
c.printer.Err("Invalid username or password")
return STATE_AUTHORIZATION, nil
}
err := c.backend.Lock(c.user)
if err != nil {
c.printer.Err("Server was unable to lock maildrop")
return 0, fmt.Errorf("Error locking maildrop for user %s: %v", c.user, err)
}
c.printer.Ok("User Successfully Logged on")
return STATE_TRANSACTION, nil
}
type StatCommand struct{}
func (cmd StatCommand) Run(c *Client, args []string) (int, error) {
if c.currentState != STATE_TRANSACTION {
return 0, ErrInvalidState
}
messages, octets, err := c.backend.Stat(c.user)
if err != nil {
return 0, fmt.Errorf("Error calling Stat for user %s: %v", c.user, err)
}
c.printer.Ok("%d %d", messages, octets)
return STATE_TRANSACTION, nil
}
type ListCommand struct{}
func (cmd ListCommand) Run(c *Client, args []string) (int, error) {
if c.currentState != STATE_TRANSACTION {
return 0, ErrInvalidState
}
if len(args) > 0 {
msgId, err := strconv.Atoi(args[0])
if err != nil {
c.printer.Err("Invalid argument: %s", args[0])
return 0, fmt.Errorf("Invalid argument for LIST given by user %s: %v", c.user, err)
}
exists, octets, err := c.backend.ListMessage(c.user, msgId)
if err != nil {
return 0, fmt.Errorf("Error calling 'LIST %d' for user %s: %v", msgId, c.user, err)
}
if !exists {
c.printer.Err("no such message")
return STATE_TRANSACTION, nil
}
c.printer.Ok("%d %d", msgId, octets)
} else {
octets, err := c.backend.List(c.user)
if err != nil {
return 0, fmt.Errorf("Error calling LIST for user %s: %v", c.user, err)
}
c.printer.Ok("%d messages", len(octets))
messagesList := make([]string, len(octets))
for i, octet := range octets {
messagesList[i] = fmt.Sprintf("%d %d", i, octet)
}
c.printer.MultiLine(messagesList)
}
return STATE_TRANSACTION, nil
}
type RetrCommand struct{}
func (cmd RetrCommand) Run(c *Client, args []string) (int, error) {
if c.currentState != STATE_TRANSACTION {
return 0, ErrInvalidState
}
if len(args) == 0 {
c.printer.Err("Missing argument for RETR command")
return 0, fmt.Errorf("Missing argument for RETR called by user %s", c.user)
}
msgId, err := strconv.Atoi(args[0])
if err != nil {
c.printer.Err("Invalid argument: %s", args[0])
return 0, fmt.Errorf("Invalid argument for RETR given by user %s: %v", c.user, err)
}
message, err := c.backend.Retr(c.user, msgId)
if err != nil {
return 0, fmt.Errorf("Error calling 'RETR %d' for user %s: %v", msgId, c.user, err)
}
lines := strings.Split(message, "\n")
c.printer.Ok("")
c.printer.MultiLine(lines)
return STATE_TRANSACTION, nil
}
type DeleCommand struct{}
func (cmd DeleCommand) Run(c *Client, args []string) (int, error) {
if c.currentState != STATE_TRANSACTION {
return 0, ErrInvalidState
}
if len(args) == 0 {
c.printer.Err("Missing argument for DELE command")
return 0, fmt.Errorf("Missing argument for DELE called by user %s", c.user)
}
msgId, err := strconv.Atoi(args[0])
if err != nil {
c.printer.Err("Invalid argument: %s", args[0])
return 0, fmt.Errorf("Invalid argument for DELE given by user %s: %v", c.user, err)
}
err = c.backend.Dele(c.user, msgId)
if err != nil {
return 0, fmt.Errorf("Error calling 'DELE %d' for user %s: %v", msgId, c.user, err)
}
c.printer.Ok("Message %d deleted", msgId)
return STATE_TRANSACTION, nil
}
type NoopCommand struct{}
func (cmd NoopCommand) Run(c *Client, args []string) (int, error) {
if c.currentState != STATE_TRANSACTION {
return 0, ErrInvalidState
}
c.printer.Ok("")
return STATE_TRANSACTION, nil
}
type RsetCommand struct{}
func (cmd RsetCommand) Run(c *Client, args []string) (int, error) {
if c.currentState != STATE_TRANSACTION {
return 0, ErrInvalidState
}
err := c.backend.Rset(c.user)
if err != nil {
return 0, fmt.Errorf("Error calling 'RSET' for user %s: %v", c.user, err)
}
c.printer.Ok("")
return STATE_TRANSACTION, nil
}
type UidlCommand struct{}
func (cmd UidlCommand) Run(c *Client, args []string) (int, error) {
if c.currentState != STATE_TRANSACTION {
return 0, ErrInvalidState
}
if len(args) > 0 {
msgId, err := strconv.Atoi(args[0])
if err != nil {
c.printer.Err("Invalid argument: %s", args[0])
return 0, fmt.Errorf("Invalid argument for UIDL given by user %s: %v", c.user, err)
}
exists, uid, err := c.backend.UidlMessage(c.user, msgId)
if err != nil {
return 0, fmt.Errorf("Error calling 'UIDL %d' for user %s: %v", msgId, c.user, err)
}
if !exists {
c.printer.Err("no such message")
return STATE_TRANSACTION, nil
}
c.printer.Ok("%d %s", msgId, uid)
} else {
uids, err := c.backend.Uidl(c.user)
if err != nil {
return 0, fmt.Errorf("Error calling UIDL for user %s: %v", c.user, err)
}
c.printer.Ok("%d messages", len(uids))
uidsList := make([]string, len(uids))
for i, uid := range uids {
uidsList[i] = fmt.Sprintf("%d %s", i, uid)
}
c.printer.MultiLine(uidsList)
}
return STATE_TRANSACTION, nil
}
type CapaCommand struct{}
func (cmd CapaCommand) Run(c *Client, args []string) (int, error) {
c.printer.Ok("")
var commands []string
commands = []string{"USER", "UIDL"}
c.printer.MultiLine(commands)
return c.currentState, nil
}