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.
 
 
 
 

66 lines
2.9 KiB

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