Skip to content

Commit

Permalink
Add utility function for generating Message-Id
Browse files Browse the repository at this point in the history
  • Loading branch information
Drew DeVault authored and emersion committed May 16, 2019
1 parent 35cfd68 commit e2aba7d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ go 1.12

require (
github.com/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe
github.com/martinlindhe/base36 v0.0.0-20190418230009-7c6542dfbb41
golang.org/x/text v0.3.2
)
31 changes: 31 additions & 0 deletions mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,34 @@
//
// RFC 5322 defines the Internet Message Format.
package mail

import (
"bytes"
"encoding/binary"
"fmt"
"crypto/rand"
"os"
"time"

"github.com/martinlindhe/base36"
)

// Generates an RFC 2822-compliant Message-Id based on the informational draft
// "Recommendations for generating Message IDs", for lack of a better
// authoritative source.
func GenerateMessageID() string {
var (
now bytes.Buffer
nonce []byte = make([]byte, 8)
)
binary.Write(&now, binary.BigEndian, time.Now().UnixNano())
rand.Read(nonce)
hostname, err := os.Hostname()
if err != nil {
hostname = "localhost"
}
return fmt.Sprintf("<%s.%s@%s>",
base36.EncodeBytes(now.Bytes()),
base36.EncodeBytes(nonce),
hostname)
}
15 changes: 15 additions & 0 deletions mail/mail_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package mail_test

import (
"regexp"
"testing"

"github.com/emersion/go-message/mail"
)

const mailString = "Subject: Your Name\r\n" +
"Content-Type: multipart/mixed; boundary=message-boundary\r\n" +
"\r\n" +
Expand Down Expand Up @@ -33,3 +40,11 @@ const nestedMailString = "Subject: Fwd: Your Name\r\n" +
"\r\n" +
mailString +
"--outer-message-boundary--\r\n"

func TestGenerateMessageID(t *testing.T) {
msgId := mail.GenerateMessageID()
regex := regexp.MustCompile(`^<.*@.*>$`)
if !regex.MatchString(msgId) {
t.Error("Generated message ID does not meet RFC requirement")
}
}
5 changes: 5 additions & 0 deletions textproto/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"net/textproto"
"regexp"
"strings"

"log"
)

type headerField struct {
Expand Down Expand Up @@ -260,6 +262,7 @@ func (h *Header) FieldsByKey(k string) HeaderFields {
func readLineSlice(r *bufio.Reader, line []byte) ([]byte, error) {
for {
l, more, err := r.ReadLine()
log.Println("LINE", string(l), more)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -326,6 +329,7 @@ func hasContinuationLine(r *bufio.Reader) bool {
func readContinuedLineSlice(r *bufio.Reader) ([]byte, error) {
// Read the first line.
line, err := readLineSlice(r, nil)
log.Println("HEY", string(line), err)
if err != nil {
return nil, err
}
Expand All @@ -338,6 +342,7 @@ func readContinuedLineSlice(r *bufio.Reader) ([]byte, error) {

// Read continuation lines.
for hasContinuationLine(r) {
log.Println("CONTINUATION")

This comment has been minimized.

Copy link
@foxcpp

foxcpp May 17, 2019

Collaborator

Is it left here accidentally or what?

This comment has been minimized.

Copy link
@emersion

emersion May 17, 2019

Owner

Gah, wtf? From the debugging style, I'd guess it's my stuff. Maybe my tree wasn't clean, but I thought git am warned you about this?

Anyway, this shouldn't be here.

This comment has been minimized.

Copy link
@emersion

emersion May 17, 2019

Owner

Should be fixed now. Thanks for the heads-up.

line, err = readLineSlice(r, line)
if err != nil {
break // bufio will keep err until next read.
Expand Down

0 comments on commit e2aba7d

Please sign in to comment.