imapwatch.sh 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # uses IMAP NOTIFY extension
  2. # Derived from
  3. # https://github.com/johan-adriaans/shell-imap-notify/blob/master/imap-notify
  4. if [ -z "$3" ]; then
  5. echo "Imap idle listener"
  6. echo "Usage: $0 user@domain.com server:993 /usr/bin/notify_command"
  7. exit 1
  8. fi
  9. read_secret()
  10. {
  11. stty -echo
  12. trap 'stty echo' EXIT
  13. read "$@"
  14. stty echo
  15. trap - EXIT
  16. echo
  17. }
  18. user=$1
  19. server=$2
  20. command=$3
  21. printf "Password:"
  22. read_secret password
  23. start_idle () {
  24. echo ". login \"$user\" \"$password\""
  25. echo ". select lists"
  26. # Change lists to a different folder
  27. echo ". notify set (subtree lists (MessageNew MessageExpunge))"
  28. echo ". idle"
  29. while true; do
  30. sleep 600;
  31. echo "done"
  32. echo ". noop"
  33. echo ". idle"
  34. done
  35. }
  36. # Start ssl connection
  37. echo "Starting imap idle client, logging in as $user at $server"
  38. while read -r line ; do
  39. # Debug info, turn this off for silent operation
  40. echo "$line"
  41. if echo "$line" | grep -Eq ". STATUS .*"; then
  42. echo "Message added or deleted, executing $command"
  43. $command
  44. fi
  45. done < <(openssl s_client -crlf -quiet -connect "$server" 2>/dev/null < <(start_idle))