#--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*
#-- FILE:
#--   composeYMail.txt
#-- DESCRIPTION:
#--   This script logs onto a particular Yahoo! Mail
#--   account and opens the new mail composing form.
#--  
#--
#--   A description of the process of the script is as
#--   follows:
#--    1) Get the Yahoo! Mail Login Form from http://mail.yahoo.com
#--    2) Put the user account information into the form
#--       (user-name & password)
#--    3) Reformat the 'input' fields in the form so that
#--       they can be used by the curl -d switch
#--       (i.e. strip html tags, url encode fields, remove
#--        newline characters etc)
#--    4) Post (submit) this information to the Form's target
#--       and save any cookies and headers which the server
#--       sends. Also follow any redirects which the server
#--       specifies.
#--    5) Get the final Yahoo! Mail home-page location by looking at
#--       the final redirection instruction sent by the server
#--    6) Get the Yahoo! Mail home-page hostname. 
#--    7) Get the url of the Yahoo! Mail 'Inbox' from the mail
#--       home-page html.
#--    8) Go to the Yahoo! Mail Compose Form
#--    9) Extract the target of the Compose Mail Form
#--    10) Fill out the mail fields
#--    11) Submit the Compose Mail Form
#--
#--
#-- NOTES:
#--
#--   This script was based on the getYMail2 script
#--
#--   The script needs to overcome several devices used
#--   by yahoo.
#--     a) The use of cookies
#--     b) The use of automatic redirects of the web-client
#--        (using the LOCATION http header field)
#--     c) The use of ssl to encrypt the logon process
#--     d) The use of hidden fields with random numbers within
#--        the Yahoo! Mail Log-in Form to discourage automatic logins
#--
#--   These things can all be overcome because of the
#--   capabilities of the 'curl' non-interactive web client.
#--
#--   This script has only been used within the 'cygwin'
#--   unix emulating bash shell running on WinMe.
#-- IDEA:
#--   It should not be necessary to log-in to yahoo
#--   every time. The headers file (containing the cookies)
#--   and the relevant urls should be sufficient.
#--
#-- NOTE TO YAHOO ENGINEERS:
#--   This script was not written out of malicious-ness
#--   towards yahoo but because I am not able to get
#--   a credit card since I dont have a job currently
#--   and therefor I am not able to pay for a 'premium'
#--   package which includes pop forwarding of mail.
#--
#-- TESTING:
#--    22 June 02; on the Cygwin Bash s
#--    it worked that is, it returned the Yahoo! confirmation
#--    of email successfully sent. The mail was put in the
#--    'bulk mail' (spam) folder on the Yahoo! Mail Account.
#--    
#--
#--   
#-- DATE:
#--   may 02, june 02
#-- AUTHOR: mj bishop (matth3wbishop@yahoo.com)
#--
#--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*

sTargetUrl=""              #-- The initial target of the login form                                                           
sYahooMailHomeUrl=""       #-- The final url of the Yahoo! Mail home page
sYahooMailHostName=""      #-- Just the host-name of the above.


if [ "$1" = "" ]
then
  echo "
    usage: composeYMail password

    password    is the Yahoo! Mail Account user password for
                mail-box which you wish to access
        "
  exit 1;
else
  sUserPassword="$1"
fi

#-- Extract the form fields from the html
#--

