From e8a6e8316a4b6e923f75b1e9a2d06089033e480b Mon Sep 17 00:00:00 2001 From: Karel Balej Date: Tue, 30 Jan 2024 20:11:28 +0100 Subject: lib: add function to obtain Message-ID hostname Make the function already present in app/compose.go reusable while also changing its signature for it not to require involvement of a Composer instance. Signed-off-by: Karel Balej Acked-by: Robin Jarry --- lib/send/parse.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'lib') diff --git a/lib/send/parse.go b/lib/send/parse.go index 460e91dc..d1088ee2 100644 --- a/lib/send/parse.go +++ b/lib/send/parse.go @@ -2,8 +2,13 @@ package send import ( "fmt" + "math/rand" "net/url" + "os" + "strconv" "strings" + + "github.com/emersion/go-message/mail" ) func parseScheme(uri *url.URL) (protocol string, auth string, err error) { @@ -30,3 +35,18 @@ func parseScheme(uri *url.URL) (protocol string, auth string, err error) { } return protocol, auth, nil } + +func GetMessageIdHostname(sendWithHostname bool, from *mail.Address) (string, error) { + if sendWithHostname { + return os.Hostname() + } + if from == nil { + // no from address present, generate a random hostname + return strings.ToUpper(strconv.FormatInt(rand.Int63(), 36)), nil + } + _, domain, found := strings.Cut(from.Address, "@") + if !found { + return "", fmt.Errorf("Invalid address %q", from) + } + return domain, nil +} -- cgit