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

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