7 "golang.org/x/exp/maps"
9 "github.com/mjl-/mox/config"
10 "github.com/mjl-/mox/dns"
11 "github.com/mjl-/mox/mox-"
17 TLSModeImmediate TLSMode = 0
18 TLSModeSTARTTLS TLSMode = 1
19 TLSModeNone TLSMode = 2
22type ProtocolConfig struct {
29type ClientConfig struct {
31 Submission ProtocolConfig
34// ClientConfigDomain returns a single IMAP and Submission client configuration for
36func ClientConfigDomain(d dns.Domain) (rconfig ClientConfig, rerr error) {
37 var haveIMAP, haveSubmission bool
39 domConf, ok := mox.Conf.Domain(d)
41 return ClientConfig{}, fmt.Errorf("%w: unknown domain", ErrRequest)
44 gather := func(l config.Listener) (done bool) {
45 host := mox.Conf.Static.HostnameDomain
47 host = l.HostnameDomain
49 if domConf.ClientSettingsDomain != "" {
50 host = domConf.ClientSettingsDNSDomain
52 if !haveIMAP && l.IMAPS.Enabled {
53 rconfig.IMAP.Host = host
54 rconfig.IMAP.Port = config.Port(l.IMAPS.Port, 993)
55 rconfig.IMAP.TLSMode = TLSModeImmediate
56 rconfig.IMAP.EnabledOnHTTPS = l.IMAPS.EnabledOnHTTPS
59 if !haveIMAP && l.IMAP.Enabled {
60 rconfig.IMAP.Host = host
61 rconfig.IMAP.Port = config.Port(l.IMAP.Port, 143)
62 rconfig.IMAP.TLSMode = TLSModeSTARTTLS
64 rconfig.IMAP.TLSMode = TLSModeNone
68 if !haveSubmission && l.Submissions.Enabled {
69 rconfig.Submission.Host = host
70 rconfig.Submission.Port = config.Port(l.Submissions.Port, 465)
71 rconfig.Submission.TLSMode = TLSModeImmediate
72 rconfig.Submission.EnabledOnHTTPS = l.Submissions.EnabledOnHTTPS
75 if !haveSubmission && l.Submission.Enabled {
76 rconfig.Submission.Host = host
77 rconfig.Submission.Port = config.Port(l.Submission.Port, 587)
78 rconfig.Submission.TLSMode = TLSModeSTARTTLS
80 rconfig.Submission.TLSMode = TLSModeNone
84 return haveIMAP && haveSubmission
87 // Look at the public listener first. Most likely the intended configuration.
88 if public, ok := mox.Conf.Static.Listeners["public"]; ok {
93 // Go through the other listeners in consistent order.
94 names := maps.Keys(mox.Conf.Static.Listeners)
96 for _, name := range names {
97 if gather(mox.Conf.Static.Listeners[name]) {
101 return ClientConfig{}, fmt.Errorf("%w: no listeners found for imap and/or submission", ErrRequest)
104// ClientConfigs holds the client configuration for IMAP/Submission for a
106type ClientConfigs struct {
107 Entries []ClientConfigsEntry
110type ClientConfigsEntry struct {
118// ClientConfigsDomain returns the client configs for IMAP/Submission for a
120func ClientConfigsDomain(d dns.Domain) (ClientConfigs, error) {
121 domConf, ok := mox.Conf.Domain(d)
123 return ClientConfigs{}, fmt.Errorf("%w: unknown domain", ErrRequest)
127 c.Entries = []ClientConfigsEntry{}
128 var listeners []string
130 for name := range mox.Conf.Static.Listeners {
131 listeners = append(listeners, name)
133 sort.Slice(listeners, func(i, j int) bool {
134 return listeners[i] < listeners[j]
137 note := func(tls bool, requiretls bool) string {
139 return "plain text, no STARTTLS configured"
142 return "STARTTLS required"
144 return "STARTTLS optional"
147 for _, name := range listeners {
148 l := mox.Conf.Static.Listeners[name]
149 host := mox.Conf.Static.HostnameDomain
150 if l.Hostname != "" {
151 host = l.HostnameDomain
153 if domConf.ClientSettingsDomain != "" {
154 host = domConf.ClientSettingsDNSDomain
156 if l.Submissions.Enabled {
158 if l.Submissions.EnabledOnHTTPS {
159 note += "; also served on port 443 with TLS ALPN \"smtp\""
161 c.Entries = append(c.Entries, ClientConfigsEntry{"Submission (SMTP)", host, config.Port(l.Submissions.Port, 465), name, note})
165 if l.IMAPS.EnabledOnHTTPS {
166 note += "; also served on port 443 with TLS ALPN \"imap\""
168 c.Entries = append(c.Entries, ClientConfigsEntry{"IMAP", host, config.Port(l.IMAPS.Port, 993), name, note})
170 if l.Submission.Enabled {
171 c.Entries = append(c.Entries, ClientConfigsEntry{"Submission (SMTP)", host, config.Port(l.Submission.Port, 587), name, note(l.TLS != nil, !l.Submission.NoRequireSTARTTLS)})
174 c.Entries = append(c.Entries, ClientConfigsEntry{"IMAP", host, config.Port(l.IMAPS.Port, 143), name, note(l.TLS != nil, !l.IMAP.NoRequireSTARTTLS)})