-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd.go
372 lines (335 loc) · 10.3 KB
/
cmd.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package botcli
import (
"fmt"
"strings"
"github.com/deltachat/deltachat-rpc-client-go/deltachat"
"github.com/deltachat/deltachat-rpc-client-go/deltachat/option"
"github.com/spf13/cobra"
)
func initializeRootCmd(cli *BotCli) {
defDir := getDefaultAppDir(cli.AppName)
cli.RootCmd.PersistentFlags().StringVarP(&cli.AppDir, "folder", "f", defDir, "program's data folder")
cli.RootCmd.PersistentFlags().StringVarP(&cli.SelectedAddr, "account", "a", "", "operate over this account only when running any subcommand")
initCmd := &cobra.Command{
Use: "init",
Short: "do initial login configuration of a new Delta Chat account, if the account already exist, the credentials are updated",
Args: cobra.ExactArgs(2),
}
cli.AddCommand(initCmd, initCallback)
listCmd := &cobra.Command{
Use: "list",
Short: "show a list of existing bot accounts",
Args: cobra.ExactArgs(0),
}
cli.AddCommand(listCmd, listCallback)
removeCmd := &cobra.Command{
Use: "remove",
Short: "remove Delta Chat accounts from the bot",
Args: cobra.ExactArgs(0),
}
cli.AddCommand(removeCmd, removeCallback)
configCmd := &cobra.Command{
Use: "config",
Short: "set/get account configuration values",
Args: cobra.MaximumNArgs(2),
}
cli.AddCommand(configCmd, configCallback)
serveCmd := &cobra.Command{
Use: "serve",
Short: "start processing messages",
Args: cobra.ExactArgs(0),
}
cli.AddCommand(serveCmd, serveCallback)
qrCmd := &cobra.Command{
Use: "qr",
Short: "get bot's verification QR",
Args: cobra.ExactArgs(0),
}
qrCmd.Flags().BoolP("invert", "i", false, "invert QR colors")
cli.AddCommand(qrCmd, qrCallback)
adminCmd := &cobra.Command{
Use: "admin",
Short: "get the invitation QR to the bot administration group, WARNING: don't share this QR",
Args: cobra.ExactArgs(0),
}
adminCmd.Flags().BoolP("invert", "i", false, "invert QR colors")
adminCmd.Flags().BoolP("reset", "r", false, "reset admin chat, removes all existing admins")
cli.AddCommand(adminCmd, adminCallback)
}
func initCallback(cli *BotCli, bot *deltachat.Bot, cmd *cobra.Command, args []string) {
bot.On(deltachat.EventConfigureProgress{}, func(bot *deltachat.Bot, accId deltachat.AccountId, event deltachat.Event) {
ev := event.(deltachat.EventConfigureProgress)
addr, _ := cli.GetAddress(bot.Rpc, accId)
if addr == "" {
addr = fmt.Sprintf("account #%v", accId)
}
cli.Logger.Infof("[%v] Configuration progress: %v", addr, ev.Progress)
})
var accId deltachat.AccountId
var err error
if cli.SelectedAddr == "" { // auto-select based on first argument (or create a new one if not found)
accId, err = cli.GetOrCreateAccount(bot.Rpc, args[0])
} else { // re-configure the selected account
accId, err = cli.GetAccount(bot.Rpc, cli.SelectedAddr)
if err == nil {
_, err = cli.GetAccount(bot.Rpc, args[0])
if err == nil {
cli.Logger.Errorf("Configuration failed: an account with address %q already exists", args[0])
return
}
}
}
if err != nil {
cli.Logger.Errorf("Configuration failed: %v", err)
return
}
go func() {
if err := bot.Configure(accId, args[0], args[1]); err != nil {
cli.Logger.Errorf("Configuration failed: %v", err)
} else {
cli.Logger.Infof("Account %q configured successfully.", args[0])
}
bot.Stop()
}()
bot.Run() //nolint:errcheck
}
func configCallback(cli *BotCli, bot *deltachat.Bot, cmd *cobra.Command, args []string) {
var err error
var accounts []deltachat.AccountId
if cli.SelectedAddr == "" { // set config for all accounts
accounts, err = bot.Rpc.GetAllAccountIds()
} else {
var accId deltachat.AccountId
accId, err = cli.GetAccount(bot.Rpc, cli.SelectedAddr)
accounts = []deltachat.AccountId{accId}
}
if err != nil {
cli.Logger.Error(err)
return
}
for _, accId := range accounts {
addr, err := cli.GetAddress(bot.Rpc, accId)
if err != nil {
cli.Logger.Error(err)
continue
}
fmt.Printf("Account #%v (%v):\n", accId, addr)
configForAcc(cli, bot, cmd, args, accId)
fmt.Println("")
}
if len(accounts) == 0 {
cli.Logger.Errorf("There are no accounts yet, add a new account using the init subcommand")
}
}
func configForAcc(cli *BotCli, bot *deltachat.Bot, cmd *cobra.Command, args []string, accId deltachat.AccountId) {
if len(args) == 0 {
keys, _ := bot.Rpc.GetConfig(accId, "sys.config_keys")
for _, key := range strings.Fields(keys.Unwrap()) {
val, _ := bot.Rpc.GetConfig(accId, key)
fmt.Printf("%v=%q\n", key, val.UnwrapOr(""))
}
return
}
var val option.Option[string]
var err error
if len(args) == 2 {
err = bot.Rpc.SetConfig(accId, args[0], option.Some(args[1]))
}
if err == nil {
val, err = bot.Rpc.GetConfig(accId, args[0])
}
if err == nil {
fmt.Printf("%v=%v\n", args[0], val.UnwrapOr(""))
} else {
cli.Logger.Error(err)
}
}
func serveCallback(cli *BotCli, bot *deltachat.Bot, cmd *cobra.Command, args []string) {
if cli.SelectedAddr != "" {
cli.Logger.Errorf("operation not supported for a single account, discard the -a/--account option and try again")
return
}
accounts, err := bot.Rpc.GetAllAccountIds()
if err != nil {
cli.Logger.Error(err)
return
}
var addrs []string
for _, accId := range accounts {
if isConf, _ := bot.Rpc.IsConfigured(accId); !isConf {
cli.Logger.Errorf("account #%v not configured", accId)
} else {
addr, _ := bot.Rpc.GetConfig(accId, "configured_addr")
if addr.UnwrapOr("") != "" {
addrs = append(addrs, addr.Unwrap())
}
}
}
if len(addrs) != 0 {
cli.Logger.Infof("Listening at: %v", strings.Join(addrs, ", "))
if cli.onStart != nil {
cli.onStart(cli, bot, cmd, args)
}
bot.Run() //nolint:errcheck
} else {
cli.Logger.Errorf("There are no configured accounts to serve")
}
}
func qrCallback(cli *BotCli, bot *deltachat.Bot, cmd *cobra.Command, args []string) {
var err error
var accounts []deltachat.AccountId
if cli.SelectedAddr == "" { // for all accounts
accounts, err = bot.Rpc.GetAllAccountIds()
} else {
var accId deltachat.AccountId
accId, err = cli.GetAccount(bot.Rpc, cli.SelectedAddr)
accounts = []deltachat.AccountId{accId}
}
if err != nil {
cli.Logger.Error(err)
return
}
for _, accId := range accounts {
addr, err := cli.GetAddress(bot.Rpc, accId)
if err != nil {
cli.Logger.Error(err)
continue
}
fmt.Printf("Account #%v (%v):\n", accId, addr)
qrForAcc(cli, bot, cmd, args, accId, addr)
fmt.Println("")
}
if len(accounts) == 0 {
cli.Logger.Errorf("There are no accounts yet, add a new account using the init subcommand")
}
}
func qrForAcc(cli *BotCli, bot *deltachat.Bot, cmd *cobra.Command, args []string, accId deltachat.AccountId, addr string) {
if isConf, _ := bot.Rpc.IsConfigured(accId); isConf {
qrdata, _, err := bot.Rpc.GetChatSecurejoinQrCodeSvg(accId, option.None[deltachat.ChatId]())
if err != nil {
cli.Logger.Errorf("Failed to generate QR: %v", err)
return
}
fmt.Println("Scan this QR to verify", addr)
invert, _ := cmd.Flags().GetBool("invert")
printQr(qrdata, invert)
fmt.Println(qrdata)
} else {
cli.Logger.Error("account not configured")
}
}
func adminCallback(cli *BotCli, bot *deltachat.Bot, cmd *cobra.Command, args []string) {
var err error
var accounts []deltachat.AccountId
if cli.SelectedAddr == "" { // for all accounts
accounts, err = bot.Rpc.GetAllAccountIds()
if err == nil && len(accounts) == 0 {
cli.Logger.Errorf("There are no accounts yet, add a new account using the init subcommand")
}
} else {
var accId deltachat.AccountId
accId, err = cli.GetAccount(bot.Rpc, cli.SelectedAddr)
accounts = []deltachat.AccountId{accId}
}
if err != nil {
cli.Logger.Error(err)
return
}
for _, accId := range accounts {
addr, err := cli.GetAddress(bot.Rpc, accId)
if err != nil {
cli.Logger.Error(err)
continue
}
fmt.Printf("Account #%v (%v):\n", accId, addr)
adminForAcc(cli, bot, cmd, args, accId)
fmt.Println("")
}
}
func adminForAcc(cli *BotCli, bot *deltachat.Bot, cmd *cobra.Command, args []string, accId deltachat.AccountId) {
if isConf, _ := bot.Rpc.IsConfigured(accId); !isConf {
cli.Logger.Error("account not configured")
return
}
errMsg := "Failed to generate QR: %v"
reset, err := cmd.Flags().GetBool("reset")
if err != nil {
cli.Logger.Errorf(errMsg, err)
return
}
var chatId deltachat.ChatId
if reset {
chatId, err = cli.ResetAdminChat(bot, accId)
} else {
chatId, err = cli.AdminChat(bot, accId)
}
if err != nil {
cli.Logger.Errorf(errMsg, err)
return
}
qrdata, _, err := bot.Rpc.GetChatSecurejoinQrCodeSvg(accId, option.Some(chatId))
if err != nil {
cli.Logger.Errorf(errMsg, err)
return
}
fmt.Println("Scan this QR to become bot administrator")
invert, _ := cmd.Flags().GetBool("invert")
printQr(qrdata, invert)
fmt.Println(qrdata)
}
func listCallback(cli *BotCli, bot *deltachat.Bot, cmd *cobra.Command, args []string) {
if cli.SelectedAddr != "" {
cli.Logger.Errorf("operation not supported for a single account, discard the -a/--account option and try again")
return
}
accounts, err := bot.Rpc.GetAllAccountIds()
if err != nil {
cli.Logger.Error(err)
return
}
for _, accId := range accounts {
addr, err := cli.GetAddress(bot.Rpc, accId)
if err != nil {
cli.Logger.Error(err)
continue
}
if isConf, _ := bot.Rpc.IsConfigured(accId); !isConf {
addr = addr + " (not configured)"
}
fmt.Printf("#%v - %v\n", accId, addr)
}
}
func removeCallback(cli *BotCli, bot *deltachat.Bot, cmd *cobra.Command, args []string) {
var err error
var accounts []deltachat.AccountId
if cli.SelectedAddr == "" { // for all accounts
accounts, err = bot.Rpc.GetAllAccountIds()
if err == nil && len(accounts) == 0 {
cli.Logger.Errorf("There are no accounts yet, add a new account using the init subcommand")
}
} else {
var accId deltachat.AccountId
accId, err = cli.GetAccount(bot.Rpc, cli.SelectedAddr)
accounts = []deltachat.AccountId{accId}
}
if err != nil {
cli.Logger.Error(err)
return
}
if len(accounts) > 1 {
cli.Logger.Error("There are more than one account, to remove one of them, pass the account address with -a/--account option")
return
}
for _, accId := range accounts {
addr, err := cli.GetAddress(bot.Rpc, accId)
if err != nil {
cli.Logger.Error(err)
}
err = bot.Rpc.RemoveAccount(accId)
if err != nil {
cli.Logger.Error(err)
} else {
cli.Logger.Infof("Account #%v (%q) removed successfully.", accId, addr)
}
}
}