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

81 строка
3.3 KiB

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