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.
 
 
 
 

73 regels
3.5 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 || ping -q -c 1 1.0.0.1 > /dev/null || ping -q -c 1 example.org || { 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. export 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. [ -f "$HOME/.config/isync/mbsyncrc" ] && MBSYNC="mbsync -c \"$HOME/.config/isync/mbsyncrc\"" || MBSYNC="mbsync"
  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. messageinfo() { osascript -e "display notification with title \"📧 $from\" subtitle \"$subject\"" ;}
  20. else
  21. notify() { notify-send --app-name="mutt-wizard" "mutt-wizard" "📬 $2 new mail(s) in \`$1\` account." ;}
  22. messageinfo() { notify-send --app-name="mutt-wizard" "📧$from:" "$subject" ;}
  23. fi
  24. # Check account for new mail. Notify if there is new content.
  25. syncandnotify() {
  26. acc="$(echo "$account" | sed "s/.*\///")"
  27. if [ -z "$opts" ]; then eval "$MBSYNC $acc"; else eval "$MBSYNC $opts $acc"; fi
  28. 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)
  29. newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
  30. if [ "$newcount" -gt "0" ]; then
  31. notify "$acc" "$newcount" &
  32. for file in $new; do
  33. # Extract subject and sender from mail.
  34. 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:]]*$//')
  35. 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')
  36. messageinfo &
  37. done
  38. fi
  39. }
  40. # Sync accounts passed as argument or all.
  41. if [ "$#" -eq "0" ]; then
  42. [ -f "$HOME/.config/isync/mbsyncrc" ] && accounts="$(awk '/^Channel/ {print $2}' "$HOME/.config/isync/mbsyncrc")" || accounts="$(awk '/^Channel/ {print $2}' "$HOME/.mbsyncrc")"
  43. else
  44. for arg in "$@"; do
  45. [ "${arg%${arg#?}}" = '-' ] && opts="${opts:+${opts} }${arg}" && shift 1
  46. done
  47. accounts=$*
  48. fi
  49. ( kill -46 "$(pidof "${STATUSBAR:-dwmblocks}")" >/dev/null 2>&1 ) 2>/dev/null
  50. # Parallelize multiple accounts
  51. for account in $accounts
  52. do
  53. syncandnotify &
  54. done
  55. wait
  56. ( kill -46 "$(pidof "${STATUSBAR:-dwmblocks}")" >/dev/null 2>&1 ) 2>/dev/null
  57. notmuch new 2>/dev/null
  58. #Create a touch file that indicates the time of the last run of mailsync
  59. touch "$HOME/.config/mutt/.mailsynclastrun"