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]>
53 lines
1.7 KiB
Go
53 lines
1.7 KiB
Go
package message
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/mail/smtp"
|
|
)
|
|
|
|
var errBadMessageID = errors.New("not a message-id")
|
|
|
|
// MessageIDCanonical parses the Message-ID, returning a canonical value that is
|
|
// lower-cased, without <>, and no unneeded quoting. For matching in threading,
|
|
// with References/In-Reply-To. If the message-id is invalid (e.g. no <>), an error
|
|
// is returned. If the message-id could not be parsed as address (localpart "@"
|
|
// domain), the raw value and the bool return parameter true is returned. It is
|
|
// quite common that message-id's don't adhere to the localpart @ domain
|
|
// syntax.
|
|
func MessageIDCanonical(s string) (string, bool, error) {
|
|
// ../rfc/5322:1383
|
|
|
|
s = strings.TrimSpace(s)
|
|
if !strings.HasPrefix(s, "<") {
|
|
return "", false, fmt.Errorf("%w: missing <", errBadMessageID)
|
|
}
|
|
s = s[1:]
|
|
// Seen in practice: Message-ID: <[email protected]> (added by [email protected])
|
|
// Doesn't seem valid, but we allow it.
|
|
s, rem, have := strings.Cut(s, ">")
|
|
if !have || (rem != "" && (Pedantic || !strings.HasPrefix(rem, " "))) {
|
|
return "", false, fmt.Errorf("%w: missing >", errBadMessageID)
|
|
}
|
|
// We canonicalize the Message-ID: lower-case, no unneeded quoting.
|
|
s = strings.ToLower(s)
|
|
if s == "" {
|
|
return "", false, fmt.Errorf("%w: empty message-id", errBadMessageID)
|
|
}
|
|
addr, err := smtp.ParseAddress(s)
|
|
if err != nil {
|
|
// Common reasons for not being an address:
|
|
// 1. underscore in hostname.
|
|
// 2. ip literal instead of domain.
|
|
// 3. two @'s, perhaps intended as time-separator
|
|
// 4. no @'s, so no domain/host
|
|
return s, true, nil
|
|
}
|
|
// We preserve the unicode-ness of domain.
|
|
t := strings.Split(s, "@")
|
|
s = addr.Localpart.String() + "@" + t[len(t)-1]
|
|
return s, false, nil
|
|
}
|