25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mailsync 3.2 KiB

3 년 전
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\|PASSWORD_STORE_DIR\|NOTMUCH_CONFIG\|GNUPGHOME\)=" \
  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. lastrun="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/.mailsynclastrun"
  25. # Settings are different for MacOS (Darwin) systems.
  26. case "$(uname)" in
  27. Darwin)
  28. notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2 ;}
  29. ;;
  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. notify() { [ -n "$pgrepoutput" ] && for x in ${displays:-0:}; do
  38. export DISPLAY=$x
  39. notify-send --app-name="mutt-wizard" "New mail!" "📬 $2 new mail(s) in \`$1\` account."
  40. done ;}
  41. ;;
  42. esac
  43. # Check account for new mail. Notify if there is new content.
  44. syncandnotify() {
  45. acc="$(echo "$account" | sed "s/.*\///")"
  46. if [ -z "$opts" ]; then mbsync "$acc"; else mbsync "$opts" "$acc"; fi
  47. new=$(find\
  48. "$HOME/.local/share/mail/$acc/"[Ii][Nn][Bb][Oo][Xx]/new/\
  49. "$HOME/.local/share/mail/$acc/"[Ii][Nn][Bb][Oo][Xx]/cur/\
  50. -type f -newer "$lastrun" 2> /dev/null)
  51. newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
  52. case 1 in
  53. $((newcount > 0)) ) notify "$acc" "$newcount" ;;
  54. esac
  55. }
  56. # Sync accounts passed as argument or all.
  57. if [ "$#" -gt "0" ]; then
  58. for arg in "$@"; do
  59. [ "${arg%${arg#?}}" = '-' ] && opts="${opts:+${opts} }${arg}" && shift 1
  60. done
  61. accounts=$*
  62. fi
  63. [ -z "$accounts" ] && accounts="$(awk '/^Channel/ {print $2}' "$MBSYNCRC")"
  64. # Parallelize multiple accounts
  65. for account in $accounts; do
  66. syncandnotify &
  67. done
  68. wait
  69. notmuch new --quiet
  70. #Create a touch file that indicates the time of the last run of mailsync
  71. touch "$lastrun"