Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

102 rindas
4.5 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. # Workaround for zsh users who only have .zshenv in their $HOME.
  16. usersh="$(awk -F: -v user="$USER" '$1 == user {print $NF}' /etc/passwd)"
  17. [ "$usersh" = /usr/bin/zsh ] && eval "$(grep -h -- \
  18. "^\s*\(export \)\?\(ZDOTDIR\|XDG_CONFIG_HOME\|XDG_DATA_HOME\)=" \
  19. "$HOME/.zshenv" 2>/dev/null)"
  20. # First, we have to get the right variables for the mbsync file, the pass
  21. # archive, notmuch and the GPG home. This is done by searching common profile
  22. # files for variable assignments. This is ugly, but there are few options that
  23. # will work on the maximum number of machines.
  24. eval "$(grep -h -- \
  25. "^\s*\(export \)\?\(MBSYNCRC\|PASSWORD_STORE_DIR\|NOTMUCH_CONFIG\|GNUPGHOME\)=" \
  26. "$HOME/.profile" "$HOME/.bash_profile" "${ZDOTDIR:-$HOME}/.zprofile" "$HOME/.bashrc" "${ZDOTDIR:-$HOME}/.zshrc" "$HOME/.pam_environment" 2>/dev/null)"
  27. # One alternative to this kind of command would be marking the script for
  28. # /bin/sh -l. That might cause other problems on other particular setups that
  29. # do more complicated things on login, or those people who assign environmental
  30. # variables in shell rc files.
  31. case "$(readlink -f /sbin/init)" in
  32. *systemd*) export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus ;;
  33. esac
  34. export GPG_TTY=$TTY
  35. muttconfig="${XDG_CONFIG_HOME:-$HOME/.config}/mutt"
  36. [ -n "$MBSYNCRC" ] && alias mbsync="mbsync -c $MBSYNCRC" || MBSYNCRC="$HOME/.mbsyncrc"
  37. # Settings are different for MacOS (Darwin) systems.
  38. case "$(uname)" in
  39. Darwin)
  40. notify() { osascript -e "display notification \"$2 in $1\" with title \"You've got Mail\" subtitle \"Account: $account\"" && sleep 2 ;}
  41. messageinfo() { osascript -e "display notification with title \"📧 $from\" subtitle \"$subject\"" ;}
  42. ;;
  43. *)
  44. displays="$(pgrep -a Xorg | grep -wo "[0-9]*:[0-9]\+")"
  45. notify() { for x in $displays; do
  46. export DISPLAY=$x
  47. notify-send --app-name="mutt-wizard" "mutt-wizard" "📬 $2 new mail(s) in \`$1\` account."
  48. done ;}
  49. messageinfo() { for x in $displays; do
  50. export DISPLAY=$x
  51. notify-send --app-name="mutt-wizard" "📧$from:" "$subject"
  52. done ;}
  53. ;;
  54. esac
  55. # Check account for new mail. Notify if there is new content.
  56. syncandnotify() {
  57. acc="$(echo "$account" | sed "s/.*\///")"
  58. if [ -z "$opts" ]; then mbsync "$acc"; else mbsync "$opts" "$acc"; fi
  59. 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 "$muttconfig/.mailsynclastrun" 2> /dev/null)
  60. newcount=$(echo "$new" | sed '/^\s*$/d' | wc -l)
  61. if [ "$newcount" -gt "0" ]; then
  62. notify "$acc" "$newcount" &
  63. for file in $new; do
  64. # Extract subject and sender from mail.
  65. 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:]]*$//')
  66. 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')
  67. messageinfo &
  68. done
  69. fi
  70. }
  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. #Create a touch file that indicates the time of the last run of mailsync
  87. touch "$muttconfig/.mailsynclastrun"