Browse Source

iterate mail list in python and add script to delete entries

Pi 1 year ago
parent
commit
a22330d1a3
5 changed files with 46 additions and 28 deletions
  1. 2 0
      cropswap-register-new.sh
  2. 8 5
      cropswap-resendmail.sh
  3. 7 20
      cropswap-sendmail.sh
  4. 22 0
      gdbm-delete.py
  5. 7 3
      gdbm-iter-subscribed.py

+ 2 - 0
cropswap-register-new.sh

@@ -42,6 +42,8 @@ for group in cropswap cropswap-members; do
 ' {} \;)
 
 	for email in "${!SIGNUP[@]}"; do
+		set -x
 		"${SCRIPTDIR}/cropswap-register.sh" "${group}@shandan.one" "${SIGNUP["$email"]}" "$email" NONE
+		set +x
 	done
 done

+ 8 - 5
cropswap-resendmail.sh

@@ -3,12 +3,15 @@ set -aeuo pipefail
 MAILGROUP="$1" # mailgroup e-mail
 FROM="$2"      # from full name (alias)
 NOFORWARD="$3" # from e-mail
-TO="$4"        # to e-mail
-SUBJECT="$5"   # subject flag [SUBJECT]
-MLIST="$6"     # members list
-SLIST="$7"     # subscription list
-REPLYTO="${8:-}" # from e-mail (non-member)
+TOALIAS="$4"   # to e-mail
+TO="$5"        # to e-mail
+SUBJECT="$6"   # subject flag [SUBJECT]
+MLIST="$7"     # members list
+SLIST="$8"     # subscription list
+REPLYTO="${9:-}" # from e-mail (non-member)
 
+UNHANDLED=5
+MEMBERGROUP=cropswap-members@shandan.one
 DEBUGMAIL=$(readlink -f "${BASH_SOURCE[0]%/*}/cropswap-debug.sh")
 DEBUG="${DEBUG:=0}"
 TEST="${TEST:=0}"

+ 7 - 20
cropswap-sendmail.sh

@@ -53,31 +53,17 @@ mk_list_headers(){
 tee >(mk_headers > mail) | "${MIME_INJECT}" "${REPLYTO:-$MAILGROUP}" | reformime -s1 -e > body
 cat body >> mail
 
-declare -A SUBSCRIBED
-declare -a actions
-update(){
-	alias="${actions[$1-1]}"
-	email="${2}"
-	SUBSCRIBED["$email"]="$alias"
-}
-mapfile -t -c 2 -C update actions <<EOF
-$("${ITER_SUBS}" "${MLIST}" "${SLIST}")
-EOF
+declare -a SUBSCRIBED
+mapfile -t SUBSCRIBED < <("${ITER_SUBS}" "${MLIST}" "${SLIST}" "${NOFORWARD}" "${TO}")
 
-for email in "${!SUBSCRIBED[@]}"; do
-	if [ "${email,,}" == "${NOFORWARD,,}" ]; then
-		continue
-	fi
-	if [ "$TO" != "" ] && [ "$email" != "$TO" ]; then
-		continue
-	fi
+for addr in "${SUBSCRIBED[@]}"; do
 	echo "Forwarding mail: "
 	echo "From: ${FROM} <${MAILGROUP}>"
-	echo "To: ${SUBSCRIBED["$email"]} <${email}>"
-	"$SENDMAIL" -F "${FROM}" -f "${MAILGROUP}" "${SUBSCRIBED["$email"]} <${email}>" < mail
+	echo "To: $addr"
+	"$SENDMAIL" -F "${FROM}" -f "${MAILGROUP}" "$addr" < mail
 	DELIVERED=true
 done
-if [ "${MAILGROUP}" == "cropswap-members@shandan.one" ]
+if [ "${MAILGROUP}" == "${MEMBERGROUP:-x}" ]
 then
 	reformail \
 		-I "List-Id:" \
@@ -85,6 +71,7 @@ then
 		-I "List-Subscribe:" \
 		-I "List-Post:" \
 		-I "List-Unsubscribe-Post:" \
+		-I "To: ${TOALIAS:-} <${MAILGROUP}>" \
 	< mail | "$SENDMAIL" -F "${FROM}" -f "${MAILGROUP}" "${CROPSWAPARCHIVE}"
 fi
 

+ 22 - 0
gdbm-delete.py

@@ -0,0 +1,22 @@
+#!/usr/bin/env python3
+# depends (debian) python3-gdbm
+import dbm
+import sys
+
+mailgroup = sys.argv[1]
+keys = sys.argv[2:]
+
+for f in ('alist', 'mlist', 'slist'):
+    with dbm.open(f'/var/mail/maildrop/{mailgroup}/{f}', 'w') as db:
+        for k in keys:
+            v = db.get(k.lower(), None)
+            if v is None:
+                sys.stdout.buffer.write(f'Key not found: {k}\n'.encode('utf-8'))
+                continue
+
+            sys.stdout.buffer.write(f'Deleting key: {k}\n'.encode('utf-8'))
+            del db[k.lower()]
+            if f == 'mlist':
+                sys.stdout.buffer.write(f'Deleting reverse map: {v}\n'.encode('utf-8'))
+                del db[v.lower()]
+

+ 7 - 3
gdbm-iter-subscribed.py

@@ -4,6 +4,7 @@ import dbm
 import sys
 #mlist, alist, slist = sys.argv[1:4]
 mlist, slist = sys.argv[1:3]
+noforward, to = sys.argv[3:5]
 
 def gdbm_iter_keys(dbpath):
     with dbm.open(dbpath, 'r') as db:
@@ -27,8 +28,11 @@ subscribed = set(map(lambda x: x[0], filter(
 )))
 
 for email, alias in {
-    k: v for k,v in members if k in subscribed
+    k.decode('utf-8'): v.decode('utf-8') for k, v in members if k in subscribed
 }.items():
-    print(alias.decode('utf-8'))
-    print(email.decode('utf-8'))
+    if email.lower() == noforward.lower():
+        continue
+    if to and to.lower() != email.lower():
+        continue
+    sys.stdout.buffer.write(f"{alias} <{email}>\n".encode('utf-8'))