login_greeting = ''

auth_allow_cleartext = yes
mail_uid=mail
mail_gid=mail
first_valid_uid=8
mail_privileged_group = mail
mail_driver = maildir
maildir_very_dirty_syncs = yes
mail_path = /var/mail/%{ user | domain }/%{ user | username }
mail_inbox_path = /var/mail/%{ user | domain }/%{ user | username }
mail_home = /var/mail/mailbox/%{ user | domain }/%{ user | username }

sql_driver = mysql

mysql /var/run/mysqld/mysqld.sock {
	user = mail
	password = <PASSWORD HERE>

	dbname = mail
}

# Disable system passdb else login is slowed down by a lot!
passdb pam {
	driver = static
}
userdb passwd {
	driver = static
}

userdb sql {
	query = SELECT concat('*:bytes=', quota) AS quota_rule FROM accounts WHERE username = '%{ user | username }' AND domain = '%{ user | domain }' AND sendonly = false;
	iterate_query = SELECT username, domain FROM accounts where sendonly = false;
}

passdb sql {
	query = SELECT username AS user, domain, password, concat('*:bytes=', quota) AS quota_rule FROM accounts WHERE username = '%{ user | username }' AND domain = '%{ user | domain }' and enabled = true;
}

protocols = imap lmtp pop3 sieve

protocol imap {
	mail_plugins {
		quota = yes
		imap_quota = yes
		imap_sieve = yes
	}
	mail_max_userip_connections = 20
	imap_idle_notify_interval = 29 min
}

protocol pop3 {
	mail_plugins {
		quota = yes
	}
}

protocol lmtp {
	postmaster_address = postmaster@linux4.de
	mail_plugins {
		sieve = yes
		quota = yes
	}
	# This is overriden to just user without domain in 20-ltmp.conf which is WRONG for SQL setup!
	auth_username_format = %{ user }
}

service anvil {
	unix_listener anvil {
		user = dovecot
		group = dovecot
		mode = 0660
	}
}

service lmtp {
	unix_listener /var/spool/postfix/private/dovecot-lmtp {
		mode = 0660
		group = postfix
		user = postfix
	}

	user = mail
}

namespace inbox {
	type = private
	#separator = /
	inbox = yes

	mailbox Spam { 
		auto = subscribe 
		special_use = \Junk
	}
 
	mailbox Sent {
		auto = subscribe
		special_use = \Sent
	}

	mailbox Trash {
		auto = subscribe
		special_use = \Trash
	}

	mailbox Drafts {
		auto = subscribe
		special_use = \Drafts
	}

	mailbox Archive {
		auto = subscribe
		special_use = \Archive
	}
}

service auth {
	unix_listener /var/spool/postfix/private/auth {
		group = postfix
		mode = 0660
		user = postfix
	}

	unix_listener auth-userdb {
		mode = 0660
		user = mail
		group = mail
	}
}

ssl = yes
ssl_server_cert_file = /etc/letsencrypt/live/linux4.de/fullchain.pem
ssl_server_key_file = /etc/letsencrypt/live/linux4.de/privkey.pem
auth_mechanisms = plain login
ssl_server_dh_file = /etc/dovecot/dh.pem

# Plugins
sieve_plugins = sieve_imapsieve sieve_extprograms
sieve_script spam_global {
	type = before
	path = /var/mail/sieve/global/spam-global.sieve
}
sieve_script personal {
	type = personal
	path = /var/mail/sieve/%{ user | domain}/%{ user | username}/scripts
	active_path = /var/mail/sieve/%{ user | domain }/%{ user | username }/active-script.sieve
}
imapsieve_from Spam {
	sieve_script ham {
		type = before
		cause = copy
		path = /var/mail/sieve/global/learn-spam.sieve
	}
}
mailbox Spam {
	sieve_script spam {
		type = before
		cause = copy
		path = /var/mail/sieve/global/learn-ham.sieve
	}
}
sieve_pipe_bin_dir = /usr/bin
quota "User quota" {
	quota_exceeded_message = Benutzer %{ user } hat das Speichervolumen aufgebraucht. / User %{ user } has exhausted allowed storage space.
}
