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

81 строка
3.2 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. maildir="${MAILDIR:-${XDG_DATA_HOME:-$HOME/.local/share}/mail}"
  14. lastrun=${XDG_CACHE_HOME:-$HOME/.cache}/.mailsynclastrun
  15. if [[ -f $configdir/isync/mbsyncrc ]]; then
  16. mbsyncrc="$configdir/isync/mbsyncrc"
  17. else
  18. mbsyncrc="$HOME/.mbsyncrc"
  19. fi
  20. mbsyncbin="$prefix/bin/mbsync -c $mbsyncrc"
  21. # Run only if user logged in (prevent cron errors)
  22. pgrep -u "$USER" >/dev/null || { echo "$USER not logged in; sync will not run."; exit ;}
  23. # Run only if not already running in other instance
  24. pgrep -x mbsync >/dev/null && { echo "mbsync is already running." ; exit ;}
  25. # Checks for internet connection and set notification script.
  26. ping -q -c 1 1.1.1.1 > /dev/null || { echo "No internet connection detected."; exit ;}
  27. command -v notify-send >/dev/null || echo "Note that \`libnotify\` or \`libnotify-send\` should be installed for pop-up mail notifications with this script."
  28. # Settings are different for MacOS (Darwin) systems.
  29. if [ "$(uname)" = "Darwin" ]; then
  30. notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2 ;}
  31. else
  32. notify() { notify-send "mutt-wizard" "📬 $2 new mail(s) in \`$1\` account." ;}
  33. fi
  34. # Check account for new mail. Notify if there is new content.
  35. syncandnotify() {
  36. acc="$(echo "$account" | sed "s/.*\///")"
  37. $mbsyncbin "$acc"
  38. new=$(find "$maildir/$acc/INBOX/new/" "$maildir/$acc/Inbox/new/" "$maildir/$acc/inbox/new/" -type f -newer "$lastrun" 2> /dev/null)
  39. newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
  40. if [ "$newcount" -gt "0" ]; then
  41. notify "$acc" "$newcount" &
  42. for file in $new; do
  43. # Extract subject and sender from mail.
  44. 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:]]*$//')
  45. 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')
  46. notify-send "📧$from:" "$subject" &
  47. done
  48. fi
  49. }
  50. # Sync accounts passed as argument or all.
  51. if [ "$#" -eq "0" ]; then
  52. accounts="$(awk '/^Channel/ {print $2}' "$mbsyncrc")"
  53. else
  54. accounts=$*
  55. fi
  56. echo " 🔃" > /tmp/imapsyncicon_"$USER"
  57. pkill -RTMIN+12 i3blocks >/dev/null 2>&1
  58. # Parallelize multiple accounts
  59. for account in $accounts
  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 "$lastrun"