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.

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