diff options
-rwxr-xr-x | wgetpaste | 149 |
1 files changed, 98 insertions, 51 deletions
@@ -1,86 +1,133 @@ #!/bin/bash -# A Script that automates pasting to pastebin(s).. -# based on http://floyd-n-milan.blogspot.com/2006/11/bash-script-to-post-on-pastebins.html -# but uses wget instead of curl and supports only rafb.net -###################################################################### +# A Script that automates pasting to a number of pastebin services +# relying only on bash v3, sed, coreutils (mktemp/sort/wc/whoami) and wget +# Author: Bo Ørsted Andresen, bo.andresen@zlin.dk +########################################################################## -showusage() { - cat 1>&2 <<EOF -Usage: $0 [-n nick] [-l language] [-d description] [-f source] [-h|--help|help] +VERSION="1.9" - -n nick set nick (defaults to your username) - -l language set language (defaults to "Plain Text") - -d description set description (defaults to "stdin" or filename) - -f source set source (defaults to stdin) +### helper functions - Languages supported: - "C", "C89", "C99", "C++", "C#", "Java", "Pascal", "Perl", - "PHP", "PL/I", "Python", "Ruby", "SQL", "VB", "Plain Text" -EOF +# show an error message and die +die() { + echo "$*" 1>&2 + exit 1 } + +# escape % (used for escaping), & (used as separator in POST data), + (used as space in POST data) and space escape() { echo "$*" | sed -e 's|%|%25|g' -e 's|&|%26|g' -e 's|+|%2b|g' -e 's| |+|g' } -while getopts ":n:l:d:f:h" opt; do - case $opt in - n) - nick="${OPTARG:=$(whoami)}" - ;; +show_usage() { + echo "Usage: $0 [options] [file]" + echo + echo "Options:" + echo " -l, --language LANG set language (defaults to \"Plain Text\")" + echo " -d, --description DESCRIPTION set description (defaults to \"stdin\" or filename)" + echo " -n, --nick NICK set nick (defaults to your username))" + echo + echo " --debug be *very* verbose (implies -v)" + echo + echo " -h, --help show this help" + echo " --version show version information" + echo + echo "Languages supported:" + echo ' "C", "C89", "C99", "C++", "C#", "Java", "Pascal", "Perl",' + echo ' "PHP", "PL/I", "Python", "Ruby", "SQL", "VB", "Plain Text"' +} - l) - lang="${OPTARG:="Plain Text"}" - ;; +### read cli options - d) - desc="${OPTARG:="shpost Post"}" +# if you know a better option (like getopts) which provides the same features +# please let me know how. +while [[ ! -z "${1}" ]]; do + case "${1}" in + --debug ) + DEBUG=true + set -x + shift ;; - - f) - from="${OPTARG:=/dev/stdin}" + --description=* ) + [[ -z "${1#*=}" ]] && show_usage 1>&2 && exit 1 + DESCRIPTION="$(escape "${1#*=}")" + shift ;; - - h) - showusage + -d | --description ) + [[ -z "${2}" ]] && show_usage 1>&2 && exit 1 + DESCRIPTION="$(escape "${2}")" + shift 2 + ;; + -h | --help ) + show_usage exit 0 ;; - - ?) - showusage - exit 64 #E_WRONGARGS (What's that?) + --language=* ) + [[ -z "${1#*=}" ]] && show_usage 1>&2 && exit 1 + LANGUAGE="${1#*=}" + shift + ;; + -l | --language ) + [[ -z "${2}" ]] && show_usage 1>&2 && exit 1 + LANGUAGE="${2}" + shift 2 + ;; + --nick=* ) + [[ -z "${1#*=}" ]] && show_usage 1>&2 && exit 1 + NICK="$(escape "${1#*=}")" + shift + ;; + -n | --nick ) + [[ -z "${2}" ]] && show_usage 1>&2 && exit 1 + NICK="$(escape "${2}")" + shift 2 + ;; + --version ) + echo "$0, version ${VERSION}" + exit 0 + ;; + *) + if [[ -f "${1}" ]]; then + SOURCE="${1}" + shift + else + show_usage 1>&2 + exit 1 + fi + ;; esac done -from="${from:-/dev/stdin}" +SOURCE="${SOURCE:-/dev/stdin}" -if [[ ! -r "${from}" ]]; then - echo "The input source: \"${from}\" is not readable. Please specify a readable input source with -f. Aborting." +if [[ ! -r "${SOURCE}" ]]; then + echo "The input source: \"${SOURCE}\" is not readable. Please specify a readable input source with -f. Aborting." exit 1 fi -input="$(escape "$(<${from})")" -nick="${nick:-"$(whoami)"}" -lang="${lang:-"Plain Text"}" -if [[ "${from}" == "/dev/stdin" ]]; then - desc="${desc:-"stdin"}" +INPUT="$(escape "$(<${SOURCE})")" +NICK="${NICK:-"$(whoami)"}" +LANGUAGE="${LANGUAGE:-"Plain Text"}" +if [[ "${SOURCE}" == "/dev/stdin" ]]; then + DESCRIPTION="${DESCRIPTION:-"stdin"}" else - desc="${desc:-${from}}" + DESCRIPTION="${DESCRIPTION:-${SOURCE}}" fi -nick=$(escape "${nick}") -lang=$(escape "${lang}") -desc=$(escape "${desc}") +NICK=$(escape "${NICK}") +LANGUAGE=$(escape "${LANGUAGE}") +DESCRIPTION=$(escape "${DESCRIPTION}") -url=$(wget -O - --timeout=10 --post-data="lang=${lang}&nick=${nick}&desc=${desc}&cvt_tabs=2&text=${input}" http://rafb.net/paste/paste.php 2>&1 | sed -n 's|^.*Location:\ \(http://rafb.net/p[^\ ]\+\).*$|\1|p') +URL=$(wget -O - --timeout=10 --post-data="lang=${LANGUAGE}&nick=${NICK}&desc=${DESCRIPTION}&cvt_tabs=2&text=${INPUT}" http://rafb.net/paste/paste.php 2>&1 | sed -n 's|^.*Location:\ \(http://rafb.net/p[^\ ]\+\).*$|\1|p') -if [[ "${url}" == "http://rafb.net/p/toofast.html" ]]; then +if [[ "${URL}" == "http://rafb.net/p/toofast.html" ]]; then echo "You must wait at least 10 seconds between each paste! Try again in 10 seconds." exit 1 -elif [[ -z "${url}" ]]; then +elif [[ -z "${URL}" ]]; then echo "Nothing received. Perhaps the connection failed. Please try again later." exit 1 fi -echo "Your paste can be seen here: ${url}" +echo "Your paste can be seen here: ${URL}" exit 0 |