summaryrefslogtreecommitdiff
path: root/bin/scripts
diff options
context:
space:
mode:
authorA Farzat <a@farzat.xyz>2024-10-24 20:40:37 +0900
committerA Farzat <a@farzat.xyz>2024-11-04 20:54:30 +0900
commitd7a3bcd650d8dee4d31ba458ecf84a5bd1d16596 (patch)
tree68848622f120ee27d0b9b73d3af3aed58b5e78d8 /bin/scripts
parentcc5c235277d6227006238572f63a9d3e483d8a34 (diff)
downloaddotfiles-d7a3bcd650d8dee4d31ba458ecf84a5bd1d16596.tar.gz
dotfiles-d7a3bcd650d8dee4d31ba458ecf84a5bd1d16596.zip
Add mailsync script
Used to sync mails in parallel while also providing notifications. Based on the helper script provided with mutt-wizard by Luke Smith. https://github.com/LukeSmithxyz/mutt-wizard/blob/master/bin/mailsync Might try to add a man page later based on the one provided as well. https://github.com/LukeSmithxyz/mutt-wizard/blob/master/mailsync.1
Diffstat (limited to 'bin/scripts')
-rwxr-xr-xbin/scripts/mailsync45
1 files changed, 45 insertions, 0 deletions
diff --git a/bin/scripts/mailsync b/bin/scripts/mailsync
new file mode 100755
index 0000000..edd0da1
--- /dev/null
+++ b/bin/scripts/mailsync
@@ -0,0 +1,45 @@
+#!/usr/bin/env sh
+
+# - Syncs mail for all accounts, or a single account given as an argument.
+# - Displays a notification showing the number of new mails.
+# - Displays a notification for each new mail with its subject displayed.
+# - Runs notmuch to index new mail.
+
+MAILDIR="${MAILDIR:-${XDG_DATA_HOME:-$HOME/.local/share}/mail}"
+MBSYNCRC="${MBSYNCRC:-${XDG_CONFIG_HOME:-$HOME/.config}/isyncrc}"
+MS_last_run="${XDG_STATE_HOME:-$HOME/.local/state}/.mailsynclastrun"
+
+# Run only if not already running in other instance
+pgrep mbsync >/dev/null && { echo "mbsync is already running." >&2; exit ;}
+
+# Check account for new mail. Notify if there is new content.
+syncandnotify() {
+ acc="$(printf %s "$account" | sed "s/.*\///")"
+ if [ -z "$opts" ]; then mbsync -c "$MBSYNCRC" "$acc"; else mbsync -c "$MBSYNCRC" "$opts" "$acc"; fi
+ newcount=$(find "$MAILDIR/$acc/"[Ii][Nn][Bb][Oo][Xx]/new/ -type f -newer "$MS_last_run" 2> /dev/null | sed '/^\s*$/d' | wc -l)
+ notify-send --app-name="mailsync" "Mailsync" "📬 $newcount new mail(s) in \`$acc\` account."
+}
+
+# Sync accounts passed as argument or all.
+if [ "$#" -eq "0" ]; then
+ accounts="$(awk '/^Channel/ {print $2}' "$MBSYNCRC")"
+else
+ for arg in "$@"; do
+ [ "${arg%${arg#?}}" = '-' ] && opts="${opts:+${opts} }${arg}" && shift 1
+ done
+ accounts=$*
+fi
+
+# Parallelize multiple accounts
+for account in $accounts; do
+ syncandnotify &
+done
+
+wait
+
+notmuch new 2>/dev/null
+
+#Create a touch file that indicates the time of the last run of mailsync
+touch "$MS_last_run"
+# Update the corresponding block in the statusbar.
+pkill -RTMIN+12 "${STATUSBAR:-dwmblocks}"