1
0
Fork 0

Merge pull request 'Add inline attachments to comments and prevent double handling of mails' (#3504) from Beowulf/forgejo:mail-inline-attachments-not-added-to-comment into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/3504
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
This commit is contained in:
Earl Warren 2024-04-30 12:19:42 +00:00
commit 9a3a3feb4c
3 changed files with 59 additions and 0 deletions

View file

@ -0,0 +1 @@
Fixed that inline attachments of emails (as they occur for example with Apple Mail) are not attached to comments.

View file

@ -219,6 +219,11 @@ loop:
} }
err := func() error { err := func() error {
if isAlreadyHandled(handledSet, msg) {
log.Debug("Skipping already handled message")
return nil
}
r := msg.GetBody(section) r := msg.GetBody(section)
if r == nil { if r == nil {
return fmt.Errorf("could not get body from message: %w", err) return fmt.Errorf("could not get body from message: %w", err)
@ -277,6 +282,11 @@ loop:
return nil return nil
} }
// isAlreadyHandled tests if the message was already handled
func isAlreadyHandled(handledSet *imap.SeqSet, msg *imap.Message) bool {
return handledSet.Contains(msg.SeqNum)
}
// isAutomaticReply tests if the headers indicate an automatic reply // isAutomaticReply tests if the headers indicate an automatic reply
func isAutomaticReply(env *enmime.Envelope) bool { func isAutomaticReply(env *enmime.Envelope) bool {
autoSubmitted := env.GetHeader("Auto-Submitted") autoSubmitted := env.GetHeader("Auto-Submitted")
@ -367,6 +377,14 @@ func getContentFromMailReader(env *enmime.Envelope) *MailContent {
Content: attachment.Content, Content: attachment.Content,
}) })
} }
for _, inline := range env.Inlines {
if inline.FileName != "" {
attachments = append(attachments, &Attachment{
Name: inline.FileName,
Content: inline.Content,
})
}
}
return &MailContent{ return &MailContent{
Content: reply.FromText(env.Text), Content: reply.FromText(env.Text),

View file

@ -7,10 +7,24 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/emersion/go-imap"
"github.com/jhillyerd/enmime" "github.com/jhillyerd/enmime"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestNotHandleTwice(t *testing.T) {
handledSet := new(imap.SeqSet)
msg := imap.NewMessage(90, []imap.FetchItem{imap.FetchBody})
handled := isAlreadyHandled(handledSet, msg)
assert.Equal(t, false, handled)
handledSet.AddNum(msg.SeqNum)
handled = isAlreadyHandled(handledSet, msg)
assert.Equal(t, true, handled)
}
func TestIsAutomaticReply(t *testing.T) { func TestIsAutomaticReply(t *testing.T) {
cases := []struct { cases := []struct {
Headers map[string]string Headers map[string]string
@ -95,6 +109,32 @@ func TestGetContentFromMailReader(t *testing.T) {
assert.Equal(t, "attachment.txt", content.Attachments[0].Name) assert.Equal(t, "attachment.txt", content.Attachments[0].Name)
assert.Equal(t, []byte("attachment content"), content.Attachments[0].Content) assert.Equal(t, []byte("attachment content"), content.Attachments[0].Content)
mailString = "Content-Type: multipart/mixed; boundary=message-boundary\r\n" +
"\r\n" +
"--message-boundary\r\n" +
"Content-Type: multipart/alternative; boundary=text-boundary\r\n" +
"\r\n" +
"--text-boundary\r\n" +
"Content-Type: text/plain\r\n" +
"Content-Disposition: inline\r\n" +
"\r\n" +
"mail content\r\n" +
"--text-boundary--\r\n" +
"--message-boundary\r\n" +
"Content-Type: text/plain\r\n" +
"Content-Disposition: inline; filename=attachment.txt\r\n" +
"\r\n" +
"attachment content\r\n" +
"--message-boundary--\r\n"
env, err = enmime.ReadEnvelope(strings.NewReader(mailString))
assert.NoError(t, err)
content = getContentFromMailReader(env)
assert.Equal(t, "mail content", content.Content)
assert.Len(t, content.Attachments, 1)
assert.Equal(t, "attachment.txt", content.Attachments[0].Name)
assert.Equal(t, []byte("attachment content"), content.Attachments[0].Content)
mailString = "Content-Type: multipart/mixed; boundary=message-boundary\r\n" + mailString = "Content-Type: multipart/mixed; boundary=message-boundary\r\n" +
"\r\n" + "\r\n" +
"--message-boundary\r\n" + "--message-boundary\r\n" +