Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

3 роки тому
3 роки тому
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 user logged in (prevent cron errors)
  12. pgrep -u "${USER:=$LOGNAME}" >/dev/null || { echo "$USER not logged in; sync will not run."; exit ;}
  13. # Run only if not already running in other instance
  14. pgrep mbsync >/dev/null && { echo "mbsync is already running."; exit ;}
  15. # First, we have to get the right variables for the mbsync file, the pass
  16. # archive, notmuch and the GPG home. This is done by searching common profile
  17. # files for variable assignments. This is ugly, but there are few options that
  18. # will work on the maximum number of machines.
  19. eval "$(grep -h -- \
  20. "^\s*\(export \)\?\(MBSYNCRC\|PASSWORD_STORE_DIR\|NOTMUCH_CONFIG\|GNUPGHOME\)=" \
  21. "$HOME/.profile" "$HOME/.bash_profile" "$HOME/.zprofile" "$HOME/.config/zsh/.zprofile" "$HOME/.zshenv" \
  22. "$HOME/.config/zsh/.zshenv" "$HOME/.bashrc" "$HOME/.zshrc" "$HOME/.config/zsh/.zshrc" \
  23. "$HOME/.pam_environment" 2>/dev/null)"
  24. export GPG_TTY="$(tty)"
  25. [ -n "$MBSYNCRC" ] && alias mbsync="mbsync -c $MBSYNCRC" || MBSYNCRC="$HOME/.mbsyncrc"
  26. lastrun="${XDG_CONFIG_HOME:-$HOME/.config}/mutt/.mailsynclastrun"
  27. # Settings are different for MacOS (Darwin) systems.
  28. case "$(uname)" in
  29. Darwin)
  30. notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2 ;}
  31. ;;
  32. *)
  33. case "$(readlink -f /sbin/init)" in
  34. *systemd*|*openrc*) export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus ;;
  35. esac
  36. # remember if a display server is running since `ps` doesn't always contain a display
  37. pgrepoutput="$(pgrep -a X\(org\|wayland\))"
  38. displays="$(echo "$pgrepoutput" | grep -wo "[0-9]*:[0-9]\+" | sort -u)"
  39. notify() { [ -n "$pgrepoutput" ] && for x in ${displays:-0:}; do
  40. export DISPLAY=$x
  41. notify-send --app-name="mutt-wizard" "New mail!" "📬 $2 new mail(s) in \`$1\` account."
  42. done ;}
  43. ;;
  44. esac
  45. # Check account for new mail. Notify if there is new content.
  46. syncandnotify() {
  47. acc="$(echo "$account" | sed "s/.*\///")"
  48. if [ -z "$opts" ]; then mbsync "$acc"; else mbsync "$opts" "$acc"; fi
  49. new=$(find\
  50. "$HOME/.local/share/mail/$acc/INBOX/new/"\
  51. "$HOME/.local/share/mail/$acc/Inbox/new/"\
  52. "$HOME/.local/share/mail/$acc/inbox/new/"\
  53. "$HOME/.local/share/mail/$acc/INBOX/cur/"\
  54. "$HOME/.local/share/mail/$acc/Inbox/cur/"\
  55. "$HOME/.local/share/mail/$acc/inbox/cur/"\
  56. -type f -newer "$lastrun" 2> /dev/null)
  57. newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
  58. case 1 in
  59. $((newcount > 0)) ) notify "$acc" "$newcount" ;;
  60. esac
  61. }
  62. # Sync accounts passed as argument or all.
  63. if [ "$#" -eq "0" ]; then
  64. accounts="$(awk '/^Channel/ {print $2}' "$MBSYNCRC")"
  65. else
  66. for arg in "$@"; do
  67. [ "${arg%${arg#?}}" = '-' ] && opts="${opts:+${opts} }${arg}" && shift 1
  68. done
  69. accounts=$*
  70. fi
  71. # Parallelize multiple accounts
  72. for account in $accounts; do
  73. syncandnotify &
  74. done
  75. wait
  76. notmuch new 2>/dev/null
  77. #Create a touch file that indicates the time of the last run of mailsync
  78. touch "$lastrun"