From b413e5871adc02a891ab4ebe18b72009973e289e Mon Sep 17 00:00:00 2001 From: Aine Date: Sat, 23 Sep 2023 17:33:58 +0300 Subject: [PATCH] partial complexity refactoring --- bot/command.go | 52 +++++++++++++------- bot/command_owner.go | 110 ++++++++++++++++++++++++++++--------------- 2 files changed, 108 insertions(+), 54 deletions(-) diff --git a/bot/command.go b/bot/command.go index 3f9e321..3a2142c 100644 --- a/bot/command.go +++ b/bot/command.go @@ -496,12 +496,33 @@ func (b *Bot) sendHelp(ctx context.Context) { b.SendNotice(ctx, evt.RoomID, msg.String()) } -//nolint:gocognit // TODO func (b *Bot) runSend(ctx context.Context) { evt := eventFromContext(ctx) - if !b.allowSend(evt.Sender, evt.RoomID) { + to, subject, body, shouldSend := b.getSendDetails(ctx) + if !shouldSend { return } + + cfg, err := b.cfg.GetRoom(evt.RoomID) + if err != nil { + b.Error(ctx, evt.RoomID, "failed to retrieve room settings: %v", err) + return + } + + var htmlBody string + if !cfg.NoHTML() { + htmlBody = format.RenderMarkdown(body, true, true).FormattedBody + } + + tos := strings.Split(to, ",") + b.runSendCommand(ctx, cfg, tos, subject, body, htmlBody) +} + +func (b *Bot) getSendDetails(ctx context.Context) (string, string, string, bool) { + evt := eventFromContext(ctx) + if !b.allowSend(evt.Sender, evt.RoomID) { + return "", "", "", false + } commandSlice := b.parseCommand(evt.Content.AsMessage().Body, false) to, subject, body, err := utils.ParseSend(commandSlice) if err == utils.ErrInvalidArgs { @@ -515,35 +536,32 @@ func (b *Bot) runSend(ctx context.Context) { "as you want.\n"+ "```", b.prefix)) - return + return "", "", "", false } cfg, err := b.cfg.GetRoom(evt.RoomID) if err != nil { b.Error(ctx, evt.RoomID, "failed to retrieve room settings: %v", err) - return + return "", "", "", false } mailbox := cfg.Mailbox() if mailbox == "" { b.SendNotice(ctx, evt.RoomID, "mailbox is not configured, kupo") - return + return "", "", "", false } - var htmlBody string - if !cfg.NoHTML() { - htmlBody = format.RenderMarkdown(body, true, true).FormattedBody + signature := cfg.Signature() + if signature != "" { + body += "\n\n---\n" + signature } - signature := format.RenderMarkdown(cfg.Signature(), true, true) - if signature.Body != "" { - body += "\n\n---\n" + signature.Body - if htmlBody != "" { - htmlBody += "


