notify_mail.sh 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/bin/sh
  2. MUHOME="$HOME/.mu"
  3. usage="usage: $(basename "$0") [-h] NUMBER_SECONDS
  4. Runs mu commaind to find any new messages for any account
  5. where:
  6. -h show this help text
  7. NUMBER_SECONDS number minute efore to find new/unred messages"
  8. : ${1?"$usage"}
  9. while getopts ':hs:' option; do
  10. case "$option" in
  11. h) echo "$usage"
  12. exit
  13. ;;
  14. \?) printf "illegal option: -%s\n" "$OPTARG" >&2
  15. echo "$usage" >&2
  16. exit 1
  17. ;;
  18. esac
  19. done
  20. # timestamp of previous mail sync
  21. NBACK=$(($(date +%s) - $1*60))
  22. # number of messages after timestamp
  23. ACTION="mu find --muhome $MUHOME flag:new AND flag:unread AND NOT flag:trashed AND maildir:/inbox/ --sortfield=date --reverse --after=$NBACK"
  24. NMAIL=$($ACTION 2>/dev/null | wc -l)
  25. # Notify config
  26. appname="mu4e"
  27. icon=$HOME/.icons/emacs/mail-unread.svg
  28. category="email"
  29. urgency=normal
  30. timeout=12000
  31. if [ $NMAIL -eq 1 ]
  32. then
  33. summary="1 New message in $($ACTION --fields="m" 2>/dev/null)"
  34. body="$($ACTION --fields='📩 f: s\n' 2>/dev/null)"
  35. notify-send -a ${appname} -i ${icon} -c ${category} -u ${urgency} -t ${timeout} "${summary}" "${body}" &
  36. elif [ $NMAIL -gt 1 ]
  37. then
  38. summary="$NMAIL New messages"
  39. body="$($ACTION --fields='📩 f: s\n' 2>/dev/null)"
  40. notify-send -a ${appname} -i ${icon} -c ${category} -u ${urgency} -t ${timeout} "${summary}" "${body}" &
  41. fi
  42. exit 0