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() 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 // Send email to matrix room
func (b *Bot) Send(ctx context.Context, email *utils.Email) error { func (b *Bot) Send(ctx context.Context, email *utils.Email) error {
roomID, ok := b.GetMapping(utils.Mailbox(email.To)) 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") return errors.New("room not found")
} }
settings, err := b.getSettings(roomID) cfg, err := b.getSettings(roomID)
if err != nil { if err != nil {
b.Error(ctx, roomID, "cannot get settings: %v", err) b.Error(ctx, roomID, "cannot get settings: %v", err)
} }
var text strings.Builder contentParsed := b.email2content(email, cfg)
if !settings.NoSender() { content := &event.Content{
text.WriteString("From: ") Raw: map[string]interface{}{
text.WriteString(email.From) eventMessageIDkey: email.MessageID,
text.WriteString("\n\n") 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 var threadID id.EventID
if email.InReplyTo != "" && !settings.NoThreads() { if email.InReplyTo != "" && !cfg.NoThreads() {
threadID = b.getThreadID(roomID, email.InReplyTo) threadID = b.getThreadID(roomID, email.InReplyTo)
if threadID != "" { if threadID != "" {
contentParsed.SetRelatesTo(&event.RelatesTo{ 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) 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) eventID, serr := b.lp.Send(roomID, content)
if serr != nil { if serr != nil {
return serr return serr
@@ -140,7 +144,9 @@ func (b *Bot) Send(ctx context.Context, email *utils.Email) error {
threadID = eventID threadID = eventID
} }
if !cfg.NoFiles() {
b.sendFiles(ctx, roomID, email.Files, threadID) b.sendFiles(ctx, roomID, email.Files, threadID)
}
return nil return nil
} }

View File

@@ -78,6 +78,13 @@ var (
optionNoThreads, 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 // sanitizers is map of option name => sanitizer function
@@ -87,6 +94,7 @@ var (
optionNoSubject: utils.SanitizeBoolString, optionNoSubject: utils.SanitizeBoolString,
optionNoHTML: utils.SanitizeBoolString, optionNoHTML: utils.SanitizeBoolString,
optionNoThreads: utils.SanitizeBoolString, optionNoThreads: utils.SanitizeBoolString,
optionNoFiles: utils.SanitizeBoolString,
} }
) )

View File

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

View File

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