/usr/share/plowshare/modules/bitshare.sh is in plowshare-modules 0~git20160124.8a8190d-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 | # Plowshare bitshare.com module
# Copyright (c) 2013 Plowshare team
#
# This file is part of Plowshare.
#
# Plowshare is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Plowshare is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Plowshare. If not, see <http://www.gnu.org/licenses/>.
MODULE_BITSHARE_REGEXP_URL='http://\(www\.\)\?bitshare\.com/'
MODULE_BITSHARE_DOWNLOAD_OPTIONS="
AUTH,a,auth,a=USER:PASSWORD,User account"
MODULE_BITSHARE_DOWNLOAD_RESUME=yes
MODULE_BITSHARE_DOWNLOAD_FINAL_LINK_NEEDS_COOKIE=no
MODULE_BITSHARE_DOWNLOAD_SUCCESSIVE_INTERVAL=
MODULE_BITSHARE_UPLOAD_OPTIONS="
METHOD,,method,s=METHOD,Upload method (openapi or form, default: openapi)
AUTH,a,auth,a=USER:PASSWORD,User account
HASHKEY,,hashkey,s=HASHKEY,Hashkey used in openapi (override -a/--auth)"
MODULE_BITSHARE_UPLOAD_REMOTE_SUPPORT=yes
MODULE_BITSHARE_DELETE_OPTIONS=""
MODULE_BITSHARE_PROBE_OPTIONS=""
# Login to bitshare (HTML form)
# $1: authentication
# $2: cookie file
bitshare_login() {
local AUTH=$1
local COOKIE_FILE=$2
local LOGIN
post_login "$AUTH" "$COOKIE_FILE" \
'user=$USER&password=$PASSWORD&rememberlogin=&submit=Login' \
"http://bitshare.com/login.html" -b "$COOKIE_FILE" > /dev/null || return
LOGIN=$(parse_cookie_quiet 'login' < "$COOKIE_FILE")
if test -z "$LOGIN"; then
return $ERR_LOGIN_FAILED
else
log_debug 'successfully logged in'
fi
}
# Output a bitshare file download URL
# $1: cookie file
# $2: bitshare url
# stdout: real file download link
bitshare_download() {
local COOKIEFILE=$1
local URL=$2
local BASE_URL='http://bitshare.com'
local FILE_ID POST_URL WAIT AJAXDL DATA RESPONSE
local NEED_RECAPTCHA FILE_URL FILENAME
FILE_ID=$(echo "$URL" | parse_quiet 'bitshare' 'bitshare\.com/files/\([^/]\+\)/')
if test -z "$FILE_ID"; then
FILE_ID=$(echo "$URL" | parse 'bitshare' 'bitshare\.com/?f=\(.\+\)$') || return
fi
log_debug "file id=$FILE_ID"
POST_URL="$BASE_URL/files-ajax/$FILE_ID/request.html"
# Set website language to english (language_selection=EN)
curl -c "$COOKIEFILE" -o /dev/null "$BASE_URL/?language=EN" || return
# Login
if test "$AUTH"; then
bitshare_login "$AUTH" "$COOKIEFILE" || return
fi
# Add cookie entries: last_file_downloaded, trafficcontrol
RESPONSE=$(curl -i -b "$COOKIEFILE" -c "$COOKIEFILE" "$URL") || return
# Error - File not available
! match 'File not available' "$RESPONSE" || return $ERR_LINK_DEAD
# Download limit
if match "You reached your hourly traffic limit\." "$RESPONSE"; then
WAIT=$(echo "$RESPONSE" | parse '<span id="blocktimecounter">' \
'<span id="blocktimecounter">\([[:digit:]]\+\) seconds\?</span>')
echo $((WAIT))
return $ERR_LINK_TEMP_UNAVAILABLE
elif match "Sorry, you cant download more then [[:digit:]]\+ files\? at time\." "$RESPONSE"; then
return $ERR_LINK_TEMP_UNAVAILABLE
fi
# Note: filename is <h1> tag might be truncated
FILENAME=$(echo "$RESPONSE" | parse 'http://bitshare\.com/files/' \
'value="http://bitshare\.com/files/'"$FILE_ID"'/\(.*\)\.html"') || return
# Premium account direct download
FILE_URL=$(echo "$RESPONSE" | grep_http_header_location_quiet) || return
if [ "$FILE_URL" ]; then
log_debug 'using premium direct download'
echo "$FILE_URL"
echo "$FILENAME"
return
fi
# Add cookie entry: ads_download=1
curl -b "$COOKIEFILE" -c "$COOKIEFILE" -o /dev/null \
"$BASE_URL/getads.html" || return
# Get ajaxdl id
AJAXDL=$(echo "$RESPONSE" | parse 'var ajaxdl = ' \
'var ajaxdl = "\([^"]\+\)";') || return
# Retrieve parameters
# Example: file:60:1
DATA="request=generateID&ajaxid=$AJAXDL"
RESPONSE=$(curl -b "$COOKIEFILE" --referer "$URL" --data "$DATA" \
"$POST_URL") || return
if match '^ERROR' "$RESPONSE"; then
log_error "failed in retrieving parameters: $RESPONSE"
return $ERR_LINK_TEMP_UNAVAILABLE
fi
WAIT=$(echo "$RESPONSE" | parse ':' ':\([[:digit:]]\+\):') || return
NEED_RECAPTCHA=$(echo "$RESPONSE" | parse ':' ':\([^:]\+\)$') || return
if [ "$NEED_RECAPTCHA" -eq 1 ]; then
log_debug 'need recaptcha'
else
log_debug 'no recaptcha needed'
fi
wait $WAIT seconds || return
# ReCaptcha
if [ "$NEED_RECAPTCHA" -eq 1 ]; then
local PUBKEY WCI CHALLENGE WORD ID RECAPTCHA_RESULT
PUBKEY='6LdtjrwSAAAAACepq37DE6GDMp1TxvdbW5ui0rdE'
WCI=$(recaptcha_process $PUBKEY) || return
{ read WORD; read CHALLENGE; read ID; } <<<"$WCI"
DATA="request=validateCaptcha&ajaxid=$AJAXDL&recaptcha_challenge_field=$CHALLENGE&recaptcha_response_field=$WORD"
RECAPTCHA_RESULT=$(curl -b "$COOKIEFILE" --referer "$URL" --data "$DATA" \
"$POST_URL") || return
if ! match '^SUCCESS:\?' "$RECAPTCHA_RESULT"; then
log_error 'Wrong captcha'
captcha_nack $ID
return $ERR_CAPTCHA
fi
captcha_ack $ID
log_debug 'correct captcha'
fi
# Get file url
DATA="request=getDownloadURL&ajaxid=$AJAXDL"
RESPONSE=$(curl -b "$COOKIEFILE" --referer "$URL" --data "$DATA" \
"$POST_URL") || return
if match 'ERROR#' "$RESPONSE"; then
log_error "getting file url fail: $RESPONSE"
return $ERR_LINK_TEMP_UNAVAILABLE
fi
FILE_URL=$(echo "$RESPONSE" | parse 'SUCCESS#' '^SUCCESS#\(.*\)$')
echo "$FILE_URL"
echo "$FILENAME"
}
# Upload a file to bitshare
# Need md5sum or md5 when using the openapi method
# $1: cookie file
# $2: file path or remote url
# $3: remote filename
# stdout: bitshare download link and delete link
bitshare_upload() {
if [ -z "$METHOD" -o "$METHOD" = 'openapi' ]; then
if [ -n "$HASHKEY" ]; then
[ -z "$AUTH" ] || \
log_error 'Both --hashkey & --auth_free are defined. Taking hashkey.'
elif [ -n "$AUTH" ]; then
# Login to openapi
local USER PASSWORD PASSWORD_HASH RESPONSE HASHKEY
split_auth "$AUTH" USER PASSWORD || return
PASSWORD_HASH=$(md5 "$PASSWORD") || return
RESPONSE=$(curl --form-string "user=$USER" \
--form-string "password=$PASSWORD_HASH" \
'http://bitshare.com/api/openapi/login.php') || return
if ! match '^SUCCESS:' "$RESPONSE"; then
return $ERR_LOGIN_FAILED
fi
HASHKEY="${RESPONSE:8}"
log_debug "successful login to openapi as $USER member, hashkey: $HASHKEY"
fi
bitshare_upload_openapi "$HASHKEY" "$2" "$3" || return
elif [ "$METHOD" = form ]; then
if match_remote_url "$2"; then
log_error 'Remote upload is not supported with this method. Use openapi method.'
return $ERR_FATAL
fi
bitshare_upload_form "$AUTH" "$1" "$2" "$3" || return
else
log_error 'Unknow method (check --method parameter)'
return $ERR_FATAL
fi
}
# Upload a file to bitshare using openapi
# Official API: http://bitshare.com/openAPI.html
# $1: hashkey
# $2: file path or remote url
# $3: remote filename
bitshare_upload_openapi() {
local HASHKEY=$1
local FILE=$2
local REMOTE_FILENAME=$3
local UPLOAD_URL='http://bitshare.com/api/openapi/upload.php'
local RESPONSE MAX_SIZE SIZE FILESERVER_URL DOWNLOAD_URL DELETE_URL
if match_remote_url "$FILE"; then
if [ -z "$HASHKEY" ]; then
log_error 'Remote upload requires an account'
return $ERR_LINK_NEED_PERMISSIONS
fi
# Remote url upload
local REMOTE_UPLOAD_KEY
RESPONSE=$(curl --form-string 'action=addRemoteUpload' \
-F "hashkey=$HASHKEY" \
-F "url=$FILE" \
"$UPLOAD_URL") || return
if ! match '^SUCCESS:' "$RESPONSE"; then
log_error "Failed in adding url: $RESPONSE"
return $ERR_FATAL
fi
REMOTE_UPLOAD_KEY="${RESPONSE:8}"
log_debug "remote upload key: $REMOTE_UPLOAD_KEY"
while :; do
wait 60 || return
RESPONSE=$(curl --form-string 'action=remoteUploadStatus' \
-F "hashkey=$HASHKEY" \
-F "key=$REMOTE_UPLOAD_KEY" \
"$UPLOAD_URL") || return
if ! match '^SUCCESS:' "$RESPONSE"; then
log_error "Failed in retrieving upload status: $RESPONSE"
break
fi
RESPONSE=${RESPONSE:8}
if match '^Finished#' "$RESPONSE"; then
local FILE_URL=${RESPONSE:9}
# Do we need to rename file ?
if [ "$REMOTE_FILENAME" != dummy ]; then
local RESPONSE2 FILEID_INT FILEID_URL
UPLOAD_URL='http://bitshare.com/api/openapi/filestructure.php'
RESPONSE2=$(curl --form-string 'action=getfiles' \
--form-string 'mainfolder=0' \
-F "hashkey=$HASHKEY" \
"$UPLOAD_URL") || return
FILEID_URL=$(echo "$FILE_URL" | parse . '/files/\([^/]\+\)')
FILEID_INT=$(echo "$RESPONSE2" | parse_quiet "$FILEID_URL" '^\([^#]\+\)')
if [ -n "$FILEID_INT" ]; then
RESPONSE2=$(curl --form-string 'action=renamefile' \
-F "hashkey=$HASHKEY" \
-F "name=$REMOTE_FILENAME" \
-F "file=$FILEID_INT" \
"$UPLOAD_URL") || return
if ! match '^SUCCESS:' "$RESPONSE2"; then
log_error "Failed to rename file: $RESPONSE2"
fi
else
log_debug "can't find file id, cannot rename"
fi
fi
echo "$FILE_URL"
return 0
elif match '^Failed#' "$RESPONSE"; then
log_error "Remote download failed: $RESPONSE"
break
else # Pending, Processing, Downloading
log_debug "status: ${RESPONSE/\#/: }"
fi
done
return $ERR_FATAL
fi
# Get max file size
# RESPONSE=SUCCESS:[max. filesize]#[max. entries]
RESPONSE=$(curl --form-string 'action=maxFileSize' \
-F "hashkey=$HASHKEY" \
"$UPLOAD_URL") || return
if ! match '^SUCCESS:' "$RESPONSE"; then
log_error "Failed in getting max file size: $RESPONSE"
return $ERR_FATAL
fi
RESPONSE=${RESPONSE:8}
MAX_SIZE=${RESPONSE%%#*}
SIZE=$(get_filesize "$FILE")
if [ $SIZE -gt "$MAX_SIZE" ]; then
log_debug "file is bigger than $MAX_SIZE"
return $ERR_SIZE_LIMIT_EXCEEDED
fi
# Get fileserver url
# RESPONSE=SUCCESS:[fileserver url]
RESPONSE=$(curl --form-string 'action=getFileserver' \
"$UPLOAD_URL") || return
if ! match '^SUCCESS:' "$RESPONSE"; then
log_error "Failed in getting file server url: $RESPONSE"
return $ERR_FATAL
fi
FILESERVER_URL="${RESPONSE:8}"
log_debug "file server: $FILESERVER_URL"
# Upload
# RESPONSE=SUCCESS:[downloadlink]#[bblink]#[htmllink]#[shortlink]#[deletelink]
RESPONSE=$(curl_with_log \
-F "hashkey=$HASHKEY" \
-F "filesize=$SIZE" \
-F "file=@$FILE;filename=$REMOTE_FILENAME" \
"$FILESERVER_URL") || return
if ! match '^SUCCESS:' "$RESPONSE"; then
log_error "Failed in uploading: $RESPONSE"
return $ERR_FATAL
fi
DOWNLOAD_URL=$(echo "$RESPONSE" | parse '#' '^SUCCESS:\([^#]\+\)') || return
DELETE_URL=${RESPONSE##*#}
echo "$DOWNLOAD_URL"
echo "$DELETE_URL"
}
# Upload file to bitshare using html form
# $1: authentication
# $2: cookie file
# $3: file path
# $4: remote filename
bitshare_upload_form() {
local AUTH=$1
local COOKIEFILE=$2
local FILE=$3
local REMOTE_FILENAME=$4
local BASE_URL='http://bitshare.com'
local HTML DOWNLOAD_URL DELETE_URL
# Set website language to english (language_selection=EN)
curl -c "$COOKIEFILE" -o /dev/null "$BASE_URL/?language=EN" || return
# Login
if test "$AUTH"; then
bitshare_login "$AUTH" "$COOKIEFILE" || return
fi
HTML=$(curl -b "$COOKIEFILE" -c "$COOKIEFILE" "$BASE_URL") || return
# Get file size
local SIZE MAX_SIZE
MAX_SIZE=$(echo "$HTML" | parse 'Maximum file size' \
'Maximum file size \([[:digit:]]\+\) Mbyte') || return
MAX_SIZE=$((MAX_SIZE*1048576))
SIZE=$(get_filesize "$FILE")
if [ $SIZE -gt $MAX_SIZE ]; then
log_debug "file is bigger than $MAX_SIZE"
return $ERR_SIZE_LIMIT_EXCEEDED
fi
# Extract form parameters
local FORM ACTION PROGRESS_KEY USERGROUP_KEY UPLOAD_IDENTIFIER RESPONSE
FORM=$(grep_form_by_id "$HTML" uploadform) || return
ACTION=$(echo "$FORM" | parse_form_action) || return
PROGRESS_KEY=$(echo "$FORM" | parse_form_input_by_id 'progress_key') || return
USERGROUP_KEY=$(echo "$FORM" | parse_form_input_by_id 'usergroup_key') || return
UPLOAD_IDENTIFIER=$(echo "$FORM" | parse_form_input_by_name 'UPLOAD_IDENTIFIER') || return
# Upload
RESPONSE=$(curl_with_log -L --referer "$BASE_URL/" -b "$COOKIEFILE" \
--form-string APC_UPLOAD_PROGRESS="$PROGRESS_KEY" \
--form-string APC_UPLOAD_USERGROUP="$USERGROUP_KEY" \
--form-string UPLOAD_IDENTIFIER="$UPLOAD_IDENTIFIER" \
-F file[]='@/dev/null;filename=' \
-F file[]="@$FILE;filename=$REMOTE_FILENAME" \
"${ACTION}?X-Progress-ID=undefined$(random h 32)") || return
DOWNLOAD_URL=$(echo "$RESPONSE" | parse \
'<td style="text-align:right">Download:</td>' 'value="\([^"]\+\)"' 1) || return
DELETE_URL=$(echo "$RESPONSE" | parse \
'<td style="text-align:right">Delete link:</td>' 'value="\([^"]\+\)"' 1) || return
echo "$DOWNLOAD_URL"
echo "$DELETE_URL"
}
# Delete a file from Bitshare
# $1: cookie file (unused here)
# $2: bitshare (delete) link
bitshare_delete() {
local -r URL=$2
local PAGE
PAGE=$(curl -b 'language_selection=EN' "$URL") || return
match '<p class="error">Wrong Link</p>' "$PAGE" && return $ERR_LINK_DEAD
# Note: Page tries to show captcha. It's broken, but deletion works anyway.
if match '<h1>Delete File?</h1>' "$PAGE"; then
PAGE=$(curl -d 'sum' -d 'submit=Delete' "$URL") || return
if match '<p class="success">File deleted!</p>' "$PAGE"; then
return 0
fi
fi
log_error 'Unexpected content. Site updated?'
return $ERR_FATAL
}
# Probe a download URL
# $1: cookie file
# $2: bitshare url
# $3: requested capability list
# stdout: 1 capability per line
bitshare_probe() {
local -r COOKIE_FILE=$1
local -r URL=$2
local -r REQ_IN=$3
local PAGE REQ_OUT FILE_NAME FILE_SIZE
PAGE=$(curl -c "$COOKIE_FILE" -b 'language_selection=EN' "$URL") || return
! match 'File not available' "$PAGE" || return $ERR_LINK_DEAD
REQ_OUT=c
# Filename can be truncated
if [[ $REQ_IN = *f* ]]; then
FILE_NAME=$(echo "$PAGE" | parse_tag title)
FILE_NAME=${FILE_NAME#Download }
FILE_NAME=${FILE_NAME% - BitShare.com - Free File Hosting and Cloud Storage}
test "$FILE_NAME" && echo "$FILE_NAME" && REQ_OUT="${REQ_OUT}f"
fi
if [[ $REQ_IN = *s* ]]; then
FILE_SIZE=$(echo "$PAGE" | parse '<h1>' \
'[[:space:]]-[[:space:]]\([[:digit:]]\+\(\.[[:digit:]]\+\)\?[[:space:]][KM]B\)yte') && \
translate_size "$FILE_SIZE" && REQ_OUT="${REQ_OUT}s"
fi
echo $REQ_OUT
}
|