Debian Codegraph @

lib/parse/header.go

raw
1 package parse
2
3 import (
4 "strings"
5
6 "git.sr.ht/~rjarry/aerc/lib/log"
7 "github.com/emersion/go-message/mail"
8 )
9
10 // MsgIDList parses a list of message identifiers. It returns message
11 // identifiers without angle brackets. If the header field is missing,
12 // it returns nil.
13 //
14 // This can be used on In-Reply-To and References header fields.
15 // If the field does not conform to RFC 5322, fall back
16 // to greedily parsing a subsequence of the original field.
17 func MsgIDList(h *mail.Header, key string) []string {
18 l, err := h.MsgIDList(key)
19 if err == nil {
20 return l
21 }
22 log.Errorf("%s: %s", err, h.Get(key))
23
24 // Expensive, fix your peer's MUA instead!
25 var list []string
26 header := &mail.Header{Header: h.Header.Copy()}
27 value := header.Get(key)
28 for err != nil && len(value) > 0 {
29 // Skip parsed IDs
30 if len(l) > 0 {
31 last := "<" + l[len(l)-1] + ">"
32 value = value[strings.Index(value, last)+len(last):]
33 list = append(list, l...)
34 }
35
36 // Skip a character until some IDs can be parsed
37 value = value[1:]
38 header.Set(key, value)
39 l, err = header.MsgIDList(key)
40 }
41 return append(list, l...)
42 }
43
44 // Mailto parses a URI string and extracts the mail address.
45 // Example: "<mailto:[email protected]>" -> "[email protected]"
46 func Mailto(val string) ([]*mail.Address, error) {
47 s := strings.Trim(val, "<> \t\r\n")
48 return mail.ParseAddressList(strings.TrimPrefix(s, "mailto:"))
49 }