選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

mailsync 3.2 KiB

5年前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. 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. # 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 -e "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 "$opts" "$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. for arg in "$@"; do
  44. [ "${arg%${arg#?}}" = '-' ] && opts="${opts:+${opts} }${arg}" && shift 1
  45. done
  46. accounts=$*
  47. fi
  48. echo " 🔃" > /tmp/imapsyncicon_"$USER"
  49. ( pkill -RTMIN+12 "${STATUSBAR:-blocks}" >/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. rm -f /tmp/imapsyncicon_"$USER"
  57. ( pkill -RTMIN+12 "${STATUSBAR:-blocks}" >/dev/null 2>&1 ) 2>/dev/null
  58. notmuch new 2>/dev/null
  59. #Create a touch file that indicates the time of the last run of mailsync
  60. touch "$HOME/.config/mutt/.mailsynclastrun"