blob: a0590eb46f055a79737294ff10baf23d33ca065c (
plain)
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
|
#!/bin/sh
#----------------------------------------------------
# Author: Florian "Bluewind" Pritz <flo@xssn.at>
# Contributor: Moritz Wilhelmy
#
# Licensed under GPLv3
# (see COPYING for full license text)
#
#----------------------------------------------------
# Optional dependency: xclip
#----------------------------------------------------
VERSION="@VERSION@"
DELETE=
EXTENSION=""
GET=
TAR=
COMPRESS=0
DISPLAYHISTORY=
PASTEBIN="http://paste.xinu.at"
WARNSIZE=10485760
CLIPBOARD=""
EXITCODE=0
LIBDIR="@LIBDIR@"
# the calling conventions for stat(1) are highly system dependent
STAT='stat -c %s' # GNU stat(1) is the default since most people have it
case "`uname -s`" in
*BSD) STAT='stat -f %z';;
Minix) STAT='stat -size';;
Darwin) STAT='stat -f %b';;
esac
base64_encode() {
if type base64 2>&1 >/dev/null; then
echo $1 | base64
elif type openssl 2>&1 >/dev/null; then
echo $1 | openssl enc -base64
else
echo "Warning: can't find base64 nor openssl executable" >&2
echo " filename of uploaded file will be set to stdin" >&2
echo "stdin"
fi
}
request_helper() {
mode=$1
url=$2
file=$3
# if available use the external helper, else fall back to calling curl
if [ -x "$LIBDIR/fb-helper" ]; then
$LIBDIR/fb-helper "$mode" "$url" "$file"
else
USERAGENT="fb-client/shell-$VERSION"
CURLOPTS="-# -n -L -A $USERAGENT"
if [ "$mode" = "d" ]; then
CURLOPTS="$CURLOPTS -s"
fi
if [ "$mode" = "u" ]; then
basefilename=`basename -- "$file"`
if [ `$STAT -- "$file"` -eq "0" ] || echo "$basefilename" | grep -F -q ","; then
base64fn="`base64_encode "$basefilename"`"
curl $CURLOPTS -F "file=@-" -F "filename=$base64fn" "$url" < "$file" -o /dev/stdout
else
curl $CURLOPTS -F "file=@$file" "$url" -o /dev/stdout
fi
else
curl $CURLOPTS "$url"
fi
fi
}
require_executable() {
if ! type $1 >/dev/null; then
echo "Error: $1 not found. Please install." >&2
exit 1
fi
}
is_url() {
if echo "$i" | grep -qE "^(f|ht)tp(s)?://.+"; then
return 0
fi
return 1
}
do_tar_upload() {
if [ "$COMPRESS" = "1" ]; then
file="$TMPDIR/upload.tar.gz"
tar -cf - -- "$@" | gzip -c > "$file" || return 1
elif [ "$COMPRESS" = "2" ]; then
file="$TMPDIR/upload.tar.xz"
tar -cf - -- "$@" | xz -c > "$file" || return 1
else
file="$TMPDIR/upload.tar"
tar -cf "$file" -- "$@" || return 1
fi
COMPRESS=0
do_upload "$file" || return 1
}
do_upload() {
local EXTRA=""
file="$1"
basefilename="`basename -- "$file"`"
basedirname="`dirname -- "$file"`"
if [ ! -e "$file" ]; then
echo "Error: File \"$file\" doesn't exist" >&2
return 1
fi
if [ -d "$file" ]; then
cd "$basedirname"
if [ "$COMPRESS" = "1" ]; then
file="$TMPDIR/$basefilename.tar.gz"
tar -cf - -- "$basefilename" | gzip -c > "$file" || return 1
elif [ "$COMPRESS" = "2" ]; then
file="$TMPDIR/$basefilename.tar.xz"
tar -cf - -- "$basefilename" | xz -c > "$file" || return 1
else
file="$TMPDIR/$basefilename.tar"
tar -cf "$file" -- "$basefilename" || return 1
fi
else
if [ "$COMPRESS" = "1" ]; then
gzip -c -- "$file" > "$TMPDIR/$basefilename.gz" || return 1
file="$TMPDIR/$basefilename.gz"
elif [ "$COMPRESS" = "2" ]; then
xz -c -- "$file" > "$TMPDIR/$basefilename.xz" || return 1
file="$TMPDIR/$basefilename.xz"
fi
fi
TMPFILE=`mktemp "$TMPDIR/data.XXXXXX"`
if [ `$STAT -- "$file"` -gt "$WARNSIZE" ]; then
WARNSIZE=`request_helper d "$PASTEBIN/file/get_max_size"`
if [ `$STAT -- "$file"` -gt "$WARNSIZE" ]; then
echo "Warning: Your upload is too big and would be rejected. Maximum size is: $WARNSIZE bytes. Skipping..." >&2
return 1
fi
fi
request_helper u "$PASTEBIN/file/do_upload" "$file" > $TMPFILE || return 1
sed '$d' $TMPFILE >&2
URL=`tail -1 $TMPFILE`"$EXTENSION"
echo $URL
if echo $URL | grep -qE "^https?://"; then
CLIPBOARD="$CLIPBOARD $URL"
fi
}
read_stdin() {
if tty -s; then
echo "^C to exit, ^D to send"
fi
cat > "$1"
}
id_from_arg() {
if echo "$1" | grep -qE "^https?://"; then
echo "$1" | sed -r 's/https?:\/\/[^\/]+\/([^\/]+).*/\1/'
else
echo "$1"
fi
}
help() {
cat <<!
fb-client version $VERSION
usage: [cat |] `basename "$0"` [switches] [options] [<file(s)|ID(s)|folder(s)>]
Upload/nopaste file(s)/stdin to paste.xinu.at and copy URL(s) to clipboard.
~/.netrc: machine paste.xinu.at password PASSWORD
Switches:
-d <ID(s)> delete the IDs
-g <ID(s)> download the IDs and output on stdout (use with care!)
-h this help
-v show the client version
-H display an upload history
Options:
-e <extension> extension for default highlighting (e.g. "diff")
-t upload a tar file containing all files (and directories)
-c compress the file being uploaded with gz or xz if used 2 times
When used in conjunction with -g this decompresses the download
!
}
if ! type getopts >/dev/null 2>&1; then
echo "Error: getopts is not supported by your shell" >&2
exit 1
fi
while getopts "e:gdhHtcv" OPTION; do
case $OPTION in
e) EXTENSION="$OPTARG";;
g) GET=1;;
c) COMPRESS=`expr $COMPRESS + 1`;;
t) TAR=1;;
d) DELETE=1;;
H) DISPLAYHISTORY=1;;
v) echo "$VERSION"; exit 0;;
h|\?) help; exit 0;;
esac
done
shift `expr $OPTIND - 1`
if [ "$COMPRESS" = "1" ]; then
require_executable gzip
elif [ "$COMPRESS" = "2" ]; then
require_executable xz
fi
TMPDIR="`mktemp -dt "fb.XXXXXX"`"
trap "rm -rf '${TMPDIR}'" EXIT TERM
if [ "$DELETE" ] || [ "$GET" ]; then
if [ $# -eq 0 ]; then
echo "Error: no ID specified" >&2
exit 1
fi
for i in "$@"; do
i=$(id_from_arg "$i")
if [ "$DELETE" ]; then
request_helper d "$PASTEBIN/file/delete/$i" || EXITCODE=1
elif [ "$GET" ]; then
if [ "$COMPRESS" = "1" ]; then
request_helper d "$PASTEBIN/$i" | gzip -cd || EXITCODE=1
elif [ "$COMPRESS" = "2" ]; then
request_helper d "$PASTEBIN/$i" | xz -cd || EXITCODE=1
else
request_helper d "$PASTEBIN/$i" || EXITCODE=1
fi
fi
done
elif [ "$DISPLAYHISTORY" ]; then
request_helper d "$PASTEBIN/file/upload_history" || EXITCODE=1
elif [ $# -eq 0 ]; then
if [ "$TAR" ]; then
echo "Error: -t is not supported when operating on stdin" >&2
exit 1
fi
read_stdin "$TMPDIR/stdin"
do_upload "$TMPDIR/stdin" || EXITCODE=1
else
if [ "$TAR" ]; then
HAVE_URL=
for i in "$@"; do
if is_url "$i"; then
HAVE_URL=1
fi
done
if [ "$HAVE_URL" ]; then
# TODO: support -t when passing URLs as arguments
echo "Error: -t is not yet supported when operating on a URL" >&2
exit 1
else
do_tar_upload "$@" || EXITCODE=1
fi
else
for i in "$@"; do
if is_url "$i"; then
cd $TMPDIR
if ! request_helper d "$i" > "`basename "$i"`"; then
EXITCODE=1
continue
fi
for f in *; do
if ! do_upload "$f"; then
EXITCODE=1
fi
rm -f -- "$f"
done
else
do_upload "$i" || EXITCODE=1
fi
done
fi
fi
if [ "$CLIPBOARD" != "" ]; then
type xclip >/dev/null 2>&1 && echo -n $CLIPBOARD | nohup xclip >/dev/null 2>&1
fi
exit $EXITCODE
#vim: set noet:
|