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.
 
 
 
 

69 lines
3.0 KiB

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