-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
341 lines (326 loc) · 9.15 KB
/
main.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package main
import (
"bufio"
"errors"
"fmt"
"os"
imapsql "github.com/foxcpp/go-imap-sql"
"github.com/urfave/cli"
)
var backend *imapsql.Backend
var stdinScnr *bufio.Scanner
func connectToDB(ctx *cli.Context) error {
if ctx.GlobalIsSet("unsafe") && !ctx.GlobalIsSet("quiet") {
fmt.Fprintln(os.Stderr, "WARNING: Using --unsafe with running server may lead to accidential damage to data due to desynchronization with connected clients.")
}
driver := ctx.GlobalString("driver")
dsn := ctx.GlobalString("dsn")
fsstore := ctx.GlobalString("fsstore")
if driver == "" {
return errors.New("Error: driver is required")
}
if dsn == "" {
return errors.New("Error: dsn is required")
}
if fsstore == "" {
return errors.New("Error: fsstrore is required")
}
opts := imapsql.Opts{}
opts.NoWAL = ctx.GlobalIsSet("no-wal")
var err error
backend, err = imapsql.New(driver, dsn, &imapsql.FSStore{Root: fsstore}, opts)
if err != nil {
return err
}
return nil
}
func closeBackend(ctx *cli.Context) (err error) {
if backend != nil {
return backend.Close()
}
return nil
}
func main() {
stdinScnr = bufio.NewScanner(os.Stdin)
app := cli.NewApp()
app.Name = "imapsql-ctl"
app.Copyright = "(c) 2019 Max Mazurov <[email protected]>\n Published under the terms of the MIT license (https://opensource.org/licenses/MIT)"
app.Usage = "go-imap-sql database management utility"
app.Version = fmt.Sprintf("%s (go-imap-sql), %d (DB schema)", imapsql.VersionStr, imapsql.SchemaVersion)
app.After = closeBackend
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "driver",
Usage: "SQL driver to use for communication with DB",
EnvVar: "IMASPSQL_DRIVER",
},
cli.StringFlag{
Name: "dsn",
Usage: "Data Source Name to use\n\t\tWARNING: Provided only for debugging convenience. Don't leave your passwords in shell history!",
EnvVar: "IMAPSQL_DSN",
},
cli.BoolFlag{
Name: "quiet,q",
Usage: "Don't print user-friendly messages to stderr",
},
cli.BoolFlag{
Name: "unsafe",
Usage: "Allow to perform actions that can be safely done only without running server",
},
cli.BoolFlag{
Name: "allow-schema-upgrade",
Usage: "Allow go-imap-sql to automatically update database schema to version imapsql-ctl is compiled with\n\t\tWARNING: Make a backup before using this flag!",
},
cli.BoolFlag{
Name: "no-wal",
Usage: "(SQLite only) Don't force WAL mode",
},
cli.StringFlag{
Name: "fsstore",
Usage: "Use fsstore with specified directory",
EnvVar: "IMAPSQL_FSSTORE",
},
}
app.Commands = []cli.Command{
{
Name: "mboxes",
Usage: "Mailboxes (folders) management",
Subcommands: []cli.Command{
{
Name: "list",
Usage: "Show mailboxes of user",
ArgsUsage: "USERNAME",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "subscribed,s",
Usage: "List only subscribed mailboxes",
},
},
Action: mboxesList,
},
{
Name: "create",
Usage: "Create mailbox",
ArgsUsage: "USERNAME NAME",
Action: mboxesCreate,
Flags: []cli.Flag{
cli.StringFlag{
Name: "special",
Usage: "Set SPECIAL-USE attribute on mailbox; valid values: archive, drafts, junk, sent, trash",
},
},
},
{
Name: "remove",
Usage: "Remove mailbox (requires --unsafe)",
Description: "WARNING: All contents of mailbox will be irrecoverably lost.",
ArgsUsage: "USERNAME MAILBOX",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "yes,y",
Usage: "Don't ask for confirmation",
},
},
Action: mboxesRemove,
},
{
Name: "rename",
Usage: "Rename mailbox (requires --unsafe)",
Description: "Rename may cause unexpected failures on client-side so be careful.",
ArgsUsage: "USERNAME OLDNAME NEWNAME",
Action: mboxesRename,
},
{
Name: "appendlimit",
Usage: "Query or set user's APPENDLIMIT value",
ArgsUsage: "USERNAME",
Flags: []cli.Flag{
cli.IntFlag{
Name: "value,v",
Usage: "Set APPENDLIMIT to specified value (in bytes). Pass -1 to disable limit.",
},
},
Action: mboxesAppendLimit,
},
},
},
{
Name: "msgs",
Usage: "Messages management",
Subcommands: []cli.Command{
{
Name: "add",
Usage: "Add message to mailbox (requires --unsafe)",
ArgsUsage: "USERNAME MAILBOX",
Description: "Reads message body (with headers) from stdin. Prints UID of created message on success.",
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "flag,f",
Usage: "Add flag to message. Can be specified multiple times",
},
cli.Int64Flag{
Name: "date,d",
Usage: "Set internal date value to specified UNIX timestamp",
},
},
Action: msgsAdd,
},
{
Name: "add-flags",
Usage: "Add flags to messages (requires --unsafe)",
ArgsUsage: "USERNAME MAILBOX SEQ FLAGS...",
Description: "Add flags to all messages matched by SEQ.",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "uid,u",
Usage: "Use UIDs for SEQSET instead of sequence numbers",
},
},
Action: msgsFlags,
},
{
Name: "rem-flags",
Usage: "Remove flags from messages (requires --unsafe)",
ArgsUsage: "USERNAME MAILBOX SEQ FLAGS...",
Description: "Remove flags from all messages matched by SEQ.",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "uid,u",
Usage: "Use UIDs for SEQSET instead of sequence numbers",
},
},
Action: msgsFlags,
},
{
Name: "set-flags",
Usage: "Set flags on messages (requires --unsafe)",
ArgsUsage: "USERNAME MAILBOX SEQ FLAGS...",
Description: "Set flags on all messages matched by SEQ.",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "uid,u",
Usage: "Use UIDs for SEQSET instead of sequence numbers",
},
},
Action: msgsFlags,
},
{
Name: "remove",
Usage: "Remove messages from mailbox (requires --unsafe)",
ArgsUsage: "USERNAME MAILBOX SEQSET",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "uid,u",
Usage: "Use UIDs for SEQSET instead of sequence numbers",
},
cli.BoolFlag{
Name: "yes,y",
Usage: "Don't ask for confirmation",
},
},
Action: msgsRemove,
},
{
Name: "copy",
Usage: "Copy messages between mailboxes (requires --unsafe)",
Description: "Note: You can't copy between mailboxes of different users. APPENDLIMIT of target mailbox is not enforced.",
ArgsUsage: "USERNAME SRCMAILBOX SEQSET TGTMAILBOX",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "uid,u",
Usage: "Use UIDs for SEQSET instead of sequence numbers",
},
},
Action: msgsCopy,
},
{
Name: "move",
Usage: "Move messages between mailboxes (requires --unsafe)",
Description: "Note: You can't move between mailboxes of different users. APPENDLIMIT of target mailbox is not enforced.",
ArgsUsage: "USERNAME SRCMAILBOX SEQSET TGTMAILBOX",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "uid,u",
Usage: "Use UIDs for SEQSET instead of sequence numbers",
},
},
Action: msgsMove,
},
{
Name: "list",
Usage: "List messages in mailbox",
Description: "If SEQSET is specified - only show messages that match it.",
ArgsUsage: "USERNAME MAILBOX [SEQSET]",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "uid,u",
Usage: "Use UIDs for SEQSET instead of sequence numbers",
},
cli.BoolFlag{
Name: "full,f",
Usage: "Show entire envelope and all server meta-data",
},
},
Action: msgsList,
},
{
Name: "dump",
Usage: "Dump message body",
Description: "If passed SEQ matches multiple messages - they will be joined.",
ArgsUsage: "USERNAME MAILBOX SEQ",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "uid,u",
Usage: "Use UIDs for SEQ instead of sequence numbers",
},
},
Action: msgsDump,
},
},
},
{
Name: "users",
Usage: "User accounts management",
Subcommands: []cli.Command{
{
Name: "list",
Usage: "List created user accounts",
Action: usersList,
},
{
Name: "create",
Usage: "Create user account",
ArgsUsage: "USERNAME",
Action: usersCreate,
},
{
Name: "remove",
Usage: "Delete user account (requires --unsafe)",
ArgsUsage: "USERNAME",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "yes,y",
Usage: "Don't ask for confirmation",
},
},
Action: usersRemove,
},
{
Name: "appendlimit",
Usage: "Query or set user's APPENDLIMIT value",
ArgsUsage: "USERNAME",
Flags: []cli.Flag{
cli.IntFlag{
Name: "value,v",
Usage: "Set APPENDLIMIT to specified value (in bytes)",
},
},
Action: usersAppendLimit,
},
},
},
}
if err := app.Run(os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}