-
Notifications
You must be signed in to change notification settings - Fork 0
/
nopaste
executable file
·252 lines (225 loc) · 7.04 KB
/
nopaste
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
#!/bin/bash
#
# nopaste -- paste to pastebin.com
#
# Copyright 2005,2007,2009,2010 Aron Griffis <[email protected]>
# Released under the GNU General Public License v2
#
nopaste_version=HEAD
pastebin_url=${PASTEBIN_URL:-http://pastebin.com}
main() {
declare opt_title=
declare opt_email=$PASTEBIN_EMAIL
declare opt_language=text
declare opt_download=false
declare opt_raw=false
declare opt_xcut=false
declare opt_private=false
declare opt_subdomain=
declare expire=N # set by opt_expire()
declare shortopts=( e:expire l:language m:email t:title x:xcut )
declare -a parsed_opts parsed_params
parse_cmdline "$@" || exit
set -- "${parsed_params[@]}"
declare -a urls
if [[ $# -gt 0 ]]; then
declare f u
for f; do
if [[ ! -r $f ]]; then
echo "nopaste: can't read $f" >&2
exit 1
fi
u=$(docurl -F "paste_code=<$f") || exit
urls+=( "$u" )
done
else
declare buf
if $opt_xcut; then
buf=$(get_x_clipboard) || exit 1
exec <<<"$buf"
fi
urls=( "$(docurl -F 'paste_code=<-')" ) || exit
fi
put_x_clipboard <<<"${urls[*]}"
printf "%s\n" "${urls[@]}"
}
docurl() {
declare u
u=$(curl -0 --silent --show-error \
--form-string "paste_name=$opt_title" \
--form-string "paste_email=$opt_email" \
--form-string "paste_format=$opt_language" \
--form-string "paste_expire_date=$expire" \
--form-string "paste_private=$($opt_private && echo 1 || echo 0)" \
--form-string "paste_subdomain=$opt_subdomain" \
"$@" "$pastebin_url/api_public.php") || exit 1
if [[ -z $u ]]; then
echo "nopaste: server returned null" >&2
exit 1
elif [[ $u != http://* ]]; then
echo "nopaste: server returned unexpected result:" >&2
echo "$u" >&2
exit 1
fi
if $opt_download; then
u="${u%/*}/download.php?i=${u##*/}"
elif $opt_raw; then
u="${u%/*}/raw.php?i=${u##*/}"
fi
echo "$u"
}
get_x_clipboard() {
if type -P xclip &>/dev/null; then
xclip -o
elif type -P xcut &>/dev/null; then
xcut -p
else
echo "nopaste: xclip or xcut required for --xcut" >&2
return 1
fi
}
put_x_clipboard() {
xclip 2>/dev/null || xcut 2>/dev/null
}
opt_expire:() {
case $1 in
[FfNn]*) expire=N ;; # never/forever
10[Mm]*) expire=10M ;; # 10 minutes
1[Hh]*|[Hh]*) expire=1H ;; # 1 hour
1[Dd]*|[Dd]*) expire=1D ;; # 1 day
1[Mm]*|[Mm]*) expire=1M ;; # 1 month
*) echo "nopaste: invalid expiration" >&2; exit 1 ;;
esac
}
opt_help() { usage; exit; }
opt_help_lang() {
curl -s $pastebin_url/api.php | grep ABAP | grep -o '[^;]* = [^<]*'
exit ${PIPESTATUS[0]}
}
opt_version() { echo "nopaste $nopaste_version"; exit; }
usage() {
cat <<'EOT'
usage: nopaste [options] [files]
-e --expire WHEN Set expiration, N/10M/1H/1D/1M (defaults to N)
-l --language LANG Set language, defaults to text
-m --email EMAIL Address for confirmation, honors PASTE_EMAIL
--private Make the paste private
--subdomain STR Use subdomain
-t --title STR Set paste title
-x --xcut Paste from X selection (using xclip or xcut)
--download Return download URL
--raw Return raw URL
--help Show this help
--help-lang Show list of languages
--version Show version info
EOT
}
#=============================================================================
# simple bash command-line processing
#
# (c) Copyright 2008 Aron Griffis <agriffis n01se.net>
# Released under the GNU GPL v2
#=============================================================================
parse_cmdline() {
# extract long options from variable declarations
declare getopt_long=$(set | \
sed '/^opt_/!d; s/^opt_//; s/_/-/g;
s/\(.*\)=false$/\1 no-\1/;
s/\(.*\)=true$/\1 no-\1/;
s/=.*/:/; s/()//;' | xargs | sed 's/ /,/g')
# augment the shortopts array with takes-a-value colon;
# for example f:file becomes f::file
declare shortopts=( "${shortopts[@]}" )
declare i x
for ((i=0; i<${#shortopts[@]}; i++)); do
x=${shortopts[i]}
if [[ ",$getopt_long," == *,"${x#?:}":,* ]]; then
shortopts[i]=${x/:/::}
fi
done
declare getopt_short=$(IFS=''; echo "${shortopts[*]%:*}")
declare args
args=$(getopt -o "$getopt_short" \
--long "$getopt_long" -n "$0" -- "$@") || return
eval set -- "$args"
declare opt var val
parsed_opts=()
while true; do
[[ $1 == -- ]] && { shift; break; }
# translate short options to long
if [[ $1 == -? ]]; then
opt=${1#-}
for x in "${shortopts[@]}"; do
if [[ $x == "$opt":* ]]; then
opt=${x##*:}
break
fi
done
else
opt=${1#--}
fi
# figure out $var and $val; shift positional params
var=opt_${opt//-/_}
case ",$getopt_long," in
# make sure to handle opt_no_something (--no-something)
# which has a (silly) negation of --no-no-something
*",no-$opt,"*)
val=true
parsed_opts=( "${parsed_opts[@]}" "$1" )
shift ;;
*",$opt,"*)
if [[ $opt == no-* ]]; then
var=${var/no_/}
val=false
else
val=true
fi
parsed_opts=( "${parsed_opts[@]}" "$1" )
shift ;;
*",$opt:,"*)
val=$2
parsed_opts=( "${parsed_opts[@]}" "$1" "$2" )
shift 2 ;;
*)
echo "error processing $1: not in getopt_long?" >&2
return 1 ;;
esac
if [[ $(type -t "$var") == function ]]; then
$var
elif [[ $(type -t "$var:") == function ]]; then
$var: "$val"
elif is_array "$var"; then
eval "$var=( \"\${$var[@]}\" $(printf %q "$val") )"
elif is_var "$var"; then
eval "$var=\$val"
else
echo "error processing $var: no func/array/var?" >&2
return 1
fi
done
parsed_params=( "$@" )
}
getopt() {
declare cmd=${cmd:-${0##*/}}
if [[ $(command getopt --help 2>&1) == *--longoptions* ]]; then
command getopt "$@"
elif [[ $OSTYPE == darwin* ]]; then
if [[ ! -x /opt/local/bin/getopt ]]; then
echo "Error: $cmd requires GNU getopt" >&2
echo "Please install from http://getopt.darwinports.com/" >&2
exit 1
fi
/opt/local/bin/getopt "$@"
else
echo "Error: $cmd requires GNU getopt" >&2
exit 1
fi
}
is_var() {
declare -p "$1" &>/dev/null
}
is_array() {
set -- $(declare -p "$1" 2>/dev/null)
[[ $2 == -*a* ]]
}
main "$@"