---

- name: install postfix
  pacman: name=postfix,postfix-pcre state=present

- name: install template configs
  template: src={{ item }}.j2 dest=/etc/postfix/{{ item }} owner=root group=root mode=0644
  with_items:
    - main.cf
    - master.cf
    - transport
    - transport.pcre
    - aliases
    - users.pcre
  notify:
    - restart postfix
    - postmap additional files
    - update aliases db

- name: install additional files
  copy: src={{ item }} dest=/etc/postfix/{{ item }} owner=root group=root mode=0644
  with_items:
    - access_client
    - access_sender
    - access_sender-post-filter
    - access_helo
    - access_recipient
    - body_checks
    - header_checks
    - relocated
    - domains
    - msa_header_checks
  notify:
    - postmap additional files

- name: create dhparam 2048
  command: openssl dhparam -out /etc/postfix/dh2048.pem 2048 creates=/etc/postfix/dh2048.pem
  notify:
    - reload postfix

- name: create ssl cert
  include_role:
    name: certificate
  vars:
    domains: ["{{ mail_domain }}"]

- name: install postfix cert renewal hook
  template: src=letsencrypt.hook.d.j2 dest=/etc/letsencrypt/hook.d/postfix owner=root group=root mode=0755

- name: install bouncehandler config
  template: src=wiki-bouncehandler.conf.j2 dest={{ postfix_wiki_bounce_config }} owner={{ postfix_wiki_bounce_user }} group=root mode=0600

- name: install packages for bounce handler
  pacman: name=perl-mediawiki-api,perl-config-simple state=present

- name: install bouncehandler script
  copy: src=bouncehandler.pl dest={{ postfix_wiki_bounce_mail_handler }} owner=root group=root mode=0755

- name: make bouncehandler user
  user: name={{ postfix_wiki_bounce_user }} shell=/bin/false skeleton=/var/empty state=present

- name: start and enable postfix
  service: name=postfix enabled=yes state=started

- name: remove old files
  file: path={{ item }} state=absent
  with_items:
    - compat_maps
    - compat_maps.db

- name: open firewall holes
  ansible.posix.firewalld: service={{ item }} permanent=true state=enabled immediate=yes
  with_items:
    - smtp
    - smtps
  when: configure_firewall
  tags:
    - firewall
