diff --git a/README.md b/README.md index 8b89413..52a556b 100644 --- a/README.md +++ b/README.md @@ -10,3 +10,17 @@ mailman_antispam_chain_behaviour: discard Mailing-list owners can manage list-specific header matches and select their action (`accept`, `discard`, `hold`, or `reject`) from Postorius. The global chain is used only when a matching rule has no explicit action. + +Existing lists without any header matches can be initialized through the +Mailman database model by enabling the optional seed operation: + +```yaml +mailman_seed_empty_list_header_matches: true +mailman_empty_list_header_matches: + - header: 'X-Spam-Flag' + pattern: '^YES$' + action: 'discard' +``` + +The operation is idempotent. A list is modified only when its header-match +collection is empty; lists with one or more existing rules are left untouched. diff --git a/defaults/main.yml b/defaults/main.yml index c2a6439..f7dfcf2 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -127,6 +127,12 @@ mailman_antispam_header_checks: [] # 'accept', otherwise 'hold' will be used. mailman_antispam_chain_behaviour: "discard" +# Optionally seed list-specific header matches, but only on mailing lists that +# do not already have any rules. Existing list configuration is never merged +# with or replaced by these defaults. +mailman_seed_empty_list_header_matches: false +mailman_empty_list_header_matches: [] + mailman_start_nntp_runner: 'no' mailman_repository: 'https://gitlab.com/mailman/mailman-suite.git' @@ -182,5 +188,3 @@ mailman_postorius_cronjob_frequency: mailman_postorius_cronjob_special_times: - 'quarter_hourly' - 'minutely' - - diff --git a/tasks/mailman.yml b/tasks/mailman.yml index 283d355..c56b0cd 100644 --- a/tasks/mailman.yml +++ b/tasks/mailman.yml @@ -33,7 +33,7 @@ - '{{ mailman_spool_dir }}' - name: Create the mailman virtualenv. Manually, because python 3.6 - become: True + become: true become_user: '{{ mailman_user }}' shell: cd '{{ mailman_home }}' && python3 -m venv '{{ mailman_virtualenv_name }}' args: @@ -111,6 +111,47 @@ tags: [ 'mailman', 'mailman_conf' ] +- name: Seed header matches on lists without existing rules + block: + - name: Create the Mailman administration scripts directory + ansible.builtin.file: + path: '{{ mailman_conf_dir }}/scripts' + state: directory + owner: root + group: '{{ mailman_user }}' + mode: '0750' + + - name: Install the list header matches seed script + ansible.builtin.template: + src: seed-empty-list-header-matches.py.j2 + dest: '{{ mailman_conf_dir }}/scripts/seed_empty_list_header_matches.py' + owner: root + group: '{{ mailman_user }}' + mode: '0440' + + - name: Add header matches only to lists without rules + become: true + become_user: '{{ mailman_user }}' + ansible.builtin.command: + argv: + - '{{ mailman_bindir }}/mailman' + - -C + - '{{ mailman_conf_dir }}/mailman.cfg' + - shell + - --run + - seed_empty_list_header_matches.apply + - --listspec + - '^.*' + environment: + PYTHONPATH: '{{ mailman_conf_dir }}/scripts' + register: mailman_seed_header_matches_result + changed_when: "'CHANGED ' in mailman_seed_header_matches_result.stdout" + + when: + - not ansible_check_mode + - mailman_seed_empty_list_header_matches | bool + - mailman_empty_list_header_matches | length > 0 + tags: [ 'mailman', 'mailman_conf', 'mailman_header_matches' ] - name: Add the maintenance cron jobs tags: ['mailman', 'mailman_conf', 'mailman_cron']