SHARE
TWEET

Plowshare bug patch

a guest Sep 19th, 2011 151 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. #
  3. # megaupload.com module
  4. # Copyright (c) 2010-2011 Plowshare team
  5. #
  6. # This file is part of Plowshare.
  7. #
  8. # Plowshare is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # Plowshare is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with Plowshare.  If not, see <http://www.gnu.org/licenses/>.
  20.  
  21. MODULE_MEGAUPLOAD_REGEXP_URL="http://\(www\.\)\?mega\(upload\|rotic\|porn\|video\).com/"
  22.  
  23. MODULE_MEGAUPLOAD_DOWNLOAD_OPTIONS="
  24. AUTH,a:,auth:,USER:PASSWORD,Free-membership or Premium account
  25. LINK_PASSWORD,p:,link-password:,PASSWORD,Used in password-protected files"
  26. MODULE_MEGAUPLOAD_DOWNLOAD_RESUME=yes
  27. MODULE_MEGAUPLOAD_DOWNLOAD_FINAL_LINK_NEEDS_COOKIE=no
  28.  
  29. MODULE_MEGAUPLOAD_UPLOAD_OPTIONS="
  30. MULTIFETCH,m,multifetch,,Use URL multifetch upload
  31. CLEAR_LOG,,clear-log,,Clear upload log after upload process
  32. AUTH,a:,auth:,USER:PASSWORD,Use a free-membership or Premium account
  33. LINK_PASSWORD,p:,link-password:,PASSWORD,Protect a link with a password (premium only)
  34. DESCRIPTION,d:,description:,DESCRIPTION,Set file description
  35. FROMEMAIL,,email-from:,EMAIL,<From> field for notification email
  36. TOEMAIL,,email-to:,EMAIL,<To> field for notification email
  37. MULTIEMAIL,,multiemail:,EMAIL1[;EMAIL2;...],List of emails to notify upload"
  38. MODULE_MEGAUPLOAD_DELETE_OPTIONS="
  39. AUTH,a:,auth:,USER:PASSWORD,Login to free or Premium account (required)"
  40. MODULE_MEGAUPLOAD_LIST_OPTIONS=""
  41.  
  42. # Output a megaupload file download URL
  43. # $1: cookie file
  44. # $2: megaupload (or similar) url
  45. # stdout: real file download link
  46. megaupload_download() {
  47.     eval "$(process_options megaupload "$MODULE_MEGAUPLOAD_DOWNLOAD_OPTIONS" "$@")"
  48.  
  49.     local COOKIEFILE="$1"
  50.     local URL=$(echo "$2" | replace 'rotic.com/' 'porn.com/' | \
  51.                             replace 'video.com/' 'upload.com/')
  52.     local BASEURL=$(basename_url "$URL")
  53.     local ERRORURL="http://www.megaupload.com/?c=msg"
  54.  
  55.     # Arbitrary wait (local variable)
  56.     NO_FREE_SLOT_IDLE=125
  57.  
  58.     # Try to login (if $AUTH not null)
  59.     if [ -n "$AUTH" ]; then
  60.         LOGIN_DATA='login=1&redir=1&username=$USER&password=$PASSWORD'
  61.         post_login "$AUTH" "$COOKIEFILE" "$LOGIN_DATA" "$BASEURL/?c=login" >/dev/null || return
  62.     fi
  63.  
  64.     # Clean the query string
  65.     URL="$BASEURL/?$(match_capture '[?|&](d=[A-Z0-9]{8})' $URL)"
  66.    
  67.     echo "$URL" | grep -q "\.com/?d=" ||
  68.         URL=$(curl -I "$URL" | grep_http_header_location)
  69.        
  70.     TRY=0
  71.     while retry_limit_not_reached || return; do
  72.         TRY=$(($TRY + 1))
  73.         log_debug "Downloading waiting page (loop $TRY)"
  74.         PAGE=$(curl -b "$COOKIEFILE" "$URL") || { echo "Error getting page: $URL"; return 1; }
  75.  
  76.         # Test for Premium account with "direct download" option
  77.         if [ -z "$PAGE" ]; then
  78.             curl -i -b "$COOKIEFILE" "$URL" | grep_http_header_location
  79.             return 0
  80.         fi
  81.  
  82.         REDIRECT=$(echo "$PAGE" | parse_quiet "document.location" \
  83.             "location[[:space:]]*=[[:space:]]*[\"']\(.*\)[\"']" || true)
  84.  
  85.         if test "$REDIRECT" = "$ERRORURL"; then
  86.             log_debug "Server returned an error page: $REDIRECT"
  87.             WAITTIME=$(curl "$REDIRECT" | parse 'check back in' \
  88.                 'check back in \([[:digit:]]\+\) minute')
  89.             # Fragile parsing, set a default waittime if something went wrong
  90.             test ! -z "$WAITTIME" -a "$WAITTIME" -ge 1 -a "$WAITTIME" -le 20 ||
  91.                 WAITTIME=2
  92.             wait $WAITTIME minutes || return
  93.             continue
  94.  
  95.         # Check for dead link
  96.         elif match 'link you have clicked is not available' "$PAGE"; then
  97.             return $ERR_LINK_DEAD
  98.  
  99.         # Test for big files (premium account required)
  100.         elif match "The file you are trying to download is larger than" "$PAGE"; then
  101.             log_debug "Premium link"
  102.             test "$CHECK_LINK" && return 0
  103.             return $ERR_LINK_NEED_PERMISSIONS
  104.  
  105.         # Test if the file is password protected
  106.         elif match 'name="filepassword"' "$PAGE"; then
  107.             test "$CHECK_LINK" && return 0
  108.  
  109.             log_debug "File is password protected"
  110.  
  111.             if [ -z "$LINK_PASSWORD" ]; then
  112.                 LINK_PASSWORD=$(prompt_for_password) || return
  113.             fi
  114.  
  115.             DATA="filepassword=$LINK_PASSWORD"
  116.  
  117.             # We must save HTTP headers to detect premium account
  118.             # (expect "HTTP/1.1 302 Found" return header)
  119.             PAGE=$(curl -i -b "$COOKIEFILE" -d "$DATA" "$URL")
  120.             HTTPCODE=$(echo "$PAGE" | sed -ne '1s/HTTP\/[^ ]*\s\(...\).*/\1/p')
  121.  
  122.             # Premium account with "direct download" option
  123.             if [ "$HTTPCODE"  = "302" ]; then
  124.                 echo "$PAGE" | grep_http_header_location
  125.                 return 0
  126.             fi
  127.  
  128.             match 'name="filepassword"' "$PAGE" &&
  129.                 { log_error "Link password incorrect"; return 1; }
  130.  
  131.             if [ -z "$AUTH" ]; then
  132.                 WAITTIME=$(echo "$PAGE" | parse_quiet "^[[:space:]]*count=" \
  133.                     "count=\([[:digit:]]\+\);") || return
  134.                 break
  135.             fi
  136.  
  137.         # Test for "come back later". Language is guessed with the help of http-user-agent.
  138.         elif match 'file you are trying to access is temporarily unavailable' "$PAGE"; then
  139.             no_arbitrary_wait || return
  140.             wait $NO_FREE_SLOT_IDLE seconds || return
  141.             continue
  142.         fi
  143.  
  144.         # ---
  145.  
  146.         test "$CHECK_LINK" && return 0
  147.  
  148.         # Test for Premium account without "direct download" option
  149.         ACC=$(curl -b $COOKIEFILE "$BASEURL/?c=account")
  150.  
  151.         if ! match '<b>Regular</b>' "$ACC" && test "$AUTH"; then
  152.             FILEURL=$(echo "$PAGE" | parse_attr 'class="down_ad_butt1"' 'href')
  153.             echo "$FILEURL"
  154.             return 0
  155.         fi
  156.  
  157.         # Look for a download link (anonymous & Free account)
  158.         FILEURL=$(echo "$PAGE" | parse_attr 'id="downloadlink"' 'href' 2>/dev/null)
  159.         if test "$FILEURL"; then
  160.             WAITTIME=$(echo "$PAGE" | parse_quiet "^[[:space:]]*count=" \
  161.                 "count=\([[:digit:]]\+\);") || return
  162.             break
  163.         fi
  164.  
  165.         # There's no more captcha on megaupload!
  166.         log_error "unknown state, site updated?"
  167.         return $ERR_FATAL
  168.     done
  169.  
  170.     FILEURL=$(echo "$PAGE" | parse_attr 'id="downloadlink"' 'href')
  171.     if [ -z "$FILEURL" ]; then
  172.         log_error "Can't parse filename (unexpected characters?)"
  173.         return 1
  174.     fi
  175.  
  176.     wait $((WAITTIME+1)) seconds || return
  177.  
  178.     echo "$FILEURL"
  179. }
  180.  
  181. # Upload a file to megaupload
  182. # $1: cookie file
  183. # $2: input file (with full path)
  184. # $3 (optional): alternate remote filename
  185. # stdout: download link on megaupload
  186. megaupload_upload() {
  187.     eval "$(process_options megaupload "$MODULE_MEGAUPLOAD_UPLOAD_OPTIONS" "$@")"
  188.  
  189.     local COOKIEFILE="$1"
  190.     local FILE="$2"
  191.     local DESTFILE=${3:-$FILE}
  192.     local LOGINURL="http://www.megaupload.com/?c=login"
  193.     local BASEURL=$(basename_url "$LOGINURL")
  194.  
  195.     if [ -n "$AUTH" ]; then
  196.         LOGIN_DATA='login=1&redir=1&username=$USER&password=$PASSWORD'
  197.         post_login "$AUTH" "$COOKIEFILE" "$LOGIN_DATA" "$LOGINURL" >/dev/null || return
  198.     elif [ -n "$LINK_PASSWORD" ]; then
  199.         log_error "password ignored, premium only"
  200.     fi
  201.  
  202.     if [ "$MULTIFETCH" ]; then
  203.         UPLOADURL="http://www.megaupload.com/?c=multifetch"
  204.         STATUSURL="http://www.megaupload.com/?c=multifetch&s=transferstatus"
  205.         STATUSLOOPTIME=5
  206.  
  207.         # Cookie file must contain sessionid
  208.         [ -s "$COOKIEFILE" ] || return $ERR_LOGIN_FAILED
  209.  
  210.         log_debug "spawn URL fetch process: $FILE"
  211.         UPLOADID=$(curl -b "$COOKIEFILE" -L \
  212.             -F "fetchurl=$FILE" \
  213.             -F "description=$DESCRIPTION" \
  214.             -F "youremail=$FROMEMAIL" \
  215.             -F "receiveremail=$TOEMAIL" \
  216.             -F "password=$LINK_PASSWORD" \
  217.             -F "multiplerecipients=$MULTIEMAIL" \
  218.             "$UPLOADURL"| parse "estimated_" 'id="estimated_\([[:digit:]]*\)' ) ||
  219.                 { log_error "cannot start multifetch upload"; return 1; }
  220.         while true; do
  221.             CSS="display:[[:space:]]*none"
  222.             STATUS=$(curl -b "$COOKIEFILE" "$STATUSURL")
  223.             ERROR=$(echo "$STATUS" | grep -v "$CSS" | \
  224.                 parse_quiet "status_$UPLOADID" '>\(.*\)<\/div>' | xargs) || true
  225.             test "$ERROR" && { log_error "Status reported error: $ERROR"; break; }
  226.             echo "$STATUS" | grep "completed_$UPLOADID" | grep -q "$CSS" || break
  227.             INFO=$(echo "$STATUS" | parse "estimated_$UPLOADID" \
  228.                 "estimated_$UPLOADID\">\(.*\)<\/div>" | xargs)
  229.             log_debug "waiting for the upload $UPLOADID to finish: $INFO"
  230.             sleep $STATUSLOOPTIME
  231.         done
  232.         log_debug "fetching process finished"
  233.         STATUS=$(curl -b "$COOKIEFILE" "$STATUSURL")
  234.         if [ "$CLEAR_LOG" ]; then
  235.             log_debug "clearing upload log for task $UPLOADID"
  236.             CLEARURL=$(echo "$STATUS" | parse "cancel=$UPLOADID" "href=[\"']\([^\"']*\)")
  237.             log_debug "clear URL: $BASEURL/$CLEARURL"
  238.             curl -b "$COOKIEFILE" "$BASEURL/$CLEARURL" > /dev/null
  239.         fi
  240.         echo "$STATUS" | parse "downloadurl_$UPLOADID" "href=[\"']\([^\"']*\)"
  241.  
  242.     else
  243.         UPLOADURL="http://www.megaupload.com/multiupload/"
  244.         log_debug "downloading upload page: $UPLOADURL"
  245.  
  246.         local PAGE=$(curl "$UPLOADURL")
  247.         local FORM_URL=$(grep_form_by_name "$PAGE" 'uploadform' | parse_form_action)
  248.         local UPLOAD_ID=$(echo "$FORM_URL" | parse 'IDENTIFIER' '=\(.*\)')
  249.  
  250.         log_debug "starting file upload: $FILE"
  251.  
  252.         curl_with_log -b "$COOKIEFILE" \
  253.             -F "UPLOAD_IDENTIFIER=$UPLOAD_ID" \
  254.             -F "sessionid=$UPLOAD_ID" \
  255.             -F "file=@$FILE;filename=$(basename_file "$DESTFILE")" \
  256.             -F "message=$DESCRIPTION" \
  257.             -F "toemail=$TOEMAIL" \
  258.             -F "fromemail=$FROMEMAIL" \
  259.             -F "password=$LINK_PASSWORD" \
  260.             -F "multiemail=$MULTIEMAIL" \
  261.             "$FORM_URL" | parse "downloadurl" "url = '\(.*\)';"
  262.     fi
  263. }
  264.  
  265. # Delete a file on megaupload (requires an account)
  266. # $1: delete link
  267. megaupload_delete() {
  268.     eval "$(process_options megaupload "$MODULE_MEGAUPLOAD_DELETE_OPTIONS" "$@")"
  269.  
  270.     local URL=$1
  271.     local BASE_URL=$(basename_url $URL)
  272.  
  273.     if ! test "$AUTH"; then
  274.         log_error "Anonymous users cannot delete links."
  275.         return $ERR_LINK_NEED_PERMISSIONS
  276.     fi
  277.  
  278.     COOKIES=$(create_tempfile)
  279.     LOGIN_DATA='login=1&redir=1&username=$USER&password=$PASSWORD'
  280.     post_login "$AUTH" "$COOKIES" "$LOGIN_DATA" $BASE_URL"/?c=login" >/dev/null || {
  281.         rm -f $COOKIES
  282.         return $ERR_FATAL
  283.     }
  284.  
  285.     TOTAL_FILES=$(curl -b $COOKIES $BASE_URL"/?c=account" | \
  286.         parse_all '<strong>' '<strong>*\([^<]*\)' | nth_line 3)
  287.  
  288.     FILEID=$(echo "$URL" | parse "." "d=\(.*\)")
  289.     DATA="action=delete&delids=$FILEID"
  290.     DELETE=$(curl -b $COOKIES -d "$DATA" $BASE_URL"/?c=filemanager&ajax=1") || return
  291.  
  292.     rm -f $COOKIES
  293.  
  294.     FILES=$(echo "$DELETE" | parse_quiet 'totalfiles' 'totalfiles":"\(.*\)","noresults":')
  295.  
  296.     if [ $TOTAL_FILES -eq $FILES ]; then
  297.         log_error "error deleting link"
  298.         return $ERR_FATAL
  299.     fi
  300. }
  301.  
  302. # List a megaupload shared file folder URL
  303. # $1: megaupload folder url (http://www.megaupload.com/?f=...)
  304. # stdout: list of links
  305. megaupload_list() {
  306.     eval "$(process_options megaupload "$MODULE_MEGAUPLOAD_LIST_OPTIONS" "$@")"
  307.  
  308.     local URL=$1
  309.     local XMLURL="http://www.megaupload.com/xml/folderfiles.php"
  310.     local XML FOLDERID
  311.  
  312.     FOLDERID=$(echo "$URL" | parse '.' 'f=\([^=]\+\)') || return
  313.     XML=$(curl "$XMLURL/?folderid=$FOLDERID") || return
  314.  
  315.     if match "<FILES></FILES>" "$XML"; then
  316.         log_debug "empty folder"
  317.         return 0
  318.     fi
  319.  
  320.     echo "$XML" | parse_all_attr "<ROW" "url"
  321. }
RAW Paste Data
Pastebin PRO Autumn Special!
Get 40% OFF on Pastebin PRO accounts!
Top