Debian Codegraph @

lib/autoconfig/autoconfig.go

raw
1 // Package autoconfig provides a somewhat smart way of automatically
2 // determining the mailservers for a given address.
3 //
4 // It works by first checking for a Thunderbird autoconfiguration, and if not
5 // found checking common names (imap|smtp|mail).example.com and performing a TCP
6 // Ping on their ports.
7 //
8 // The first part is described here: https://wiki.mozilla.org/Thunderbird:Autoconfiguration
9 package autoconfig
10
11 import (
12 "context"
13 "encoding/xml"
14 "fmt"
15 "net/http"
16 "net/url"
17 "strconv"
18 "strings"
19
20 "git.sr.ht/~rjarry/aerc/lib/log"
21 )
22
23 // getFromAutoconfig retrieves the config from the provider using a Mozilla
24 // Thunderbird autoconfig service
25 func getFromAutoconfig(ctx context.Context, localpart, domain string, result chan<- *Config) {
26 defer log.PanicHandler()
27 defer close(result)
28
29 res := make(chan *Config, 1)
30 go func(res chan *Config) {
31 defer log.PanicHandler()
32 defer close(res)
33
34 var addresses []*url.URL
35 u, err := url.Parse(fmt.Sprintf(
36 "https://autoconfig.%s/mail/config-v1.1.xml?emailaddress=%s",
37 domain, url.QueryEscape(localpart+"@"+domain)))
38 if err == nil {
39 addresses = append(addresses, u)
40 }
41 u, err = url.Parse(fmt.Sprintf(
42 "https://%s/.well-known/autoconfig/mail/config-v1.1.xml?emailaddress=%s",
43 domain, url.QueryEscape(localpart+"@"+domain)))
44 if err == nil {
45 addresses = append(addresses, u)
46 }
47
48 log.Debugf("checking for autoconfig files at %v", addresses)
49 cc := new(ClientConfig)
50 for _, u := range addresses {
51 response, err := httpGet(u.String())
52 if err != nil {
53 continue
54 }
55 err = xml.NewDecoder(response.Body).Decode(cc)
56 if err != nil {
57 continue
58 }
59 // IMAP sanity check
60 var incoming *IncomingServer
61 for i := range cc.EmailProvider.IncomingServer {
62 if strings.ToLower(cc.EmailProvider.IncomingServer[i].Type) != "imap" {
63 continue
64 }
65 incoming = &cc.EmailProvider.IncomingServer[i]
66 break
67 }
68 if incoming == nil {
69 // no imap server found
70 continue
71 }
72 var incomingPort int
73 if incomingPort, err = strconv.Atoi(incoming.Port); err != nil {
74 continue
75 }
76 inenc := EncryptionSTARTTLS
77 switch strings.ToLower(incoming.SocketType) {
78 case "plain":
79 inenc = EncryptionInsecure
80 case "ssl":
81 inenc = EncryptionTLS
82 }
83 if strings.ToLower(incoming.Username) == "%emailaddress%" {
84 incoming.Username = localpart + "@" + domain
85 }
86
87 var outport int
88 if outport, err = strconv.Atoi(cc.EmailProvider.OutgoingServer.Port); err != nil {
89 continue
90 }
91 outenc := EncryptionSTARTTLS
92 switch strings.ToLower(cc.EmailProvider.OutgoingServer.SocketType) {
93 case "plain":
94 outenc = EncryptionInsecure
95 case "ssl":
96 outenc = EncryptionTLS
97 }
98 if strings.ToLower(cc.EmailProvider.OutgoingServer.Username) == "%emailaddress%" {
99 cc.EmailProvider.OutgoingServer.Username = localpart + "@" + domain
100 }
101
102 log.Debugf("found HTTP-based autoconfig at %s", u)
103
104 res <- &Config{
105 Found: ProtocolIMAP,
106 IMAP: Credentials{
107 Encryption: inenc,
108 Address: incoming.Hostname,
109 Port: incomingPort,
110 Username: incoming.Username,
111 },
112 SMTP: Credentials{
113 Encryption: outenc,
114 Address: cc.EmailProvider.OutgoingServer.Hostname,
115 Port: outport,
116 Username: cc.EmailProvider.OutgoingServer.Username,
117 },
118 }
119 return
120 }
121 }(res)
122
123 select {
124 case r, isValue := <-res:
125 if isValue {
126 result <- r
127 }
128 case <-ctx.Done():
129 }
130 }
131
132 var httpGet = http.Get
133
134 ///////////////////////////////////////////////////////////////////////////////
135 // Autogenerated struct… you probably don't want to touch this. //
136 ///////////////////////////////////////////////////////////////////////////////
137
138 type ClientConfig struct {
139 XMLName xml.Name `xml:"clientConfig"`
140 Text string `xml:",chardata"`
141 Version string `xml:"version,attr"`
142 EmailProvider EmailProvider `xml:"emailProvider"`
143 }
144
145 type EmailProvider struct {
146 Text string `xml:",chardata"`
147 ID string `xml:"id,attr"`
148 Domain []string `xml:"domain"`
149 IncomingServer []IncomingServer `xml:"incomingServer"`
150 OutgoingServer OutgoingServer `xml:"outgoingServer"`
151 }
152
153 type IncomingServer struct {
154 Text string `xml:",chardata"`
155 // can only be "imap" or "pop3", only imap is supported by aerc
156 Type string `xml:"type,attr"`
157 Hostname string `xml:"hostname"`
158 Port string `xml:"port"`
159 SocketType string `xml:"socketType"`
160 Username string `xml:"username"`
161 Authentication string `xml:"authentication"`
162 Password string `xml:"password"`
163 }
164
165 type OutgoingServer struct {
166 Text string `xml:",chardata"`
167 Type string `xml:"type,attr"`
168 Hostname string `xml:"hostname"`
169 Port string `xml:"port"`
170 SocketType string `xml:"socketType"`
171 Username string `xml:"username"`
172 Authentication string `xml:"authentication"`
173 Restriction string `xml:"restriction"`
174 AddThisServer string `xml:"addThisServer"`
175 UseGlobalPreferredServer string `xml:"useGlobalPreferredServer"`
176 Password string `xml:"password"`
177 }