account-wizard: look up imap and smtp server by SRV records (#100)

This commit is contained in:
Daniel Lublin 2019-06-25 01:36:11 +02:00 committed by Drew DeVault
parent 198661bfbd
commit 61c94e54cd
1 changed files with 42 additions and 1 deletions

View File

@ -3,10 +3,12 @@ package widgets
import ( import (
"errors" "errors"
"fmt" "fmt"
"net"
"net/url" "net/url"
"os" "os"
"os/exec" "os/exec"
"path" "path"
"strconv"
"strings" "strings"
"time" "time"
@ -176,7 +178,29 @@ func NewAccountWizard(conf *config.AercConfig, aerc *Aerc) *AccountWizard {
basics.AddChild(wizard.email). basics.AddChild(wizard.email).
At(8, 0) At(8, 0)
selecter := newSelecter([]string{"Next"}, 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) basics.AddChild(selecter).At(9, 0)
wizard.basics = []ui.Interactive{ wizard.basics = []ui.Interactive{
wizard.accountName, wizard.fullName, wizard.email, selecter, wizard.accountName, wizard.fullName, wizard.email, selecter,
@ -785,3 +809,20 @@ func (sel *selecter) Event(event tcell.Event) bool {
} }
return false 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
}