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 {
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
57 if !haveIMAP && l.IMAP.Enabled {
58 rconfig.IMAP.Host = host
59 rconfig.IMAP.Port = config.Port(l.IMAP.Port, 143)
60 rconfig.IMAP.TLSMode = TLSModeSTARTTLS
62 rconfig.IMAP.TLSMode = TLSModeNone
66 if !haveSubmission && l.Submissions.Enabled {
67 rconfig.Submission.Host = host
68 rconfig.Submission.Port = config.Port(l.Submissions.Port, 465)
69 rconfig.Submission.TLSMode = TLSModeImmediate
72 if !haveSubmission && l.Submission.Enabled {
73 rconfig.Submission.Host = host
74 rconfig.Submission.Port = config.Port(l.Submission.Port, 587)
75 rconfig.Submission.TLSMode = TLSModeSTARTTLS
77 rconfig.Submission.TLSMode = TLSModeNone
81 return haveIMAP && haveSubmission
84 // Look at the public listener first. Most likely the intended configuration.
85 if public, ok := mox.Conf.Static.Listeners["public"]; ok {
90 // Go through the other listeners in consistent order.
91 names := maps.Keys(mox.Conf.Static.Listeners)
93 for _, name := range names {
94 if gather(mox.Conf.Static.Listeners[name]) {
98 return ClientConfig{}, fmt.Errorf("%w: no listeners found for imap and/or submission", ErrRequest)
101// ClientConfigs holds the client configuration for IMAP/Submission for a
103type ClientConfigs struct {
104 Entries []ClientConfigsEntry
107type ClientConfigsEntry struct {
115// ClientConfigsDomain returns the client configs for IMAP/Submission for a
117func ClientConfigsDomain(d dns.Domain) (ClientConfigs, error) {
118 domConf, ok := mox.Conf.Domain(d)
120 return ClientConfigs{}, fmt.Errorf("%w: unknown domain", ErrRequest)
124 c.Entries = []ClientConfigsEntry{}
125 var listeners []string
127 for name := range mox.Conf.Static.Listeners {
128 listeners = append(listeners, name)
130 sort.Slice(listeners, func(i, j int) bool {
131 return listeners[i] < listeners[j]
134 note := func(tls bool, requiretls bool) string {
136 return "plain text, no STARTTLS configured"
139 return "STARTTLS required"
141 return "STARTTLS optional"
144 for _, name := range listeners {
145 l := mox.Conf.Static.Listeners[name]
146 host := mox.Conf.Static.HostnameDomain
147 if l.Hostname != "" {
148 host = l.HostnameDomain
150 if domConf.ClientSettingsDomain != "" {
151 host = domConf.ClientSettingsDNSDomain
153 if l.Submissions.Enabled {
154 c.Entries = append(c.Entries, ClientConfigsEntry{"Submission (SMTP)", host, config.Port(l.Submissions.Port, 465), name, "with TLS"})
157 c.Entries = append(c.Entries, ClientConfigsEntry{"IMAP", host, config.Port(l.IMAPS.Port, 993), name, "with TLS"})
159 if l.Submission.Enabled {
160 c.Entries = append(c.Entries, ClientConfigsEntry{"Submission (SMTP)", host, config.Port(l.Submission.Port, 587), name, note(l.TLS != nil, !l.Submission.NoRequireSTARTTLS)})
163 c.Entries = append(c.Entries, ClientConfigsEntry{"IMAP", host, config.Port(l.IMAPS.Port, 143), name, note(l.TLS != nil, !l.IMAP.NoRequireSTARTTLS)})