#-- A user progress message
#--
echo "
  --------------------------------------------------             
  Extracting the HTML login form from the Yahoo! Mail
  site [http://mail.yahoo.com]
  --------------------------------------------------
     "

curl mail.yahoo.com \
  | sed -n '/<form /,/<\/form>/p' \
  | grep -P '(<form|<input|<\/form>)' \
  | sed -e '/name="passwd"/s/type="password"/& value=""/g' > mailForm.html

sTargetUrl=$(sed -n '/<form /s/.*action="\([^"]*\)".*/\1/p' mailForm.html)
echo "sTargetUrl = $sTargetUrl"

#-- Reformat the HTML form fields into a format which
#-- is usable by the curl -d switch. i.e Name=Value
cat mailForm.html \
  | grep '<input' \
  | sed 's/.*name="\([.a-zA-Z0-9 ]*\)".*value="\([.a-zA-Z0-9 ]*\)".*/\1=\2/g' \
  | sed 's/.*name="\([.a-zA-Z0-9 ]*\)".*value=\([0-9]*\).*/\1=\2/g' \
  | expand | sed -e 's/ *$//g' -e 's/^ *//g' > formData0.txt

#-- Put user info into Log-in Form
 cat formData0.txt \
    | sed -e 's/^login=$/login=matth3wbishop/g' \
          -e "s/^passwd=$/passwd=${sUserPassword}/g" > formData1.txt
   
# cat formData1.txt | less; exit;

#-- Basic Url encoding & removing new-lines
cat formData1.txt \
  | sed 's/ /+/g' \
  | sed 's/$/\&/g' | tr -d '\012' > formData.txt


#--
#--
echo "
  --------------------------------------------------
  Submitting (post method) the Yahoo! Mail Login form
    -- Saving cookies and other header into 'headers.txt'
    -- Automatically following any redirects
  --------------------------------------------------
     "
curl -D headers.txt -d @formData.txt -L $sTargetUrl > ymail.html
 exit 1

sYahooMailHomeUrl=$(sed -n '/Location:/s/Location: *//p' headers.txt)
echo "sYahooMailHomeUrl = $sYahooMailHomeUrl"

sYahooMailHostName=$(echo $sYahooMailHomeUrl | sed 's/\.com\/.*/\.com/g')
echo "sYahooMailHostName = $sYahooMailHostName"

sComposeMailUrl=$( grep -i 'compose' ymail.html | sed 's/.*href="\([^"]*\)".*/\1/g')
echo "sComposeMailUrl = $sComposeMailUrl"

sCheckMailUrl=$( grep -i 'check mail' ymail.html | sed 's/.*href="\([^"]*\)".*/\1/g')
echo "sCheckMailUrl = $sCheckMailUrl"

#-- User Message
echo "
  --------------------------------------------------
  Following the link to the Yahoo! Mail Compose Form
  using previously set cookies and urls

  Mail Host Name: $sYahooMailHostName
  Compose Url   : $sComposeMailUrl
  --------------------------------------------------
     "

#-- Follow the 'Compose' Link to the New Mail Form
curl -b headers.txt ${sYahooMailHostName}${sComposeMailUrl} \
  | sed -n '/<form/,/<\/form>/p' \
  | grep -P '(<form|<input|<textarea|<\/form>)' \
  | sed 's/^.*\(<input[^>]*>\).*$/\1/g' \
  > composeMailForm.html

sDataString=$(cat composeMailForm.html \
  | grep -P '<input' \
  | sed '/type=submit/d' \
  | sed 's/.*name="\([.a-zA-Z0-9 ]*\)".*value="\([.a-zA-Z0-9 ]*\)".*/\1=\2/g' \
  | sed 's/.*name="\([.a-zA-Z0-9 ]*\)".*value=\([0-9]*\).*/\1=\2/g' \
  | sed 's/.*name=\([.a-zA-Z0-9]*\).*value="\([0-9]*\)".*/\1=\2/g' \
  | expand | sed -e 's/ *$//g' -e 's/^ *//g' | sed 's/ /+/g' \
  | sed 's/$/\&/g' \
  | sed 's/^Subj=\&/Subj=testing111\&/g' \
  | sed 's/^To=\&/To=matth3wbishop@yahoo.com\&/g' \
  | tr -d '\012')

sDataString=$sDataString"Body=hello&SEND=Send"
echo "sDataString = $sDataString"

# curl -b headers.txt ${sYahooMailHostName}${sCheckMailUrl} \
#  | less

# exit 1

#-- Need to extract the target of the 'Compose Mail' Form
#-- 
sComposeTargetUrl=$(sed -n '/<form /s/.*action="\([^"]*\)".*/\1/p' composeMailForm.html)
echo "sComposeTargetUrl = $sComposeTargetUrl"

#--
#-- 
echo "
  --------------------------------------------------
  Submitting the New Mail Form

  Compose Mail Target   : $sComposeTargetUrl
  --------------------------------------------------
     "
curl -b headers.txt \
     -d $sDataString \
     -L ${sYahooMailHostName}${sComposeTargetUrl} \
     > ymail.html


links -dump ymail.html | tr -s ' ' | less

#--                                                     
#-- It is not working to manually redirect as the lines below
#-- attempt to do. Maybe have to redirect twice.
#-- Using the curl -L switch seems an easier solution
#-- 
# sYahooMailHomeUrl=$(sed -n '/Location:/s/Location: *//gp' headers.txt)
# echo "sYahooMailHomeUrl = $sYahooMailHomeUrl"
# curl -i -b headers.txt -d @formData.txt $sYahooMailHomeUrl | less