neomutt_hook.sh 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/bin/bash
  2. ## This script is meant to be run by neomutt via a timeout-hook.
  3. ## It will run ${sync_cmd}, and check/deal a few things:
  4. ## * that your gpg key or smart card is cached
  5. ## * that ${sync_cmd} isn't already running
  6. ## * that you have internet
  7. ## TODO: list more things ^?
  8. sync_cmd="mbsync --quiet --all"
  9. gpg_cached="$({ gpg-connect-agent 'keyinfo --list' /bye 2>/dev/null; gpg-connect-agent 'scd getinfo card_list' /bye 2>/dev/null; } | awk 'BEGIN{CH=0} /^S/ {if($7==1){CH=1}; if($2=="SERIALNO"){CH=1}} END{if($0!=""){print CH} else {print "none"}}')"
  10. ## Check for gpg key/smartcard in cache, exit if not.
  11. ## see my blog post for a breakdown of the gpg cache check
  12. ## https://demu.red/blog/2017/03/how-to-check-if-your-smartcards-gpg-key-is-in-cache-part-3/
  13. if [ "${gpg_cached}" == 0 ]; then
  14. exit ## Exit if key not cached
  15. fi
  16. ## Exit if ${sync_cmd} already running
  17. if [ "$(pgrep "${sync_cmd%% *}")" ]; then
  18. exit
  19. fi
  20. ## Check for internet
  21. ## see https://unix.stackexchange.com/a/190610/39115
  22. test=google.com
  23. if nc -zw1 $test 443 2>/dev/null && echo |openssl s_client -connect $test:443 2>&1 |awk 'handshake && $1 == "Verification" { if ($2=="OK") exit; exit 1 } $1 $2 == "SSLhandshake" { handshake = 1 }'; then
  24. : ## noop
  25. else
  26. exit ## Exit if no internet
  27. fi
  28. ## run ${sync_cmd}
  29. ${sync_cmd}