You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

46 lines
1.7 KiB

  1. #!/usr/bin/env sh
  2. # Sync mail and give notification if there is new mail.
  3. # Run only if user logged in (prevent cron errors)
  4. pgrep -cu "$USER" >/dev/null || exit
  5. # Run only if not already running in other instance
  6. [ $(pgrep -xf "sh /usr/bin/mailsync" | wc -l) -eq 2 ] || exit
  7. # Checks for internet connection and set notification script.
  8. ping -q -c 1 1.1.1.1 > /dev/null || 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. export DISPLAY=:0.0
  11. # Settings are different for MacOS (Darwin) systems.
  12. if [ "$(uname)" = "Darwin" ]; then
  13. notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2 ;}
  14. else
  15. notify() { notify-send "mutt-wizard" "📬 $2 new mail(s) in \`$1\` account." ;}
  16. fi
  17. echo " 🔃" > /tmp/imapsyncicon_$USER
  18. pkill -RTMIN+12 i3blocks
  19. # Run mbsync. You can feed this script different settings.
  20. if [ $# -eq 0 ]; then
  21. mbsync -a
  22. else
  23. mbsync "$@"
  24. fi
  25. rm -f /tmp/imapsyncicon_$USER
  26. pkill -RTMIN+12 i3blocks
  27. # Check all accounts/mailboxes for new mail. Notify if there is new content.
  28. for account in "$HOME/.local/share/mail/"*
  29. do
  30. acc="$(echo "$account" | sed "s/.*\///")"
  31. newcount=$(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 | wc -l)
  32. [ "$newcount" -gt "0" ] && notify "$acc" "$newcount" &
  33. done
  34. notmuch new 2>/dev/null
  35. #Create a touch file that indicates the time of the last run of mailsync
  36. touch "$HOME/.config/mutt/.mailsynclastrun"