This repository has been archived on 2023-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
dotfiles/.local/bin/mailsync
David JULIEN 18fa36a655 Merge branch 'dev' into mercury
git: fix gitignore
mail: fix globbing in subdirectories
        use XDG_DIRECTORIES variables
        use PGP keys as default keys, not just for signing
        add keybindings to univ-nantes
        fix display in statusbar
        notify only the amount of mail, not content
        fix timestamp for sync
nvim: fix deprecated functions in nvim-cmp
        change Telescope keymaps prefixes
        add Telescope keymap for grepping
2022-10-25 22:28:50 +02:00

63 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env sh
######################################################################
# @author : swytch (adapted from Luke Smith - lukesmith.xyz)
# @file : mailsync
# @license : GPLv3
# @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.
timestamp="${XDG_STATE_HOME:-$HOME/.local/state}/mailsynclastrun"
eval "$(grep -h -- \
"^\s*\(export \)\?\(XDG_CACHE_HOME\|XDG_CONFIG_HOME\|XDG_DATA_HOME\)=" \
"$HOME/.zshenv" 2>/dev/null)"
eval "$(grep -h -- \
"^\s*\(export \)\?\(MBSYNCRC\|PASSWORD_STORE_DIR\|NOTMUCH_CONFIG\|GNUPGHOME\)=" \
"$XDG_CONFIG_HOME/zsh/.zprofile" "$HOME/.pam_environment" 2>/dev/null)"
case "$(readlink -f /sbin/init)" in
*systemd*) export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus ;;
esac
export GPG_TTY=$TTY
# Config file location must be passed at execution, not as envrionment variable
[ -n "$MBSYNCRC" ] && alias mbsync="mbsync -c $MBSYNCRC" || MBSYNCRC="$HOME/.mbsyncrc"
notify() {
notify-send "neomutt" "$2 new mail(s) in \`$1\` account."
}
# Check account for new mail. Notify if there is new content.
syncandnotify() {
acc="$(echo "$account" | sed "s/.*\///")"
mbsync "$acc"
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)"
newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
if [ "$newcount" -gt "0" ]; then
notify "$acc" "$newcount" &
[ -n "$(pidof dwmblocks)" ] && kill -40 $(pidof dwmblocks)
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
touch $timestamp