mirror of
https://git.sr.ht/~rjarry/aerc
synced 2025-07-06 19:30:22 +02:00

Add a direct mailserver poker that tries to connect to the mailserver with the highest priority retrieved from via DNS. Signed-off-by: Moritz Poldrack <git@moritz.sh> Acked-by: Robin Jarry <robin@jarry.cc>
74 lines
1.5 KiB
Go
74 lines
1.5 KiB
Go
package autoconfig
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"sort"
|
|
"strings"
|
|
"sync"
|
|
|
|
"git.sr.ht/~rjarry/aerc/lib/log"
|
|
)
|
|
|
|
func guessMX(ctx context.Context, localpart, domain string, result chan *Config) {
|
|
defer log.PanicHandler()
|
|
defer close(result)
|
|
|
|
res := make(chan *Config)
|
|
go func(res chan *Config) {
|
|
defer log.PanicHandler()
|
|
defer close(res)
|
|
|
|
var imapConfig, smtpConfig *Credentials
|
|
var wg sync.WaitGroup
|
|
|
|
records, err := lookupMX(domain)
|
|
if err != nil || len(records) == 0 {
|
|
return
|
|
}
|
|
|
|
sort.Slice(records, func(a, b int) bool { return records[a].Pref < records[b].Pref })
|
|
|
|
mailserver := records[0].Host
|
|
mailserver = strings.TrimSuffix(mailserver, ".")
|
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
|
defer log.PanicHandler()
|
|
defer wg.Done()
|
|
imapConfig = tryPort(mailserver, []portEncryption{{143, EncryptionSTARTTLS}, {993, EncryptionTLS}})
|
|
}()
|
|
|
|
go func() {
|
|
defer log.PanicHandler()
|
|
defer wg.Done()
|
|
smtpConfig = tryPort(mailserver, []portEncryption{{587, EncryptionSTARTTLS}, {465, EncryptionTLS}})
|
|
}()
|
|
wg.Wait()
|
|
|
|
if imapConfig == nil || smtpConfig == nil {
|
|
return
|
|
}
|
|
log.Debugf("found MX records: %v %v", imapConfig, smtpConfig)
|
|
|
|
imapConfig.Username = localpart + "@" + domain
|
|
smtpConfig.Username = localpart + "@" + domain
|
|
|
|
res <- &Config{
|
|
Found: ProtocolIMAP,
|
|
IMAP: *imapConfig,
|
|
SMTP: *smtpConfig,
|
|
}
|
|
}(res)
|
|
|
|
select {
|
|
case r, next := <-res:
|
|
if next {
|
|
result <- r
|
|
}
|
|
case <-ctx.Done():
|
|
}
|
|
}
|
|
|
|
var lookupMX = net.LookupMX
|