Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

79 wiersze
3.2 KiB

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