bridge thread replies from matrix to email
This commit is contained in:
@@ -35,7 +35,7 @@ so you can use it to send emails from your apps and scripts as well.
|
|||||||
- [x] SMTP client
|
- [x] SMTP client
|
||||||
- [x] SMTP server (you can use Postmoogle as general purpose SMTP server to send emails from your scripts or apps)
|
- [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
|
- [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
|
## Configuration
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"maunium.net/go/mautrix/event"
|
"maunium.net/go/mautrix/event"
|
||||||
|
"maunium.net/go/mautrix/format"
|
||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
|
|
||||||
"gitlab.com/etke.cc/postmoogle/utils"
|
"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]
|
from := mailbox + "@" + b.domains[0]
|
||||||
ID := fmt.Sprintf("<%s@%s>", evt.ID, b.domains[0])
|
ID := fmt.Sprintf("<%s@%s>", evt.ID, b.domains[0])
|
||||||
for _, to := range tos {
|
for _, to := range tos {
|
||||||
data := utils.
|
email := utils.NewEmail(ID, "", subject, from, to, body, "", nil)
|
||||||
NewEmail(ID, "", subject, from, to, body, "", nil).
|
data := email.Compose(b.getBotSettings().DKIMPrivateKey())
|
||||||
Compose(b.getBotSettings().DKIMPrivateKey())
|
|
||||||
err = b.sendmail(from, to, data)
|
err = b.sendmail(from, to, data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Error(ctx, evt.RoomID, "cannot send email to %s: %v", to, err)
|
b.Error(ctx, evt.RoomID, "cannot send email to %s: %v", to, err)
|
||||||
} else {
|
} else {
|
||||||
b.SendNotice(ctx, evt.RoomID, "Email has been sent to "+to)
|
b.forgeSentMetadata(ctx, email, &cfg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(tos) > 1 {
|
if len(tos) > 1 {
|
||||||
b.SendNotice(ctx, evt.RoomID, "All emails were sent.")
|
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)
|
||||||
|
}
|
||||||
|
|||||||
67
bot/email.go
67
bot/email.go
@@ -3,7 +3,6 @@ package bot
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"maunium.net/go/mautrix/event"
|
"maunium.net/go/mautrix/event"
|
||||||
@@ -24,6 +23,7 @@ const (
|
|||||||
eventInReplyToKey = "cc.etke.postmoogle.inReplyTo"
|
eventInReplyToKey = "cc.etke.postmoogle.inReplyTo"
|
||||||
eventSubjectKey = "cc.etke.postmoogle.subject"
|
eventSubjectKey = "cc.etke.postmoogle.subject"
|
||||||
eventFromKey = "cc.etke.postmoogle.from"
|
eventFromKey = "cc.etke.postmoogle.from"
|
||||||
|
eventToKey = "cc.etke.postmoogle.to"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SetSendmail sets mail sending func to the bot
|
// 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 {
|
if !ok {
|
||||||
return errors.New("room not found")
|
return errors.New("room not found")
|
||||||
}
|
}
|
||||||
b.lock(roomID)
|
|
||||||
defer b.unlock(roomID)
|
|
||||||
|
|
||||||
cfg, err := b.getRoomSettings(roomID)
|
cfg, err := b.getRoomSettings(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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
b.lock(roomID)
|
||||||
|
defer b.unlock(roomID)
|
||||||
|
|
||||||
var threadID id.EventID
|
var threadID id.EventID
|
||||||
if email.InReplyTo != "" && !cfg.NoThreads() {
|
if email.InReplyTo != "" && !cfg.NoThreads() {
|
||||||
threadID = b.getThreadID(roomID, email.InReplyTo)
|
threadID = b.getThreadID(roomID, email.InReplyTo)
|
||||||
@@ -115,27 +115,28 @@ func (b *Bot) IncomingEmail(ctx context.Context, email *utils.Email) error {
|
|||||||
return nil
|
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()
|
content := evt.Content.AsMessage()
|
||||||
parentID := utils.EventParent(evt.ID, content)
|
parentID := utils.EventParent(evt.ID, content)
|
||||||
if parentID == evt.ID {
|
if parentID == evt.ID {
|
||||||
return "", "", ""
|
return "", "", "", ""
|
||||||
}
|
}
|
||||||
parentID = b.getLastEventID(evt.RoomID, parentID)
|
parentID = b.getLastEventID(evt.RoomID, parentID)
|
||||||
parentEvt, err := b.lp.GetClient().GetEvent(evt.RoomID, parentID)
|
parentEvt, err := b.lp.GetClient().GetEvent(evt.RoomID, parentID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.log.Error("cannot get parent event: %v", err)
|
b.log.Error("cannot get parent event: %v", err)
|
||||||
return "", "", ""
|
return "", "", "", ""
|
||||||
}
|
}
|
||||||
if parentEvt.Content.Parsed == nil {
|
if parentEvt.Content.Parsed == nil {
|
||||||
perr := parentEvt.Content.ParseRaw(event.EventMessage)
|
perr := parentEvt.Content.ParseRaw(event.EventMessage)
|
||||||
if perr != nil {
|
if perr != nil {
|
||||||
b.log.Error("cannot parse event content: %v", perr)
|
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)
|
inReplyTo := utils.EventField[string](&parentEvt.Content, eventMessageIDkey)
|
||||||
if inReplyTo == "" {
|
if inReplyTo == "" {
|
||||||
inReplyTo = parentID.String()
|
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]
|
subject = strings.SplitN(content.Body, "\n", 1)[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
return to, inReplyTo, subject
|
return from, to, inReplyTo, subject
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send2Email sends message to email
|
// SendEmailReply sends replies from matrix thread to email thread
|
||||||
// TODO rewrite to thread replies only
|
func (b *Bot) SendEmailReply(ctx context.Context) {
|
||||||
func (b *Bot) SendEmailReply(ctx context.Context, to, subject, body string) error {
|
|
||||||
var inReplyTo string
|
var inReplyTo string
|
||||||
evt := eventFromContext(ctx)
|
evt := eventFromContext(ctx)
|
||||||
cfg, err := b.getRoomSettings(evt.RoomID)
|
cfg, err := b.getRoomSettings(evt.RoomID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
b.Error(ctx, evt.RoomID, "cannot retrieve room settings: %v", err)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
mailbox := cfg.Mailbox()
|
mailbox := cfg.Mailbox()
|
||||||
if 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)
|
b.lock(evt.RoomID)
|
||||||
inReplyTo = pInReplyTo
|
defer b.unlock(evt.RoomID)
|
||||||
if pTo != "" && to == "" {
|
fromMailbox := mailbox + "@" + b.domains[0]
|
||||||
to = pTo
|
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()
|
content := evt.Content.AsMessage()
|
||||||
if subject == "" {
|
if subject == "" {
|
||||||
subject = strings.SplitN(content.Body, "\n", 1)[0]
|
subject = strings.SplitN(content.Body, "\n", 1)[0]
|
||||||
}
|
}
|
||||||
if body == "" {
|
body := content.Body
|
||||||
if content.FormattedBody != "" {
|
|
||||||
body = content.FormattedBody
|
|
||||||
} else {
|
|
||||||
body = content.Body
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ID := evt.ID.String()[1:] + "@" + b.domains[0]
|
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.
|
data := utils.
|
||||||
NewEmail(ID, inReplyTo, subject, from, to, body, "", nil).
|
NewEmail(ID, inReplyTo, subject, from, to, body, "", nil).
|
||||||
Compose(b.getBotSettings().DKIMPrivateKey())
|
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) {
|
func (b *Bot) sendFiles(ctx context.Context, roomID id.RoomID, files []*utils.File, noThreads bool, parentID id.EventID) {
|
||||||
|
|||||||
@@ -20,6 +20,9 @@ func (b *Bot) handle(ctx context.Context) {
|
|||||||
message := strings.TrimSpace(content.Body)
|
message := strings.TrimSpace(content.Body)
|
||||||
cmd := b.parseCommand(message, true)
|
cmd := b.parseCommand(message, true)
|
||||||
if cmd == nil {
|
if cmd == nil {
|
||||||
|
if content.RelatesTo != nil {
|
||||||
|
b.SendEmailReply(ctx)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -146,6 +146,7 @@ func (s roomSettings) ContentOptions() *utils.ContentOptions {
|
|||||||
Subject: !s.NoSubject(),
|
Subject: !s.NoSubject(),
|
||||||
Threads: !s.NoThreads(),
|
Threads: !s.NoThreads(),
|
||||||
|
|
||||||
|
ToKey: eventToKey,
|
||||||
FromKey: eventFromKey,
|
FromKey: eventFromKey,
|
||||||
SubjectKey: eventSubjectKey,
|
SubjectKey: eventSubjectKey,
|
||||||
MessageIDKey: eventMessageIDkey,
|
MessageIDKey: eventMessageIDkey,
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ type ContentOptions struct {
|
|||||||
InReplyToKey string
|
InReplyToKey string
|
||||||
SubjectKey string
|
SubjectKey string
|
||||||
FromKey string
|
FromKey string
|
||||||
|
ToKey string
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddressValid checks if email address is valid
|
// 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.InReplyToKey: e.InReplyTo,
|
||||||
options.SubjectKey: e.Subject,
|
options.SubjectKey: e.Subject,
|
||||||
options.FromKey: e.From,
|
options.FromKey: e.From,
|
||||||
|
options.ToKey: e.To,
|
||||||
},
|
},
|
||||||
Parsed: parsed,
|
Parsed: parsed,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user