mirror of
https://github.com/hanzoai/mail.git
synced 2026-09-23 23:53:02 +00:00
Fork of github.com/mjl-/mox at 5656ca6d1b
(upstream v0.0.15 plus 55 commits).
Rename the module path in go.mod and the import paths in 268 Go files. Also
update the five scripts that "go install" the module (genwebsite.sh,
docker-release.sh, apidiff.sh, test-upgrade.sh, Dockerfile.release), which
would otherwise build upstream rather than this fork.
Add NOTICE recording the upstream project, author, license, and fork point,
and stating which license covers which files: MIT for the mox source and our
changes, MPL-2.0 for publicsuffix/public_suffix_list.txt alone, and the
vendored dependencies' own licenses under licenses/. Embed NOTICE so
"mail licenses" prints the lineage from the running binary. LICENSE.MIT,
LICENSE.MPLv2.0 and the licenses/ tree are preserved verbatim; no code is
relicensed.
The mox command name, config keys, data directory layout and website content
are unchanged. LLM.md records the fork point, the rename, what was left alone,
and the steps to stand the server up.
Co-authored-by: Hanzo Dev <[email protected]>
75 lines
2.2 KiB
Go
75 lines
2.2 KiB
Go
package message
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/hanzoai/mail/smtp"
|
|
)
|
|
|
|
// ReferencedIDs returns the Message-IDs referenced from the References header(s),
|
|
// with a fallback to the In-Reply-To header(s). The ids are canonicalized for
|
|
// thread-matching, like with MessageIDCanonical. Empty message-id's are skipped.
|
|
func ReferencedIDs(references []string, inReplyTo []string) ([]string, error) {
|
|
var refids []string // In thread-canonical form.
|
|
|
|
// parse and add 0 or 1 reference, returning the remaining refs string for a next attempt.
|
|
parse1 := func(refs string, one bool) string {
|
|
refs = strings.TrimLeft(refs, " \t\r\n")
|
|
if !strings.HasPrefix(refs, "<") {
|
|
// To make progress, we skip to next space or >.
|
|
i := strings.IndexAny(refs, " >")
|
|
if i < 0 {
|
|
return ""
|
|
}
|
|
return refs[i+1:]
|
|
}
|
|
refs = refs[1:]
|
|
// Look for the ending > or next <. If < is before >, this entry is truncated.
|
|
i := strings.IndexAny(refs, "<>")
|
|
if i < 0 {
|
|
return ""
|
|
}
|
|
if refs[i] == '<' {
|
|
// Truncated entry, we ignore it.
|
|
return refs[i:]
|
|
}
|
|
ref := strings.ToLower(refs[:i])
|
|
// Some MUAs wrap References line in the middle of message-id's, and others
|
|
// recombine them. Take out bare WSP in message-id's.
|
|
ref = strings.ReplaceAll(ref, " ", "")
|
|
ref = strings.ReplaceAll(ref, "\t", "")
|
|
refs = refs[i+1:]
|
|
// Canonicalize the quotedness of the message-id.
|
|
addr, err := smtp.ParseAddress(ref)
|
|
if err == nil {
|
|
// Leave the hostname form intact.
|
|
t := strings.Split(ref, "@")
|
|
ref = addr.Localpart.String() + "@" + t[len(t)-1]
|
|
}
|
|
// log.Errorx("assigning threads: bad reference in references header, using raw value", err, mlog.Field("msgid", mid), mlog.Field("reference", ref))
|
|
if ref != "" {
|
|
refids = append(refids, ref)
|
|
}
|
|
return refs
|
|
}
|
|
|
|
// References is the modern way (for a long time already) to reference ancestors.
|
|
// The direct parent is typically at the end of the list.
|
|
for _, refs := range references {
|
|
for refs != "" {
|
|
refs = parse1(refs, false)
|
|
}
|
|
}
|
|
// We only look at the In-Reply-To header if we didn't find any References.
|
|
if len(refids) == 0 {
|
|
for _, s := range inReplyTo {
|
|
parse1(s, true)
|
|
if len(refids) > 0 {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
return refids, nil
|
|
}
|