2021-02-14 18:42:53 +01:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
# @author : swytch (adapted from Luke Smith - lukesmith.xyz)
|
|
|
|
# @file : mailsync
|
2021-02-15 00:37:34 +01:00
|
|
|
# @license : GPLv3
|
2021-02-14 18:42:53 +01:00
|
|
|
# @created : Sunday Feb 14, 2021 12:46:24 CET
|
|
|
|
#
|
|
|
|
# @description : synchronize mail accounts
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
# First, 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.
|
|
|
|
|
2022-10-25 19:51:42 +02:00
|
|
|
timestamp="${XDG_STATE_HOME:-$HOME/.local/state}/mailsynclastrun"
|
|
|
|
|
2021-02-14 18:42:53 +01:00
|
|
|
eval "$(grep -h -- \
|
|
|
|
"^\s*\(export \)\?\(XDG_CACHE_HOME\|XDG_CONFIG_HOME\|XDG_DATA_HOME\)=" \
|
2021-03-10 12:08:16 +01:00
|
|
|
"$HOME/.zshenv" 2>/dev/null)"
|
2021-02-14 18:42:53 +01:00
|
|
|
|
|
|
|
eval "$(grep -h -- \
|
|
|
|
"^\s*\(export \)\?\(MBSYNCRC\|PASSWORD_STORE_DIR\|NOTMUCH_CONFIG\|GNUPGHOME\)=" \
|
2021-03-10 12:08:16 +01:00
|
|
|
"$XDG_CONFIG_HOME/zsh/.zprofile" "$HOME/.pam_environment" 2>/dev/null)"
|
2021-02-14 18:42:53 +01:00
|
|
|
|
|
|
|
export GPG_TTY=$TTY
|
2022-11-24 09:24:20 +01:00
|
|
|
[ -r "$HOME/.dbus/Xdbus" ] && . "$HOME/.dbus/Xdbus"
|
2021-02-14 18:42:53 +01:00
|
|
|
|
|
|
|
# Config file location must be passed at execution, not as envrionment variable
|
|
|
|
[ -n "$MBSYNCRC" ] && alias mbsync="mbsync -c $MBSYNCRC" || MBSYNCRC="$HOME/.mbsyncrc"
|
|
|
|
|
2022-11-23 23:32:30 +01:00
|
|
|
pgrepoutput="$(pgrep -ax X\(\|org\|wayland\))"
|
|
|
|
displays="$(echo "$pgrepoutput" | grep -wo "[0-9]*:[0-9]\+" | sort -u)"
|
2021-09-11 16:26:17 +02:00
|
|
|
notify() {
|
2022-11-23 23:32:30 +01:00
|
|
|
[ -n "$pgrepoutput" ] && for x in ${displays:-0:}; do
|
|
|
|
export DISPLAY=$x
|
|
|
|
notify-send "neomutt" " $2 new mail(s) in \`$1\` account."
|
|
|
|
done;
|
2021-09-11 16:26:17 +02:00
|
|
|
}
|
2021-02-14 18:42:53 +01:00
|
|
|
|
|
|
|
# Check account for new mail. Notify if there is new content.
|
|
|
|
syncandnotify() {
|
|
|
|
acc="$(echo "$account" | sed "s/.*\///")"
|
2021-03-16 00:32:19 +01:00
|
|
|
mbsync "$acc"
|
2022-10-25 19:51:42 +02:00
|
|
|
new="$(find "${XDG_DATA_HOME:-$HOME/.local/share}"/mail/$acc -path */new/* -type f -newer "$timestamp" | grep -iv -e "trash" -e "drafts" 2>/dev/null)"
|
2021-02-14 18:42:53 +01:00
|
|
|
newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
|
|
|
|
if [ "$newcount" -gt "0" ]; then
|
2022-10-25 12:02:47 +02:00
|
|
|
notify "$acc" "$newcount" &
|
|
|
|
[ -n "$(pidof dwmblocks)" ] && kill -40 $(pidof dwmblocks)
|
2021-02-14 18:42:53 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
accounts="$(awk '/^Channel/ {print $2}' "$MBSYNCRC")"
|
|
|
|
|
|
|
|
# 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
|
2022-10-25 19:51:42 +02:00
|
|
|
touch $timestamp
|