Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

76 строки
3.3 KiB

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