8 "github.com/mjl-/mox/config"
9 "github.com/mjl-/mox/dns"
10 "github.com/mjl-/mox/mox-"
16 TLSModeImmediate TLSMode = 0
17 TLSModeSTARTTLS TLSMode = 1
18 TLSModeNone TLSMode = 2
21type ProtocolConfig struct {
28type ClientConfig struct {
30 Submission ProtocolConfig
33// ClientConfigDomain returns a single IMAP and Submission client configuration for
35func ClientConfigDomain(d dns.Domain) (rconfig ClientConfig, rerr error) {
36 var haveIMAP, haveSubmission bool
38 domConf, ok := mox.Conf.Domain(d)
40 return ClientConfig{}, fmt.Errorf("%w: unknown domain", ErrRequest)
43 gather := func(l config.Listener) (done bool) {
44 host := mox.Conf.Static.HostnameDomain
46 host = l.HostnameDomain
48 if domConf.ClientSettingsDomain != "" {
49 host = domConf.ClientSettingsDNSDomain
51 if !haveIMAP && l.IMAPS.Enabled {
52 rconfig.IMAP.Host = host
53 rconfig.IMAP.Port = config.Port(l.IMAPS.Port, 993)
54 rconfig.IMAP.TLSMode = TLSModeImmediate
55 rconfig.IMAP.EnabledOnHTTPS = l.IMAPS.EnabledOnHTTPS
58 if !haveIMAP && l.IMAP.Enabled {
59 rconfig.IMAP.Host = host
60 rconfig.IMAP.Port = config.Port(l.IMAP.Port, 143)
61 rconfig.IMAP.TLSMode = TLSModeSTARTTLS
63 rconfig.IMAP.TLSMode = TLSModeNone
67 if !haveSubmission && l.Submissions.Enabled {
68 rconfig.Submission.Host = host
69 rconfig.Submission.Port = config.Port(l.Submissions.Port, 465)
70 rconfig.Submission.TLSMode = TLSModeImmediate
71 rconfig.Submission.EnabledOnHTTPS = l.Submissions.EnabledOnHTTPS
74 if !haveSubmission && l.Submission.Enabled {
75 rconfig.Submission.Host = host
76 rconfig.Submission.Port = config.Port(l.Submission.Port, 587)
77 rconfig.Submission.TLSMode = TLSModeSTARTTLS
79 rconfig.Submission.TLSMode = TLSModeNone
83 return haveIMAP && haveSubmission
86 // Look at the public listener first. Most likely the intended configuration.
87 if public, ok := mox.Conf.Static.Listeners["public"]; ok {
92 // Go through the other listeners in consistent order.
93 names := slices.Sorted(maps.Keys(mox.Conf.Static.Listeners))
94 for _, name := range names {
95 if gather(mox.Conf.Static.Listeners[name]) {
99 return ClientConfig{}, fmt.Errorf("%w: no listeners found for imap and/or submission", ErrRequest)
102// ClientConfigs holds the client configuration for IMAP/Submission for a
104type ClientConfigs struct {
105 Entries []ClientConfigsEntry
108type ClientConfigsEntry struct {
116// ClientConfigsDomain returns the client configs for IMAP/Submission for a
118func ClientConfigsDomain(d dns.Domain) (ClientConfigs, error) {
119 domConf, ok := mox.Conf.Domain(d)
121 return ClientConfigs{}, fmt.Errorf("%w: unknown domain", ErrRequest)
125 c.Entries = []ClientConfigsEntry{}
126 var listeners []string
128 for name := range mox.Conf.Static.Listeners {
129 listeners = append(listeners, name)
131 slices.Sort(listeners)
133 note := func(tls bool, requiretls bool) string {
135 return "plain text, no STARTTLS configured"
138 return "STARTTLS required"
140 return "STARTTLS optional"
143 for _, name := range listeners {
144 l := mox.Conf.Static.Listeners[name]
145 host := mox.Conf.Static.HostnameDomain
146 if l.Hostname != "" {
147 host = l.HostnameDomain
149 if domConf.ClientSettingsDomain != "" {
150 host = domConf.ClientSettingsDNSDomain
152 if l.Submissions.Enabled {
154 if l.Submissions.EnabledOnHTTPS {
155 note += "; also served on port 443 with TLS ALPN \"smtp\""
157 c.Entries = append(c.Entries, ClientConfigsEntry{"Submission (SMTP)", host, config.Port(l.Submissions.Port, 465), name, note})
161 if l.IMAPS.EnabledOnHTTPS {
162 note += "; also served on port 443 with TLS ALPN \"imap\""
164 c.Entries = append(c.Entries, ClientConfigsEntry{"IMAP", host, config.Port(l.IMAPS.Port, 993), name, note})
166 if l.Submission.Enabled {
167 c.Entries = append(c.Entries, ClientConfigsEntry{"Submission (SMTP)", host, config.Port(l.Submission.Port, 587), name, note(l.TLS != nil, !l.Submission.NoRequireSTARTTLS)})
170 c.Entries = append(c.Entries, ClientConfigsEntry{"IMAP", host, config.Port(l.IMAPS.Port, 143), name, note(l.TLS != nil, !l.IMAP.NoRequireSTARTTLS)})