Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Added pbin.sh to upload text to p.libren.ms #6175

Merged
merged 2 commits into from Mar 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/General/Acknowledgement.md
Expand Up @@ -30,6 +30,7 @@ LibreNMS 3rd party acknowledgements
- HTML Purifier (http://htmlpurifier.org/): LGPL v2.1
- Symfony Yaml (https://github.com/symfony/yaml): MIT
- PHPMailer (https://github.com/PHPMailer/PHPMailer): LGPL v2.1
- pbin (https://github.com/glensc/pbin): GPLv2 (or later - see script header)

#### 3rd Party GPLv3 Non-compliant

Expand Down
180 changes: 180 additions & 0 deletions pbin.sh
@@ -0,0 +1,180 @@
#!/bin/sh

# 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
sed -e '/^$/d' >&2 <<-EOF
${title+title: "$title"}
${name+name: "$name"}
${private+private: "$private"}
${language+language: "$language"}
${expire+expire: "$expire"}
${reply+reply: "$reply"}
EOF
if [$expire == '']; then
expire=0;
fi

# 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
-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}@${HOSTNAME:-$(hostname)}
}

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)
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