aboutsummaryrefslogtreecommitdiff
path: root/scripts/.local/bin/personal/external/mailsync
diff options
context:
space:
mode:
authordavidpkj <davidpenkow1@gmail.com>2023-03-15 14:09:28 +0100
committerdavidpkj <davidpenkow1@gmail.com>2023-03-15 14:09:28 +0100
commit08a332d0daa13e52e4c8ae9bf2dbcb6408041a34 (patch)
treeb75fbf58009572ed8edeb81452276ff6d5d66ec8 /scripts/.local/bin/personal/external/mailsync
parent2c71b057b66f817ca66b3a16eba901a9d214a3cb (diff)
Cleaned up some scripts
Diffstat (limited to 'scripts/.local/bin/personal/external/mailsync')
-rwxr-xr-xscripts/.local/bin/personal/external/mailsync89
1 files changed, 89 insertions, 0 deletions
diff --git a/scripts/.local/bin/personal/external/mailsync b/scripts/.local/bin/personal/external/mailsync
new file mode 100755
index 0000000..72cce26
--- /dev/null
+++ b/scripts/.local/bin/personal/external/mailsync
@@ -0,0 +1,89 @@
+#!/bin/sh
+
+# Stolen from https://github.com/LukeSmithxyz/mutt-wizard/blob/master/bin/mailsync
+# and modified to suit my needs
+
+# - 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.
+# - This script can be set up as a cron job for automated mail syncing.
+
+# There are many arbitrary and ugly features in this script because it is
+# inherently difficult to pass environmental variables to cronjobs and other
+# issues. It also should at least be compatible with Linux (and maybe BSD) with
+# Xorg and MacOS as well.
+
+# Run only if user logged in (prevent cron errors)
+pgrep -u "${USER:=$LOGNAME}" >/dev/null || { echo "$USER not logged in; sync will not run."; exit ;}
+# Run only if not already running in other instance
+pgrep mbsync >/dev/null && { echo "mbsync is already running."; exit ;}
+
+# First, we have to get the right variables for the mbsync file, the pass
+# archive, notmuch and the GPG home. This is done by searching common profile
+# files for variable assignments. This is ugly, but there are few options that
+# will work on the maximum number of machines.
+eval "$(grep -h -- \
+ "^\s*\(export \)\?\(MBSYNCRC\|PASSWORD_STORE_DIR\|NOTMUCH_CONFIG\|GNUPGHOME\)=" \
+ "$HOME/.profile" "$HOME/.bash_profile" "$HOME/.zprofile" "$HOME/.config/zsh/.zprofile" "$HOME/.zshenv" \
+ "$HOME/.config/zsh/.zshenv" "$HOME/.bashrc" "$HOME/.zshrc" "$HOME/.config/zsh/.zshrc" \
+ "$HOME/.pam_environment" 2>/dev/null)"
+
+export GPG_TTY="$(tty)"
+
+[ -n "$MBSYNCRC" ] && alias mbsync="mbsync -c $MBSYNCRC" || MBSYNCRC="$HOME/.mbsyncrc"
+
+lastrun="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/.mailsynclastrun"
+
+# Settings are different for MacOS (Darwin) systems.
+case "$(readlink -f /sbin/init)" in
+ *systemd*|*openrc*) export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus ;;
+esac
+
+# remember if a display server is running since `ps` doesn't always contain a display
+pgrepoutput="$(pgrep -a X\(org\|wayland\))"
+displays="$(echo "$pgrepoutput" | grep -wo "[0-9]*:[0-9]\+" | sort -u)"
+notify() { [ -n "$pgrepoutput" ] && for x in ${displays:-0:}; do
+ export DISPLAY=$x
+ notification "New mail!" "Fully synced $1" &
+done ;}
+
+# Check account for new mail. Notify if there is new content.
+syncandnotify() {
+ acc="$(echo "$account" | sed "s/.*\///")"
+ if [ -z "$opts" ]; then mbsync "$acc"; else mbsync "$opts" "$acc"; fi
+ new=$(find\
+ "$HOME/.local/share/mail/$acc/INBOX/new/"\
+ "$HOME/.local/share/mail/$acc/Inbox/new/"\
+ "$HOME/.local/share/mail/$acc/inbox/new/"\
+ "$HOME/.local/share/mail/$acc/INBOX/cur/"\
+ "$HOME/.local/share/mail/$acc/Inbox/cur/"\
+ "$HOME/.local/share/mail/$acc/inbox/cur/"\
+ -type f -newer "$lastrun" 2> /dev/null)
+ newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
+ case 1 in
+ $((newcount > 0)) ) notify "$acc" "$newcount" ;;
+ esac
+}
+
+# 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 "$lastrun"