From 56e9a9269c5a56aab21138f7258e3fed227e6001 Mon Sep 17 00:00:00 2001 From: Joe Banks Date: Mon, 2 Sep 2024 01:08:25 +0100 Subject: Migrate service scripts from files to templates --- .../roles/postfix/files/services/fortune-reply.sh | 39 ---------------------- ansible/roles/postfix/tasks/main.yml | 22 +++++++++--- .../postfix/templates/services/fortune-reply.sh.j2 | 39 ++++++++++++++++++++++ 3 files changed, 56 insertions(+), 44 deletions(-) delete mode 100755 ansible/roles/postfix/files/services/fortune-reply.sh create mode 100644 ansible/roles/postfix/templates/services/fortune-reply.sh.j2 (limited to 'ansible') diff --git a/ansible/roles/postfix/files/services/fortune-reply.sh b/ansible/roles/postfix/files/services/fortune-reply.sh deleted file mode 100755 index 97f2ce8..0000000 --- a/ansible/roles/postfix/files/services/fortune-reply.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/bash - -# Read the entire email into a variable -EMAIL=$(cat) - -# Extract the sender's email address -SENDER=$(echo "$EMAIL" | maddr -a -h from -) - -# Extract the Message-ID of the original email -MESSAGE_ID=$(echo "$EMAIL" | mhdr -h message-id -) - -# Extract the original Subject and prefix it with "Re: " if necessary -ORIGINAL_SUBJECT=$(echo "$EMAIL" | mhdr -h subject -) - -# Construct the reply subject -REPLY_SUBJECT="Re: $ORIGINAL_SUBJECT" - -# Extract the To address -TO_ADDRESS=$(echo "$EMAIL" | maddr -a -h to -) - -# Generate the fortune output -if [[ "$TO_ADDRESS" =~ "fortune+cowsay" ]]; then - FORTUNE_OUTPUT=$(/usr/games/fortune | /usr/games/cowsay) -else - FORTUNE_OUTPUT=$(/usr/games/fortune) -fi - -# Send a reply with the proper headers -( - echo "Subject: $REPLY_SUBJECT" - echo "From: Fortunes by Python Discord " - echo "To: $SENDER" - echo "In-Reply-To: $MESSAGE_ID" - echo "References: $MESSAGE_ID" - echo - echo "Here's your fortune:" - echo - echo "$FORTUNE_OUTPUT" -) | /usr/sbin/sendmail -t diff --git a/ansible/roles/postfix/tasks/main.yml b/ansible/roles/postfix/tasks/main.yml index 7536961..c71f800 100644 --- a/ansible/roles/postfix/tasks/main.yml +++ b/ansible/roles/postfix/tasks/main.yml @@ -89,14 +89,26 @@ notify: - Restart postfix -- name: Copy Postfix scripts directory - copy: - src: services/ - dest: /etc/postfix/service-scripts/ - directory_mode: "0755" +- name: Create Postfix service directory + file: + path: "/etc/postfix/service-scripts" + owner: root + group: servicemailer + mode: "0755" + state: directory + tags: + - role::postfix + +- name: Template Postfix automated scripts + template: + src: "{{ item }}" + dest: "/etc/postfix/service-scripts/{{ item | basename | splitext | first }}" mode: "0754" owner: root group: servicemailer + loop_control: + label: "{{ item | basename | splitext | first }}" + with_fileglob: "../templates/services/*" tags: - role::postfix diff --git a/ansible/roles/postfix/templates/services/fortune-reply.sh.j2 b/ansible/roles/postfix/templates/services/fortune-reply.sh.j2 new file mode 100644 index 0000000..97f2ce8 --- /dev/null +++ b/ansible/roles/postfix/templates/services/fortune-reply.sh.j2 @@ -0,0 +1,39 @@ +#!/bin/bash + +# Read the entire email into a variable +EMAIL=$(cat) + +# Extract the sender's email address +SENDER=$(echo "$EMAIL" | maddr -a -h from -) + +# Extract the Message-ID of the original email +MESSAGE_ID=$(echo "$EMAIL" | mhdr -h message-id -) + +# Extract the original Subject and prefix it with "Re: " if necessary +ORIGINAL_SUBJECT=$(echo "$EMAIL" | mhdr -h subject -) + +# Construct the reply subject +REPLY_SUBJECT="Re: $ORIGINAL_SUBJECT" + +# Extract the To address +TO_ADDRESS=$(echo "$EMAIL" | maddr -a -h to -) + +# Generate the fortune output +if [[ "$TO_ADDRESS" =~ "fortune+cowsay" ]]; then + FORTUNE_OUTPUT=$(/usr/games/fortune | /usr/games/cowsay) +else + FORTUNE_OUTPUT=$(/usr/games/fortune) +fi + +# Send a reply with the proper headers +( + echo "Subject: $REPLY_SUBJECT" + echo "From: Fortunes by Python Discord " + echo "To: $SENDER" + echo "In-Reply-To: $MESSAGE_ID" + echo "References: $MESSAGE_ID" + echo + echo "Here's your fortune:" + echo + echo "$FORTUNE_OUTPUT" +) | /usr/sbin/sendmail -t -- cgit v1.2.3