bridge thread replies from matrix to email

This commit is contained in:
Aine
2022-11-10 21:58:29 +02:00
parent 19dec770b9
commit f835a7560d
6 changed files with 78 additions and 35 deletions

View File

@@ -35,7 +35,7 @@ so you can use it to send emails from your apps and scripts as well.
- [x] SMTP client
- [x] SMTP server (you can use Postmoogle as general purpose SMTP server to send emails from your scripts or apps)
- [x] Send a message to matrix room with special format to send a new email, even to multiple email addresses at once
- [ ] Reply to matrix thread sends reply into email thread
- [x] Reply to matrix thread sends reply into email thread
## Configuration

View File

@@ -7,6 +7,7 @@ import (
"time"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/format"
"maunium.net/go/mautrix/id"
"gitlab.com/etke.cc/postmoogle/utils"
@@ -359,20 +360,49 @@ func (b *Bot) runSend(ctx context.Context) {
}
}
b.lock(evt.RoomID)
defer b.unlock(evt.RoomID)
from := mailbox + "@" + b.domains[0]
ID := fmt.Sprintf("<%s@%s>", evt.ID, b.domains[0])
for _, to := range tos {
data := utils.
NewEmail(ID, "", subject, from, to, body, "", nil).
Compose(b.getBotSettings().DKIMPrivateKey())
email := utils.NewEmail(ID, "", subject, from, to, body, "", nil)
data := email.Compose(b.getBotSettings().DKIMPrivateKey())
err = b.sendmail(from, to, data)
if err != nil {
b.Error(ctx, evt.RoomID, "cannot send email to %s: %v", to, err)
} else {
b.SendNotice(ctx, evt.RoomID, "Email has been sent to "+to)
b.forgeSentMetadata(ctx, email, &cfg)
}
}
if len(tos) > 1 {
b.SendNotice(ctx, evt.RoomID, "All emails were sent.")
}
}
// forgeSentMetadata used to save metadata from !pm sent event to a separate notice message
// because that metadata is needed to determine email thread relations
func (b *Bot) forgeSentMetadata(ctx context.Context, email *utils.Email, cfg *roomSettings) {
evt := eventFromContext(ctx)
threadID := utils.EventParent(evt.ID, evt.Content.AsMessage())
content := email.Content(threadID, cfg.ContentOptions())
notice := format.RenderMarkdown("Email has been sent to "+email.To, true, true)
notice.MsgType = event.MsgNotice
msgContent, ok := content.Parsed.(event.MessageEventContent)
if !ok {
b.Error(ctx, evt.RoomID, "cannot parse message")
return
}
msgContent.Body = notice.Body
msgContent.FormattedBody = notice.FormattedBody
content.Parsed = msgContent
msgID, err := b.lp.Send(evt.RoomID, &content)
if err != nil {
b.Error(ctx, evt.RoomID, "cannot parse message")
return
}
if threadID != "" {
b.setThreadID(evt.RoomID, fmt.Sprintf("<%s@%s>", msgID, b.domains[0]), threadID)
}
b.setLastEventID(evt.RoomID, threadID, msgID)
}

View File

