diff options
-rwxr-xr-x | wgetpaste | 356 |
1 files changed, 174 insertions, 182 deletions
@@ -1,54 +1,12 @@ #!/bin/bash # A Script that automates pasting to a number of pastebin services -# relying only on bash, sed, coreutils (mktemp/sort/tr/wc/whoami) and wget +# relying only on bash, sed, coreutils (mktemp/sort/tr/wc/whoami/tee) and wget # Copyright (c) 2007 Bo Ørsted Andresen <bo.andresen@zlin.dk> # Distributed as-is. With no warranties. VERSION="9999" -### helper functions - -die() { - echo "$@" >&2 - exit 1 -} - -# escape % (used for escaping), & (used as separator in POST data), + (used as space in POST data) and space -escape() { - sed -e 's|%|%25|g' -e 's|&|%26|g' -e 's|+|%2b|g' -e 's| |+|g' <<< "$*" -} - - -### default clipboard functions - -noxclip() { - cat <<EOF >&2 -Could not find xclip on your system. In order to use --x$1 you must -either emerge x11-misc/xclip or define x_$1() globally in /etc/wgetpaste -or per user in ~/.wgetpaste to use another program (such as e.g. xcut or -klipper) to $2 your clipboard. - -EOF - exit 1 -} - -x_cut() { - if [[ -x $(type -P xclip) ]]; then - xclip -o - else - noxclip cut "read from" - fi -} - -x_paste() { - if [[ -x $(type -P xclip) ]]; then - xclip -loops 10 <<< "$*" - else - noxclip paste "write to" - fi -} - -### escape and new line characters +# escape and new line characters E=$'\e' N=$'\n' @@ -110,8 +68,166 @@ REGEX_RAW_osl='s|^\(http://[^/]\+/\)\([0-9]\+\)$|\1pastebin.php?dl=\2|' # sh POST_sh="% poster % % % % code" -### usage +### errors +die() { + echo "$@" >&2 + exit 1 +} + +noargument() { + die "$0: option $1 requires an argument" +} +notreadable() { + die "The input source: \"$1\" is not readable. Please specify a readable input source." +} + +noxclip() { + cat <<EOF >&2 +Could not find xclip on your system. In order to use --x$1 you must +either emerge x11-misc/xclip or define x_$1() globally in /etc/wgetpaste +or per user in ~/.wgetpaste to use another program (such as e.g. xcut or +klipper) to $2 your clipboard. + +EOF + exit 1 +} + +### conversions + +# escape % (used for escaping), & (used as separator in POST data), + (used as space in POST data) and space +escape() { + sed -e 's|%|%25|g' -e 's|&|%26|g' -e 's|+|%2b|g' -e 's| |+|g' <<< "$*" +} + +# if possible convert URL to raw +converttoraw() { + local regex + regex=REGEX_RAW_$ENGINE + if [[ -n ${!regex} ]]; then + RAWURL=$(sed -e "${!regex}" <<< "$URL") + set +x + return 0 + fi + echo "Raw download of pastes is not supported by $(getrecipient)." >&2 + return 1 +} + +### verification +verifyservice() { + for s in $SERVICES; do + [[ $s == $* ]] && return 0 + done + echo "\"$*\" is not a supported service.$N" >&2 + showservices >&2 + exit 1 +} + +verifylanguage() { + local i j l lang count v values + lang=LANGUAGES_$ENGINE + count=LANGUAGE_COUNT_$ENGINE + values=LANGUAGE_VALUES_$ENGINE + if [[ -n ${!lang} ]]; then + ((i=0)) + for l in ${!lang}; do + if [[ $LANGUAGE == ${l//%/ } ]]; then + if [[ -n ${!count} ]]; then + ((LANGUAGE=i+1)) + elif [[ -n ${!values} ]]; then + ((j=0)) + for v in ${!values}; do + [[ i -eq j ]] && LANGUAGE=${v//%/ } && break + ((j++)) + done + fi + return 0 + fi + ((i++)) + done + else + [[ $LANGUAGESET = 0 ]] || return 0 + fi + echo "\"$LANGUAGE\" is not a supported language for $(getrecipient).$N" >&2 + showlanguages >&2 + exit 1 +} + +verifyexpiration() { + local i j e expiration count v values + expiration=EXPIRATIONS_$ENGINE + count=EXPIRATION_COUNT_$ENGINE + values=EXPIRATION_VALUES_$ENGINE + if [[ -n ${!expiration} ]]; then + ((i=0)) + for e in ${!expiration}; do + if [[ ${EXPIRATION} == ${e//%/ } ]]; then + if [[ -n ${!count} ]]; then + ((EXPIRATION=i+1)) + elif [[ -n {!values} ]]; then + ((j=0)) + for v in ${!values}; do + [[ i -eq j ]] && EXPIRATION=${e//%/ } && break + ((j++)) + done + fi + return 0 + fi + ((i++)) + done + else + [[ $EXPIRATIONSET = 0 ]] || return 0 + fi + echo "\"$EXPIRATION\" is not a supported expiration option for $(getrecipient).$N" >&2 + showexpirations >&2 + exit 1 +} + +# verify that the pastebin service did not return a known error url. otherwise print a helpful error message +verifyurl() { + dieifknown() { + [[ -n ${!1%% *} && ${!1%% *} == $URL ]] && die "${!1#* }" + } + local t + for t in ${!TOO*}; do + [[ $t == TOO*_$SERVICE ]] && dieifknown "$t" + done +} + +# print a warning if failure is predictable due to the mere size of the paste. sh seems to be the most reliable +# service in that regard. note that this is only a warning printed. it does not abort. +warnings() { + warn() { + if [[ -n $2 && $1 -gt $2 ]]; then + echo "Pasting > $3 often tend to fail with $SERVICE. Use --verbose or --debug to see the" + echo "error output from wget if it fails. Alternatively use another pastebin service like e.g. sh." + fi + } + local size lines + size=SIZE_$SERVICE + warn "$SIZE" "${!size% *}" "${!size#* }" + lines=LINES_$SERVICE + warn "$LINES" "${!lines}" "${!lines} lines" +} + +### input +getfilenames() { + for f in "$@"; do + [[ -f $f ]] || die "$0: $f No such file found." + SOURCE="files" + FILES[${#FILES[*]}]="$f" + done +} + +x_cut() { + if [[ -x $(type -P xclip) ]]; then + xclip -o + else + noxclip cut "read from" + fi +} + +### output usage() { cat <<EOF Usage: $0 [options] [file[s]] @@ -148,8 +264,6 @@ or ~/.wgetpaste.d/*.bash. EOF } -### show functions - showservices() { echo "Services supported: (case sensitive):" local max s IND INDV engine url d @@ -199,80 +313,22 @@ showexpirations() { done } -### verify functions - -verifyservice() { - for s in $SERVICES; do - [[ $s == $* ]] && return 0 - done - echo "\"$*\" is not a supported service.$N" >&2 - showservices >&2 - exit 1 -} - -verifylanguage() { - local i j l lang count v values - lang=LANGUAGES_$ENGINE - count=LANGUAGE_COUNT_$ENGINE - values=LANGUAGE_VALUES_$ENGINE - if [[ -n ${!lang} ]]; then - ((i=0)) - for l in ${!lang}; do - if [[ $LANGUAGE == ${l//%/ } ]]; then - if [[ -n ${!count} ]]; then - ((LANGUAGE=i+1)) - elif [[ -n ${!values} ]]; then - ((j=0)) - for v in ${!values}; do - [[ i -eq j ]] && LANGUAGE=${v//%/ } && break - ((j++)) - done - fi - return 0 - fi - ((i++)) - done - else - [[ $LANGUAGESET = 0 ]] || return 0 - fi - echo "\"$LANGUAGE\" is not a supported language for $(getrecipient).$N" >&2 - showlanguages >&2 - exit 1 +showurl() { + echo "Your ${2}paste can be seen here: $1" + [[ $XPASTE ]] && x_paste "$1" } -verifyexpiration() { - local i j e expiration count v values - expiration=EXPIRATIONS_$ENGINE - count=EXPIRATION_COUNT_$ENGINE - values=EXPIRATION_VALUES_$ENGINE - if [[ -n ${!expiration} ]]; then - ((i=0)) - for e in ${!expiration}; do - if [[ ${EXPIRATION} == ${e//%/ } ]]; then - if [[ -n ${!count} ]]; then - ((EXPIRATION=i+1)) - elif [[ -n {!values} ]]; then - ((j=0)) - for v in ${!values}; do - [[ i -eq j ]] && EXPIRATION=${e//%/ } && break - ((j++)) - done - fi - return 0 - fi - ((i++)) - done +x_paste() { + if [[ -x $(type -P xclip) ]]; then + xclip -loops 10 <<< "$*" else - [[ $EXPIRATIONSET = 0 ]] || return 0 + noxclip paste "write to" fi - echo "\"$EXPIRATION\" is not a supported expiration option for $(getrecipient).$N" >&2 - showexpirations >&2 - exit 1 } ### Posting helper functions -# get the url to post to for any given service +# get the url to post to getrecipient() { local urls target serv for s in $SERVICES; do @@ -290,22 +346,7 @@ getrecipient() { die "Failed to get url for \"$SERVICE\"." } -# print a warning if failure is predictable due to the mere size of the paste. sh seems to be the most reliable -# service in that regard. note that this is only a warning printed. it does not abort. -warnings() { - warn() { - if [[ -n $2 && $1 -gt $2 ]]; then - echo "Pasting > $3 often tend to fail with $SERVICE. Use --verbose or --debug to see the" - echo "error output from wget if it fails. Alternatively use another pastebin service like e.g. sh." - fi - } - local size lines - size=SIZE_$SERVICE - lines=LINES_$SERVICE - warn "$SIZE" "${!size% *}" "${!size#* }" - warn "$LINES" "${!lines}" "${!lines} lines" -} - +# generate POST data postdata() { local post nr extra f post=POST_$ENGINE @@ -326,6 +367,7 @@ postdata() { e "${!post#$extra }" NICK DESCRIPTION LANGUAGE EXPIRATION CVT_TABS INPUT } +# get url from response from server geturl() { local regex regex=REGEX_URL_$ENGINE @@ -338,35 +380,6 @@ geturl() { fi } -# verify that the pastebin service did not return a known error url. otherwise print a helpful error message -verifyurl() { - dieifknown() { - [[ -n ${!1%% *} && ${!1%% *} == $URL ]] && die "${!1#* }" - } - local t - for t in ${!TOO*}; do - [[ $t == TOO*_$SERVICE ]] && dieifknown "$t" - done -} - -showurl() { - echo "Your ${2}paste can be seen here: $1" - [[ $XPASTE ]] && x_paste "$1" -} - -# if possible convert URL to raw -converttoraw() { - local regex - regex=REGEX_RAW_$ENGINE - if [[ -n ${!regex} ]]; then - RAWURL=$(sed -e "${!regex}" <<< "$URL") - set +x - return 0 - fi - echo "Raw download of pastes is not supported by $(getrecipient)." >&2 - return 1 -} - ### read cli options # separate groups of short options. replace --foo=bar with --foo bar @@ -403,18 +416,6 @@ done # set the separated options as input options. set -- "${ARGS[@]}" -noargument() { - die "$0: option $1 requires an argument" -} - -getfilenames() { - for f in "$@"; do - [[ -f $f ]] || die "$0: $f No such file found." - SOURCE="files" - FILES[${#FILES[*]}]="$f" - done -} - while [[ -n $1 ]]; do case "$1" in -- ) @@ -524,13 +525,6 @@ DEFAULT_NICK=${DEFAULT_NICK:-$(whoami)} DEFAULT_SERVICE=${DEFAULT_SERVICE:-rafb} DEFAULT_LANGUAGE=${DEFAULT_LANGUAGE:-Plain Text} DEFAULT_EXPIRATION=${DEFAULT_EXPIRATION:-1 month} - -# show usage if requested -[[ $USAGE ]] && usage && exit 0 -# show services if requested (need to honour --verbose) -[[ $SHOWSERVICES ]] && showservices && exit 0 - -# enable defaults SERVICE=${SERVICE:-${DEFAULT_SERVICE}} ENGINE=ENGINE_$SERVICE ENGINE="${!ENGINE}" @@ -541,7 +535,9 @@ NICK=${NICK:-$(escape "${DEFAULT_NICK}")} [[ -z $SOURCE ]] && SOURCE="stdin" && FILES[${#FILES[*]}]="/dev/stdin" CVT_TABS=No -# show languages or expirations if requested (after the right service and default have been selected) +# show listings if requested +[[ $USAGE ]] && usage && exit 0 +[[ $SHOWSERVICES ]] && showservices && exit 0 [[ $LISTLANGUAGES ]] && showlanguages && exit 0 [[ $LISTEXPIRATION ]] && showexpirations && exit 0 @@ -550,7 +546,6 @@ CVT_TABS=No LANGUAGE=${LANGUAGE:-${DEFAULT_LANGUAGE}} verifylanguage LANGUAGE=$(escape "$LANGUAGE") - EXPIRATION=${EXPIRATION:-${DEFAULT_EXPIRATION}} verifyexpiration EXPIRATION=$(escape "$EXPIRATION") @@ -615,9 +610,6 @@ case "$SOURCE" in fi ;; files | stdin ) - notreadable() { - die "The input source: \"$1\" is not readable. Please specify a readable input source." - } if [[ ${#FILES[*]} -gt 1 ]]; then for f in "${FILES[@]}"; do [[ -r $f ]] || notreadable "$f" |