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.

mailsync 4.6 KiB

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