Debian Codegraph @

bin/mailsync

raw ◀ ▶
1 Debian patch touch this file
1 #!/bin/sh
2
3 # - Syncs mail for all accounts, or a single account given as an argument.
4 # - Displays a notification showing the number of new mails.
5 # - Displays a notification for each new mail with its subject displayed.
6 # - Runs notmuch to index new mail.
7 # - This script can be set up as a cron job for automated mail syncing.
8
9 # There are many arbitrary and ugly features in this script because it is
10 # inherently difficult to pass environmental variables to cronjobs and other
11 # issues. It also should at least be compatible with Linux (and maybe BSD) with
12 # Xorg and MacOS as well.
13
14 # Run only if user logged in (prevent cron errors)
15 pgrep -u "${USER:=$LOGNAME}" >/dev/null || { echo "$USER not logged in; sync will not run."; exit ;}
16 # Run only if not already running in other instance
17 pgrep mbsync >/dev/null && { echo "mbsync is already running."; exit ;}
18
19 # First, we have to get the right variables for the mbsync file, the pass
20 # archive, notmuch and the GPG home. This is done by searching common profile
21 # files for variable assignments. This is ugly, but there are few options that
22 # will work on the maximum number of machines.
23 eval "$(grep -h -- \
24 "^\s*\(export \)\?\(MBSYNCRC\|PASSWORD_STORE_DIR\|NOTMUCH_CONFIG\|GNUPGHOME\)=" \
25 "$HOME/.profile" "$HOME/.bash_profile" "$HOME/.zprofile" "$HOME/.config/zsh/.zprofile" "$HOME/.zshenv" \
26 "$HOME/.config/zsh/.zshenv" "$HOME/.bashrc" "$HOME/.zshrc" "$HOME/.config/zsh/.zshrc" \
27 "$HOME/.pam_environment" 2>/dev/null)"
28
29 export GPG_TTY="$(tty)"
30
31 [ -n "$MBSYNCRC" ] && alias mbsync="mbsync -c $MBSYNCRC" || MBSYNCRC="$HOME/.mbsyncrc"
32
•33 lastrun="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/.mw-mailsynclastrun"
34
35 # Settings are different for MacOS (Darwin) systems.
36 case "$(uname)" in
37 Darwin)
38 notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2 ;}
39 ;;
40 *)
41 case "$(readlink -f /sbin/init)" in
42 *systemd*|*openrc*) export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus ;;
43 esac
44 # remember if a display server is running since `ps` doesn't always contain a display
45 pgrepoutput="$(pgrep -a X\(org\|wayland\))"
46 displays="$(echo "$pgrepoutput" | grep -wo "[0-9]*:[0-9]\+" | sort -u)"
47 notify() { [ -n "$pgrepoutput" ] && for x in ${displays:-0:}; do
48 export DISPLAY=$x
49 notify-send --app-name="mutt-wizard" "New mail!" "📬 $2 new mail(s) in \`$1\` account."
50 done ;}
51 ;;
52 esac
53
54 # Check account for new mail. Notify if there is new content.
55 syncandnotify() {
56 acc="$(echo "$account" | sed "s/.*\///")"
57 if [ -z "$opts" ]; then mbsync "$acc"; else mbsync "$opts" "$acc"; fi
58 new=$(find\
59 "$HOME/.local/share/mail/$acc/INBOX/new/"\
60 "$HOME/.local/share/mail/$acc/Inbox/new/"\
61 "$HOME/.local/share/mail/$acc/inbox/new/"\
62 "$HOME/.local/share/mail/$acc/INBOX/cur/"\
63 "$HOME/.local/share/mail/$acc/Inbox/cur/"\
64 "$HOME/.local/share/mail/$acc/inbox/cur/"\
65 -type f -newer "$lastrun" 2> /dev/null)
66 newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
67 case 1 in
68 $((newcount > 0)) ) notify "$acc" "$newcount" ;;
69 esac
70 }
71
72 # Sync accounts passed as argument or all.
73 if [ "$#" -eq "0" ]; then
74 accounts="$(awk '/^Channel/ {print $2}' "$MBSYNCRC")"
75 else
76 for arg in "$@"; do
77 [ "${arg%${arg#?}}" = '-' ] && opts="${opts:+${opts} }${arg}" && shift 1
78 done
79 accounts=$*
80 fi
81
82 # Parallelize multiple accounts
83 for account in $accounts; do
84 syncandnotify &
85 done
86
87 wait
88
89 notmuch new 2>/dev/null
90
•91 #Create a touch file that indicates the time of the last run of mw-mailsync
92 touch "$lastrun"