captainVersion: 4
services:
    $$cap_appname:
        ports:
            - '25:25'
            - '110:110'
            - '143:143'
            - '465:465'
            - '587:587'
            - '993:993'
            - '995:995'
        volumes:
            - /etc/localtime:/etc/localtime
            - $$cap_appname-data-backup-mysql:/var/vmail/backup/mysql
            - $$cap_appname-data-mailboxes:/var/vmail/vmail1
            - $$cap_appname-data-mlmmj:/var/vmail/mlmmj
            - $$cap_appname-data-mlmmj-archive:/var/vmail/mlmmj-archive
            - $$cap_appname-data-imapsieve-copy:/var/vmail/imapsieve_copy
            - $$cap_appname-data-custom:/opt/iredmail/custom
            - $$cap_appname-data-ssl:/opt/iredmail/ssl
            - $$cap_appname-data-mysql:/var/lib/mysql
            - $$cap_appname-data-clamav:/var/lib/clamav
            - $$cap_appname-data-sa-rules:/var/lib/spamassassin
            - $$cap_appname-data-postfix-queue:/var/spool/postfix
        restart: always
        caproverExtra:
            dockerfileLines:
                - ARG VERSION=stable
                - FROM iredmail/mariadb:$VERSION
                - WORKDIR /iredmail
                - ARG FIRST_MAIL_DOMAIN
                - ARG HOSTNAME=mail.$FIRST_MAIL_DOMAIN
                - ARG FIRST_MAIL_DOMAIN_ADMIN_PASSWORD=1234
                - ARG MLMMJADMIN_API_TOKEN
                - ARG ROUNDCUBE_DES_KEY
                - ARG ADMIN_VIRSION=NoChange
                - ENV ADMIN_VIRSION=$ADMIN_VIRSION
                - ENV FIRST_MAIL_DOMAIN=$FIRST_MAIL_DOMAIN
                - ENV HOSTNAME=$HOSTNAME
                - ENV FIRST_MAIL_DOMAIN_ADMIN_PASSWORD=$FIRST_MAIL_DOMAIN_ADMIN_PASSWORD
                - ENV MLMMJADMIN_API_TOKEN=$MLMMJADMIN_API_TOKEN
                - ENV ROUNDCUBE_DES_KEY=ROUNDCUBE_DES_KEY
                - >-
                    RUN echo "if [\"$ADMIN_VIRSION\" != \"NoChange\" && \"$ADMIN_VIRSION\"
                    != \"\$(ls /opt/www/ | grep 'iRedAdmin-' | cut -d'-' -f2)\"]; then
                    wget -c -q
                    https://github.com/iredmail/iRedAdmin/archive/$ADMIN_VIRSION.tar.gz &&
                    tar xzf $ADMIN_VIRSION.tar.gz -C /opt/www && rm -f
                    $ADMIN_VIRSION.tar.gz && cp -R -f /opt/www/iRedAdmin-$ADMIN_VIRSION/*
                    /opt/www/iredadmin && sleep 1; rm -r
                    /opt/www/iRedAdmin-$ADMIN_VIRSION; fi" >> build.sh
                - RUN sh build.sh && rm build.sh
                - >-
                    RUN cat /opt/www/iredadmin/templates/default/layout.html | replace
                    '<li {% block navlinks_dashboard' '<li><a
                    href="/mail">Webmail</a></li><li {% block navlinks_dashboard' | tee
                    /opt/www/iredadmin/templates/default/layout.html
                - >-
                    ARG fix_http="\"server { listen 80; server_name _; include
                    /etc/nginx/templates/roundcube.tmpl; location ~ ^/iredadmin/(.*)
                    {return 301 /;}location = /iredadmin {return 301 /;} location / {
                    proxy_pass http://0.0.0.0:8080/; add_header Content-Security-Policy
                    \\\"\\\"; } location ~ \.php$ { root /opt/www/roundcubemail; include
                    /etc/nginx/templates/fastcgi_php.tmpl; fastcgi_param SCRIPT_FILENAME
                    /opt/www/roundcubemail\$fastcgi_script_name; } location ~
                    ^/(bin|config|installer|logs|SQL|temp|vendor)/.* { deny all; }
                    location ~
                    ^/(CHANGELOG|composer.json|INSTALL|jsdeps.json|LICENSE|README|UPGRADING)$
                    { deny all; } location ~ ^/plugins/.*/config.inc.php.* { deny all; }
                    location ~ ^/plugins/enigma/home(\$|/.*) { deny all; } }\""
                - >-
                    ARG full_Script="rm /etc/nginx/sites-enabled/00-default-ssl.conf \\n
                    echo -e $fix_http >> /etc/nginx/sites-enabled/00-default-ssl.conf \\n
                    cd /opt/www/iredadmin \\n python3 iredadmin.py & disown"
                - RUN echo $full_Script >> /docker/entrypoints/nginx.sh
                - >-
                    RUN echo "\$config['force_https'] = false;" >>
                    /opt/www/roundcubemail/config/config.inc.php
                - >-
                    ENV addHTML="<style>\n    .dns-t table {\n        border: solid 1px
                    #a3a79e;\n    }\n    .dns-t td {\n        overflow-wrap:
                    anywhere;\n        max-width: 180px;\n    }\n</style>\n<div
                    class=\"dns-t\">\n    <h2>MX record</h2>\n    <table>\n       
                    <tr>\n            <th>NAME</th>\n           
                    <th>PRIORITY</th>\n            <th>TTL</th>\n           
                    <th>TYPE</th>\n            <th>DATA</th>\n        </tr>\n       
                    <tr>\n            <td>{{ cur_domain }}</td>\n           
                    <td>10</td>\n            <td>14400</td>\n           
                    <td>mx</td>\n            <td class=\"host_now\"></td>\n       
                    </tr>\n        <tr>\n            <td>autodiscover.{{ cur_domain
                    }}</td>\n            <td>10</td>\n           
                    <td>14400</td>\n            <td>mx</td>\n            <td
                    class=\"host_now\"></td>>\n        </tr>\n        <tr>\n           
                    <td>autoconfig.{{ cur_domain }}</td>\n           
                    <td>10</td>\n            <td>14400</td>\n           
                    <td>mx</td>\n            <td class=\"host_now\"></td>\n       
                    </tr>\n    </table>\n    <h2>SPF record</h2>\n    <table>\n       
                    <tr>\n            <th>NAME</th>\n            <th>TTL</th>\n           
                    <th>TYPE</th>\n            <th>DATA</th>\n        </tr>\n       
                    <tr>\n            <td>{{ cur_domain }}</td>\n           
                    <td>3600</td>\n            <td>TXT</td>\n            <td>\"v=spf1 mx
                    -all\"</td>\n        </tr>\n    </table>\n    <h2>DKIM
                    record</h2>\n    <table>\n        <tr>\n           
                    <th>NAME</th>\n            <th>TTL</th>\n           
                    <th>TYPE</th>\n            <th>DATA</th>\n        </tr>\n       
                    <tr>\n            {%set get_dns=dns_data()%}\n           
                    <td>dkim._domainkey.{{ cur_domain }}</td>\n            <td>{{
                    get_dns[0] }}</td>\n            <td>{{ get_dns[1] }}</td>\n           
                    <td>{{ get_dns[2] }}</td>\n        </tr>\n    </table>\n    <h2>SRV
                    record for Jabber/XMPP service</h2>\n    <table>\n       
                    <tr>\n            <th>NAME</th>\n            <th>TTL</th>\n           
                    <th>PORT</th>\n            <th>PRIORITY</th>\n           
                    <th>WEIGHT</th>\n            <th>TYPE</th>\n           
                    <th>DATA</th>\n        </tr>\n        <tr>\n           
                    <td>_xmpp-client._tcp.{{ cur_domain }}</td>\n           
                    <td>18000</td>\n            <td>5222</td>\n           
                    <td>0</td>\n            <td>5</td>\n           
                    <td>SRV</td>\n            <td class=\"host_now\"></td>\n       
                    </tr>\n        <tr>\n            <td>_xmpp-server._tcp.{{ cur_domain
                    }}</td>\n            <td>18000</td>\n           
                    <td>5269</td>\n            <td>0</td>\n           
                    <td>5</td>\n            <td>SRV</td>\n            <td
                    class=\"host_now\"></td>\n        </tr>\n    </table>\n    <p>*You
                    also need to setup reverse PTR record for server IP address. <a
                    href=\"https://docs.iredmail.org/setup.dns.html#ptr\"
                    target=\"_blank\">Documentation</a>.</p>\n    <script>\n       
                    document.querySelectorAll('.host_now').forEach(x => x.innerText =
                    location.host);\n    </script>\n</div>"
                - >-
                    RUN cat /opt/www/iredadmin/templates/default/sql/domain/profile.html |
                    replace 'id=\"profile_general\">' 'id="profile_general">addHTML' | tee
                    /opt/www/iredadmin/templates/default/sql/domain/profile.html
                - >-
                    RUN echo "import os\nf =
                    open('/opt/www/iredadmin/templates/default/sql/domain/profile.html',
                    'r')\nprint(f.read().replace('addHTML',
                    os.environ['addHTML'].replace('\\\\\\\n', '\\\\n')))\nf.close()" >>
                    buildHTML.py
                - >-
                    RUN python3 buildHTML.py | tee
                    /opt/www/iredadmin/templates/default/sql/domain/profile2.html
                - RUN rm buildHTML.py
                - >-
                    RUN cat /opt/www/iredadmin/templates/default/sql/domain/profile2.html
                    | tee /opt/www/iredadmin/templates/default/sql/domain/profile.html &&
                    rm /opt/www/iredadmin/templates/default/sql/domain/profile2.html
                - ENV addHTML=''
                - >-
                    RUN cat /opt/www/iredadmin/libs/iredbase.py | replace 'jinja_env_vars
                    = {' "$(echo 'def dns_data():\n    all_keys = os.popen("amavisd-new -c
                    /etc/amavis/conf.d/50-user
                    showkeys").read().split(".")[-1].strip()\n    next__ =
                    all_keys.split(" ")\n    text_with_remove = "
                    ".join(next__[3:]).strip()[:-1]\n    text__ = "".join(map(lambda x:
                    x.strip(), text_with_remove.split("\"")))\n    return [next__[0],
                    next__[1], text__]\njinja_env_vars = {"dns_data":dns_data,' | sed -e
                    $'s/\\\\n/\\\n/g')" | tee /opt/www/iredadmin/libs/iredbase.py
                - RUN apt install -y amavisd-new
                - EXPOSE 80 443 25 465 587 110 995 143 993
            containerHttpPort: '80'
        environment:
            VERSION: $$cap_iredmail_version
            MLMMJADMIN_API_TOKEN: $$cap_gen_random_hex(32)
            ROUNDCUBE_DES_KEY: $$cap_gen_random_hex(24)
            FIRST_MAIL_DOMAIN: $$cap_root_domain
            HOSTNAME: $$cap_appname.$$cap_root_domain
            ADMIN_VIRSION: $$cap_iredmail_admin_version
            FIRST_MAIL_DOMAIN_ADMIN_PASSWORD: $$cap_iredmail_default_password
