From 61c94e54cd6da2e2989c252ca0b3cc7756b1ef65 Mon Sep 17 00:00:00 2001 From: Daniel Lublin Date: Tue, 25 Jun 2019 01:36:11 +0200 Subject: account-wizard: look up imap and smtp server by SRV records (#100) --- widgets/account-wizard.go | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/widgets/account-wizard.go b/widgets/account-wizard.go index 3d9cfd4..117286e 100644 --- a/widgets/account-wizard.go +++ b/widgets/account-wizard.go @@ -3,10 +3,12 @@ package widgets import ( "errors" "fmt" + "net" "net/url" "os" "os/exec" "path" + "strconv" "strings" "time" @@ -176,7 +178,29 @@ func NewAccountWizard(conf *config.AercConfig, aerc *Aerc) *AccountWizard { basics.AddChild(wizard.email). At(8, 0) selecter := newSelecter([]string{"Next"}, 0). - OnChoose(wizard.advance) + OnChoose(func(option string) { + email := wizard.email.String() + if strings.ContainsRune(email, '@') { + server := email[strings.IndexRune(email, '@')+1:] + hostport, srv := getSRV(server, []string{"imaps", "imap"}) + if hostport != "" { + wizard.imapServer.Set(hostport) + if srv == "imaps" { + wizard.imapMode = IMAP_OVER_TLS + } else { + wizard.imapMode = IMAP_STARTTLS + } + wizard.imapUri() + } + hostport, srv = getSRV(server, []string{"submission"}) + if hostport != "" { + wizard.smtpServer.Set(hostport) + wizard.smtpMode = SMTP_STARTTLS + wizard.smtpUri() + } + } + wizard.advance(option) + }) basics.AddChild(selecter).At(9, 0) wizard.basics = []ui.Interactive{ wizard.accountName, wizard.fullName, wizard.email, selecter, @@ -785,3 +809,20 @@ func (sel *selecter) Event(event tcell.Event) bool { } return false } + +func getSRV(host string, services []string) (string, string) { + var hostport, srv string + for _, srv = range services { + _, addrs, err := net.LookupSRV(srv, "tcp", host) + if err != nil { + continue + } + if addrs[0].Target != "" && addrs[0].Port > 0 { + hostport = net.JoinHostPort( + strings.TrimSuffix(addrs[0].Target, "."), + strconv.Itoa(int(addrs[0].Port))) + break + } + } + return hostport, srv +} -- cgit v1.2.3