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.
 
 
 
 

116 lines
4.4 KiB

  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. # For non-interactive shell (e.g. cron job) run only when the GPG key (in $GNUPGHOME or pass --homedir) is unlocked
  23. tty -s || (echo "dummy" | gpg --sign --batch --pinentry-mode error -o /dev/null > /dev/null 2>&1) || exit
  24. export GPG_TTY="$(tty)"
  25. [ -n "$MBSYNCRC" ] && alias mbsync="mbsync -c $MBSYNCRC" || MBSYNCRC="$HOME/.mbsyncrc"
  26. [ -n "$MPOPRC" ] || MPOPRC="$HOME/.config/mpop/config"
  27. lastrun="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/.mailsynclastrun"
  28. # Settings are different for MacOS (Darwin) systems.
  29. case "$(uname)" in
  30. Darwin) notify() { osascript -e "display notification \"$2\" with title \"$1\"" ;} ;;
  31. *)
  32. case "$(readlink -f /sbin/init)" in
  33. *systemd*|*openrc*) export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus ;;
  34. esac
  35. # remember if a display server is running since `ps` doesn't always contain a display
  36. pgrepoutput="$(pgrep -ax X\(\|org\|wayland\))"
  37. displays="$(echo "$pgrepoutput" | grep -wo "[0-9]*:[0-9]\+" | sort -u)"
  38. [ -z "$displays" ] && [ -d /tmp/.X11-unix ] && displays=$(cd /tmp/.X11-unix && for x in X*; do echo ":${x#X}"; done)
  39. notify() { [ -n "$pgrepoutput" ] && for x in ${displays:-:0}; do
  40. export DISPLAY="$x"
  41. notify-send --app-name="mutt-wizard" "$1" "$2"
  42. done ;}
  43. ;;
  44. esac
  45. # Check account for new mail. Notify if there is new content.
  46. syncandnotify() {
  47. case "$1" in
  48. imap) mbsync -q "$2" ;;
  49. pop) mpop -q "$2" ;;
  50. esac
  51. new=$(find\
  52. "$HOME/.local/share/mail/$2/"[Ii][Nn][Bb][Oo][Xx]/new/ \
  53. "$HOME/.local/share/mail/$2/"[Ii][Nn][Bb][Oo][Xx]/cur/ \
  54. -type f -newer "$lastrun" 2> /dev/null)
  55. newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
  56. case 1 in
  57. $((newcount > 5)) )
  58. echo "$newcount new mail for $2."
  59. [ -z "$MAILSYNC_MUTE" ] && notify "New Mail!" "📬 $newcount new mail(s) in \`$2\` account."
  60. ;;
  61. $((newcount > 0)) )
  62. echo "$newcount new mail for $2."
  63. [ -z "$MAILSYNC_MUTE" ] &&
  64. for file in $new; do
  65. # Extract and decode subject and sender from mail.
  66. subject="$(sed -n "/^Subject:/ s|Subject: *|| p" "$file" |
  67. perl -CS -MEncode -ne 'print decode("MIME-Header", $_)')"
  68. from="$(sed -n "/^From:/ s|From: *|| p" "$file" |
  69. perl -CS -MEncode -ne 'print decode("MIME-Header", $_)')"
  70. from="${from% *}" ; from="${from%\"}" ; from="${from#\"}"
  71. notify "📧$from:" "$subject"
  72. done
  73. ;;
  74. *) echo "No new mail for $2." ;;
  75. esac
  76. }
  77. allaccounts="$(grep -hs "^\(Channel\|account\)" "$MBSYNCRC" "$MPOPRC")"
  78. # Get accounts to sync. All if no argument. Prefix with `error` if non-existent.
  79. IFS='
  80. '
  81. if [ -z "$1" ]; then
  82. tosync="$allaccounts"
  83. else
  84. tosync="$(for arg in "$@"; do for availacc in $allaccounts; do
  85. [ "$arg" = "${availacc##* }" ] && echo "$availacc" && break
  86. done || echo "error $arg"; done)"
  87. fi
  88. for account in $tosync; do
  89. case $account in
  90. Channel*) syncandnotify imap "${account##* }" & ;;
  91. account*) syncandnotify pop "${account##* }" & ;;
  92. error*) echo "ERROR: Account ${account##* } not found." ;;
  93. esac
  94. done
  95. wait
  96. notmuch new --quiet
  97. #Create a touch file that indicates the time of the last run of mailsync
  98. touch "$lastrun"