-
Notifications
You must be signed in to change notification settings - Fork 1
/
listinfo.go
47 lines (40 loc) · 1.41 KB
/
listinfo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package ulist
import (
"encoding/base64"
"math/rand"
"mime"
"net/mail"
"strings"
"time"
"github.com/wansing/ulist/mailutil"
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
type ListInfo struct {
ID int
Addr
}
func (li *ListInfo) BounceAddress() string {
copy := li.Addr
copy.Local += BounceAddressSuffix
return copy.RFC5322AddrSpec()
}
// NewMessageId creates a new RFC5322 compliant Message-Id with the list domain as "id-right".
func (li *ListInfo) NewMessageId() string {
var randBytes = make([]byte, 24)
rand.Read(randBytes)
// URLEncoding is alphanumeric, "-" and "_", which is all covered by RFC5322 "atext"
var idLeft = strings.ToLower(base64.URLEncoding.EncodeToString(randBytes))
// RFC 5322: The message identifier (msg-id) syntax is a limited version of the addr-spec construct enclosed in the angle bracket characters, "<" and ">".
// Golang's mail.Address.String() encloses the result in angle brackets.
return (&mail.Address{Address: idLeft + "@" + li.Domain}).String()
}
func (li *ListInfo) PrefixSubject(subject string) string {
subject = mailutil.RobustWordDecode(subject)
var prefix = "[" + li.DisplayOrLocal() + "]"
if firstSquareBracket := strings.Index(subject, "["); firstSquareBracket == -1 || firstSquareBracket != strings.Index(subject, prefix) { // square bracket not found or before prefix
subject = prefix + " " + subject
}
return mime.QEncoding.Encode("utf-8", subject)
}