您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

5 年前
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/bin/sh
  2. # Sync mail and give notification if there is new mail.
  3. # Run only if user logged in (prevent cron errors)
  4. pgrep -u "${USER:=$LOGNAME}" >/dev/null || { echo "$USER not logged in; sync will not run."; exit ;}
  5. # Run only if not already running in other instance
  6. pgrep -x mbsync >/dev/null && { echo "mbsync is already running." ; exit ;}
  7. # Checks for internet connection and set notification script.
  8. ping -q -c 1 1.1.1.1 > /dev/null || ping -q -c 1 1.0.0.1 > /dev/null || ping -q -c 1 example.org || { echo "No internet connection detected."; exit ;}
  9. command -v notify-send >/dev/null || echo "Note that \`libnotify\` or \`libnotify-send\` should be installed for pop-up mail notifications with this script."
  10. # Required to display notifications if run as a cronjob:
  11. export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus
  12. export DISPLAY=:0.0
  13. # For individual configurations:
  14. [ -d "$HOME/.local/share/password-store" ] && export PASSWORD_STORE_DIR="$HOME/.local/share/password-store"
  15. # Settings are different for MacOS (Darwin) systems.
  16. if [ "$(uname)" = "Darwin" ]; then
  17. notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2 ;}
  18. messageinfo() { osascript -e "display notification with title \"📧 $from\" subtitle \"$subject\"" ;}
  19. else
  20. notify() { notify-send --app-name="mutt-wizard" "mutt-wizard" "📬 $2 new mail(s) in \`$1\` account." ;}
  21. messageinfo() { notify-send --app-name="mutt-wizard" "📧$from:" "$subject" ;}
  22. fi
  23. # Check account for new mail. Notify if there is new content.
  24. syncandnotify() {
  25. acc="$(echo "$account" | sed "s/.*\///")"
  26. if [ -z "$opts" ]; then mbsync "$acc"; else mbsync "$opts" "$acc"; fi
  27. new=$(find "$HOME/.local/share/mail/$acc/INBOX/new/" "$HOME/.local/share/mail/$acc/Inbox/new/" "$HOME/.local/share/mail/$acc/inbox/new/" -type f -newer "$HOME/.config/mutt/.mailsynclastrun" 2> /dev/null)
  28. newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
  29. if [ "$newcount" -gt "0" ]; then
  30. notify "$acc" "$newcount" &
  31. for file in $new; do
  32. # Extract subject and sender from mail.
  33. from=$(awk '/^From: / && ++n ==1,/^\<.*\>:/' "$file" | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | awk '{ $1=""; if (NF>=3)$NF=""; print $0 }' | sed 's/^[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//')
  34. subject=$(awk '/^Subject: / && ++n == 1,/^\<.*\>: / && ++i == 2' "$file" | head -n 1 | perl -CS -MEncode -ne 'print decode("MIME-Header", $_)' | sed 's/^Subject: //' | sed 's/^{[[:blank:]]*[\"'\''\<]*//;s/[\"'\''\>]*[[:blank:]]*$//' | tr -d '\n')
  35. messageinfo &
  36. done
  37. fi
  38. }
  39. # Sync accounts passed as argument or all.
  40. if [ "$#" -eq "0" ]; then
  41. accounts="$(awk '/^Channel/ {print $2}' "$HOME/.mbsyncrc")"
  42. else
  43. for arg in "$@"; do
  44. [ "${arg%${arg#?}}" = '-' ] && opts="${opts:+${opts} }${arg}" && shift 1
  45. done
  46. accounts=$*
  47. fi
  48. ( kill -46 "$(pidof "${STATUSBAR:-dwmblocks}")" >/dev/null 2>&1 ) 2>/dev/null
  49. # Parallelize multiple accounts
  50. for account in $accounts
  51. do
  52. syncandnotify &
  53. done
  54. wait
  55. ( kill -46 "$(pidof "${STATUSBAR:-dwmblocks}")" >/dev/null 2>&1 ) 2>/dev/null
  56. notmuch new 2>/dev/null
  57. #Create a touch file that indicates the time of the last run of mailsync
  58. touch "$HOME/.config/mutt/.mailsynclastrun"