caproverOneClickApp:
    variables:
        - id: $$cap_iredmail_version
          label: iRedMail version tag
          description: >-
              Check out their Docker page for the valid tags
              https://hub.docker.com/r/iredmail/mariadb/tag
          defaultValue: stable
        - id: $$cap_iredmail_admin_version
          label: iRedMail Admin version tag
          description: >-
              Set 'NoChange' to stay by the docker image version
              OR
              Check out their GitHub page for the newest version
              https://github.com/iredmail/iRedAdmin/releases
          defaultValue: NoChange
        - id: $$cap_iredmail_default_password
          label: First Password
          description: >-
              Password for the admin login (you can change that later from the admin panel)
          defaultValue: $$cap_gen_random_hex(8)
    instructions:
        start: >-
            iRedMail is a free, open-source and full-featured mail server that allows you to host your own mail server at no cost.
            SMTP + IMAP + POP3 + Antispam & Antivirus + Web administration.
            Recommended: Use 'mail' as the App Name.
        end: >-
            You **should** enable https to access the Admin panel and the Webmail safely.
            Admin panel: [$$cap_appname.$$cap_root_domain](//$$cap_appname.$$cap_root_domain), Webmail: [$$cap_appname.$$cap_root_domain/mail](//$$cap_appname.$$cap_root_domain/mail)
            Credentials: postmaster@$$cap_root_domain/$$cap_iredmail_default_password
            The deployment can take up to 5 minutes.
    displayName: iRedMail
    isOfficial: false
    description: iRedMail - Open Source Mail Server, built in one container
    documentation: Read the iRedMail documentation -> https://docs.iredmail.org/
