Another copy of my dotfiles. Because I don't completely trust GitHub.
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.

103 lines
4.5 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  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\|IMAPFILTER_CONFIG\|PASSWORD_STORE_DIR\|NOTMUCH_CONFIG\|GNUPGHOME\|XDG_DATA_HOME\|XDG_CONFIG_HOME\|XDG_RUNTIME_DIR\|XDG_CACHE_HOME\)=" \
  21. "$HOME/.profile" "$HOME/.bash_profile" "$HOME/.zprofile" "$HOME/.config/zsh/.zprofile" "$HOME/.zshenv" \
  22. "$HOME/.bashrc" "$HOME/.zshrc" "$HOME/.config/zsh/.zshrc" "$HOME/.pam_environment" 2>/dev/null)"
  23. sound=0
  24. case "$(readlink -f /sbin/init)" in
  25. *systemd*) export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus ;;
  26. esac
  27. export GPG_TTY=$TTY
  28. [ -n "$MBSYNCRC" ] && alias mbsync="mbsync -c $MBSYNCRC" || MBSYNCRC="$HOME/.mbsyncrc"
  29. # Settings are different for MacOS (Darwin) systems.
  30. case "$(uname)" in
  31. Darwin)
  32. notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2 ;}
  33. messageinfo() { osascript -e "display notification with title \"📧 $from\" subtitle \"$subject\"" ;}
  34. ;;
  35. *)
  36. # remember if a display server is running since `ps` doesn't always contain a display
  37. pgrepoutput="$(pgrep -a X\(org\|wayland\))"
  38. displays="$(echo "$pgrepoutput" | grep -wo "[0-9]*:[0-9]\+" | sort -u)"
  39. notify() { [ -n "$pgrepoutput" ] && for x in ${displays:-:0}; do
  40. export DISPLAY=$x
  41. notify-send --app-name="NeoMutt" " $2 new mail(s) in \`$1\` account."
  42. if [ $sound = 0 ]; then
  43. canberra-gtk-play -i message -V 25
  44. sound=1
  45. fi
  46. done ;}
  47. messageinfo() { [ -n "$pgrepoutput" ] && for x in ${displays:-:0}; do
  48. export DISPLAY=$x
  49. notify-send --app-name="NeoMutt" " $from:" "$subject"
  50. done ;}
  51. ;;
  52. esac
  53. # Check account for new mail. Notify if there is new content.
  54. syncandnotify() {
  55. acc="$(echo "$account" | sed "s/.*\///")"
  56. if [ -z "$opts" ]; then mbsync "$acc"; else mbsync "$opts" "$acc"; fi
  57. 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_CACHE_HOME:-$HOME/.cache}/.mailsynclastrun" 2> /dev/null)
  58. find "$HOME/.local/share/mail/$acc/INBOX/new/" "$HOME/.local/share/mail/$acc/Inbox/new/" "$HOME/.local/share/mail/$acc/inbox/new/" -type f | sed '/^\s*$/d' | wc -l > "$XDG_CACHE_HOME/mcount"
  59. newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
  60. if [ "$newcount" -gt "0" ]; then
  61. notify "$acc" "$newcount" &
  62. for file in $new; do
  63. # Extract subject and sender from mail.
  64. 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:]]*$//')
  65. 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')
  66. messageinfo &
  67. done
  68. fi
  69. }
  70. imapfilter -c "$IMAPFILTER_CONFIG"
  71. # Sync accounts passed as argument or all.
  72. if [ "$#" -eq "0" ]; then
  73. accounts="$(awk '/^Channel/ {print $2}' "$MBSYNCRC")"
  74. else
  75. for arg in "$@"; do
  76. [ "${arg%${arg#?}}" = '-' ] && opts="${opts:+${opts} }${arg}" && shift 1
  77. done
  78. accounts=$*
  79. fi
  80. # Parallelize multiple accounts
  81. for account in $accounts; do
  82. syncandnotify &
  83. done
  84. wait
  85. notmuch new 2>/dev/null
  86. touch "${XDG_CACHE_HOME:-$HOME/.cache}/.mailsynclastrun"
  87. kill -57 $(pidof dwmblocks)