From d7e2bd9c904e51c346196c842a91d16026a680c9 Mon Sep 17 00:00:00 2001
From: Tunui Franken <tfranken@easter-eggs.com>
Date: Tue, 25 Mar 2025 14:29:59 +0100
Subject: [PATCH] Fix creation of default mailboxes

---
 roles/mailserver_dovecot/tasks/main.yml       | 34 ++-----
 .../templates/conf.d/15-mailboxes.conf.j2     | 88 +++++++++++++++++++
 2 files changed, 93 insertions(+), 29 deletions(-)
 create mode 100644 roles/mailserver_dovecot/templates/conf.d/15-mailboxes.conf.j2

diff --git a/roles/mailserver_dovecot/tasks/main.yml b/roles/mailserver_dovecot/tasks/main.yml
index cfeff83..fc18442 100644
--- a/roles/mailserver_dovecot/tasks/main.yml
+++ b/roles/mailserver_dovecot/tasks/main.yml
@@ -237,36 +237,12 @@
     mode: "644"
   notify: Compile spam-to-folder.sieve
 
-- name: Add autoexpunge mailbox config (Junk)
+- name: Copy 15-mailboxes.conf
   become: true
-  ansible.builtin.lineinfile:
-    path: /etc/dovecot/conf.d/15-mailboxes.conf
-    line: "    autoexpunge = 30d"
-    insertafter: "mailbox Junk"
-  notify: Reload dovecot service
-
-- name: Add autoexpunge mailbox config (Trash)
-  become: true
-  ansible.builtin.lineinfile:
-    path: /etc/dovecot/conf.d/15-mailboxes.conf
-    line: "    autoexpunge = 30d"
-    insertafter: "mailbox Trash"
-  notify: Reload dovecot service
-
-- name: Add subscribe mailbox config (Junk)
-  become: true
-  ansible.builtin.lineinfile:
-    path: /etc/dovecot/conf.d/15-mailboxes.conf
-    line: "    auto = subscribe"
-    insertafter: 'special_use =.*Junk'
-  notify: Reload dovecot service
-
-- name: Add subscribe mailbox config (Trash)
-  become: true
-  ansible.builtin.lineinfile:
-    path: /etc/dovecot/conf.d/15-mailboxes.conf
-    line: "    auto = subscribe"
-    insertafter: 'special_use =.*Trash'
+  ansible.builtin.template:
+    src: conf.d/15-mailboxes.conf.j2
+    dest: /etc/dovecot/conf.d/15-mailboxes.conf
+    mode: "644"
   notify: Reload dovecot service
 
 - name: Add imap_sieve plugin
diff --git a/roles/mailserver_dovecot/templates/conf.d/15-mailboxes.conf.j2 b/roles/mailserver_dovecot/templates/conf.d/15-mailboxes.conf.j2
new file mode 100644
index 0000000..bda68db
--- /dev/null
+++ b/roles/mailserver_dovecot/templates/conf.d/15-mailboxes.conf.j2
@@ -0,0 +1,88 @@
+##
+## Mailbox definitions
+##
+
+# {{ ansible_managed }}
+
+# Each mailbox is specified in a separate mailbox section. The section name
+# specifies the mailbox name. If it has spaces, you can put the name
+# "in quotes". These sections can contain the following mailbox settings:
+#
+# auto:
+#   Indicates whether the mailbox with this name is automatically created
+#   implicitly when it is first accessed. The user can also be automatically
+#   subscribed to the mailbox after creation. The following values are
+#   defined for this setting:
+#
+#     no        - Never created automatically.
+#     create    - Automatically created, but no automatic subscription.
+#     subscribe - Automatically created and subscribed.
+#
+# special_use:
+#   A space-separated list of SPECIAL-USE flags (RFC 6154) to use for the
+#   mailbox. There are no validity checks, so you could specify anything
+#   you want in here, but it's not a good idea to use flags other than the
+#   standard ones specified in the RFC:
+#
+#     \All       - This (virtual) mailbox presents all messages in the
+#                  user's message store.
+#     \Archive   - This mailbox is used to archive messages.
+#     \Drafts    - This mailbox is used to hold draft messages.
+#     \Flagged   - This (virtual) mailbox presents all messages in the
+#                  user's message store marked with the IMAP \Flagged flag.
+#     \Important - This (virtual) mailbox presents all messages in the
+#                  user's message store deemed important to user.
+#     \Junk      - This mailbox is where messages deemed to be junk mail
+#                  are held.
+#     \Sent      - This mailbox is used to hold copies of messages that
+#                  have been sent.
+#     \Trash     - This mailbox is used to hold messages that have been
+#                  deleted.
+#
+# comment:
+#   Defines a default comment or note associated with the mailbox. This
+#   value is accessible through the IMAP METADATA mailbox entries
+#   "/shared/comment" and "/private/comment". Users with sufficient
+#   privileges can override the default value for entries with a custom
+#   value.
+
+# NOTE: Assumes "namespace inbox" has been defined in 10-mail.conf.
+namespace inbox {
+  # These mailboxes are widely used and could perhaps be created automatically:
+  mailbox Drafts {
+    special_use = \Drafts
+    auto = subscribe
+  }
+  mailbox Junk {
+    autoexpunge = 30d
+    special_use = \Junk
+    auto = subscribe
+  }
+  mailbox Trash {
+    autoexpunge = 30d
+    special_use = \Trash
+    auto = subscribe
+  }
+  mailbox Sent {
+    special_use = \Sent
+    auto = subscribe
+  }
+
+  # If you have a virtual "All messages" mailbox:
+  #mailbox virtual/All {
+  #  special_use = \All
+  #  comment = All my messages
+  #}
+
+  # If you have a virtual "Flagged" mailbox:
+  #mailbox virtual/Flagged {
+  #  special_use = \Flagged
+  #  comment = All my flagged messages
+  #}
+
+  # If you have a virtual "Important" mailbox:
+  #mailbox virtual/Important {
+  #  special_use = \Important
+  #  comment = All my important messages
+  #}
+}