Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

71 řádky
3.1 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. # 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. messageinfo() { osascript "display notification with title \"📧 $from\" subtitle \"$subject\"" ;}
  19. else
  20. notify() { notify-send --app-name="mutt-wizard" "mutt-wizard" "📬 $2 new mail(s) in \`$1\` account." ;}
  21. messageinfo() { notify-send --app-name="mutt-wizard" "📧$from:" "$subject" ;}
  22. fi
  23. # Check account for new mail. Notify if there is new content.
  24. syncandnotify() {
  25. acc="$(echo "$account" | sed "s/.*\///")"
  26. mbsync "$acc"
  27. 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)
  28. newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
  29. if [ "$newcount" -gt "0" ]; then
  30. notify "$acc" "$newcount" &
  31. for file in $new; do
  32. # Extract subject and sender from mail.
  33. 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:]]*$//')
  34. 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')
  35. messageinfo &
  36. done
  37. fi
  38. }
  39. # Sync accounts passed as argument or all.
  40. if [ "$#" -eq "0" ]; then
  41. accounts="$(awk '/^Channel/ {print $2}' "$HOME/.mbsyncrc")"
  42. else
  43. accounts=$*
  44. fi
  45. echo " 🔃" > /tmp/imapsyncicon_"$USER"
  46. ( pkill -RTMIN+12 "${STATUSBAR:-blocks}" >/dev/null 2>&1 ) 2>/dev/null
  47. # Parallelize multiple accounts
  48. for account in $accounts
  49. do
  50. syncandnotify &
  51. done
  52. wait
  53. rm -f /tmp/imapsyncicon_"$USER"
  54. ( pkill -RTMIN+12 "${STATUSBAR:-blocks}" >/dev/null 2>&1 ) 2>/dev/null
  55. notmuch new 2>/dev/null
  56. #Create a touch file that indicates the time of the last run of mailsync
  57. touch "$HOME/.config/mutt/.mailsynclastrun"