add !pm threadify option, fixes #60

This commit is contained in:
Aine
2023-10-13 22:27:46 +03:00
parent 0d0dcf20b9
commit 1bf8ba4700
6 changed files with 61 additions and 6 deletions

View File

@@ -125,6 +125,7 @@ If you want to change them - check available options in the help message (`!pm h
* **`!pm autoreply`** - Get or set autoreply of the room (markdown supported) that will be sent on any new incoming email thread * **`!pm autoreply`** - Get or set autoreply of the room (markdown supported) that will be sent on any new incoming email thread
* **`!pm signature`** - Get or set signature of the room (markdown supported) * **`!pm signature`** - Get or set signature of the room (markdown supported)
* **`!pm threadify`** - Get or set `threadify` of the room (`true` - send incoming email body in thread; `false` - send incoming email body as part of the message)
* **`!pm nosend`** - Get or set `nosend` of the room (`true` - disable email sending; `false` - enable email sending) * **`!pm nosend`** - Get or set `nosend` of the room (`true` - disable email sending; `false` - enable email sending)
* **`!pm noreplies`** - Get or set `noreplies` of the room (`true` - ignore matrix replies; `false` - parse matrix replies) * **`!pm noreplies`** - Get or set `noreplies` of the room (`true` - ignore matrix replies; `false` - parse matrix replies)
* **`!pm nosender`** - Get or set `nosender` of the room (`true` - hide email sender; `false` - show email sender) * **`!pm nosender`** - Get or set `nosender` of the room (`true` - hide email sender; `false` - show email sender)

View File

@@ -116,6 +116,15 @@ func (b *Bot) initCommands() commandList {
sanitizer: func(s string) string { return s }, sanitizer: func(s string) string { return s },
allowed: b.allowOwner, allowed: b.allowOwner,
}, },
{
key: config.RoomThreadify,
description: fmt.Sprintf(
"Get or set `%s` of the room (`true` - send incoming email body in thread; `false` - send incoming email body as part of the message)",
config.RoomThreadify,
),
sanitizer: utils.SanitizeBoolString,
allowed: b.allowOwner,
},
{ {
key: config.RoomNoSend, key: config.RoomNoSend,
description: fmt.Sprintf( description: fmt.Sprintf(

View File

@@ -22,6 +22,7 @@ const (
RoomSignature = "signature" RoomSignature = "signature"
RoomAutoreply = "autoreply" RoomAutoreply = "autoreply"
RoomThreadify = "threadify"
RoomNoCC = "nocc" RoomNoCC = "nocc"
RoomNoFiles = "nofiles" RoomNoFiles = "nofiles"
RoomNoHTML = "nohtml" RoomNoHTML = "nohtml"
@@ -79,6 +80,10 @@ func (s Room) Autoreply() string {
return s.Get(RoomAutoreply) return s.Get(RoomAutoreply)
} }
func (s Room) Threadify() bool {
return utils.Bool(s.Get(RoomThreadify))
}
func (s Room) NoSend() bool { func (s Room) NoSend() bool {
return utils.Bool(s.Get(RoomNoSend)) return utils.Bool(s.Get(RoomNoSend))
} }
@@ -193,6 +198,7 @@ func (s Room) ContentOptions() *email.ContentOptions {
Recipient: !s.NoRecipient(), Recipient: !s.NoRecipient(),
Subject: !s.NoSubject(), Subject: !s.NoSubject(),
Threads: !s.NoThreads(), Threads: !s.NoThreads(),
Threadify: s.Threadify(),
ToKey: "cc.etke.postmoogle.to", ToKey: "cc.etke.postmoogle.to",
CcKey: "cc.etke.postmoogle.cc", CcKey: "cc.etke.postmoogle.cc",

View File

@@ -154,6 +154,13 @@ func (b *Bot) IncomingEmail(ctx context.Context, eml *email.Email) error {
b.setThreadID(roomID, eml.MessageID, threadID) b.setThreadID(roomID, eml.MessageID, threadID)
b.setLastEventID(roomID, threadID, eventID) b.setLastEventID(roomID, threadID, eventID)
if newThread && cfg.Threadify() {
_, berr := b.lp.Send(roomID, eml.ContentBody(threadID, cfg.ContentOptions()))
if berr != nil {
return berr
}
}
if !cfg.NoInlines() { if !cfg.NoInlines() {
b.sendFiles(ctx, roomID, eml.InlineFiles, cfg.NoThreads(), threadID) b.sendFiles(ctx, roomID, eml.InlineFiles, cfg.NoThreads(), threadID)
} }

View File

@@ -132,8 +132,15 @@ func (e *Email) contentHeader(threadID id.EventID, text *strings.Builder, option
text.WriteString("\n\n") text.WriteString("\n\n")
} }
if options.Subject && threadID == "" { if options.Subject && threadID == "" {
if options.Threadify {
text.WriteString("**")
text.WriteString(e.Subject)
text.WriteString("**")
} else {
text.WriteString("# ") text.WriteString("# ")
text.WriteString(e.Subject) text.WriteString(e.Subject)
}
text.WriteString("\n\n") text.WriteString("\n\n")
} }
} }
@@ -144,11 +151,13 @@ func (e *Email) Content(threadID id.EventID, options *ContentOptions) *event.Con
e.contentHeader(threadID, &text, options) e.contentHeader(threadID, &text, options)
if threadID != "" || (threadID == "" && !options.Threadify) {
if e.HTML != "" && options.HTML { if e.HTML != "" && options.HTML {
text.WriteString(format.HTMLToMarkdown(e.HTML)) text.WriteString(format.HTMLToMarkdown(e.HTML))
} else { } else {
text.WriteString(e.Text) text.WriteString(e.Text)
} }
}
parsed := format.RenderMarkdown(text.String(), true, true) parsed := format.RenderMarkdown(text.String(), true, true)
parsed.RelatesTo = linkpearl.RelatesTo(threadID, !options.Threads) parsed.RelatesTo = linkpearl.RelatesTo(threadID, !options.Threads)
@@ -174,6 +183,28 @@ func (e *Email) Content(threadID id.EventID, options *ContentOptions) *event.Con
return &content return &content
} }
// ContentBody converts the email object to a Matrix event content that contains email body only
// NOTE: returns nil if threadify is disabled
func (e *Email) ContentBody(threadID id.EventID, options *ContentOptions) *event.Content {
if !options.Threadify {
return nil
}
var text string
if e.HTML != "" && options.HTML {
text = format.HTMLToMarkdown(e.HTML)
} else {
text = e.Text
}
parsed := format.RenderMarkdown(text, true, true)
parsed.RelatesTo = linkpearl.RelatesTo(threadID, !options.Threads)
content := event.Content{
Parsed: &parsed,
}
return &content
}
// Compose converts the email object to a string (to be used for delivery via SMTP) and possibly DKIM-signs it // Compose converts the email object to a string (to be used for delivery via SMTP) and possibly DKIM-signs it
func (e *Email) Compose(privkey string) string { func (e *Email) Compose(privkey string) string {
textSize := len(e.Text) textSize := len(e.Text)

View File

@@ -18,6 +18,7 @@ type ContentOptions struct {
Subject bool Subject bool
HTML bool HTML bool
Threads bool Threads bool
Threadify bool
// Keys // Keys
MessageIDKey string MessageIDKey string