# DESCRIPTION:
#     This script upload a file to a free yahoo geocities web-site account
#     It needs to work in conjunction with a script called 'login-geocities.sh'
#     which gets the necessary headers (cookies) file, to make the geocities server
#     believe that it is dealing with a genuine 'interactive' web-browser rather
#     than the various unix webtools as below
#
#
# NOTES:
#
#     This script appears to be currently working. A file can be uploaded to the 
#     yahoo geocities account using this command line script
#
#
# 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 therefore I am not able to pay for a 'premium'
#   package which includes pop forwarding of mail (or ftp access to yahoo
#   geocities).
#   
# Revisions:
#   may 2002, I created this script,
#   august 2003, Barcelona, I am revising this script in order to adapt it
#    to uploading a file to a free geocities account. Also I would like to
#    rewrite this as a java application, (even an applet, although that is a
#    little optimistic) so that it could be used from an internet cafe or from
#    some other somewhat 'hostile' location. 
#   See Also:
#     getYMail.txt  
#       this was an earlier incarnation of this script which was designed to
#       extract mail from a Yahoo Mail Account. This script is currently not
#       working because of some changes Yahoo have made to the way in which the
#       login process happens. However these changes should be easily
#       accomodated for.
#    login-geocities.sh
#       This script does the 'hard work' of logging in to the geocities server and 
#       getting the necessary cookies file
#
#  Dependencies:
#     curl   
#       this unix program does all the hard work, including the actual uploading of the
#     a bash shell
#     login-geocities.sh
#       This script is really necessary for the present script to do anything useful
#  
# AUTHOR: mj bishop (matth3wbishop@yahoo.com)
#
#*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*--*

 sInitialUrl="http://www.geocities.com"
 sHeadersFileName="headers.txt"
 sTargetDirectory=""
 sResultPage="junk.html"

 #- Here are few example user agent strings to disguise ourselves with
 sUserAgent='Lynx/2.7.1 libwww-FM/2.14'
 sUserAgent='Lynx (Linux; U; Redhat Linux; en-US;'
 sUserAgent='Mozilla/4.05 [en] (X11; U; Linux 2.0.32 i586)'     
   
 if [ "$1" = "" ]
 then
   echo "usage: $0 fileToUpload [targetDirectory] [cookieFileName] "
   cat $0 | sed -n "/^[ ]*#[^-]/p" 
   exit 1;
 else
   sUploadFile="$1"
 fi

 if [ "$2" != "" ]
 then
   sTargetDirectory="$2"
 fi


 if [ "$3" != "" ]
 then
   sHeadersFileName="$3"
 fi

 sBaseUrl="http://geocities.yahoo.com"

 #- used for debugging
 if [ "y" = "x" ]
 then
   echo "
     sHeadersFileName=$sHeadersFileName
     sUploadFile=$sUploadFile
     sTargetDirectory=$sTargetDirectory
     sUserAgent=$sUserAgent
     sBaseUrl=$sBaseUrl"
 fi


 #- The -L switch below automatically follows redirection instructions sent by the 
 #- yahoo geocities server. If the 'headers file' which we are using (and which should
 #- contain the correct cookies to allow access to the 'internal' or 'logged in' parts
 #- of the geocities account) is no good, then the geocities server will redirect the 
 #- web-client back to the login page. However, if the headers file is OK (the cookies
 #- have not expired, etc), the no redirect will be issued by the web-server, and in 
 #- this case the -L switch will not be needed.
 
 #- The -A switch is the browser which curl is pretending to be. This is not necessary
 #- but seems prudent
 #curl -A "$sUserAgent" -L -b $sHeadersFileName $sBaseUrl/filemanager/upload > other-side.html

 #-- Curl syntax
 # directory=&op-uploadtodir=upload+files
 # -F/--form <name=content> Specify HTTP POST data (H)



 #- Disguise what we are; follow redirections; send back cookies
 curl \
   -A "$sUserAgent" -L  -b $sHeadersFileName \
   -F "userfile=@$sUploadFile;type=text/plain" \
   -F "op-upload=upload+files" \
   -F "directory=$sTargetDirectory" $sBaseUrl/filemanager/upload > $sResultPage 

#cat $sResultPage | grep -Ei "(input|form)"

#- If the resulting html page contains a password login box then we
#- probably can assume that the cookie/headers file was not accepted for some reason
sLoginBox=""
sErrorFlag=""
sSuccessFlag=""

sLoginBox=$(cat $sResultPage | grep -Ei "<input.*name=[\"']?passwd[\"']?")
sErrorFlag=$(cat $sResultPage | grep -Ei "invalid filename")
sSuccessFlag=$(cat $sResultPage | grep -Ei "uploaded successfully")

if [ "$sSuccessFlag" != "" ]
then
  echo "
    SUCCESS: [$sUploadFile]
      The file \"$sUploadFile\" was successfully uploaded "
elif [ "$sErrorFlag" != "" ]
then
  echo "
    ERROR: [invalid filename- $sUploadFile]
     The file \"$sUploadFile\" was not successfully uploaded. Geocities does not accept files 
     with certain extensions (such as .sh). You will have to rename the file to
     something with a suitable filename extension (such as .txt)"
elif [ "$sLoginBox" != "" ]
then
  echo "
    ERROR: [unable to connect]
    
    The headers file which you used ($sHeadersFileName) to try to access the 
    yahoo geocities account may not contain the correct cookies. Either you have typed
    the name incorrectly, or else the cookies within that file have expired.

    You can try running the script 'login-geocities.sh'. This will create a new
    headers file, containing a new set of cookies. This file will be called by default
    'headers.txt'
    eg:  ./login-geocities.sh; ./upload-geocities.sh the-file-to-upload [target-directory] [cooki-file-name] 
         
    If the above does not work then Yahoo may have altered the geocities login process since
    this script was written (august 2003). In this case you will have to modify the 
    script 'login-geocities.sh' in some way.

    No file has been uploaded.
    "
    
    exit 1;
fi


#- Curl syntax for uploading a file using the multipart/form-data encoding
#  curl -F "file=@cooltext1.txt;type=text/plain,file=" -F "yourname=Daniel"  -F "filedescription=Cool

#- The line below worked at least once. Multiple files can be uploaded by seperating the list
#- with commas. 
#
# curl -L -b headers.txt \
#   -F "userfile=@exe-describe.txt;type=text/plain" \
#   -F "op-upload=upload+files"  \
#   -F "directory=" http://geocities.yahoo.com/filemanager/upload > junk.html