From b269d5ba89c8c1232153f5ae3251ab597eb13948 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Fri, 19 Apr 2019 14:53:25 -0400 Subject: [PATCH] Automatically add addresses from email to your aliases file. Automatically refresh aliases on mail, bounce or forward. --- fleacollar.sh | 43 +++++++++++++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/fleacollar.sh b/fleacollar.sh index 5378844..c534e87 100755 --- a/fleacollar.sh +++ b/fleacollar.sh @@ -83,16 +83,36 @@ yesno() { initialize_directory() { - if ! [ -d "$muttHome" ]; then - mkdir -p "${muttHome}/cache/bodies" - fi + mkdir -p "${muttHome}/cache/bodies" + mkdir -p "${muttHome}/scripts" if ! [ -f "$muttHome/aliases" ]; then touch "$muttHome/aliases" fi + # Create scripts + if ! [ -f "$muttHome/scripts/add_address.sh" ]; then + cat << EOF > "$muttHome/scripts/add_address.sh" +#!/bin/bash + +message="\$(cat)" + +newAlias=\$(echo "\${message}" | grep "^From: " | sed 's/[\,\"\']//g' | awk '{\$1=""; if (NF == 3) {print "alias" \$0;} else if (NF == 2) {print "alias" \$0 \$0;} else if (NF > 3) {print "alias", tolower(\$2)"-"tolower(\$(NF-1)) \$0;}}') +emailAddress="<\${newAlias##*<}" + +if ! grep -Fq "\$emailAddress" "$muttHome/aliases" ; then + echo "\$newAlias" >> "$muttHome/aliases" + sort -u "$muttHome/aliases" -o "$muttHome/aliases" +fi + +echo "\${message}" + +EOF + fi + # End of add_address script if ! [ -f "$muttHome/mailcap" ]; then # Find desired browser local x=0 for i in\ + $BROWSER\ chromium\ elinks\ epiphany\ @@ -157,13 +177,15 @@ initialize_directory() keyName="$(gpg --list-secret-keys --keyid-format short | grep -B1 ^uid | head -n1 | rev | cut -c -8 | rev)" echo "set pgp_sign_as=$keyName" >> "$muttHome/gpg.rc" fi - # Create macro file - if ! [ -f "$muttHome/macros" ]; then - echo "macro index 'c' '?^K='" > "$muttHome/macros" - echo "bind index \"^\" imap-fetch-mail\"" >> "$muttHome/macros" - echo "macro pager \cb 'urlview' 'Follow links with urlview'" >> "$muttHome/macros" - echo "macro attach 'B' \"cat >~/.cache/mutt/mail.html && $BROWSER ~/.cache/\"" >> "$muttHome/macros" - fi + # Create or update macro file + echo "macro index 'c' '?^K='" >> "$muttHome/macros" + echo "bind index \"^\" imap-fetch-mail\"" >> "$muttHome/macros" + echo "macro pager \cb 'urlview' 'Follow links with urlview'" >> "$muttHome/macros" + echo "macro attach 'B' \"cat >~/.cache/mutt/mail.html && $BROWSER ~/.cache/\"" >> "$muttHome/macros" + echo "macro index,pager b 'source $muttHome/aliases'" >> "$muttHome/macros" + echo "macro index,pager f 'source $muttHome/aliases'" >> "$muttHome/macros" + echo "macro index,pager m 'source $muttHome/aliases'" >> "$muttHome/macros" + # Create basic muttrc if ! [ -f "$muttHome/muttrc" ]; then # Find desired editor @@ -204,6 +226,7 @@ initialize_directory() echo "set pager_stop = 'yes'" >> "$muttHome/muttrc" echo "set sort=threads" >> "$muttHome/muttrc" echo "set beep_new=yes" >> "$muttHome/muttrc" + echo "set display_filter = '$muttHome/scripts/add_address.sh'" >> "$muttHome/muttrc" echo "set print=yes" >> "$muttHome/muttrc" echo "set imap_check_subscribed" >> "$muttHome/muttrc" echo "set sort_alias=alias" >> "$muttHome/muttrc"