From 9f3aa3dd6862681ca47c39b68997d4ef4c8dc4a1 Mon Sep 17 00:00:00 2001 From: Slavi Pantaleev Date: Tue, 23 Aug 2022 18:18:06 +0300 Subject: [PATCH] Add ability to hide sender's email address (hide-sender-address setting) The configuration setting is called `Hide*` instead of `Show*`, because it's backward compatible with existing configuration settings. This is useful for when you setup an email forwarding inbox and you're always sending to it through the same email address. In that case, you don't need to see the email address in each Matrix message. In the future, another similar `bool` setting (`hide-subject`) will land, which controls whether the email's subject is shown in the final message or not. That setting can make use of most of the same setup (all of `handleBooleanConfigurationKey`). --- bot/bot.go | 15 +++++-- bot/command.go | 9 +++-- bot/data.go | 5 ++- bot/mailbox.go | 104 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 9 deletions(-) diff --git a/bot/bot.go b/bot/bot.go index 8b2daaa..1bbe17e 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -89,17 +89,24 @@ func (b *Bot) Send(ctx context.Context, from, to, subject, body string, files [] return errors.New("room not found") } + settings, err := b.getSettings(ctx, roomID) + if err != nil { + return err + } + var text strings.Builder - text.WriteString("From: ") - text.WriteString(from) - text.WriteString("\n\n") + if !settings.HideSenderAddress { + text.WriteString("From: ") + text.WriteString(from) + text.WriteString("\n\n") + } text.WriteString("# ") text.WriteString(subject) text.WriteString("\n\n") text.WriteString(format.HTMLToMarkdown(body)) content := format.RenderMarkdown(text.String(), true, true) - _, err := b.lp.Send(roomID, content) + _, err = b.lp.Send(roomID, content) if err != nil { return err } diff --git a/bot/command.go b/bot/command.go index cc93976..827a930 100644 --- a/bot/command.go +++ b/bot/command.go @@ -11,9 +11,10 @@ import ( ) var commands = map[string]string{ - "mailbox": "Get or set mailbox of that room", - "owner": "Get or set owner of that room", - "help": "Get help", + "mailbox": "Get or set mailbox of that room", + "owner": "Get or set owner of that room", + "hide-sender-address": "Get or set the `hide-sender-address` setting (controls if the sender's email address is displayed or not; default `false`)", + "help": "Get help", } func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, command []string) { @@ -33,6 +34,8 @@ func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, command []str b.handleOwner(ctx, evt, command) case "mailbox": b.handleMailbox(ctx, evt, command) + case "hide-sender-address": + b.handleHideSenderAddress(ctx, evt, command) } } diff --git a/bot/data.go b/bot/data.go index be75ae0..f5c36e1 100644 --- a/bot/data.go +++ b/bot/data.go @@ -14,8 +14,9 @@ var migrations = []string{} // settings of a room type settings struct { - Mailbox string - Owner id.UserID + Mailbox string + Owner id.UserID + HideSenderAddress bool } // Allowed checks if change is allowed diff --git a/bot/mailbox.go b/bot/mailbox.go index d74a71e..c48568a 100644 --- a/bot/mailbox.go +++ b/bot/mailbox.go @@ -2,6 +2,7 @@ package bot import ( "context" + "fmt" "github.com/getsentry/sentry-go" "gitlab.com/etke.cc/postmoogle/utils" @@ -110,3 +111,106 @@ func (b *Bot) setMailbox(ctx context.Context, evt *event.Event, mailbox string) b.Error(span.Context(), evt.RoomID, "cannot send message: %v", err) } } + +func (b *Bot) handleHideSenderAddress(ctx context.Context, evt *event.Event, command []string) { + getter := func(entity settings) bool { + return entity.HideSenderAddress + } + + setter := func(entity *settings, value bool) error { + entity.HideSenderAddress = value + return nil + } + + b.handleBooleanConfigurationKey(ctx, evt, command, "hide-sender-address", getter, setter) +} + +func (b *Bot) handleBooleanConfigurationKey( + ctx context.Context, + evt *event.Event, + command []string, + configKey string, + getter func(entity settings) bool, + setter func(entity *settings, value bool) error, +) { + if len(command) == 1 { + b.getBooleanConfigurationKey(ctx, evt, configKey, getter, setter) + return + } + + b.setBooleanConfigurationKey(ctx, evt, command[1], configKey, getter, setter) +} + +func (b *Bot) getBooleanConfigurationKey( + ctx context.Context, + evt *event.Event, + configKey string, + getter func(entity settings) bool, + setter func(entity *settings, value bool) error, +) { + span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName(fmt.Sprintf("getBooleanConfigurationKey.%s", configKey))) + defer span.Finish() + + cfg, err := b.getSettings(span.Context(), evt.RoomID) + if err != nil { + b.log.Warn("cannot get %s settings: %v", evt.RoomID, err) + return + } + + value := getter(cfg) + + content := format.RenderMarkdown(fmt.Sprintf("`%s` configuration setting for this room is currently set to `%v`", configKey, value), true, true) + content.MsgType = event.MsgNotice + _, err = b.lp.Send(evt.RoomID, content) + if err != nil { + b.Error(span.Context(), evt.RoomID, "cannot send message: %v", err) + } +} + +func (b *Bot) setBooleanConfigurationKey( + ctx context.Context, + evt *event.Event, + value string, + configKey string, + getter func(entity settings) bool, + setter func(entity *settings, value bool) error, +) { + var actualValue bool + if value == "true" { + actualValue = true + } else if value == "false" { + actualValue = false + } else { + b.Notice(ctx, evt.RoomID, "you are supposed to send a true or false value") + return + } + + span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName(fmt.Sprintf("setBooleanConfigurationKey.%s", configKey))) + defer span.Finish() + + cfg, err := b.getSettings(span.Context(), evt.RoomID) + if err != nil { + b.Error(span.Context(), evt.RoomID, "failed to retrieve setting: %v", err) + return + } + + if !cfg.Allowed(b.noowner, evt.Sender) { + b.Notice(span.Context(), evt.RoomID, "you don't have permission to do that") + return + } + + setter(&cfg, actualValue) + + err = b.setSettings(span.Context(), evt.RoomID, cfg) + if err != nil { + b.Error(span.Context(), evt.RoomID, "cannot update settings: %v", err) + return + } + + content := format.RenderMarkdown(fmt.Sprintf("`%s` configuration setting for this room has been set to `%v`", configKey, actualValue), true, true) + content.MsgType = event.MsgNotice + _, err = b.lp.Send(evt.RoomID, content) + if err != nil { + b.Error(span.Context(), evt.RoomID, "cannot send message: %v", err) + } +}