about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDaniel Lublin <daniel@lublin.se>2019-06-25 01:36:11 +0200
committerDrew DeVault <sir@cmpwn.com>2019-06-25 10:31:28 -0400
commit61c94e54cd6da2e2989c252ca0b3cc7756b1ef65 (patch)
treec4164d682949b3ca4a56996496786d4542f937e1
parent198661bfbdf97690ea766da7396c496555b3a4e0 (diff)
downloadaerc-61c94e54cd6da2e2989c252ca0b3cc7756b1ef65.tar.gz
account-wizard: look up imap and smtp server by SRV records (#100)
-rw-r--r--widgets/account-wizard.go43
1 files changed, 42 insertions, 1 deletions
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
+}