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.
 
 
 
 

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