From 87440d6ea49bf7606c210db913bd808cd478a893 Mon Sep 17 00:00:00 2001
From: Pierre Ozoux <pierre@ozoux.net>
Date: Thu, 24 Feb 2022 11:46:10 +0100
Subject: [PATCH] Adds function for email creation.

---
 scripts/functions.sh | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/scripts/functions.sh b/scripts/functions.sh
index 66bc8db..6ca74b5 100644
--- a/scripts/functions.sh
+++ b/scripts/functions.sh
@@ -100,3 +100,36 @@ function pg_set_archive_mode_and_wait() {
     pg_ensure_rolling_update_is_done
   fi 
 }
+
+function create_emails() {
+  noreply_email_password=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 18 | head -n 1)
+  noreply_email="${NS}@liiib.re"
+  forum_email_password=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 18 | head -n 1)
+  forum_email="forum-${NS}@liiib.re"
+  chat_email_password=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 18 | head -n 1)
+  chat_email="${CHAT_SUBDOMAIN}-${NS}@liiib.re"
+
+  source /etc/env
+
+  mail_username=contact@indie.host
+  mail_hostname=mail.indie.host
+
+  noreply_local_part=`echo $noreply_email | cut -d@ -f1`
+  forum_local_part=`echo $forum_email | cut -d@ -f1`
+  chat_local_part=`echo $chat_email | cut -d@ -f1`
+  email_domain=`echo $noreply_email | cut -d@ -f2`
+
+  curl --data "username=${mail_username}&password=${mail_password}&login=Log+In&rememberme=0" -c /tmp/cookie.txt https://${mail_hostname}/auth/login
+  domain_id=`curl -b /tmp/cookie.txt https://${mail_hostname}/domain | grep $email_domain | grep purge-domain | grep -o 'purge-domain-[0-9]*' | grep -o '[0-9]*'`
+
+  curl --data "local_part=${noreply_local_part}&domain=${domain_id}&password=${noreply_email_password}" -b /tmp/cookie.txt https://${mail_hostname}/mailbox/add
+  curl --data "local_part=${forum_local_part}&domain=${domain_id}&password=${forum_email_password}" -b /tmp/cookie.txt https://${mail_hostname}/mailbox/add
+  curl --data "local_part=${chat_local_part}&domain=${domain_id}&password=${chat_email_password}" -b /tmp/cookie.txt https://${mail_hostname}/mailbox/add
+
+  rm /tmp/cookie.txt
+
+  kubectl -n ${NS} create secret generic ${NS}-smtp --from-literal=host=mail.indie.host --from-literal=port=587 --from-literal=from_email=${noreply_email} --from-literal=username=${noreply_email} --from-literal=password=${noreply_email_password} --from-literal=mail_from_address=${noreply_local_part}
+  kubectl -n ${NS} create secret generic forum-${NS}-smtp --from-literal=host=mail.indie.host --from-literal=port=587 --from-literal=from_email=${forum_email} --from-literal=username=${forum_email} --from-literal=password=${forum_email_password} --from-literal=mail_from_address=${forum_local_part}
+  kubectl -n ${NS} create secret generic ${CHAT_SUBDOMAIN}-${NS}-smtp --from-literal=host=mail.indie.host --from-literal=port=587 --from-literal=from_email=${chat_email} --from-literal=username=${chat_email} --from-literal=password=${chat_email_password} --from-literal=mail_from_address=${chat_local_part}
+}
+
-- 
GitLab