---
- name: Install mlmmj
  package:
    name:
      - mlmmj
    state: present

- name: Create mlmmj group
  group:
    name: mlmmj
    gid: 20003

- name: Create mlmmj user
  user:
    name: mlmmj
    uid: 20003
    group: mlmmj
    create_home: no
    home: /var/vmail/mlmmj
    shell: /sbin/nologin

- name: Create lists directory
  file:
    path: /var/vmail/mlmmj
    state: directory
    owner: mlmmj
    group: mlmmj

- name: Create archive directory
  file:
    path: /var/vmail/mlmmj-archive
    state: directory
    owner: mlmmj
    group: mlmmj

- name: Create mlmmj postfix service
  lineinfile:
    path: /usr/local/etc/postfix/master.cf
    regexp: '^{{ transport_service.name }} +{{ transport_service.type }}'
    value: '{{ transport_service.value }}'
  notify:
    - Restart Postfix

- name: Configure Postfix
  postconf:
    name: '{{ item.name }}'
    value: '{{ item.value | default(omit) }}'
    state: '{{ item.state | default(omit) }}'
  with_items: '{{ postfix_config }}'
  notify:
    - Reload Postfix

- name: Generate mlmmj virtual aliases
  template:
    dest: /usr/local/etc/postfix/virtual_mlmmj
    src: virtual_mlmmj.j2
    mode: 0444
    owner: root
    group: wheel
  notify:
    - Rebuild mlmmj virtual aliases

- name: Generate mlmmj transport map
  template:
    dest: /usr/local/etc/postfix/transport_mlmmj
    src: transport_mlmmj.j2
    mode: 0444
    owner: root
    group: wheel
  notify:
    - Rebuild mlmmj transport map

- name: Create mlmmj-maintd service directories
  file:
    path: '{{ s6_etc_dir }}/service/{{ item }}'
    state: directory
    owner: root
    group: wheel
    mode: 0755
  with_items: '{{ mlmmj_service_dirs }}'
  notify:
    - Reload s6-rc
    - Restart mlmmj-maintd

- name: Generate mlmmj-maintd service scripts
  template:
    dest: '{{ s6_etc_dir }}/service/{{ item }}'
    src: '{{ item }}.j2'
    mode: 0555
    owner: root
    group: wheel
  with_items: '{{ mlmmj_service_scripts }}'
  notify:
    - Reload s6-rc
    - Restart mlmmj-maintd

- name: Generate mlmmj-maintd service configuration
  copy:
    dest: '{{ s6_etc_dir }}/service/{{ item.name }}'
    content: '{{ item.content }}'
    mode: 0444
    owner: root
    group: wheel
  loop_control:
    label: '{{ item.name }} = {{ item.content }}'
  with_items: '{{ mlmmj_service_config }}'
  notify:
    - Reload s6-rc
    - Restart mlmmj-maintd

- name: Flush handlers
  meta: flush_handlers

- name: Start mlmmj-maintd
  command: fdmove -c 2 1 s6-rc -l {{ s6_live_dir }} -u -v 2 change mlmmj-maintd
  register: change
  changed_when: change.stdout | length > 0

- name: Enable mlmmj-maintd
  lineinfile:
    path: '{{ s6_etc_dir }}/service/enabled/contents'
    regexp: "^mlmmj-maintd$"
    line: "mlmmj-maintd"
  notify:
    - Reload s6-rc

- name: Flush handlers (again)
  meta: flush_handlers

- name: Create mailing lists
  command: >
    mlmmj-make-ml -s /var/vmail/mlmmj -L {{ item.name }}
  args:
    creates: '/var/vmail/mlmmj/{{ item.name }}/control/listaddress'
    stdin: |
      {{ item.domain }}
      {{ item.owner }}
      en
  loop_control:
    label: '{{ item.name }}@{{ item.domain }}'
  with_items: '{{ mlmmj_lists }}'

- name: Change ownership and permissions
  file:
    path: '/var/vmail/mlmmj/{{ item.name }}'
    recurse: yes
    owner: mlmmj
    group: mlmmj
  loop_control:
    label: '{{ item.name }}@{{ item.domain }}'
  with_items: '{{ mlmmj_lists }}'

- name: Disable subscriber listing
  copy:
    dest: '/var/vmail/mlmmj/{{ item.name }}/control/nolistsubsemail'
    content: ''
    owner: mlmmj
    group: mlmmj
    mode: 0444
    force: no
  loop_control:
    label: '{{ item.name }}@{{ item.domain }}'
  with_items: '{{ mlmmj_lists }}'

- name: Strip these headers from incoming messages
  lineinfile:
    path: '/var/vmail/mlmmj/{{ item.0.name }}/control/delheaders'
    create: yes
    owner: mlmmj
    group: mlmmj
    mode: 0444
    regexp: '^{{ item.1 }}:$'
    line: '{{ item.1 }}:'
  loop_control:
    label: '{{ item.0.name }}@{{ item.0.domain }} : {{ item.1 }}'
  with_nested:
    - '{{ mlmmj_lists }}'
    - '{{ mlmmj_delete_headers }}'

- name: Add List-Id header
  lineinfile:
    path: '/var/vmail/mlmmj/{{ item.name }}/control/customheaders'
    create: yes
    owner: mlmmj
    group: mlmmj
    mode: 0444
    regexp: '^List-Id:'
    line: 'List-Id: {{ item.description }} <{{ item.name }}.{{ item.domain }}>'
  loop_control:
    label: '{{ item.name }}@{{ item.domain }}'
  with_items: '{{ mlmmj_lists }}'