@@ -3,7 +3,6 @@ package bot
import (
"context"
"errors"
"fmt"
"strings"
"maunium.net/go/mautrix/event"
@@ -24,6 +23,7 @@ const (
eventInReplyToKey = "cc.etke.postmoogle.inReplyTo"
eventSubjectKey = "cc.etke.postmoogle.subject"
eventFromKey = "cc.etke.postmoogle.from"
eventToKey = "cc.etke.postmoogle.to"
)
// SetSendmail sets mail sending func to the bot
@@ -81,14 +81,14 @@ func (b *Bot) IncomingEmail(ctx context.Context, email *utils.Email) error {
if !ok {
return errors.New("room not found")
}
b.lock(roomID)
defer b.unlock(roomID)
cfg, err := b.getRoomSettings(roomID)
if err != nil {
b.Error(ctx, roomID, "cannot get settings: %v", err)
}
b.lock(roomID)
defer b.unlock(roomID)
var threadID id.EventID
if email.InReplyTo != "" && !cfg.NoThreads() {
threadID = b.getThreadID(roomID, email.InReplyTo)
@@ -115,27 +115,28 @@ func (b *Bot) IncomingEmail(ctx context.Context, email *utils.Email) error {
return nil
}
func (b *Bot) getParentEmail(evt *event.Event) (string, string, string) {
func (b *Bot) getParentEmail(evt *event.Event) (string, string, string, string) {
content := evt.Content.AsMessage()
parentID := utils.EventParent(evt.ID, content)
if parentID == evt.ID {
return "", "", ""
return "", "", "", ""
}
parentID = b.getLastEventID(evt.RoomID, parentID)
parentEvt, err := b.lp.GetClient().GetEvent(evt.RoomID, parentID)
if err != nil {
b.log.Error("cannot get parent event: %v", err)
return "", "", ""
return "", "", "", ""
}
if parentEvt.Content.Parsed == nil {
perr := parentEvt.Content.ParseRaw(event.EventMessage)
if perr != nil {
b.log.Error("cannot parse event content: %v", perr)
return "", "", ""
return "", "", "", ""
}
}
to := utils.EventField[string](&parentEvt.Content, eventFromKey)
from := utils.EventField[string](&parentEvt.Content, eventFromKey)
to := utils.EventField[string](&parentEvt.Content, eventToKey)
inReplyTo := utils.EventField[string](&parentEvt.Content, eventMessageIDkey)
if inReplyTo == "" {
inReplyTo = parentID.String()
@@ -148,49 +149,55 @@ func (b *Bot) getParentEmail(evt *event.Event) (string, string, string) {
subject = strings.SplitN(content.Body, "\n", 1)[0]
}
return to, inReplyTo, subject
return from, to, inReplyTo, subject
}
// Send2Email sends message to email
// TODO rewrite to thread replies only
func (b *Bot) SendEmailReply(ctx context.Context, to, subject, body string) error {
// SendEmailReply sends replies from matrix thread to email thread
func (b *Bot) SendEmailReply(ctx context.Context) {
var inReplyTo string
evt := eventFromContext(ctx)
cfg, err := b.getRoomSettings(evt.RoomID)
if err != nil {
return err
b.Error(ctx, evt.RoomID, "cannot retrieve room settings: %v", err)
return
}
mailbox := cfg.Mailbox()
if mailbox == "" {
return fmt.Errorf("mailbox not configured, kupo")
b.Error(ctx, evt.RoomID, "mailbox is not configured, kupo")
return
}
from := mailbox + "@" + b.domains[0]
pTo, pInReplyTo, pSubject := b.getParentEmail(evt)
inReplyTo = pInReplyTo
if pTo != "" && to == "" {
to = pTo
b.lock(evt.RoomID)
defer b.unlock(evt.RoomID)
fromMailbox := mailbox + "@" + b.domains[0]
from, to, inReplyTo, subject := b.getParentEmail(evt)
// when email was sent from matrix and reply was sent from matrix again
if fromMailbox != from {
to = from
}
if pSubject != "" && subject == "" {
subject = pSubject
if to == "" {
b.Error(ctx, evt.RoomID, "cannot find parent email and continue the thread. Please, start a new email thread")
return
}
content := evt.Content.AsMessage()
if subject == "" {
subject = strings.SplitN(content.Body, "\n", 1)[0]
}
if body == "" {
if content.FormattedBody != "" {
body = content.FormattedBody
} else {
body = content.Body
}
}
body := content.Body
ID := evt.ID.String()[1:] + "@" + b.domains[0]
b.log.Debug("send email reply ID=%s from=%s to=%s inReplyTo=%s subject=%s body=%s", ID, from, to, inReplyTo, subject, body)
data := utils.
NewEmail(ID, inReplyTo, subject, from, to, body, "", nil).
Compose(b.getBotSettings().DKIMPrivateKey())
return b.sendmail(from, to, data)
err = b.sendmail(from, to, data)
if err != nil {
b.Error(ctx, evt.RoomID, "cannot send email: %v", err)
return
}
}
func (b *Bot) sendFiles(ctx context.Context, roomID id.RoomID, files []*utils.File, noThreads bool, parentID id.EventID) {

View File

@@ -20,6 +20,9 @@ func (b *Bot) handle(ctx context.Context) {
message := strings.TrimSpace(content.Body)
cmd := b.parseCommand(message, true)
if cmd == nil {
if content.RelatesTo != nil {
b.SendEmailReply(ctx)
}
return
}

View File

@@ -146,6 +146,7 @@ func (s roomSettings) ContentOptions() *utils.ContentOptions {
Subject: !s.NoSubject(),
Threads: !s.NoThreads(),
ToKey: eventToKey,
FromKey: eventFromKey,
SubjectKey: eventSubjectKey,
MessageIDKey: eventMessageIDkey,

View File

@@ -48,6 +48,7 @@ type ContentOptions struct {
InReplyToKey string
SubjectKey string
FromKey string
ToKey string
}
// AddressValid checks if email address is valid
@@ -122,6 +123,7 @@ func (e *Email) Content(threadID id.EventID, options *ContentOptions) *event.Con
options.InReplyToKey: e.InReplyTo,
options.SubjectKey: e.Subject,
options.FromKey: e.From,
options.ToKey: e.To,
},
Parsed: parsed,
}