neomutt_hook.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. 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; TO=0} /^S/ {if($7==1){CH=1}; {if($4=="T"){TO=1}}; if(TO==1 && $2=="SERIALNO"){CH=1}} END{if($0!=""){print CH} else {print "none"}}')"
  11. ## Check for gpg key/smartcard in cache, exit if not.
  12. ## see my blog post for a breakdown of the gpg cache check
  13. ## https://demu.red/blog/2017/03/how-to-check-if-your-smartcards-gpg-key-is-in-cache-part-3/
  14. if [ "${gpg_cached}" == 0 ]; then
  15. exit ## Exit if key not cached
  16. fi
  17. ## Exit if ${sync_cmd} already running
  18. if [ "$(pgrep "${sync_cmd%% *}")" ]; then
  19. exit
  20. fi
  21. ## Check for internet
  22. ## see https://unix.stackexchange.com/a/190610/39115
  23. test=google.com
  24. 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
  25. : ## noop
  26. else
  27. exit ## Exit if no internet
  28. fi
  29. ## run ${sync_cmd}
  30. ${sync_cmd}