summary refs log tree commit diff stats
path: root/lib/msgid.go
diff options
context:
space:
mode:
Diffstat (limited to 'lib/msgid.go')
-rw-r--r--lib/msgid.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/msgid.go b/lib/msgid.go
new file mode 100644
index 0000000..8282d1d
--- /dev/null
+++ b/lib/msgid.go
@@ -0,0 +1,34 @@
+package lib
+
+// TODO: Remove this pending merge into github.com/emersion/go-message
+
+import (
+	"bytes"
+	"encoding/binary"
+	"fmt"
+	"math/rand"
+	"os"
+	"time"
+
+	"github.com/martinlindhe/base36"
+)
+
+// Generates an RFC 2822-complaint 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 bytes.Buffer
+	)
+	binary.Write(&now, binary.BigEndian, time.Now().UnixNano())
+	binary.Write(&nonce, binary.BigEndian, rand.Uint64())
+	hostname, err := os.Hostname()
+	if err != nil {
+		hostname = "localhost"
+	}
+	return fmt.Sprintf("<%s.%s@%s>",
+		base36.EncodeBytes(now.Bytes()),
+		base36.EncodeBytes(nonce.Bytes()),
+		hostname)
+}
'>128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174