diff options
author | Karel Balej <balejk@matfyz.cz> | 2024-01-30 20:11:28 +0100 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2024-02-12 23:01:55 +0100 |
commit | e8a6e8316a4b6e923f75b1e9a2d06089033e480b (patch) | |
tree | 079385307b6428f4a70ebd0450febdb7715a2a99 | |
parent | 3553e4f27165b18be84123d0ca015a019d35e41c (diff) | |
download | aerc-e8a6e8316a4b6e923f75b1e9a2d06089033e480b.tar.gz aerc-e8a6e8316a4b6e923f75b1e9a2d06089033e480b.zip |
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 <balejk@matfyz.cz>
Acked-by: Robin Jarry <robin@jarry.cc>
-rw-r--r-- | app/compose.go | 31 | ||||
-rw-r--r-- | lib/send/parse.go | 20 |
2 files changed, 30 insertions, 21 deletions
diff --git a/app/compose.go b/app/compose.go index be8e5563..c71c7013 100644 --- a/app/compose.go +++ b/app/compose.go @@ -5,7 +5,6 @@ import ( "bytes" "fmt" "io" - "math/rand" "net/textproto" "os" "os/exec" @@ -24,6 +23,7 @@ import ( "git.sr.ht/~rjarry/aerc/config" "git.sr.ht/~rjarry/aerc/lib" "git.sr.ht/~rjarry/aerc/lib/format" + "git.sr.ht/~rjarry/aerc/lib/send" "git.sr.ht/~rjarry/aerc/lib/state" "git.sr.ht/~rjarry/aerc/lib/templates" "git.sr.ht/~rjarry/aerc/lib/ui" @@ -820,7 +820,15 @@ func (c *Composer) PrepareHeader() (*mail.Header, error) { // control headers not normally set by the user // repeated calls to PrepareHeader should be a noop if !c.header.Has("Message-Id") { - hostname, err := getMessageIdHostname(c) + froms, err := c.header.AddressList("from") + if err != nil { + return nil, err + } + if len(froms) == 0 { + return nil, fmt.Errorf("no valid From address found") + } + hostname, err := send.GetMessageIdHostname( + c.acctConfig.SendWithHostname, froms[0]) if err != nil { return nil, err } @@ -839,25 +847,6 @@ func (c *Composer) PrepareHeader() (*mail.Header, error) { return c.header, nil } -func getMessageIdHostname(c *Composer) (string, error) { - if c.acctConfig.SendWithHostname { - return os.Hostname() - } - addrs, err := c.header.AddressList("from") - if err != nil { - return "", err - } - if len(addrs) == 0 { - // no from address present, generate a random hostname - return strings.ToUpper(strconv.FormatInt(rand.Int63(), 36)), nil - } - _, domain, found := strings.Cut(addrs[0].Address, "@") - if !found { - return "", fmt.Errorf("Invalid address %q", addrs[0]) - } - return domain, nil -} - func (c *Composer) parseEmbeddedHeader() (*mail.Header, error) { _, err := c.email.Seek(0, io.SeekStart) if err != nil { 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 +} |