You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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