forked from SharzyL/pastebin-worker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpb
executable file
·337 lines (280 loc) · 10.1 KB
/
pb
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
#!/usr/bin/env bash
domain="https://shz.al"
hist_file="${XDG_CONFIG_DIR:-$HOME/.config}/pb_hist"
script_name=${0##*/}
_print_with_color() {
color=$1
shift
[ -t 2 ] && printf "\x1b[0;%sm" "$color" >&2
echo -n "$*" >&2
[ -t 2 ] && printf "\x1b[0m\n" >&2
}
_die() {
_print_with_color 31 "$@"
exit 1
}
[ -d "${hist_file%/*}" ] || mkdir -p "${hist_file%/*}" || _die "cannot create directory for '$hist_file'"
touch "$hist_file" || _die "cannot create history file '$hist_file'"
_verbose() {
_print_with_color 90 "$script_name: $*"
}
_clip() {
echo "$@" | xclip -selection clipboard
_verbose "'$*' is copied to your clipboard"
}
curl_code() {
status_code=$(curl -sS -w '%{response_code}' "$@")
if [ "$status_code" != '200' ] && [ "$status_code" != '302' ]; then
return 1
fi
}
main() {
action="$1"
shift
case "$action" in
(p|post ) pb_post "$@";;
(u|update) pb_update "$@";;
(d|delete) pb_delete "$@";;
(g|get ) pb_get "$@";;
(-h|--help) usage;;
(*) _die "no action given. try '$script_name' -h for more information"
esac
}
usage() {
cat <<-EOF
Usage:
${script_name} [-h|--help]
print this help message
${script_name} [p|post] [OPTIONS] [-f] FILE
upload your text to pastebin, if neither 'FILE' and 'CONTENT' are given,
read the paste from stdin.
${script_name} [u|update] NAME[:PASSWD]
Update your text to pastebin, if neither 'FILE' and 'CONTENT' are given,
read the paste from stdin. If 'PASSWD' is not given, try to read password
from the history file.
${script_name} [g|get] [OPTIONS] NAME[.EXT]
fetch the paste with name 'NAME' and extension 'EXT'
${script_name} [d|delete] [OPTIONS] NAME
delete the paste with name 'NAME'
Options:
post options:
-c, --content CONTENT the content of the paste
-e, --expire SECONDS the expiration time of the paste (in seconds)
-n, --name NAME the name of the paste
-s, --passwd PASSWD the password
-p, --private make the generated paste name longer for better privacy
-x, --clip clip the url to the clipboard
update options:
-f, --file FILE read the paste from file
-c, --content CONTENT the content of the paste
-e, --expire SECONDS the expiration time of the paste (in seconds)
-s, --passwd PASSWD the password
-x, --clip clip the url to the clipboard
get options:
-l, --lang LANG highlight the paste with language 'LANG' in a web page
-m, --mime MIME set the 'Content-Type' header according to mime-type MIME
-o, --output FILE output the paste in file 'FILE'
-u, --url make a 302 URL redirection
delete options:
none
general options:
-v, --verbose display the 'underlying' curl command
-d, --dry do a dry run, executing no 'curl' command at all
EOF
}
pb_post() {
# parse args
short_args="c:e:n:s:f:vhpxd"
long_args="content:,expire:,name:,passwd:,file:,verbose,private,clip,dry"
TEMP=$(getopt -o "$short_args" --long "$long_args" -n "$script_name" -- "$@") \
|| return 1
eval set -- "$TEMP";
local content expire name passwd private verbose clip dry
while [[ ${1:0:1} == - ]]; do
[[ $1 =~ ^-f|--|--file$ ]] && {
shift 1;
if [ -n "$1" ]; then file="$1"; shift 1; continue; fi
};
[[ $1 =~ ^-c|--content$ ]] && { content="$2"; shift 2; continue; };
[[ $1 =~ ^-e|--expire$ ]] && { expire="$2"; shift 2; continue; };
[[ $1 =~ ^-n|--name$ ]] && { name="$2"; shift 2; continue; };
[[ $1 =~ ^-s|--passwd$ ]] && { passwd="$2"; shift 2; continue; };
[[ $1 =~ ^-p|--private$ ]] && { private="true"; shift 1; continue; };
[[ $1 =~ ^-v|--verbose$ ]] && { verbose="true"; shift 1; continue; };
[[ $1 =~ ^-x|--clip$ ]] && { clip="true"; shift 1; continue; };
[[ $1 =~ ^-d|--dry$ ]] && { dry="true"; shift 1; continue; };
break;
done
[ "$#" -gt 0 ] && _die "redundant arguments '$*'"
# check arguments
[ -n "$content" ] && [ -n "$file" ] && _die "cannot set both 'content' and 'file'"
[ -n "$file" ] && [ ! -r "$file" ] && _die "cannot read file '${file}'"
[ -n "$private" ] && [ -n "$name" ] && _die "cannot set both 'private' and 'name'"
# build arguments
declare -a args=("curl_code" "-sS" "$domain")
[ -n "$file" ] && args+=("-Fc=@$file")
[ -n "$content" ] && args+=("-Fc=\"$content\"")
[ -n "$expire" ] && args+=("-Fe=\"$expire\"")
[ -n "$name" ] && args+=("-Fn=\"$name\"")
[ -n "$passwd" ] && args+=("-Fs=\"$passwd\"")
[ -n "$private" ] && args+=("-Fp=true")
[ -z "$content" ] && [ -z "$file" ] && args+=("-Fc=@-")
# prepare curl
tmp_file=$(mktemp)
trap 'rm -f "$tmp_file"' EXIT
[ -n "$verbose" ] && _verbose "${args[@]}"
# curl if not dry-run
if [ -z "$dry" ]; then
"${args[@]}" -o "$tmp_file" || _die "$status_code: $(cat "$tmp_file")"
else
return 0
fi
# report error if jq parse error
jq . "$tmp_file"
# record history
admin_url=$(jq -r '.admin' "$tmp_file")
admin_path=${admin_url##*/}
echo "$admin_path" >> "$hist_file"
# copy URL to clipboard
url=$(jq -r '.url' "$tmp_file")
[ -n "$clip" ] && _clip "$url"
return 0
}
pb_update() {
# parse args
short_args="c:e:s:f:vhxd"
long_args="content:,expire:,passwd:,file:,verbose,clip,dry"
TEMP=$(getopt -o "$short_args" --long "$long_args" -n "$script_name" -- "$@") \
|| return 1
eval set -- "$TEMP";
local name_passwd file content expire passwd verbose clip dry
while [[ ${1:0:1} == - ]]; do
[[ $1 == -- ]] && { name_passwd="$2"; shift 2; break; };
[[ $1 =~ ^-f|--file$ ]] && { file="$2"; shift 2; continue; };
[[ $1 =~ ^-c|--content$ ]] && { content="$2"; shift 2; continue; };
[[ $1 =~ ^-e|--expire$ ]] && { expire="$2"; shift 2; continue; };
[[ $1 =~ ^-s|--passwd$ ]] && { passwd="$2"; shift 2; continue; };
[[ $1 =~ ^-v|--verbose$ ]] && { verbose="true"; shift 1; continue; };
[[ $1 =~ ^-x|--clip$ ]] && { clip="true"; shift 1; continue; };
[[ $1 =~ ^-d|--dry$ ]] && { dry="true"; shift 1; continue; };
break;
done
[ "$#" -gt 0 ] && _die "redundant arguments '$*'"
# parse name and passwd
[ -z "$name_passwd" ] && _die "no name and passwd given"
name=${name_passwd%:*}
if [[ "$name_passwd" =~ : ]]; then
passwd=${name_passwd#*:}
else
name_passwd=$(grep "^$name:" "$hist_file" | tail -1) && [ -n "$name_passwd" ] && passwd=${name_passwd#*:}
fi
[ -z "$passwd" ] && _die "no passwd given, and cannot find passwd in history file '$hist_file'"
# check arguments
[ -n "$content" ] && [ -n "$file" ] && _die "cannot set both 'content' and 'file'"
[ -n "$file" ] && [ ! -r "$file" ] && _die "cannot read file '${file}'"
# build arguments
declare -a args=("curl_code" "-X" "PUT" "$domain/$name:$passwd")
[ -n "$file" ] && args+=("-Fc=@$file")
[ -n "$content" ] && args+=("-Fc=\"$content\"")
[ -n "$expire" ] && args+=("-Fe=\"$expire\"")
[ -n "$passwd" ] && args+=("-Fs=\"$passwd\"")
[ -z "$content" ] && [ -z "$file" ] && args+=("-Fc=@-")
# prepare curl
tmp_file=$(mktemp)
trap 'rm -f "$tmp_file"' EXIT
[ -n "$verbose" ] && _verbose "${args[@]}"
# curl if not dry-run
if [ -z "$dry" ]; then
"${args[@]}" -o "$tmp_file" || _die "$status_code: $(cat "$tmp_file")"
else
return 0
fi
# report error if jq parse error
jq . "$tmp_file" 2>/dev/null || _die "$(cat "$tmp_file")"
# record history
admin_url=$(jq -r '.admin' "$tmp_file")
admin_path=${admin_url##*/}
echo "$admin_path" >> "$hist_file"
# copy URL to clipboard
url=$(jq -r '.url' "$tmp_file")
[ -n "$clip" ] && _clip "$url"
return 0
}
pb_get() {
# parse args
short_args="l:m:o::uvd"
long_args="lang:,mime:,output:,url,verbose,dry"
TEMP=$(getopt -o "$short_args" --long "$long_args" -n "$script_name" -- "$@") \
|| return 1
eval set -- "$TEMP";
local name lang mime output url verbose dry
while [[ ${1:0:1} == - ]]; do
[[ $1 == -- ]] && { name="$2"; shift 2; break; };
[[ $1 =~ ^-l|--lang$ ]] && { lang="$2"; shift 2; continue; };
[[ $1 =~ ^-m|--mime$ ]] && { mime="$2"; shift 2; continue; };
[[ $1 =~ ^-o|--output$ ]] && { output="$2"; shift 2; continue; };
[[ $1 =~ ^-u|--url$ ]] && { url="true"; shift 1; continue; };
[[ $1 =~ ^-v|--verbose$ ]] && { verbose="true"; shift 1; continue; };
[[ $1 =~ ^-d|--dry$ ]] && { dry="true"; shift 1; continue; };
break;
done
[ "$#" -gt 1 ] && _die "redundant arguments '$*'"
# check args
[ -z "$name" ] && _die "no paste name is given"
# build arguments
tmp_file=$(mktemp)
trap 'rm -f "$tmp_file"' EXIT
declare -a args=("curl_code" "-G")
[ -n "$url" ] && args+=("$domain/u/$name") || args+=("$domain/$name")
[ -n "$lang" ] && args+=("-d" "lang=$lang")
[ -n "$mime" ] && args+=("-d" "mime=$mime")
[ -n "$output" ] && args+=("-o" "$output") || args+=("-o" "$tmp_file")
# prepare curl
[ -n "$verbose" ] && _verbose "${args[@]}"
# curl if not dry-run
if [ -z "$dry" ]; then
"${args[@]}" || _die "$status_code: $(cat "$tmp_file")"
[ -z "$output" ] && cat "$tmp_file"
else
return 0
fi
return 0
}
pb_delete() {
# parse args
short_args="vd"
long_args="verbose,dry"
TEMP=$(getopt -o "$short_args" --long "$long_args" -n "$script_name" -- "$@") \
|| return 1
eval set -- "$TEMP";
local name_passwd verbose dry
while [[ ${1:0:1} == - ]]; do
[[ $1 == -- ]] && { name_passwd="$2"; shift 2; break; };
[[ $1 =~ ^-v|--verbose$ ]] && { verbose="true"; shift 1; continue; };
[[ $1 =~ ^-d|--dry$ ]] && { dry="true"; shift 1; continue; };
break;
done
[ "$#" -gt 0 ] && _die "redundant arguments '$*'"
# parse name and passwd
[ -z "$name_passwd" ] && _die "no name and passwd given"
name=${name_passwd%:*}
if [[ "$name_passwd" =~ : ]]; then
passwd=${name_passwd#*:}
else
name_passwd=$(grep "^$name:" "$hist_file" | tail -1) && [ -n "$name_passwd" ] && passwd=${name_passwd#*:}
fi
[ -z "$passwd" ] && _die "no passwd given, and cannot find passwd in history file '$hist_file'"
# build arguments
tmp_file=$(mktemp)
trap 'rm -f "$tmp_file"' EXIT
declare -a args=("curl_code" "-X" "DELETE" "$domain/$name:$passwd")
[ -n "$verbose" ] && _verbose "${args[@]}"
# curl if not dry-run
if [ -z "$dry" ]; then
"${args[@]}" -o "$tmp_file" || _die "$status_code: $(cat "$tmp_file")"
else
return 0
fi
return 0
}
main "$@"