Browse Source

fix bug handling alias collisions and improve sendmail script

Pi 1 year ago
parent
commit
6ea7c03989
2 changed files with 35 additions and 30 deletions
  1. 32 27
      cropswap-sendmail.sh
  2. 3 3
      maildroprc

+ 32 - 27
cropswap-sendmail.sh

@@ -1,41 +1,46 @@
 #!/bin/bash
 set -euo pipefail
 MAIL="$(cat -)"
-echo "${MAIL}" > /home/pi/original
+MAILGROUP=cropswap@shandan.one
 
 declare -A SUBSCRIBED
 declare -a actions
 update(){
 	da="${actions[$1-1]:0:1}"
-        db="${2:0:1}"
+	db="${2:0:1}"
 	alias="${actions[$1-1]#?}"
 	email="${2#?}"
-	
-	if [ "$da" == "$db" ]; then
-		[ "$da" == "+" ] && SUBSCRIBED["$email"]="$alias" || SUBSCRIBED["$email"]=""
+	[ "$da" == "$db" ] || exit 1 # corrupt list
+	if [ "$da" == "+" ]; then
+		SUBSCRIBED["$email"]="$alias"
 	else
-		# corrupt list
-		exit 1
+		SUBSCRIBED["$email"]=""
 	fi
 }
-
-touch "${SLIST}.txt"
-mapfile -t -c 2 -C update actions < "${SLIST}.txt"
-
-(for email in "${!SUBSCRIBED[@]}"; do
-	if [ -z "${SUBSCRIBED["$email"]}" ]; then continue; fi
-	echo "+$alias"
-        echo "+$email"
-        if [ "${email,,}" == "${NOFORWARD,,}" ]; then continue; fi
-	formail -rkb \
-		-A "Reply-to: ${FROM} <cropswap@shandan.one>" \
-		-i "MIME-Version:" \
-		-i "Content-Type:" \
-	<<<"${MAIL}" \
-	>> /home/pi/reformail
-	#cat /home/pi/reformail | /usr/sbin/sendmail \
-	#	-F "${sendr}" \
-	#	-f "cropswap@shandan.one" \
-	#	-v "${addr}"
-done) > "${SLIST}.txt"
+exec 4<> "${SLIST}.txt"
+flock 4
+mapfile -t -c 2 -C update -u 4 actions
+for email in "${!SUBSCRIBED[@]}"; do
+	if [ -z "${SUBSCRIBED["$email"]}" ]; then
+		 continue
+	fi
+	echo "+${SUBSCRIBED["$email"]}"
+	echo "+$email"
+done > "${SLIST}".txt
+flock -u 4
+set -x
+for email in "${!SUBSCRIBED[@]}"; do
+	if [ -z "${SUBSCRIBED["$email"]}" ] || [ "${email,,}" == "${NOFORWARD,,}" ]; then
+		 continue
+	fi
+	echo "Forwarding mail to: ${SUBSCRIBED["$email"]} <${email}>"
+	formail -kb \
+		-X "MIME-Version:" \
+		-X "Content-Type:" \
+		-X "Subject:" \
+		-X "Date:" \
+	<<<"${MAIL}" | /usr/sbin/sendmail \
+		-F "${FROM}" \
+		-f "${MAILGROUP}" "${SUBSCRIBED["$email"]} <${email}>"
+done
 #TODO: report errors properly

+ 3 - 3
maildroprc

@@ -51,14 +51,14 @@ if ($HANDLE)
          gdbmopen($MLIST, "W")
          ALIAS=$MATCH1
          KEY=$ALIAS
-         EXISTS=gdbmfetch($ALIAS)
-         while ($EXISTS ne "" && $EXISTS ne gdbmfetch(tolower($FROM)))
+         EXISTS=gdbmfetch($KEY)
+         while ($EXISTS ne "" && $KEY ne gdbmfetch(tolower($FROM)))
          {
             SUFFIX=($SUFFIX + 1)
             KEY="$ALIAS $SUFFIX"
             EXISTS=gdbmfetch($KEY)
          }
-         if (gdbmstore(tolower($FROM), $ALIAS) == 0 && gdbmstore($ALIAS, tolower($FROM)) == 0)
+         if (gdbmstore(tolower($FROM), $KEY) == 0 && gdbmstore($KEY, tolower($FROM)) == 0)
          {
             gdbmclose
             #TODO: mailbot - You have been registered as $ALIAS