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

92 行
4.0 KiB

  1. #!/bin/sh
  2. # - Syncs mail for all accounts, or a single account given as an argument.
  3. # - Displays a notification showing the number of new mails.
  4. # - Displays a notification for each new mail with its subject displayed.
  5. # - Runs notmuch to index new mail.
  6. # - This script can be set up as a cron job for automated mail syncing.
  7. # There are many arbitrary and ugly features in this script because it is
  8. # inherently difficult to pass environmental variables to cronjobs and other
  9. # issues. It also should at least be compatible with Linux (and maybe BSD) with
  10. # Xorg and MacOS as well.
  11. # Run only if user logged in (prevent cron errors)
  12. pgrep -u "${USER:=$LOGNAME}" >/dev/null || { echo "$USER not logged in; sync will not run."; exit ;}
  13. # Run only if not already running in other instance
  14. pgrep -x mbsync >/dev/null && { echo "mbsync is already running." ; exit ;}
  15. # First, we have to get the right variables for the mbsync file, the pass
  16. # archive, notmuch and the GPG home. This is done by searching common profile
  17. # files for variable assignments. This is ugly, but there are few options that
  18. # will work on the maximum number of machines.
  19. eval "$(grep -h -- \
  20. "^\s*\(export \)\?\(MBSYNCRC\|PASSWORD_STORE_DIR\|NOTMUCH_CONFIG\|GNUPGHOME\)=" \
  21. "$HOME/.profile" "$HOME/.bash_profile" "$HOME/.zprofile" "$HOME/.config/zsh/.zprofile" \
  22. "$HOME/.bashrc" "$HOME/.zshrc" "$HOME/.config/zsh/.zshrc" "$HOME/.pam_environment" 2>/dev/null)"
  23. case "$(readlink -f /sbin/init)" in
  24. *systemd*) export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus ;;
  25. esac
  26. export GPG_TTY=$TTY
  27. [ -n "$MBSYNCRC" ] && alias mbsync="mbsync -c $MBSYNCRC" || MBSYNCRC="$HOME/.mbsyncrc"
  28. # Settings are different for MacOS (Darwin) systems.
  29. case "$(uname)" in
  30. Darwin)
  31. notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2 ;}
  32. messageinfo() { osascript -e "display notification with title \"📧 $from\" subtitle \"$subject\"" ;}
  33. ;;
  34. *)
  35. displays="$(pgrep -a X\(org\|wayland\) | grep -wo "[0-9]*:[0-9]\+" | sort -u)"
  36. notify() { for x in $displays; do
  37. export DISPLAY=$x
  38. notify-send --app-name="mutt-wizard" "mutt-wizard" "📬 $2 new mail(s) in \`$1\` account."
  39. done ;}
  40. messageinfo() { for x in $displays; do
  41. export DISPLAY=$x
  42. notify-send --app-name="mutt-wizard" "📧$from:" "$subject"
  43. done ;}
  44. ;;
  45. esac
  46. # Check account for new mail. Notify if there is new content.
  47. syncandnotify() {
  48. acc="$(echo "$account" | sed "s/.*\///")"
  49. if [ -z "$opts" ]; then mbsync "$acc"; else mbsync "$opts" "$acc"; fi
  50. 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 "${XDG_CONFIG_HOME:-$HOME/.config}/mutt/.mailsynclastrun" 2> /dev/null)
  51. newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
  52. if [ "$newcount" -gt "0" ]; then
  53. notify "$acc" "$newcount" &
  54. for file in $new; do
  55. # Extract subject and sender from mail.
  56. 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:]]*$//')
  57. 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')
  58. messageinfo &
  59. done
  60. fi
  61. }
  62. # Sync accounts passed as argument or all.
  63. if [ "$#" -eq "0" ]; then
  64. accounts="$(awk '/^Channel/ {print $2}' "$MBSYNCRC")"
  65. else
  66. for arg in "$@"; do
  67. [ "${arg%${arg#?}}" = '-' ] && opts="${opts:+${opts} }${arg}" && shift 1
  68. done
  69. accounts=$*
  70. fi
  71. # Parallelize multiple accounts
  72. for account in $accounts; do
  73. syncandnotify &
  74. done
  75. wait
  76. notmuch new 2>/dev/null
  77. #Create a touch file that indicates the time of the last run of mailsync
  78. touch "${XDG_CONFIG_HOME:-$HOME/.config}/mutt/.mailsynclastrun"