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 3.7 KiB

3 jaren geleden
3 jaren geleden
2 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\|NOTMUCH_CONFIG\|GNUPGHOME\|MAILSYNC_MUTE\)=" \
  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. # Mute notifications or get kernel name.
  27. # Settings are different for MacOS (Darwin) systems.
  28. [ -z "$MAILSYNC_MUTE" ] && notify="$(uname)" || notify="mute"
  29. case "$notify" in
  30. mute)
  31. notify() { : ;}
  32. ;;
  33. Darwin)
  34. notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2 ;}
  35. ;;
  36. *)
  37. case "$(readlink -f /sbin/init)" in
  38. *systemd*|*openrc*) export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus ;;
  39. esac
  40. # remember if a display server is running since `ps` doesn't always contain a display
  41. pgrepoutput="$(pgrep -ax X\(\|org\|wayland\))"
  42. displays="$(echo "$pgrepoutput" | grep -wo "[0-9]*:[0-9]\+" | sort -u)"
  43. notify() { [ -n "$pgrepoutput" ] && for x in ${displays:-0:}; do
  44. export DISPLAY=$x
  45. notify-send --app-name="mutt-wizard" "New mail!" "📬 $2 new mail(s) in \`$1\` account."
  46. done ;}
  47. ;;
  48. esac
  49. # Check account for new mail. Notify if there is new content.
  50. syncandnotify() {
  51. acc="$(echo "$account" | sed "s/.*\///")"
  52. if [ "$1" = "pop" ]; then
  53. # Handle POP
  54. mpop "$acc"
  55. else
  56. # Handle IMAP
  57. if [ -z "$opts" ]; then mbsync "$acc"; else mbsync "$opts" "$acc"; fi
  58. fi
  59. new=$(find\
  60. "$HOME/.local/share/mail/$acc/"[Ii][Nn][Bb][Oo][Xx]/new/\
  61. "$HOME/.local/share/mail/$acc/"[Ii][Nn][Bb][Oo][Xx]/cur/\
  62. -type f -newer "$lastrun" 2> /dev/null)
  63. newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
  64. case 1 in
  65. $((newcount > 0)) ) notify "$acc" "$newcount" ;;
  66. esac
  67. }
  68. # Sync accounts passed as argument or all.
  69. if [ "$#" -gt "0" ]; then
  70. for arg in "$@"; do
  71. [ "${arg%${arg#?}}" = '-' ] && opts="${opts:+${opts} }${arg}" && shift 1
  72. done
  73. accounts=$*
  74. fi
  75. # TODO handle account passed from argument
  76. [ -z "$accounts" ] && accounts="$(awk '/^Channel/ {print $2}' "$MBSYNCRC")"
  77. [ -z "$pop_accounts" ] && pop_accounts="$(awk '/^account/ {print $2}' "$MPOPRC")"
  78. # Parallelize multiple accounts
  79. for account in $accounts; do
  80. syncandnotify "imap" &
  81. done
  82. for account in $pop_accounts; do
  83. syncandnotify "pop" &
  84. done
  85. wait
  86. notmuch new --quiet
  87. #Create a touch file that indicates the time of the last run of mailsync
  88. touch "$lastrun"