- name: Add List-Subscribe header
  lineinfile:
    path: '/var/vmail/mlmmj/{{ item.name }}/control/customheaders'
    create: yes
    owner: mlmmj
    group: mlmmj
    mode: 0444
    regexp: '^List-Subscribe:'
    line: 'List-Subscribe: <mailto:{{ item.name }}+subscribe@{{ item.domain }}>'
  loop_control:
    label: '{{ item.name }}@{{ item.domain }}'
  with_items: '{{ mlmmj_lists }}'

- name: Add List-Unsubscribe header
  lineinfile:
    path: '/var/vmail/mlmmj/{{ item.name }}/control/customheaders'
    create: yes
    owner: mlmmj
    group: mlmmj
    mode: 0444
    regexp: '^List-Unsubscribe:'
    line: 'List-Unsubscribe: <mailto:{{ item.name }}+unsubscribe@{{ item.domain }}>'
  loop_control:
    label: '{{ item.name }}@{{ item.domain }}'
  with_items: '{{ mlmmj_lists }}'

- name: Add List-Post header
  lineinfile:
    path: '/var/vmail/mlmmj/{{ item.name }}/control/customheaders'
    create: yes
    owner: mlmmj
    group: mlmmj
    mode: 0444
    regexp: '^List-Post:'
    line: 'List-Post: <mailto:{{ item.name }}@{{ item.domain }}>'
  loop_control:
    label: '{{ item.name }}@{{ item.domain }}'
  with_items: '{{ mlmmj_lists }}'

- name: Add List-Help header
  lineinfile:
    path: '/var/vmail/mlmmj/{{ item.name }}/control/customheaders'
    create: yes
    owner: mlmmj
    group: mlmmj
    mode: 0444
    regexp: '^List-Help:'
    line: 'List-Help: <mailto:{{ item.name }}@{{ item.domain }}?subject=help>'
  loop_control:
    label: '{{ item.name }}@{{ item.domain }}'
  with_items: '{{ mlmmj_lists }}'

- name: Add prefix to subjects
  copy:
    dest: '/var/vmail/mlmmj/{{ item.name }}/control/prefix'
    content: |
      {{ item.prefix }}
    owner: mlmmj
    group: mlmmj
    mode: 0444
  loop_control:
    label: '{{ item.name }}@{{ item.domain }}'
  with_items: '{{ mlmmj_lists }}'

- name: Configure list addresses
  template:
    dest: '/var/vmail/mlmmj/{{ item.name }}/control/listaddress'
    src: listaddress.j2
    owner: mlmmj
    group: mlmmj
    mode: 0444
  loop_control:
    label: '{{ item.name }}@{{ item.domain }}'
  with_items: '{{ mlmmj_lists }}'

- name: Set moderate subscription
  copy:
    dest: '/var/vmail/mlmmj/{{ item.name }}/control/submod'
    content: '{{ item.subscription_moderators | default([]) | join("\n") }}'
    force: no
    owner: mlmmj
    group: mlmmj
    mode: 0444
  when: item.subscription_moderated | default(false)
  loop_control:
    label: '{{ item.name }}@{{ item.domain }}'
  with_items: '{{ mlmmj_lists }}'

- name: Unset moderate subscription
  file:
    path: '/var/vmail/mlmmj/{{ item.name }}/control/submod'
    state: absent
  when: not (item.subscription_moderated | default(false))
  loop_control:
    label: '{{ item.name }}@{{ item.domain }}'
  with_items: '{{ mlmmj_lists }}'

- name: Limit posts to subscribers
  copy:
    dest: '/var/vmail/mlmmj/{{ item.name }}/control/subonlypost'
    content: ''
    force: no
    owner: mlmmj
    group: mlmmj
    mode: 0444
  when: item.subscribers_only | default(false)
  loop_control:
    label: '{{ item.name }}@{{ item.domain }}'
  with_items: '{{ mlmmj_lists }}'

- name: Allow off list posts
  file:
    path: '/var/vmail/mlmmj/{{ item.name }}/control/subonlypost'
    state: absent
  when: not (item.subscribers_only | default(false))
  loop_control:
    label: '{{ item.name }}@{{ item.domain }}'
  with_items: '{{ mlmmj_lists }}'

- name: Moderate posts from non subscribers
  copy:
    dest: '/var/vmail/mlmmj/{{ item.name }}/control/modnonsubposts'
    content: ''
    force: no
    owner: mlmmj
    group: mlmmj
    mode: 0444
  when: item.moderate_nonsubscribers | default(false)
  loop_control:
    label: '{{ item.name }}@{{ item.domain }}'
  with_items: '{{ mlmmj_lists }}'

- name: Deny posts from non subscribers
  file:
    path: '/var/vmail/mlmmj/{{ item.name }}/control/modnonsubposts'
    state: absent
  when: not (item.moderate_nonsubscribers | default(false))
  loop_control:
    label: '{{ item.name }}@{{ item.domain }}'
  with_items: '{{ mlmmj_lists }}'

- name: Generate list of moderators
  template:
    dest: '/var/vmail/mlmmj/{{ item.name }}/control/moderators'
    src: moderators.j2
    owner: mlmmj
    group: mlmmj
    mode: 0444
  loop_control:
    label: '{{ item.name }}@{{ item.domain }}'
  with_items: '{{ mlmmj_lists }}'