summaryrefslogtreecommitdiff
path: root/bin/mailsync
diff options
context:
space:
mode:
Diffstat (limited to 'bin/mailsync')
-rwxr-xr-xbin/mailsync124
1 files changed, 66 insertions, 58 deletions
diff --git a/bin/mailsync b/bin/mailsync
index cbd36ff..bb3feae 100755
--- a/bin/mailsync
+++ b/bin/mailsync
@@ -12,17 +12,23 @@
# Xorg and MacOS as well.
# Run only if not already running in other instance
-pgrep mbsync >/dev/null && { echo "mbsync is already running."; exit ;}
+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\|MPOPRC\|PASSWORD_STORE_DIR\|PASSWORD_STORE_GPG_OPTS\|NOTMUCH_CONFIG\|GNUPGHOME\|MAILSYNC_MUTE\|XDG_CONFIG_HOME\|XDG_DATA_HOME\)=" \
- "$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)"
+ "^\s*\(export \)\?\(MBSYNCRC\|MPOPRC\|PASSWORD_STORE_DIR\|PASSWORD_STORE_GPG_OPTS\|NOTMUCH_CONFIG\|GNUPGHOME\|MAILSYNC_MUTE\|XDG_CONFIG_HOME\|XDG_DATA_HOME\)=" \
+ "$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)"
+
+# For non-interactive shell (e.g. cron job) run only when the GPG key (in $GNUPGHOME or pass --homedir) is unlocked
+tty -s || (echo "dummy" | gpg --sign --batch --pinentry-mode error -o /dev/null >/dev/null 2>&1) || exit
export GPG_TTY="$(tty)"
@@ -33,54 +39,56 @@ lastrun="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/.mailsynclastrun"
# Settings are different for MacOS (Darwin) systems.
case "$(uname)" in
- Darwin) notify() { osascript -e "display notification \"$2\" with title \"$1\"" ;} ;;
- *)
- 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 -ax X\(\|org\|wayland\))"
- displays="$(echo "$pgrepoutput" | grep -wo "[0-9]*:[0-9]\+" | sort -u)"
- [ -z "$displays" ] && [ -d /tmp/.X11-unix ] && displays=$(cd /tmp/.X11-unix && for x in X*; do echo ":${x#X}"; done)
-
- notify() { [ -n "$pgrepoutput" ] && for x in ${displays:-:0}; do
- export DISPLAY="$x"
- notify-send --app-name="mutt-wizard" "$1" "$2"
- done ;}
- ;;
+Darwin) notify() { osascript -e "display notification \"$2\" with title \"$1\""; } ;;
+*)
+ 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 -ax X\(\|org\|wayland\))"
+ displays="$(echo "$pgrepoutput" | grep -wo "[0-9]*:[0-9]\+" | sort -u)"
+ [ -z "$displays" ] && [ -d /tmp/.X11-unix ] && displays=$(cd /tmp/.X11-unix && for x in X*; do echo ":${x#X}"; done)
+
+ notify() { [ -n "$pgrepoutput" ] && for x in ${displays:-:0}; do
+ export DISPLAY="$x"
+ notify-send --app-name="mutt-wizard" -- "$1" "$2"
+ done; }
+ ;;
esac
# Check account for new mail. Notify if there is new content.
syncandnotify() {
- case "$1" in
- imap) mbsync -q "$2" ;;
- pop) mpop -q "$2" ;;
- esac
- new=$(find\
- "$HOME/.local/share/mail/$2/"[Ii][Nn][Bb][Oo][Xx]/new/ \
- "$HOME/.local/share/mail/$2/"[Ii][Nn][Bb][Oo][Xx]/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 $2."
- [ -z "$MAILSYNC_MUTE" ] && notify "New Mail!" "📬 $newcount new mail(s) in \`$2\` account."
- ;;
- $((newcount > 0)) )
- echo "$newcount new mail for $2."
- [ -z "$MAILSYNC_MUTE" ] &&
- for file in $new; do
- # Extract and decode subject and sender from mail.
- subject="$(sed -n "/^Subject:/ s|Subject: *|| p" "$file" |
- perl -CS -MEncode -ne 'print decode("MIME-Header", $_)')"
- 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 $2." ;;
-esac
+ case "$1" in
+ imap) mbsync -q "$2" ;;
+ pop) mpop -q "$2" ;;
+ esac
+ new=$(find "$HOME/.local/share/mail/$2/"[Ii][Nn][Bb][Oo][Xx]/new/ \
+ "$HOME/.local/share/mail/$2/"[Ii][Nn][Bb][Oo][Xx]/cur/ \
+ -type f -newer "$lastrun" 2>/dev/null)
+ newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
+ case 1 in
+ $((newcount > 5)))
+ echo "$newcount new mails for $2." >/dev/tty
+ [ -z "$MAILSYNC_MUTE" ] && notify "New Mail!" "📬 $newcount new mails in \`$2\` account."
+ ;;
+ $((newcount > 0)))
+ echo "$newcount new mail(s) for $2." >/dev/tty
+ [ -z "$MAILSYNC_MUTE" ] &&
+ 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 $2." ;;
+ esac
}
allaccounts="$(grep -hs "^\(Channel\|account\)" "$MBSYNCRC" "$MPOPRC")"
@@ -89,19 +97,19 @@ allaccounts="$(grep -hs "^\(Channel\|account\)" "$MBSYNCRC" "$MPOPRC")"
IFS='
'
if [ -z "$1" ]; then
- tosync="$allaccounts"
+ tosync="$allaccounts"
else
- tosync="$(for arg in "$@"; do for availacc in $allaccounts; do
- [ "$arg" = "${availacc##* }" ] && echo "$availacc" && break
- done || echo "error $arg"; done)"
+ tosync="$(for arg in "$@"; do for availacc in $allaccounts; do
+ [ "$arg" = "${availacc##* }" ] && echo "$availacc" && break
+ done || echo "error $arg"; done)"
fi
for account in $tosync; do
- case $account in
- Channel*) syncandnotify imap "${account##* }" & ;;
- account*) syncandnotify pop "${account##* }" & ;;
- error*) echo "ERROR: Account ${account##* } not found." ;;
- esac
+ case $account in
+ Channel*) syncandnotify imap "${account##* }" & ;;
+ account*) syncandnotify pop "${account##* }" & ;;
+ error*) echo "ERROR: Account ${account##* } not found." ;;
+ esac
done
wait