" + signature.FormattedBody - } - } + return to, subject, body, true +} + +func (b *Bot) runSendCommand(ctx context.Context, cfg config.Room, tos []string, subject, body, htmlBody string) { + evt := eventFromContext(ctx) - tos := strings.Split(to, ",") // validate first for _, to := range tos { if !email.AddressValid(to) { @@ -556,7 +574,7 @@ func (b *Bot) runSend(ctx context.Context) { defer b.mu.Unlock(evt.RoomID.String()) domain := utils.SanitizeDomain(cfg.Domain()) - from := mailbox + "@" + domain + from := cfg.Mailbox() + "@" + domain ID := email.MessageID(evt.ID, domain) for _, to := range tos { recipients := []string{to} diff --git a/bot/command_owner.go b/bot/command_owner.go index d72fc26..6946b64 100644 --- a/bot/command_owner.go +++ b/bot/command_owner.go @@ -43,7 +43,16 @@ func (b *Bot) handleOption(ctx context.Context, cmd []string) { b.getOption(ctx, cmd[0]) return } - b.setOption(ctx, cmd[0], cmd[1]) + switch cmd[0] { + case config.RoomActive: + return + case config.RoomMailbox: + b.setMailbox(ctx, cmd[1]) + case config.RoomPassword: + b.setPassword(ctx) + default: + b.setOption(ctx, cmd[0], cmd[1]) + } } func (b *Bot) getOption(ctx context.Context, name string) { @@ -84,39 +93,75 @@ func (b *Bot) getOption(ctx context.Context, name string) { b.SendNotice(ctx, evt.RoomID, msg) } -//nolint:gocognit // TODO -func (b *Bot) setOption(ctx context.Context, name, value string) { - cmd := b.commands.get(name) - if cmd != nil && cmd.sanitizer != nil { - value = cmd.sanitizer(value) - } - +func (b *Bot) setMailbox(ctx context.Context, value string) { evt := eventFromContext(ctx) - // ignore request - if name == config.RoomActive { + existingID, ok := b.getMapping(value) + if (ok && existingID != "" && existingID != evt.RoomID) || b.isReserved(value) { + b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("Mailbox `%s` (%s) already taken, kupo", value, utils.EmailsList(value, ""))) return } - if name == config.RoomMailbox { - existingID, ok := b.getMapping(value) - if (ok && existingID != "" && existingID != evt.RoomID) || b.isReserved(value) { - b.SendNotice(ctx, evt.RoomID, fmt.Sprintf("Mailbox `%s` (%s) already taken, kupo", value, utils.EmailsList(value, ""))) - return - } - } cfg, err := b.cfg.GetRoom(evt.RoomID) if err != nil { b.Error(ctx, evt.RoomID, "failed to retrieve settings: %v", err) return } + old := cfg.Get(config.RoomMailbox) + cfg.Set(config.RoomMailbox, value) + cfg.Set(config.RoomOwner, evt.Sender.String()) + if old != "" { + b.rooms.Delete(old) + } + active := b.ActivateMailbox(evt.Sender, evt.RoomID, value) + cfg.Set(config.RoomActive, strconv.FormatBool(active)) + value = fmt.Sprintf("%s@%s", value, utils.SanitizeDomain(cfg.Domain())) - if name == config.RoomPassword { - value = b.parseCommand(evt.Content.AsMessage().Body, false)[1] // get original value, without forced lower case - value, err = argon2pw.GenerateSaltedHash(value) - if err != nil { - b.Error(ctx, evt.RoomID, "failed to hash password: %v", err) - return - } + err = b.cfg.SetRoom(evt.RoomID, cfg) + if err != nil { + b.Error(ctx, evt.RoomID, "cannot update settings: %v", err) + return + } + + msg := fmt.Sprintf("mailbox of this room set to `%s`", value) + b.SendNotice(ctx, evt.RoomID, msg) +} + +func (b *Bot) setPassword(ctx context.Context) { + evt := eventFromContext(ctx) + cfg, err := b.cfg.GetRoom(evt.RoomID) + if err != nil { + b.Error(ctx, evt.RoomID, "failed to retrieve settings: %v", err) + return + } + + value := b.parseCommand(evt.Content.AsMessage().Body, false)[1] // get original value, without forced lower case + value, err = argon2pw.GenerateSaltedHash(value) + if err != nil { + b.Error(ctx, evt.RoomID, "failed to hash password: %v", err) + return + } + + cfg.Set(config.RoomPassword, value) + err = b.cfg.SetRoom(evt.RoomID, cfg) + if err != nil { + b.Error(ctx, evt.RoomID, "cannot update settings: %v", err) + return + } + + b.SendNotice(ctx, evt.RoomID, "SMTP password has been set") +} + +func (b *Bot) setOption(ctx context.Context, name, value string) { + cmd := b.commands.get(name) + if cmd != nil && cmd.sanitizer != nil { + value = cmd.sanitizer(value) + } + + evt := eventFromContext(ctx) + cfg, err := b.cfg.GetRoom(evt.RoomID) + if err != nil { + b.Error(ctx, evt.RoomID, "failed to retrieve settings: %v", err) + return } if name == config.RoomAutoreply || @@ -129,18 +174,12 @@ func (b *Bot) setOption(ctx context.Context, name, value string) { } old := cfg.Get(name) - cfg.Set(name, value) - - if name == config.RoomMailbox { - cfg.Set(config.RoomOwner, evt.Sender.String()) - if old != "" { - b.rooms.Delete(old) - } - active := b.ActivateMailbox(evt.Sender, evt.RoomID, value) - cfg.Set(config.RoomActive, strconv.FormatBool(active)) - value = fmt.Sprintf("%s@%s", value, utils.SanitizeDomain(cfg.Domain())) + if old == value { + b.SendNotice(ctx, evt.RoomID, "nothing changed, kupo.") + return } + cfg.Set(name, value) err = b.cfg.SetRoom(evt.RoomID, cfg) if err != nil { b.Error(ctx, evt.RoomID, "cannot update settings: %v", err) @@ -148,9 +187,6 @@ func (b *Bot) setOption(ctx context.Context, name, value string) { } msg := fmt.Sprintf("`%s` of this room set to `%s`", name, value) - if name == config.RoomPassword { - msg = "SMTP password has been set" - } b.SendNotice(ctx, evt.RoomID, msg) }