瀏覽代碼

script to add email addresses to Evolution mail list

Daniel Sheffield 1 年之前
當前提交
e5a75407b1
共有 1 個文件被更改,包括 93 次插入0 次删除
  1. 93 0
      update-evolution-mail-list.sh

+ 93 - 0
update-evolution-mail-list.sh

@@ -0,0 +1,93 @@
+#!/bin/bash
+# Depends: sqlite3
+set -euo pipefail
+mailing_list_name=$1
+form_dir=$2
+excluded=$3
+
+# Path to the Evolution contacts database
+db_file=~/.local/share/evolution/addressbook/system/contacts.db
+
+# Backup the database
+cp "$db_file" "$db_file.backup"
+
+# Define an array of contacts (name and email pairs)
+#
+# Reads files in $form_dir with structure like
+#
+#  Name: Som E. Name
+#  Email: address@example.com
+mapfile -t contacts < <(find ${form_dir} -type f -name "*.txt" -exec awk -F ":" '
+/Name:/ { name = $2 } /Email:/ { email = $2 } END {
+  printf "%s <%s>\n", substr(name,2), substr(email,2)
+}
+' {} \;)
+
+
+# Loop through the contacts array and add each contact to the mailing list
+for contact_email in "${contacts[@]}"; do
+
+    # check exclusions
+    grep -Fx "$contact_email" >/dev/null < "$excluded" && echo "Skipped (excluded): '$contact_email'" && continue
+
+
+    # Use sqlite3 to add the contact to the mailing list
+    #set -x
+    exists=$(sqlite3 "$db_file" <<EOF
+.parameter init
+.parameter set @email "'$contact_email'"
+SELECT EXISTS (SELECT * FROM folder_id_email_list WHERE value = @email);
+EOF
+)
+    [ "$exists" == "1" ] && echo "Skipped (exists): '$contact_email'" && continue
+    vcard=$(sqlite3 "$db_file" <<EOF
+.parameter init
+.parameter set @list "'${mailing_list_name,,}'"
+SELECT vcard FROM folder_id WHERE file_as = @list;
+EOF
+)
+
+    vcard=$(awk \
+        -v new_email="EMAIL;X-EVOLUTION-DEST-HTML-MAIL=FALSE;X-EVOLUTION-PARENT-UID=0:$contact_email" \
+        -v max_width=75 '
+BEGIN { FS=":"; OFS=":"; found=0 }
+/BEGIN:VCARD/ { print $0; next }
+/EMAIL/ {
+  wrapped_email = new_email
+  while (length(wrapped_email) > max_width) {
+    print substr(wrapped_email, 1, max_width)
+    wrapped_email = " " substr(wrapped_email, max_width + 1)
+  }
+  print wrapped_email
+  print $0
+  found=1
+  next
+}
+{ buffer = buffer $0 ORS } END {
+  if (found == 0) {
+    wrapped_email = new_email
+    while (length(wrapped_email) > max_width) {
+      print substr(wrapped_email, 1, max_width)
+      wrapped_email = " " substr(wrapped_email, max_width + 1)
+    }
+    print wrapped_email
+  }
+  print buffer
+}
+' <<< "$vcard")
+    vcard=$(awk '{printf "%s\\n", $0}' <<< "$vcard")
+    #read -p "$vcard"
+
+    sqlite3 "$db_file" <<EOF &&  echo "Added: '$contact_email'"
+.parameter init
+.parameter set @list "'${mailing_list_name,,}'"
+.parameter set @vcard "'${vcard}'"
+.parameter set @email "'$contact_email'"
+UPDATE folder_id SET vcard = @vcard WHERE file_as = @list;
+INSERT INTO folder_id_email_list (uid, value)
+  VALUES ((SELECT uid FROM folder_id WHERE file_as = @list), @email);
+EOF
+done
+
+echo "Restart evolution (File -> Quit) to load the changes"
+