add 'nofiles' option, refactored bot.Send(); fixes #2

This commit is contained in:
Aine
2022-08-26 16:11:08 +03:00
parent 42c9e15619
commit 2dcba843cc
4 changed files with 47 additions and 28 deletions

View File

@@ -80,6 +80,28 @@ func (b *Bot) Start() error {
return b.lp.Start()
}
func (b *Bot) email2content(email *utils.Email, cfg settings) *event.MessageEventContent {
var text strings.Builder
if !cfg.NoSender() {
text.WriteString("From: ")
text.WriteString(email.From)
text.WriteString("\n\n")
}
if !cfg.NoSubject() {
text.WriteString("# ")
text.WriteString(email.Subject)
text.WriteString("\n\n")
}
if email.HTML != "" && !cfg.NoHTML() {
text.WriteString(format.HTMLToMarkdown(email.HTML))
} else {
text.WriteString(email.Text)
}
content := format.RenderMarkdown(text.String(), true, true)
return &content
}
// Send email to matrix room
func (b *Bot) Send(ctx context.Context, email *utils.Email) error {
roomID, ok := b.GetMapping(utils.Mailbox(email.To))
@@ -87,32 +109,22 @@ func (b *Bot) Send(ctx context.Context, email *utils.Email) error {
return errors.New("room not found")
}
settings, err := b.getSettings(roomID)
cfg, err := b.getSettings(roomID)
if err != nil {
b.Error(ctx, roomID, "cannot get settings: %v", err)
}
var text strings.Builder
if !settings.NoSender() {
text.WriteString("From: ")
text.WriteString(email.From)
text.WriteString("\n\n")
contentParsed := b.email2content(email, cfg)
content := &event.Content{
Raw: map[string]interface{}{
eventMessageIDkey: email.MessageID,
eventInReplyToKey: email.InReplyTo,
},
Parsed: contentParsed,
}
if !settings.NoSubject() {
text.WriteString("# ")
text.WriteString(email.Subject)
text.WriteString("\n\n")
}
if email.HTML != "" && !settings.NoHTML() {
text.WriteString(format.HTMLToMarkdown(email.HTML))
} else {
text.WriteString(email.Text)
}
contentParsed := format.RenderMarkdown(text.String(), true, true)
var threadID id.EventID
if email.InReplyTo != "" && !settings.NoThreads() {
if email.InReplyTo != "" && !cfg.NoThreads() {
threadID = b.getThreadID(roomID, email.InReplyTo)
if threadID != "" {
contentParsed.SetRelatesTo(&event.RelatesTo{
@@ -122,14 +134,6 @@ func (b *Bot) Send(ctx context.Context, email *utils.Email) error {
b.setThreadID(roomID, email.MessageID, threadID)
}
}
content := &event.Content{
Raw: map[string]interface{}{
eventMessageIDkey: email.MessageID,
eventInReplyToKey: email.InReplyTo,
},
Parsed: contentParsed,
}
eventID, serr := b.lp.Send(roomID, content)
if serr != nil {
return serr
@@ -140,7 +144,9 @@ func (b *Bot) Send(ctx context.Context, email *utils.Email) error {
threadID = eventID
}
b.sendFiles(ctx, roomID, email.Files, threadID)
if !cfg.NoFiles() {
b.sendFiles(ctx, roomID, email.Files, threadID)
}
return nil
}

View File

@@ -78,6 +78,13 @@ var (
optionNoThreads,
),
},
{
key: optionNoFiles,
description: fmt.Sprintf(
"Get or set `%s` of the room (`true` - ignore email attachments; `false` - upload email attachments)",
optionNoFiles,
),
},
}
// sanitizers is map of option name => sanitizer function
@@ -87,6 +94,7 @@ var (
optionNoSubject: utils.SanitizeBoolString,
optionNoHTML: utils.SanitizeBoolString,
optionNoThreads: utils.SanitizeBoolString,
optionNoFiles: utils.SanitizeBoolString,
}
)

View File

@@ -26,6 +26,7 @@ const (
optionNoSubject = "nosubject"
optionNoHTML = "nohtml"
optionNoThreads = "nothreads"
optionNoFiles = "nofiles"
)
var migrations = []string{}

View File

@@ -68,6 +68,10 @@ func (s settings) NoThreads() bool {
return utils.Bool(s.Get(optionNoThreads))
}
func (s settings) NoFiles() bool {
return utils.Bool(s.Get(optionNoFiles))
}
// Set option
func (s settings) Set(key, value string) {
s[strings.ToLower(strings.TrimSpace(key))] = value