Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

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