From 15eeddc2358e9bd337dfbf5e8735b59987d3f4c4 Mon Sep 17 00:00:00 2001
From: luukvbaal <31730729+luukvbaal@users.noreply.github.com>
Date: Mon, 13 May 2019 12:46:03 +0200
Subject: [PATCH 1/9] don't allow to be run concurrently with itself
This allows mailsync to be called like this `bindsym $mod+e exec --no-startup-id mailsync; exec $term -e neomutt && pkill -RTMIN+12 i3blocks` such that it refreshes your mail when you open mutt without it needlessly syncing if a sync is already running.
---
bin/mailsync | 2 ++
1 file changed, 2 insertions(+)
diff --git a/bin/mailsync b/bin/mailsync
index 43429f5..8da6629 100755
--- a/bin/mailsync
+++ b/bin/mailsync
@@ -3,6 +3,8 @@
# Run only if user logged in (prevent cron errors)
pgrep -cu "$USER" >/dev/null || exit
+# Run only if not already running in other instance
+[ $(pgrep -xf "sh /usr/bin/mailsync" | wc -l) -eq 2 ] || exit
# Checks for internet connection and set notification script.
ping -q -c 1 1.1.1.1 > /dev/null || exit
From 7d3658238b75790cebcf744c734079dd2ec4b11c Mon Sep 17 00:00:00 2001
From: luukvbaal <31730729+luukvbaal@users.noreply.github.com>
Date: Thu, 30 May 2019 16:23:41 +0200
Subject: [PATCH 2/9] parallelize multiple accounts and mail preview
---
bin/mailsync | 55 ++++++++++++++++++++++++++++++++++++----------------
1 file changed, 38 insertions(+), 17 deletions(-)
diff --git a/bin/mailsync b/bin/mailsync
index 8da6629..b33de11 100755
--- a/bin/mailsync
+++ b/bin/mailsync
@@ -2,9 +2,9 @@
# Sync mail and give notification if there is new mail.
# Run only if user logged in (prevent cron errors)
-pgrep -cu "$USER" >/dev/null || exit
+pgrep -u "$USER" >/dev/null || exit
# Run only if not already running in other instance
-[ $(pgrep -xf "sh /usr/bin/mailsync" | wc -l) -eq 2 ] || exit
+[ "$(pgrep -xf "sh /usr/bin/mailsync.*" | wc -l)" -eq 2 ] || exit
# Checks for internet connection and set notification script.
ping -q -c 1 1.1.1.1 > /dev/null || exit
@@ -14,31 +14,52 @@ export DISPLAY=:0.0
# Settings are different for MacOS (Darwin) systems.
if [ "$(uname)" = "Darwin" ]; then
- notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2 ;}
+ notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2 ;}
else
- notify() { notify-send "mutt-wizard" "📬 $2 new mail(s) in \`$1\` account." ;}
+ notify() { notify-send "mutt-wizard" "📬 $2 new mail(s) in \`$1\` account." ;}
fi
-echo " 🔃" > /tmp/imapsyncicon_$USER
-pkill -RTMIN+12 i3blocks
+# Check account for new mail. Notify if there is new content.
+syncandnotify() {
+ acc="$(echo "$account" | sed "s/.*\///")"
+ mbsync "$acc"
+ new=$(find "$HOME/.local/share/mail/$acc/INBOX/new/" "$HOME/.local/share/mail/$acc/Inbox/new/" "$HOME/.local/share/mail/$acc/inbox/new/" -type f -newer "$HOME/.config/mutt/.mailsynclastrun" 2> /dev/null)
+ newcount=$(find "$HOME/.local/share/mail/$acc/INBOX/new/" "$HOME/.local/share/mail/$acc/Inbox/new/" "$HOME/.local/share/mail/$acc/inbox/new/" -type f -newer "$HOME/.config/mutt/.mailsynclastrun" 2> /dev/null | wc -l)
+ if [ "$newcount" -gt "0" ]; then
+ notify "$acc" "$newcount" &
+ for file in $new; do
+ # Extract subject and sender from mail (room for improvement).
+ subject=$(grep "^Subject: " "$file" | sed 's/Subject: //' | head -n4)
+ from=$(grep "^From: " "$file" | awk '{ $1=""; $NF=""; print $0 }' | tr -d "\"\'\<\>" | sed 's/^ \(.*\) $/\1/')
+ substring="=?"
+ # Some html emails contain weird Subject and/or Sender formatting, in which case the html tag content is used (room for improvement).
+ [ "${subject#*$substring}" != "$subject" ] && subject=$(tr '\n' ' ' < "$file" | sed 's/.*
\(.*\)<\/title>.*/\1/')
+ [ "${from#*$substring}" != "$from" ] && from=$(grep ^"From: " "$file" | sed 's/.*<\(.*\)>.*/\1/')
+ notify-send "📧$from:" "$subject" &
+ done
+ fi
+}
-# Run mbsync. You can feed this script different settings.
-if [ $# -eq 0 ]; then
- mbsync -a
+# Run
+if [ "$#" -eq "0" ]; then
+ accounts="$(ls "$HOME/.local/share/mail")"
else
- mbsync "$@"
+ accounts=$*
fi
-rm -f /tmp/imapsyncicon_$USER
-pkill -RTMIN+12 i3blocks
+echo " 🔃" > /tmp/imapsyncicon_"$USER"
+pkill -RTMIN+12 i3blocks >/dev/null 2>&1
-# Check all accounts/mailboxes for new mail. Notify if there is new content.
-for account in "$HOME/.local/share/mail/"*
+# Parallelize multiple accounts
+for account in $accounts
do
- acc="$(echo "$account" | sed "s/.*\///")"
- newcount=$(find "$HOME/.local/share/mail/$acc/INBOX/new/" "$HOME/.local/share/mail/$acc/Inbox/new/" "$HOME/.local/share/mail/$acc/inbox/new/" -type f -newer "$HOME/.config/mutt/.mailsynclastrun" 2> /dev/null | wc -l)
- [ "$newcount" -gt "0" ] && notify "$acc" "$newcount" &
+ syncandnotify &
done
+
+wait
+rm -f /tmp/imapsyncicon_"$USER"
+pkill -RTMIN+12 i3blocks >/dev/null 2>&1
+
notmuch new 2>/dev/null
#Create a touch file that indicates the time of the last run of mailsync
From 5d72b5da981f8c71b7fe47997b7fd309e6e861cd Mon Sep 17 00:00:00 2001
From: luukvbaal <31730729+luukvbaal@users.noreply.github.com>
Date: Thu, 30 May 2019 16:46:12 +0200
Subject: [PATCH 3/9] unnecessary sed command
---
bin/mailsync | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/bin/mailsync b/bin/mailsync
index b33de11..387ce65 100755
--- a/bin/mailsync
+++ b/bin/mailsync
@@ -24,7 +24,7 @@ syncandnotify() {
acc="$(echo "$account" | sed "s/.*\///")"
mbsync "$acc"
new=$(find "$HOME/.local/share/mail/$acc/INBOX/new/" "$HOME/.local/share/mail/$acc/Inbox/new/" "$HOME/.local/share/mail/$acc/inbox/new/" -type f -newer "$HOME/.config/mutt/.mailsynclastrun" 2> /dev/null)
- newcount=$(find "$HOME/.local/share/mail/$acc/INBOX/new/" "$HOME/.local/share/mail/$acc/Inbox/new/" "$HOME/.local/share/mail/$acc/inbox/new/" -type f -newer "$HOME/.config/mutt/.mailsynclastrun" 2> /dev/null | wc -l)
+ newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
if [ "$newcount" -gt "0" ]; then
notify "$acc" "$newcount" &
for file in $new; do
From de2be5439dab0bb357405b1d0e87ea8138dd630e Mon Sep 17 00:00:00 2001
From: luukvbaal <31730729+luukvbaal@users.noreply.github.com>
Date: Fri, 31 May 2019 00:02:48 +0200
Subject: [PATCH 4/9] perl decoding
---
bin/mailsync | 10 +++-------
1 file changed, 3 insertions(+), 7 deletions(-)
diff --git a/bin/mailsync b/bin/mailsync
index 1ef2155..44c9756 100755
--- a/bin/mailsync
+++ b/bin/mailsync
@@ -28,13 +28,9 @@ syncandnotify() {
if [ "$newcount" -gt "0" ]; then
notify "$acc" "$newcount" &
for file in $new; do
- # Extract subject and sender from mail (room for improvement).
- subject=$(grep "^Subject: " "$file" | sed 's/Subject: //' | head -n4)
- from=$(grep "^From: " "$file" | awk '{ $1=""; $NF=""; print $0 }' | tr -d "\"\'\<\>" | sed 's/^ \(.*\) $/\1/')
- substring="=?"
- # Some html emails contain weird Subject and/or Sender formatting, in which case the html tag content is used (room for improvement).
- [ "${subject#*$substring}" != "$subject" ] && subject=$(tr '\n' ' ' < "$file" | sed 's/.*\(.*\)<\/title>.*/\1/')
- [ "${from#*$substring}" != "$from" ] && from=$(grep ^"From: " "$file" | sed 's/.*<\(.*\)>.*/\1/')
+ # Extract subject and sender from mail.
+ from=$(grep "^From: " "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | awk '{ $1=""; $NF=""; print $0 }' | tr -d "\"\'\<\>" | sed 's/^ \(.*\) $/\1/')
+ subject=$(grep "^Subject: " "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | sed 's/Subject: //')
notify-send "📧$from:" "$subject" &
done
fi
From e50d51d3e56c7531cf321b7a0ed7276ad14bb90d Mon Sep 17 00:00:00 2001
From: luukvbaal <31730729+luukvbaal@users.noreply.github.com>
Date: Sat, 1 Jun 2019 16:16:58 +0200
Subject: [PATCH 5/9] fix multiline subjects and single word senders
---
bin/mailsync | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/bin/mailsync b/bin/mailsync
index 44c9756..199ede2 100755
--- a/bin/mailsync
+++ b/bin/mailsync
@@ -29,8 +29,8 @@ syncandnotify() {
notify "$acc" "$newcount" &
for file in $new; do
# Extract subject and sender from mail.
- from=$(grep "^From: " "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | awk '{ $1=""; $NF=""; print $0 }' | tr -d "\"\'\<\>" | sed 's/^ \(.*\) $/\1/')
- subject=$(grep "^Subject: " "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | sed 's/Subject: //')
+ from=$(grep "^From: " "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | awk '{ $1=""; if ($NF>=3)$NF=""; print $0 }' | tr -d "\"\'\<\>" | sed 's/^ \(.*\) $/\1/')
+ subject=$(sed -n '/^Subject: /,/^.*:/p' "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | sed 's/^Subject: //' | head -n-1)
notify-send "📧$from:" "$subject" &
done
fi
From 7955090f9f5ffc8e95070924a56f492ec6bbd0d7 Mon Sep 17 00:00:00 2001
From: luukvbaal <31730729+luukvbaal@users.noreply.github.com>
Date: Sat, 1 Jun 2019 20:34:09 +0200
Subject: [PATCH 6/9] fix forwarded emails and posix whitespace regex
---
bin/mailsync | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/bin/mailsync b/bin/mailsync
index 199ede2..d5be1de 100755
--- a/bin/mailsync
+++ b/bin/mailsync
@@ -28,9 +28,9 @@ syncandnotify() {
if [ "$newcount" -gt "0" ]; then
notify "$acc" "$newcount" &
for file in $new; do
- # Extract subject and sender from mail.
- from=$(grep "^From: " "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | awk '{ $1=""; if ($NF>=3)$NF=""; print $0 }' | tr -d "\"\'\<\>" | sed 's/^ \(.*\) $/\1/')
- subject=$(sed -n '/^Subject: /,/^.*:/p' "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | sed 's/^Subject: //' | head -n-1)
+ # Extract subject and sender from mail.
+ from=$(awk '/^From: / && ++n ==1,/^\<.*\>:/' "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | awk '{ $1=""; if (NF>=3)$NF=""; print $0 }' | sed 's/^[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//')
+ subject=$(awk '/^Subject: / && ++n == 1,/^\<.*\>: /' "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | sed 's/^Subject: //' | sed 's/^{[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//')
notify-send "📧$from:" "$subject" &
done
fi
From effa100e044de157d1db30978b8fab101e627df8 Mon Sep 17 00:00:00 2001
From: luukvbaal <31730729+luukvbaal@users.noreply.github.com>
Date: Sat, 1 Jun 2019 21:56:55 +0200
Subject: [PATCH 7/9] fix multiline subject header regression
---
bin/mailsync | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/bin/mailsync b/bin/mailsync
index d5be1de..21ebc64 100755
--- a/bin/mailsync
+++ b/bin/mailsync
@@ -30,7 +30,7 @@ syncandnotify() {
for file in $new; do
# Extract subject and sender from mail.
from=$(awk '/^From: / && ++n ==1,/^\<.*\>:/' "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | awk '{ $1=""; if (NF>=3)$NF=""; print $0 }' | sed 's/^[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//')
- subject=$(awk '/^Subject: / && ++n == 1,/^\<.*\>: /' "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | sed 's/^Subject: //' | sed 's/^{[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//')
+ subject=$(awk '/^Subject: / && ++n == 1,/^\<.*\>: / && ++i == 2' "$file" | head -n-1 | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | sed 's/^Subject: //' | sed 's/^{[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//' | tr -d '\n')
notify-send "📧$from:" "$subject" &
done
fi
From e408f4b290003a2a839d7b1c2917dd6d7345595f Mon Sep 17 00:00:00 2001
From: Luke Smith
Date: Sat, 1 Jun 2019 17:58:35 -0400
Subject: [PATCH 8/9] prep fix
---
bin/mailsync | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/bin/mailsync b/bin/mailsync
index 21ebc64..8b98d42 100755
--- a/bin/mailsync
+++ b/bin/mailsync
@@ -4,7 +4,7 @@
# Run only if user logged in (prevent cron errors)
pgrep -u "$USER" >/dev/null || exit
# Run only if not already running in other instance
-[ "$(pgrep -xf "sh /usr/bin/mailsync.*" | wc -l)" -eq 2 ] || exit
+pgrep -x mailsync >/dev/null && exit
# Checks for internet connection and set notification script.
ping -q -c 1 1.1.1.1 > /dev/null || exit
@@ -28,7 +28,7 @@ syncandnotify() {
if [ "$newcount" -gt "0" ]; then
notify "$acc" "$newcount" &
for file in $new; do
- # Extract subject and sender from mail.
+ # Extract subject and sender from mail.
from=$(awk '/^From: / && ++n ==1,/^\<.*\>:/' "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | awk '{ $1=""; if (NF>=3)$NF=""; print $0 }' | sed 's/^[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//')
subject=$(awk '/^Subject: / && ++n == 1,/^\<.*\>: / && ++i == 2' "$file" | head -n-1 | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | sed 's/^Subject: //' | sed 's/^{[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//' | tr -d '\n')
notify-send "📧$from:" "$subject" &
From dd73533565e7fd2cdc16ccfaf63c6605940ac6a7 Mon Sep 17 00:00:00 2001
From: Luke Smith
Date: Sat, 1 Jun 2019 17:59:47 -0400
Subject: [PATCH 9/9] only sync active mbsync accounts
---
bin/mailsync | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/bin/mailsync b/bin/mailsync
index 8b98d42..b59c8e3 100755
--- a/bin/mailsync
+++ b/bin/mailsync
@@ -38,7 +38,7 @@ syncandnotify() {
# Sync accounts passed as argument or all.
if [ "$#" -eq "0" ]; then
- accounts="$(ls "$HOME/.local/share/mail")"
+ accounts="$(awk '/^Channel/ {print $2}' "$HOME/.mbsyncrc")"
else
accounts=$*
fi