-
Notifications
You must be signed in to change notification settings - Fork 0
/
stikked.sh
executable file
·183 lines (162 loc) · 4.75 KB
/
stikked.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
#!/bin/bash
notice() {
echo "$*" 1>&2
}
die() {
notice "$*"
exit 1
}
check_conf_val() {
if [[ -z "$2" || $2 =~ [[:cntrl:]]|[[:space:]] ]]; then
die "Bad $1 read from '$CONFIG' file."
fi
}
check_lang() {
curl -s ${APIURL/\/create/\/langs} | jq -r 'keys_unsorted[]' | grep -wq $1
}
for CONFIG in /etc/stikked/stikkedrc ${HOME}/.stikked; do
if [[ -s "$CONFIG" ]]; then
BASEURL=$(awk -F '=' '$1 == "base_url" { print $2 }' $CONFIG)
APIKEY=$(awk -F '=' '$1 == "api_key" { print $2 }' $CONFIG)
EXPIRE=$(awk -F '=' '$1 == "expire" && $2 ~ /^[0-9]+$/ { print $2 }' $CONFIG)
PRIVATE=$(awk -F '=' '$1 == "private" && $2 ~ /^[01]$/ { print $2 }' $CONFIG)
STRIP=$(awk -F '=' '$1 == "strip_url" && $2 ~ /^[yn]$/ { print $2 }' $CONFIG)
check_conf_val 'server url' "$BASEURL"
if [[ "${BASEURL:${#BASEURL}-1}" != '/' ]]; then
BASEURL="${BASEURL}/"
fi
APIURL="${BASEURL}api/create"
if [[ -n "$APIKEY" ]]; then
check_conf_val 'API key' "$APIKEY"
APIURL="${APIURL}?apikey=${APIKEY}"
fi
fi
done
if [[ -z "$BASEURL" ]]; then
die "Please create '${HOME}/.stikked' file with your settings."
fi
for TOOL in tr mktemp file perl curl jq; do
if [[ -z "$(which $TOOL)" ]]; then
die "Please install '$TOOL' to make ${0##*/} work."
fi
done
unset DATA DEXT LNG XCLIP XCLIPMSG
if [[ -n "$DISPLAY" ]]; then
XCLIP=$(which xclip)
if [[ -n "$XCLIP" ]]; then
XCLIPMSG=" (copied to clipboard)"
else
XCLIPMSG=" (consider installing xclip to have links copied to clipboard)"
fi
fi
while [[ $# -gt 0 ]]; do
if [[ $1 =~ ^(-p|-f|--permanent|--forever)$ ]]; then
EXPIRE=0
elif [[ $1 == '--private' ]]; then
PRIVATE='1'
elif [[ $1 =~ ^(-l|--lang(uage)?)$ ]]; then
check_lang $2 || die "Unsupported language: $2"
LNG=$2
shift
elif [[ $1 =~ ^(-t|-e|--exp(ire)?)$ ]]; then
if [[ $2 =~ ^([0-9]+|burn)$ ]]; then
EXPIRE=$2
else
die "Bad expiration value: '$2'"
fi
shift
elif [[ $1 == '--noclip' ]]; then
XCLIP=''
XCLIPMSG=''
elif [[ $1 =~ ^- ]]; then
die "Bad argument: $1"
elif [[ -r "$1" ]]; then
DATA="$1"
elif [[ $1 == 'js' ]]; then
LNG='javascript'
else
if check_lang $1; then
LNG=$1
else
die "Bad argument: $1"
fi
fi
shift
done
if [[ -n "$DATA" ]]; then
if [[ ${DATA##*/} =~ \. ]]; then
DEXT=${DATA##*.}
fi
else
TMPF=$(mktemp)
if [[ -z "$TMPF" || ! -w "$TMPF" ]]; then
die "Failed to create temp file. Please make sure 'mktemp' works."
fi
trap "rm -f '$TMPF'" EXIT
cat 1>"$TMPF"
DATA="$TMPF"
fi
if [[ -n "$DEXT" ]]; then
case "$DEXT" in
(go|php|diff|sql|css|lua|c|cpp|xml|awk|ini|java|reg|vim|tcl|xpp) LNG="$DEXT";;
(sh) LNG='bash';;
(pl|pm) LNG='perl';;
(js) LNG='javascript';;
(py) LNG='python';;
(nsi) LNG='nsis';;
(pas|pp) LNG='pascal';;
(cmd|bat) LNG='dos';;
(htm|html|xhtml) LNG='html5';;
(txt|log) LNG='text';;
(patch) LNG='diff';;
(h) LNG='c';;
(hpp) LNG='cpp';;
esac
fi
if [[ -z "$LNG" ]]; then
CT=$(file -Lb --mime-type "$DATA")
case "$CT" in
(text/x-shellscript) LNG='bash';;
(text/x-perl) LNG='perl';;
(text/x-python) LNG='python';;
(text/x-pascal) LNG='pascal';;
(text/html) LNG='html5';;
(text/plain) LNG='text';;
(text/x-php) LNG='php';;
(text/x-diff) LNG='diff';;
(text/xml) LNG='xml';;
(text/x-c++) LNG='cpp';;
(text/x-objective-c) LNG='objc';;
(text/x-c) LNG='c';;
(text/x-lisp) LNG='lisp';;
(text/x-ruby) LNG='ruby';;
(text/x-lua) LNG='lua';;
(text/x-tcl) LNG='tcl';;
(text/x-asm) LNG='asm';;
(text/x-makefile) LNG='make';;
(text/x-msdos-batch) LNG='dos';;
esac
fi
URL=$(tr -d "\r" 0<"$DATA" | perl -pe 'chomp if eof' | curl -s -d lang=${LNG:-text} -d private=${PRIVATE:-1} ${EXPIRE+-d expire=$EXPIRE} --data-urlencode text@- "$APIURL")
if [[ ! $? -eq 0 ]]; then
die "Failed to fetch URL."
fi
if [[ $URL =~ value=.*${BASEURL} ]]; then
URL=$(grep -m 1 "value=" <<< "$URL" | sed 's/.*value="//g' | sed 's/".*//g')
EXPIRE=burn
fi
if [[ ! $URL =~ ^${BASEURL} ]]; then
die "Fail: $URL"
fi
if [[ $STRIP =~ ^(y|yes)$ ]]; then
URL=${URL/\/view/}
fi
if [[ -n "$XCLIP" ]]; then
printf "%s" "$URL" | xclip -selection clipboard
fi
if [[ "$EXPIRE" == 'burn' ]]; then
EXPIREMSG=" (will be deleted after first read)"
else
unset EXPIREMSG
fi
echo "${URL}${EXPIREMSG}${XCLIPMSG}"