aboutsummaryrefslogtreecommitdiff
path: root/scripts/dot-local/bin/mailsync
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/dot-local/bin/mailsync')
-rwxr-xr-xscripts/dot-local/bin/mailsync54
1 files changed, 54 insertions, 0 deletions
diff --git a/scripts/dot-local/bin/mailsync b/scripts/dot-local/bin/mailsync
new file mode 100755
index 0000000..8029444
--- /dev/null
+++ b/scripts/dot-local/bin/mailsync
@@ -0,0 +1,54 @@
+#!/usr/bin/env sh
+
+# Run only if not already running in other instance
+pgrep mbsync >/dev/null && {
+ echo "mbsync is already running."
+ exit
+}
+
+notify()
+{
+ notify-send --app-name="Mail" -- "$1" "$2"
+}
+
+# export GPG_TTY="$(tty)"
+# export DISPLAY=":0"
+# export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/"$(id -u)"/bus
+export MAILDIR="$XDG_DATA_HOME/mail"
+lastrun="$XDG_CACHE_HOME/.mailsynclastrun"
+# export PYTHONWARNINGS="ignore::UserWarning"
+
+# afew --move-mails --all
+mbsync -q mit
+
+new=$(find "$MAILDIR/mit/INBOX/new/" "$MAILDIR/mit/INBOX/cur/" -type f -newer "$lastrun" 2>/dev/null)
+newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
+
+case 1 in
+ $((newcount > 5)))
+ echo "$newcount new mail for mit."
+ notify "New Mail!" "$newcount new mail(s)."
+ ;;
+ $((newcount > 0)))
+ echo "$newcount new mail for mit."
+ for file in $new; do
+ # Extract and decode subject and sender from mail.
+ subject=$(awk '/^Subject: / && ++n == 1,/^.*: / && ++i == 2' "$file" | head -n-1 |
+ perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' |
+ sed 's/^Subject: //' | tr -d '\n\t')
+ from="$(sed -n "/^From:/ s|From: *|| p" "$file" |
+ perl -CS -MEncode -ne 'print decode("MIME-Header", $_)')"
+ from="${from% *}"
+ from="${from%\"}"
+ from="${from#\"}"
+ notify "$from:" "$subject"
+ done
+ ;;
+ *) echo "No new mail for mit." ;;
+esac
+
+# notmuch new
+# afew --tag --new
+
+#Create a touch file that indicates the time of the last run of mailsync
+touch "$lastrun"