From bc30f59e964f6fadc527c5ac1dd0752fdd5ff17c Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Mon, 29 Aug 2022 14:25:14 +0300 Subject: [PATCH] Move owner commands to their own file --- bot/command.go | 100 ---------------------------------------- bot/owner_command.go | 106 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 100 deletions(-) create mode 100644 bot/owner_command.go diff --git a/bot/command.go b/bot/command.go index 79b34a4..b6931bc 100644 --- a/bot/command.go +++ b/bot/command.go @@ -223,103 +223,3 @@ func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) { b.SendNotice(ctx, roomID, msg.String()) } - -func (b *Bot) runStop(ctx context.Context) { - evt := eventFromContext(ctx) - cfg, err := b.getSettings(evt.RoomID) - if err != nil { - b.Error(ctx, evt.RoomID, "failed to retrieve settings: %v", err) - return - } - - mailbox := cfg.Get(optionMailbox) - if mailbox == "" { - b.SendNotice(ctx, evt.RoomID, "that room is not configured yet") - return - } - - b.rooms.Delete(mailbox) - - err = b.setSettings(evt.RoomID, settings{}) - if err != nil { - b.Error(ctx, evt.RoomID, "cannot update settings: %v", err) - return - } - - b.SendNotice(ctx, evt.RoomID, "mailbox has been disabled") -} - -func (b *Bot) handleOption(ctx context.Context, cmd []string) { - if len(cmd) == 1 { - b.getOption(ctx, cmd[0]) - return - } - b.setOption(ctx, cmd[0], cmd[1]) -} - -func (b *Bot) getOption(ctx context.Context, name string) { - evt := eventFromContext(ctx) - cfg, err := b.getSettings(evt.RoomID) - if err != nil { - b.Error(ctx, evt.RoomID, "failed to retrieve settings: %v", err) - return - } - - value := cfg.Get(name) - if value == "" { - b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("`%s` is not set, kupo.", name)) - return - } - - if name == optionMailbox { - value = value + "@" + b.domain - } - - b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("`%s` of this room is `%s`", name, value)) -} - -func (b *Bot) setOption(ctx context.Context, name, value string) { - cmd := b.commands.get(name) - if cmd != nil { - value = cmd.sanitizer(value) - } - - evt := eventFromContext(ctx) - if name == optionMailbox { - existingID, ok := b.GetMapping(value) - if ok && existingID != "" && existingID != evt.RoomID { - b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("Mailbox `%s@%s` already taken, kupo", value, b.domain)) - return - } - } - - cfg, err := b.getSettings(evt.RoomID) - if err != nil { - b.Error(ctx, evt.RoomID, "failed to retrieve settings: %v", err) - return - } - - old := cfg.Get(name) - cfg.Set(name, value) - - if name == optionMailbox { - cfg.Set(optionOwner, evt.Sender.String()) - if old != "" { - b.rooms.Delete(old) - } - b.rooms.Store(value, evt.RoomID) - value = fmt.Sprintf("%s@%s", value, b.domain) - } - - err = b.setSettings(evt.RoomID, cfg) - if err != nil { - b.Error(ctx, evt.RoomID, "cannot update settings: %v", err) - return - } - - if name == optionMailbox { - value = value + "@" + b.domain - } - - b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("`%s` of this room set to `%s`", name, value)) -} diff --git a/bot/owner_command.go b/bot/owner_command.go new file mode 100644 index 0000000..84f26cd --- /dev/null +++ b/bot/owner_command.go @@ -0,0 +1,106 @@ +package bot + +import ( + "context" + "fmt" +) + +func (b *Bot) runStop(ctx context.Context) { + evt := eventFromContext(ctx) + cfg, err := b.getSettings(evt.RoomID) + if err != nil { + b.Error(ctx, evt.RoomID, "failed to retrieve settings: %v", err) + return + } + + mailbox := cfg.Get(optionMailbox) + if mailbox == "" { + b.SendNotice(ctx, evt.RoomID, "that room is not configured yet") + return + } + + b.rooms.Delete(mailbox) + + err = b.setSettings(evt.RoomID, settings{}) + if err != nil { + b.Error(ctx, evt.RoomID, "cannot update settings: %v", err) + return + } + + b.SendNotice(ctx, evt.RoomID, "mailbox has been disabled") +} + +func (b *Bot) handleOption(ctx context.Context, cmd []string) { + if len(cmd) == 1 { + b.getOption(ctx, cmd[0]) + return + } + b.setOption(ctx, cmd[0], cmd[1]) +} + +func (b *Bot) getOption(ctx context.Context, name string) { + evt := eventFromContext(ctx) + cfg, err := b.getSettings(evt.RoomID) + if err != nil { + b.Error(ctx, evt.RoomID, "failed to retrieve settings: %v", err) + return + } + + value := cfg.Get(name) + if value == "" { + b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("`%s` is not set, kupo.", name)) + return + } + + if name == optionMailbox { + value = value + "@" + b.domain + } + + b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("`%s` of this room is `%s`", name, value)) +} + +func (b *Bot) setOption(ctx context.Context, name, value string) { + cmd := b.commands.get(name) + if cmd != nil { + value = cmd.sanitizer(value) + } + + evt := eventFromContext(ctx) + if name == optionMailbox { + existingID, ok := b.GetMapping(value) + if ok && existingID != "" && existingID != evt.RoomID { + b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("Mailbox `%s@%s` already taken, kupo", value, b.domain)) + return + } + } + + cfg, err := b.getSettings(evt.RoomID) + if err != nil { + b.Error(ctx, evt.RoomID, "failed to retrieve settings: %v", err) + return + } + + old := cfg.Get(name) + cfg.Set(name, value) + + if name == optionMailbox { + cfg.Set(optionOwner, evt.Sender.String()) + if old != "" { + b.rooms.Delete(old) + } + b.rooms.Store(value, evt.RoomID) + value = fmt.Sprintf("%s@%s", value, b.domain) + } + + err = b.setSettings(evt.RoomID, cfg) + if err != nil { + b.Error(ctx, evt.RoomID, "cannot update settings: %v", err) + return + } + + if name == optionMailbox { + value = value + "@" + b.domain + } + + b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("`%s` of this room set to `%s`", name, value)) +}