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.
 
 
 
 

47 lines
1.7 KiB

  1. #!/bin/sh
  2. # This script will run offlineimap and check
  3. # for new email if there is an internet connection.
  4. #
  5. # If it detects new mail, it uses mpv to play a
  6. # notification sound: notify.opus
  7. #
  8. # I have this run as a cronjob every 5 minutes.
  9. # Check for internet connection. Exit script if none. (timeout in mac is `-t`)
  10. if [ "$(uname)" == "Darwin" ]
  11. then
  12. ping -q -t 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` >/dev/null || exit
  13. else
  14. ping -q -w 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` >/dev/null || exit
  15. fi
  16. # Get current number of new mail, then begin sync.
  17. ori=$(find ~/.mail -wholename '*/new/*' | grep -vi "spam\|trash\|junk" | wc -l)
  18. offlineimap -o "$@"
  19. # Recount new mail.
  20. new=$(find ~/.mail -wholename '*/new/*' | grep -vi "spam\|trash\|junk" | wc -l)
  21. # If new mail has grown, play a notification.
  22. if [ "$new" -gt "$ori" ]; then
  23. mpv --quiet ~/.config/mutt/etc/notify.opus
  24. fi
  25. for account in $(ls ~/.mail)
  26. do
  27. for mailbox in $(ls ~/.mail/$account/)
  28. do
  29. #List unread messages newer than last mailsync and count them
  30. newcount=$(find ~/.mail/$account/$mailbox/new/ -type f -newer ~/.config/mutt/etc/mailsynclastrun 2> /dev/null | wc -l)
  31. #Pop a Mac style notification with the count for that mailbox
  32. if [ "$(uname)" == "Darwin" -a "$newcount" -gt "0" ]
  33. then
  34. osascript -e "display notification \"$newcount in $mailbox\" with title \"Youve got Mail\" subtitle \"Account: $account\""
  35. sleep 2
  36. fi
  37. done
  38. done
  39. #Create a touch file that indicates the time of the last run of mailsync
  40. touch ~/.config/mutt/etc/mailsynclastrun