-
Notifications
You must be signed in to change notification settings - Fork 2
/
pastebin
executable file
·143 lines (114 loc) · 3.12 KB
/
pastebin
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
#!/usr/bin/env bash
set -o errexit # exit on fail
set -o pipefail # catch errors in pipelines
set -o nounset # exit on undeclared variable
# set -o xtrace # trace execution
__pastebin=$(basename "$0")
declare -A __messages
__messages[help]="
Save document to first available pastebin-like service.
Usage: ${__pastebin} [OPTIONS] [FILE]
Options:
-h, --help Print this message
-x, --xclip Insert link to clipboard
-e, --encrypt Encrypt with GPG
Examples:
\$ ${__pastebin} ~/.profile
\$ ${__pastebin} < ~/.profile
"
__messages[no_args]="
No arguments provided.
Use pastebin --help to see available options.
"
__messages[no_xclip]="
No xclip found in \$PATH. Check if it is installed.
Link was NOT copied to the clipboard.
"
for key in "${!__messages[@]}"; do
__messages[${key}]=${__messages[${key}]:1:-1}
done
declare -A __services=(
['https://brpaste.xyz']='data=<-'
['https://0x0.st']='file=@-'
['https://clbin.com']='clbin=<-'
['https://clbin.com']='clbin=<-'
['https://ttm.sh']='file=@-'
['http://sprunge.us']='sprunge=<-'
['http://ix.io']='f:1=<-'
)
declare -A __args=(
[file]='/dev/stdin'
[xclip]=''
[gpg]=''
)
__tmp_file=$(mktemp /tmp/XXXXXXXX)
trap 'rm ${__tmp_file}' EXIT
# If no arguments provided - just quit
if [[ $# == 0 ]]; then
echo "${__messages[no_args]}"
exit
fi
# Parsing arguments
while [[ "$#" != 0 ]]; do
# Parse current argument
case "$1" in
-h | --help)
echo "${__messages[help]}"
exit
;;
-x | --xclip)
__args[xclip]=true
shift
;;
-e | --encrypt)
__args[gpg]=true
shift
;;
-- )
[[ "$#" != 1 ]] && __args[file]=$2
break
;;
-*)
echo "Unrecognized option."
exit 1
;;
*)
__args[file]=$1
shift
;;
esac
done
if [[ ${__args[file]} != /dev/stdin ]] && ! tty --silent; then
echo "Provide either stdin stream OR file argument."
exit 1
fi
# Copy input file to a temporary file
cp -- "${__args[file]}" "${__tmp_file}"
# Encrypt file and replace with encrypted version
if [[ ${__args[gpg]} ]]; then
gpg --symmetric --armor -- "${__tmp_file}"
mv -- "${__tmp_file}.asc" "${__tmp_file}"
fi
# Upload file and print download link
for url in "${!__services[@]}"; do
form=${__services[${url}]}
data=$(curl --write-out '\n%{http_code}' --silent --form "${form}" "${url}" <"${__tmp_file}" || true)
code=${data##*$'\n'}
if ((200 <= code && code < 300)); then
read -r lnk < <(echo "${data}")
[[ ${url} == *brpaste.xyz* ]] && lnk=https://brpaste.xyz/${lnk}
echo "Download link: $(tput bold)${lnk}$(tput sgr0)"
break
fi
done
# Print gpg usage help, so that user could decrypt document easily
if [[ ${__args[gpg]} ]]; then
echo "Get file with: $(tput bold)curl ${lnk} | gpg --decrypt -$(tput sgr0)"
fi
# Copy link to the clipboard if possible
if [[ ${__args[xclip]} ]]; then
if ! type xclip &>/dev/null; then
echo "${__messages[no_xclip]}"
fi
printf "%s" "${lnk}" | xclip -selection clipboard
fi