---
- name: Install mailman3
  apt:
    update_cache: true
    name:
      - mailman3-full
      - python3-ipython  # Prettier shell
      - python3-pip  # CAS
      - python3-lxml  # CAS
      - sassc
    install_recommends: false
  register: apt_result
  retries: 3
  until: apt_result is succeeded

- name: Install Crans python modules
  pip:
    name: "{{ item }}"
  loop:
    - git+https://gitlab.adm.crans.org/nounous/mailman-crans-theme.git
    - git+https://gitlab.adm.crans.org/nounous/allauth-cas-crans.git

# You will need to setup postgres
# sudo -u postgres createuser -P mailman3
# sudo -u postgres createdb -O mailman3 mailman3
- name: Configure mailman3
  template:
    src: "mailman3/{{ item }}.j2"
    dest: "/etc/mailman3/{{ item }}"
    mode: 0640
    owner: root
    group: list
  loop:
    - mailman.cfg
    - mailman-hyperkitty.cfg
  notify: Restart mailman3

- name: Apply permissions to the mailman directory
  file:
    path: /var/lib/mailman3
    state: directory
    owner: list
    group: list

# You will need to setup postgres
# sudo -u postgres createuser -P mailman3web
# sudo -u postgres createdb -O mailman3web mailman3web
- name: Configure mailman3-web
  template:
    src: mailman3/mailman-web.py.j2
    dest: /etc/mailman3/mailman-web.py
    mode: 0640
    owner: root
    group: www-data
  notify: Restart mailman3-web

- name: Migrate Django database
  django_manage:
    command: migrate
    project_path: /usr/share/mailman3-web
  become: true
  become_user: www-data
  notify: Restart mailman3-web

- name: Collect static files
  django_manage:
    command: collectstatic
    project_path: /usr/share/mailman3-web
  become: true
  become_user: www-data
  notify: Restart mailman3-web

- name: Compress static files
  django_manage:
    command: compress
    project_path: /usr/share/mailman3-web
  become: true
  become_user: www-data
  notify: Restart mailman3-web

# When notifying moderators daily, add link to moderation page
- name: Patch daily moderation mails
  blockinfile:
    path: /usr/lib/python3/dist-packages/mailman/commands/cli_notify.py
    marker: "{mark}"
    marker_begin: '    # XXX This should be a template.'
    marker_end: '    msg = OwnerNotification(mlist, subject, text, mlist.administrators)'
    block: "    text = _(\"\"\"La liste {} a {} requêtes de modération en attente.\n\n{}\n\nVous pouvez gérer ces demandes via votre interface web :\nhttps://{{ mailman3.web_domains[0] }}/postorius/lists/{}/held_messages\n\"\"\").format(mlist.fqdn_listname, count, detail, mlist.fqdn_listname)"

- name: Send owner notifications from listname-bounces@domain
  lineinfile:
    path: /usr/lib/python3/dist-packages/mailman/email/message.py
    regexp: '        sender ='
    line: '        sender = f"{mlist.list_name}-bounces@{mlist.domain.mail_host}"'


# When notifying moderators of a new incoming message, add link to moderation page
- name: Patch moderation requests messages
  template:
    src: mailman/templates/fr/list:admin:action:post.txt.j2
    dest: /usr/lib/python3/dist-packages/mailman/templates/fr/list:admin:action:post.txt
    owner: root
    group: root
    mode: 0644

- name: Indicate role in motd
  template:
    src: update-motd.d/05-service.j2
    dest: /etc/update-motd.d/05-mailman3
    mode: 0755