diff --git a/bot/bot.go b/bot/bot.go index be804b9..98ba2dc 100644 --- a/bot/bot.go +++ b/bot/bot.go @@ -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 } diff --git a/bot/command.go b/bot/command.go index c8f985d..53ca21c 100644 --- a/bot/command.go +++ b/bot/command.go @@ -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, } ) diff --git a/bot/data.go b/bot/data.go index ac35f95..31b8902 100644 --- a/bot/data.go +++ b/bot/data.go @@ -26,6 +26,7 @@ const ( optionNoSubject = "nosubject" optionNoHTML = "nohtml" optionNoThreads = "nothreads" + optionNoFiles = "nofiles" ) var migrations = []string{} diff --git a/bot/settings.go b/bot/settings.go index bf5839e..ad37899 100644 --- a/bot/settings.go +++ b/bot/settings.go @@ -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