Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
Handle duplicate MessageID correctly. Fixes #67
Browse files Browse the repository at this point in the history
Sanity check that it's the same email that's been forwarded
back to us. We can only check From and To since Subject and Body
are encrypted.
  • Loading branch information
dcposch committed Nov 18, 2013
1 parent 33ba919 commit c8e3a4b
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions smtp_saver.go
Expand Up @@ -84,25 +84,21 @@ func deliverMailLocally(msg *SMTPMessage) error {
email.AncestorIDs = msg.data.ancestorIDs.AngledStringCappedToBytes(
" ", GetConfig().AncestorIDsMaxBytes)

// TODO: consider if transactions are required.
// TODO: saveMessage may fail if messageID is not unique.
err := SaveMessage(email)
if err == nil {
// all good
} else if strings.HasPrefix(err.Error(), "Error 1062: Duplicate entry") {
// tried to save mail, but the MessageID already exists
// this can happen, for example, if you send an email
// and it gets auto-forwarded back to you
// sanity check that this is the same message
// (we can only check From and To since Subject and Body are encrypted)
oldEmail := LoadMessage(email.MessageID)
if oldEmail.From != email.From ||
oldEmail.To != email.To ||
oldEmail.CipherSubject != email.CipherSubject {
oldEmail.To != email.To {
log.Panicf("Same MessageID, different headers! Email %s from %s to %s\n",
email.MessageID, email.To, email.From)
} else if oldEmail.CipherBody != email.CipherBody {
log.Panicf("Same MessageID, different body! Email %s from %s to %s\n",
email.MessageID, email.To, email.From)
}
// got an existing email forwarded to us again,
// and it matches pefectly. all good!
} else {
// unknown error trying to save mail
panic(err)
Expand Down

0 comments on commit c8e3a4b

Please sign in to comment.