add !pm stripify option

This commit is contained in:
Aine
2024-02-26 20:42:37 +02:00
parent ba1a8c8390
commit 271a4a0e31
14 changed files with 442 additions and 13 deletions

View File

@@ -125,6 +125,15 @@ func (b *Bot) initCommands() commandList {
sanitizer: utils.SanitizeBoolString,
allowed: b.allowOwner,
},
{
key: config.RoomStripify,
description: fmt.Sprintf(
"Get or set `%s` of the room (`true` - strip incoming email reply quotes and signatures; `false` - send incoming email as-is)",
config.RoomStripify,
),
sanitizer: utils.SanitizeBoolString,
allowed: b.allowOwner,
},
{
key: config.RoomNoSend,
description: fmt.Sprintf(

View File

@@ -23,6 +23,7 @@ const (
RoomAutoreply = "autoreply"
RoomThreadify = "threadify"
RoomStripify = "stripify"
RoomNoCC = "nocc"
RoomNoFiles = "nofiles"
RoomNoHTML = "nohtml"
@@ -84,6 +85,10 @@ func (s Room) Threadify() bool {
return utils.Bool(s.Get(RoomThreadify))
}
func (s Room) Stripify() bool {
return utils.Bool(s.Get(RoomStripify))
}
func (s Room) NoSend() bool {
return utils.Bool(s.Get(RoomNoSend))
}
@@ -198,6 +203,7 @@ func (s Room) ContentOptions() *email.ContentOptions {
Recipient: !s.NoRecipient(),
Subject: !s.NoSubject(),
Threads: !s.NoThreads(),
Stripify: s.Stripify(),
Threadify: s.Threadify(),
ToKey: "cc.etke.postmoogle.to",

View File

@@ -146,6 +146,18 @@ func (b *Bot) IncomingEmail(ctx context.Context, eml *email.Email) error {
b.setThreadID(ctx, roomID, eml.MessageID, threadID)
}
}
// if automatic stripping is enabled, there is a chance something important may be stripped out
// to prevent that, we use a hacky way to generate content without stripping and save it as a file fist
if cfg.Stripify() && !cfg.Threadify() {
contentOpts := cfg.ContentOptions()
contentOpts.Stripify = false
content := eml.Content(threadID, contentOpts, b.psdc)
eml.Files = append(eml.Files, //nolint:forcetypeassert // that's ok
utils.NewFile("original.md", []byte(content.Parsed.(*event.MessageEventContent).Body)),
)
}
content := eml.Content(threadID, cfg.ContentOptions(), b.psdc)
eventID, serr := b.lp.Send(ctx, roomID, content)
if serr != nil {
@@ -164,6 +176,16 @@ func (b *Bot) IncomingEmail(ctx context.Context, eml *email.Email) error {
b.setLastEventID(ctx, roomID, threadID, eventID)
if newThread && cfg.Threadify() {
// if automatic stripping is enabled, there is a chance something important may be stripped out
// to prevent that, we use a hacky way to generate content without stripping and save it as a file fist
if cfg.Stripify() {
contentOpts := cfg.ContentOptions()
contentOpts.Stripify = false
content := eml.ContentBody(threadID, contentOpts)
eml.Files = append(eml.Files, //nolint:forcetypeassert // that's ok
utils.NewFile("original.md", []byte(content.Parsed.(*event.MessageEventContent).Body)),
)
}
_, berr := b.lp.Send(ctx, roomID, eml.ContentBody(threadID, cfg.ContentOptions()))
if berr != nil {
return berr