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