forked from librenms/librenms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pbin.sh
executable file
·187 lines (158 loc) · 3.75 KB
/
pbin.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
184
185
186
187
#!/usr/bin/env bash
# Pastes from STDIN or file from commandline argument to Stikked pastebin
# Default server: paste.mate-desktop.org
# URL: https://github.com/glensc/pbin
# Copyright (C) 2014 Elan Ruusamäe
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
set -e
PROGRAM=${0##*/}
# can be overriden from env
: "${PASTE_URL=https://p.libren.ms/api/create}"
# paste. take input from stdin
pastebin() {
# show params
if [[ "${expire}" == "" ]]; then
expire=0;
fi
if [[ "${private}" == "" ]]; then
private=1;
fi
sed -e '/^$/d' >&2 <<-EOF
${title+title: "$title"}
${name+name: "$name"}
${private+private: "$private"}
${language+language: "$language"}
${expire+expire: "$expire"}
${reply+reply: "$reply"}
EOF
# do paste
curl "$PASTE_URL" \
${title+-F title="$title"} \
${name+-F name="$name"} \
${private+-F private="$private"} \
${language+-F lang="$language"} \
${expire+-F expire="$expire"} \
${reply+-F reply="$reply"} \
-F 'text=<-'
}
# try to resolve mime-type to language
mime_to_lang() {
local mime="$1"
awk -F ':' -vm="$mime" 'm==$1 {print $2}' <<-EOF
application/javascript:javascript
application/xml:xml
text/html:html5
text/x-c:c
text/x-c++:cpp
text/x-diff:diff
text/x-lua:lua
text/x-php:php
text/x-python:python
text/x-ruby:ruby
text/x-shellscript:bash
EOF
}
# detect filetype. outputs nothing if no file binary or no detected
detect_mimetype() {
local file="$1" out
out=$(file -L --mime-type "$file" 2>/dev/null || :)
echo "${out#*: }"
}
usage() {
echo "Usage: $PROGRAM [options] < [input_file]
Options:
-t, --title title of this paste
-n, --name author of this paste
-p, --private should this paste be private (0 = public, 1 = private)
-l, --language language this paste is in
-e, --expire paste expiration in minutes
-r, --reply reply to existing paste
"
}
set_defaults() {
unset title
unset private
unset language
unset expire
unset reply
# default to user@hostname
name=${SUDO_USER:-$USER}
}
set_defaults
# parse command line args
t=$(getopt -o h,t:,n:,p:,l:,e:,r:,b: --long help,title:,name:,private:,language:,expire:,reply: -n "$PROGRAM" -- "$@")
eval set -- "$t"
while :; do
case "$1" in
-h|--help)
usage
exit 0
;;
-t|--title)
shift
title="$1"
;;
-n|--name)
shift
name="$1"
;;
-p|--private)
shift
private="$1"
;;
-l|--language)
shift
language="$1"
;;
-e|--expire)
shift
expire="$1"
;;
-r|--reply)
shift
reply="$1"
;;
-b)
shift
PASTE_URL="$1"
;;
--)
shift
break
;;
*)
echo >&2 "$PROGRAM: Internal error: [$1] not recognized!"
exit 1
;;
esac
shift
done
echo "Paste endpoint: $PASTE_URL"
# if we have more commandline arguments, set these as title
if [ "${title+set}" != "set" ]; then
title="$*"
fi
# set language from first filename
if [ -n "$1" ] && [ "${language+set}" != "set" ]; then
mime=$(detect_mimetype "$1")
if [ -n "$mime" ]; then
language=$(mime_to_lang "$mime")
fi
fi
# take filenames from commandline, or from stdin
if [ $# -gt 0 ]; then
paste=$(cat "$@")
else
paste=$(cat)
fi
echo "$paste" | pastebin