## Maddy Mail Server - default configuration file (2021-08-16)
# Suitable for small-scale deployments. Uses its own format for local users DB,
# should be managed via maddyctl utility.
#
# See tutorials at https://maddy.email for guidance on typical
# configuration changes.
#
# See manual pages (also available at https://maddy.email) for reference
# documentation.

# ----------------------------------------------------------------------------
# Base variables

$(hostname) = {{ .Values.maddy.hostname }}
$(primary_domain) = {{ .Values.maddy.primary_domain }}
$(local_domains) = $(primary_domain) {{ if .Values.maddy.secondary_domains }}{{ .Values.maddy.secondary_domains }}{{ end }}
$(alias_file) = /config/aliases

# ----------------------------------------------------------------------------
# Persistency path

state_dir {{ .Values.persistence.data.mountPath }}

# ----------------------------------------------------------------------------
# TLS server certs (IMAP and SMTP)

tls file /certificates/tls.crt /certificates/tls.key

# ----------------------------------------------------------------------------
# Open Metrics

{{ if .Values.service.openmetrics.ports.openmetrics.enabled -}}
openmetrics tcp://127.0.0.1:{{ .Values.service.openmetrics.ports.openmetrics.port }} { }
{{- else -}}
# Not enabled in Helm
{{- end }}


# ----------------------------------------------------------------------------
# Local storage & authentication

# pass_table provides local hashed passwords storage for authentication of
# users. It can be configured to use any "table" module, in default
# configuration a table in SQLite DB is used.
# Table can be replaced to use e.g. a file for passwords. Or pass_table module
# can be replaced altogether to use some external source of credentials (e.g.
# PAM, /etc/shadow file).
#
# If table module supports it (sql_table does) - credentials can be managed
# using 'maddyctl creds' command.

{{ if eq .Values.maddy.auth.type "sql" -}}
auth.pass_table local_authdb {
    table sql_table {
    {{- if eq .Values.maddy.sql.type "sqlite3" }}
        driver sqlite3
        dsn credentials.db
        table_name passwords
    {{- else if eq .Values.maddy.sql.type "postgres" }}
        driver postgres
        dsn {{ $.Values.maddy.sql.postgres_dsn_line  }}
        table_name passwords
    {{- else }}
        {{- fail (printf "Not a valid sql type (%s)" .Values.maddy.sql.type ) }}
    {{- end }}
    }
}
{{- else if eq .Values.maddy.auth.type "ldap" -}}
auth.ldap local_authdb {
    {{- range $option, $value := .Values.maddy.auth.ldap }}
    {{ $option}} {{ $value }}
    {{- end }}
}
{{- else -}}
{{- fail (printf "Not a valid auth type (%s)" .Values.maddy.auth.type ) }}
{{- end }}

# imapsql module stores all indexes and metadata necessary for IMAP using a
# relational database. It is used by IMAP endpoint for mailbox access and
# also by SMTP & Submission endpoints for delivery of local messages.
#
# IMAP accounts, mailboxes and all message metadata can be inspected using
# imap-* subcommands of maddyctl utility.

storage.imapsql local_mailboxes {
{{- if eq .Values.maddy.sql.type "sqlite3" }}
    driver sqlite3
    dsn imapsql.db
{{- else if eq .Values.maddy.sql.type "postgres" }}
    driver postgres
    dsn {{ $.Values.maddy.sql.postgres_dsn_line }}
{{- else }}
    {{- fail (printf "Not a valid sql type (%s)" .Values.maddy.sql.type ) }}
{{- end }}
    # Other settings
    {{- range $option, $value := .Values.maddy.extra_settings.local_mailboxes }}
    {{ $option }} {{ tpl $value $ }}
    {{- end }}
}

# ----------------------------------------------------------------------------
# SMTP endpoints + message routing

hostname $(hostname)

{{ range $name, $rule := .Values.maddy.smtp_rules -}}
{{- if $rule.enabled }}
## Rule {{ $name }}
{{- if $rule.file }}
### from file "{{ $rule.file }}"
{{ tpl ($.Files.Get (printf "files/smtp_rules/%s" $rule.file) ) $ }}
{{- end }}
{{- if $rule.custom }}
### from custom block"
{{ tpl $rule.custom $ }}
{{- end }}
{{- end }}
{{- end }}

# ----------------------------------------------------------------------------
# IMAP endpoints

imap tls://0.0.0.0:{{ .Values.service.main.ports.imaps.port }} tcp://0.0.0.0:{{ .Values.service.main.ports.imap.port }} {
    auth &local_authdb
    storage &local_mailboxes
    # Other settings
    {{- range $option, $value := .Values.maddy.extra_settings.imap }}
    {{ $option }} {{ tpl $value $ }}
    {{- end }}
}
