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`).
This commit is contained in:
15
bot/bot.go
15
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")
|
return errors.New("room not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
settings, err := b.getSettings(ctx, roomID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
var text strings.Builder
|
var text strings.Builder
|
||||||
text.WriteString("From: ")
|
if !settings.HideSenderAddress {
|
||||||
text.WriteString(from)
|
text.WriteString("From: ")
|
||||||
text.WriteString("\n\n")
|
text.WriteString(from)
|
||||||
|
text.WriteString("\n\n")
|
||||||
|
}
|
||||||
text.WriteString("# ")
|
text.WriteString("# ")
|
||||||
text.WriteString(subject)
|
text.WriteString(subject)
|
||||||
text.WriteString("\n\n")
|
text.WriteString("\n\n")
|
||||||
text.WriteString(format.HTMLToMarkdown(body))
|
text.WriteString(format.HTMLToMarkdown(body))
|
||||||
|
|
||||||
content := format.RenderMarkdown(text.String(), true, true)
|
content := format.RenderMarkdown(text.String(), true, true)
|
||||||
_, err := b.lp.Send(roomID, content)
|
_, err = b.lp.Send(roomID, content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,9 +11,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var commands = map[string]string{
|
var commands = map[string]string{
|
||||||
"mailbox": "Get or set mailbox of that room",
|
"mailbox": "Get or set mailbox of that room",
|
||||||
"owner": "Get or set owner of that room",
|
"owner": "Get or set owner of that room",
|
||||||
"help": "Get help",
|
"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) {
|
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)
|
b.handleOwner(ctx, evt, command)
|
||||||
case "mailbox":
|
case "mailbox":
|
||||||
b.handleMailbox(ctx, evt, command)
|
b.handleMailbox(ctx, evt, command)
|
||||||
|
case "hide-sender-address":
|
||||||
|
b.handleHideSenderAddress(ctx, evt, command)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,8 +14,9 @@ var migrations = []string{}
|
|||||||
|
|
||||||
// settings of a room
|
// settings of a room
|
||||||
type settings struct {
|
type settings struct {
|
||||||
Mailbox string
|
Mailbox string
|
||||||
Owner id.UserID
|
Owner id.UserID
|
||||||
|
HideSenderAddress bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allowed checks if change is allowed
|
// Allowed checks if change is allowed
|
||||||
|
|||||||
104
bot/mailbox.go
104
bot/mailbox.go
@@ -2,6 +2,7 @@ package bot
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/getsentry/sentry-go"
|
"github.com/getsentry/sentry-go"
|
||||||
"gitlab.com/etke.cc/postmoogle/utils"
|
"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)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user