Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

83 Zeilen
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. maildir="${MAILDIR:-${XDG_DATA_HOME:-$HOME/.local/share}/mail}"
  14. lastrun=${XDG_CACHE_HOME:-$HOME/.cache}/.mailsynclastrun
  15. if [[ -f $configdir/isync/mbsyncrc ]]; then
  16. mbsyncrc="$configdir/isync/mbsyncrc"
  17. elif [[ -f $configdir/mbsync/mbsyncrc ]]; then
  18. mbsyncrc="$configdir/mbsync/mbsyncrc"
  19. else
  20. mbsyncrc="$HOME/.mbsyncrc"
  21. fi
  22. mbsyncbin="$prefix/bin/mbsync -c $mbsyncrc"
  23. # Run only if user logged in (prevent cron errors)
  24. pgrep -u "$USER" >/dev/null || { echo "$USER not logged in; sync will not run."; exit ;}
  25. # Run only if not already running in other instance
  26. pgrep -x mbsync >/dev/null && { echo "mbsync is already running." ; exit ;}
  27. # Checks for internet connection and set notification script.
  28. ping -q -c 1 1.1.1.1 > /dev/null || { echo "No internet connection detected."; exit ;}
  29. command -v notify-send >/dev/null || echo "Note that \`libnotify\` or \`libnotify-send\` should be installed for pop-up mail notifications with this script."
  30. # Settings are different for MacOS (Darwin) systems.
  31. if [ "$(uname)" = "Darwin" ]; then
  32. notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2 ;}
  33. else
  34. notify() { notify-send "mutt-wizard" "📬 $2 new mail(s) in \`$1\` account." ;}
  35. fi
  36. # Check account for new mail. Notify if there is new content.
  37. syncandnotify() {
  38. acc="$(echo "$account" | sed "s/.*\///")"
  39. $mbsyncbin "$acc"
  40. new=$(find "$maildir/$acc/INBOX/new/" "$maildir/$acc/Inbox/new/" "$maildir/$acc/inbox/new/" -type f -newer "$lastrun" 2> /dev/null)
  41. newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
  42. if [ "$newcount" -gt "0" ]; then
  43. notify "$acc" "$newcount" &
  44. for file in $new; do
  45. # Extract subject and sender from mail.
  46. 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:]]*$//')
  47. 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')
  48. notify-send "📧$from:" "$subject" &
  49. done
  50. fi
  51. }
  52. # Sync accounts passed as argument or all.
  53. if [ "$#" -eq "0" ]; then
  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"