forked from mcrapet/plowshare-modules-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendspace.sh
251 lines (203 loc) · 7.96 KB
/
sendspace.sh
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
# Plowshare sendspace.com module
# Copyright (c) 2010-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_SENDSPACE_REGEXP_URL='https\?://\(www\.\)\?sendspace\.com/\(file\|folder\|delete\)/'
MODULE_SENDSPACE_DOWNLOAD_OPTIONS="
AUTH,a,auth,a=USER:PASSWORD,User account"
MODULE_SENDSPACE_DOWNLOAD_RESUME=yes
MODULE_SENDSPACE_DOWNLOAD_FINAL_LINK_NEEDS_COOKIE=unused
MODULE_SENDSPACE_DOWNLOAD_SUCCESSIVE_INTERVAL=
MODULE_SENDSPACE_UPLOAD_OPTIONS="
AUTH,a,auth,a=USER:PASSWORD,User account
DESCRIPTION,d,description,S=DESCRIPTION,Set file description"
MODULE_SENDSPACE_UPLOAD_REMOTE_SUPPORT=no
MODULE_SENDSPACE_LIST_OPTIONS=""
MODULE_SENDSPACE_LIST_HAS_SUBFOLDERS=yes
MODULE_SENDSPACE_DELETE_OPTIONS=""
MODULE_SENDSPACE_PROBE_OPTIONS=""
# Static function. Proceed with login
# $1: authentication
# $2: cookie file
# $3: base URL
sendspace_login() {
local -r AUTH=$1
local -r COOKIE_FILE=$2
local -r BASE_URL=$3
local LOGIN_DATA STATUS USER
# Note: "remember=on" not needed
LOGIN_DATA='action=login&submit=login&target=%2F&action_type=login&remember=1&username=$USER&password=$PASSWORD'
post_login "$AUTH" "$COOKIE_FILE" "$LOGIN_DATA" \
"$BASE_URL/login.html" -o /dev/null || return
STATUS=$(parse_cookie_quiet 'ssal' < "$COOKIE_FILE")
if [ -z "$STATUS" -o "$STATUS" = 'deleted' ]; then
return $ERR_LOGIN_FAILED
fi
split_auth "$AUTH" USER || return
log_debug "Successfully logged in as member '$USER'"
}
# Output a sendspace file download URL
# $1: cookie file
# $2: sendspace.com url
# stdout: real file download link
sendspace_download() {
local -r COOKIE_FILE=$1
local -r URL=$2
local -r BASE_URL='https://www.sendspace.com'
local PAGE
if match "${BASE_URL#*/}/folder/" "$URL"; then
log_error 'This is a directory list, use plowlist!'
return $ERR_FATAL
fi
if [ -n "$AUTH" ]; then
sendspace_login "$AUTH" "$COOKIE_FILE" "$BASE_URL" || return
fi
PAGE=$(curl -L -b "$COOKIE_FILE" "$URL") || return
if match '<div class="msg error"' "$PAGE"; then
local ERR=$(echo "$PAGE" | parse_tag 'class="msg error"' 'div') || return
# Sorry, the file you requested is not available.
if match 'file you requested is not available' "$ERR"; then
return $ERR_LINK_DEAD
fi
log_error "Remote error: $ERR"
return $ERR_FATAL
fi
# parse and output URL and file name
echo "$PAGE" | parse_attr 'download_button' 'href' || return
echo "$PAGE" | parse_tag 'class="bgray"' 'b' || return
}
# Upload a file to sendspace.com
# $1: cookie file
# $2: input file (with full path)
# $3: remote filename
# stdout: sendspace.com download + delete link
sendspace_upload() {
local -r COOKIE_FILE=$1
local -r FILE=$2
local -r DEST_FILE=$3
local -r BASE_URL='https://www.sendspace.com'
local PAGE SIZE MAXSIZE OPT_USER OPT_FOLDER
local FORM_HTML FORM_URL FORM_PROG_URL FORM_DEST_DIR FORM_SIG FORM_MAIL
if [ -n "$AUTH" ]; then
sendspace_login "$AUTH" "$COOKIE_FILE" "$BASE_URL" || return
fi
PAGE=$(curl -b "$COOKIE_FILE" "$BASE_URL") || return
FORM_HTML=$(grep_form_by_order "$PAGE" -1 | break_html_lines_alt) || return
FORM_URL=$(echo "$FORM_HTML" | parse_form_action) || return
MAXSIZE=$(echo "$FORM_URL" | parse . 'MAX_FILE_SIZE=\([[:digit:]]\+\)') || return
# File size limit check
local SIZE=$(get_filesize "$FILE") || return
if [ "$SIZE" -gt "$MAXSIZE" ]; then
log_debug "File is bigger than $MAXSIZE"
return $ERR_SIZE_LIMIT_EXCEEDED
fi
FORM_PROG_URL=$(echo "$FORM_HTML" | parse_form_input_by_name 'PROGRESS_URL') || return
FORM_SIG=$(echo "$FORM_HTML" | parse_form_input_by_name 'signature') || return
if [ -n "$AUTH" ]; then
local FORM_USER
FORM_MAIL=$(echo "$FORM_HTML" | parse_form_input_by_name 'ownemail') || return
FORM_USER=$(echo "$FORM_HTML" | parse_form_input_by_name 'userid') || return
OPT_USER="-F userid=$FORM_USER"
OPT_FOLDER='-F folder_id=0'
fi
PAGE=$(curl_with_log -b "$COOKIE_FILE" \
-F "PROGRESS_URL=$FORM_PROG_URL" \
-F 'js_enabled=1' \
-F "signature=$FORM_SIG" \
-F 'upload_files=' \
$OPT_USER \
-F 'terms=1' \
-F 'file[]=' \
--form-string "description[]=$DESCRIPTION" \
-F "upload_file[]=@$FILE;filename=$DEST_FILE" \
$OPT_FOLDER \
-F '[email protected]' \
-F "ownemail=$FORM_MAIL" \
-F 'recpemail=' \
"$FORM_URL") || return
if [ -z "$PAGE" ] || match '403 Forbidden Request' "$PAGE"; then
log_error 'Upload unsuccessful. Site updated?'
return $ERR_FATAL
fi
# Parse and output download and delete link
echo "$PAGE" | parse_attr 'share link.>' 'href' || return
echo "$PAGE" | parse_attr '/delete/' 'href' || return
}
# Delete a file on sendspace
# $1: cookie file (unused here)
# $2: delete link
sendspace_delete() {
local URL=$2
local PAGE FORM_HTML FORM_URL FORM_SUBMIT
PAGE=$(curl "$URL") || return
if match 'You are about to delete the folowing file' "$PAGE"; then
FORM_HTML=$(grep_form_by_order "$PAGE" 3)
FORM_URL=$(echo "$FORM_HTML" | parse_form_action)
FORM_SUBMIT=$(echo "$FORM_HTML" | parse_form_input_by_name_quiet 'delete')
PAGE=$(curl -F "submit=$FORM_SUBMIT" $FORM_URL) || return
if ! match 'file has been successfully deleted' "$PAGE"; then
return $ERR_FATAL
fi
# Error, the deletion code you provided is incorrect or incomplete. Please make sure to use the full link.
else
log_error 'Bad deletion code'
return $ERR_FATAL
fi
return 0
}
# List a sendspace shared folder
# $1: sendspace folder URL
# $2: recurse subfolders (null string means not selected)
# stdout: list of links (file and/or folder)
sendspace_list() {
local URL=$1
local PAGE LINKS NAMES
if ! match 'sendspace\.com/folder/' "$URL"; then
log_error 'This is not a directory list'
return $ERR_FATAL
fi
test "$2" && log_error "Recursive flag not implemented, ignoring"
PAGE=$(curl "$URL") || return
LINKS=$(echo "$PAGE" | parse_all_attr_quiet 'class="dl" align="center' href)
NAMES=$(echo "$PAGE" | parse_all_attr_quiet 'class="dl" align="center' title)
list_submit "$LINKS" "$NAMES" || return
}
# Probe a download URL
# $1: cookie file
# $2: sendspace url
# $3: requested capability list
# stdout: 1 capability per line
sendspace_probe() {
local -r COOKIE_FILE=$1
local -r URL=$2
local -r REQ_IN=$3
local PAGE REQ_OUT FILE_SIZE
PAGE=$(curl -L -c "$COOKIE_FILE" "$URL") || return
if match '<div class="msg error"' "$PAGE"; then
return $ERR_LINK_DEAD
fi
REQ_OUT=c
if [[ $REQ_IN = *f* ]]; then
parse_tag '"bgray"' b <<< "$PAGE" && \
REQ_OUT="${REQ_OUT}f"
fi
if [[ $REQ_IN = *s* ]]; then
FILE_SIZE=$(echo "$PAGE" | parse '>File Size:<' 'b>\(.*\)</div') && \
translate_size "$FILE_SIZE" && \
REQ_OUT="${REQ_OUT}s"
fi
echo $REQ_OUT
}