#!/bin/bash # # nopaste -- paste to pastebin.com # # Copyright 2005,2007,2009,2010 Aron Griffis # Released under the GNU General Public License v2 # nopaste_version=3.1 pastebin_url=${PASTEBIN_URL:-http://pastebin.com} main() { declare opt_title= declare opt_email=$PASTEBIN_EMAIL declare opt_language=text declare opt_download=false declare opt_raw=false declare opt_xcut=false declare opt_private=false declare opt_subdomain= declare expire=N # set by opt_expire() declare shortopts=( e:expire l:language m:email t:title x:xcut ) declare -a parsed_opts parsed_params parse_cmdline "$@" || exit set -- "${parsed_params[@]}" declare -a urls if [[ $# -gt 0 ]]; then declare f u for f; do if [[ ! -r $f ]]; then echo "nopaste: can't read $f" >&2 exit 1 fi u=$(docurl -F "paste_code=<$f") || exit urls+=( "$u" ) done else declare buf if $opt_xcut; then buf=$(get_x_clipboard) || exit 1 exec <<<"$buf" fi urls=( "$(docurl -F 'paste_code=<-')" ) || exit fi put_x_clipboard <<<"${urls[*]}" printf "%s\n" "${urls[@]}" } docurl() { declare u u=$(curl -0 --silent --show-error \ --form-string "paste_name=$opt_title" \ --form-string "paste_email=$opt_email" \ --form-string "paste_format=$opt_language" \ --form-string "paste_expire_date=$expire" \ --form-string "paste_private=$($opt_private && echo 1 || echo 0)" \ --form-string "paste_subdomain=$opt_subdomain" \ "$@" "$pastebin_url/api_public.php") || exit 1 if [[ -z $u ]]; then echo "nopaste: server returned null" >&2 exit 1 elif [[ $u != http://* ]]; then echo "nopaste: server returned unexpected result:" >&2 echo "$u" >&2 exit 1 fi if $opt_download; then u="${u%/*}/download.php?i=${u##*/}" elif $opt_raw; then u="${u%/*}/raw.php?i=${u##*/}" fi echo "$u" } get_x_clipboard() { if type -P xclip &>/dev/null; then xclip -o elif type -P xcut &>/dev/null; then xcut -p else echo "nopaste: xclip or xcut required for --xcut" >&2 return 1 fi } put_x_clipboard() { xclip 2>/dev/null || xcut 2>/dev/null } opt_expire:() { case $1 in [FfNn]*) expire=N ;; # never/forever 10[Mm]*) expire=10M ;; # 10 minutes 1[Hh]*|[Hh]*) expire=1H ;; # 1 hour 1[Dd]*|[Dd]*) expire=1D ;; # 1 day 1[Mm]*|[Mm]*) expire=1M ;; # 1 month *) echo "nopaste: invalid expiration" >&2; exit 1 ;; esac } opt_help() { usage; exit; } opt_help_lang() { curl -s $pastebin_url/api.php | grep ABAP | grep -o '[^;]* = [^<]*' exit ${PIPESTATUS[0]} } opt_version() { echo "nopaste $nopaste_version"; exit; } usage() { cat <<'EOT' usage: nopaste [options] [files] -e --expire WHEN Set expiration, N/10M/1H/1D/1M (defaults to N) -l --language LANG Set language, defaults to text -m --email EMAIL Address for confirmation, honors PASTE_EMAIL --private Make the paste private --subdomain STR Use subdomain -t --title STR Set paste title -x --xcut Paste from X selection (using xclip or xcut) --download Return download URL --raw Return raw URL --help Show this help --help-lang Show list of languages --version Show version info EOT } #============================================================================= # simple bash command-line processing # # (c) Copyright 2008 Aron Griffis # Released under the GNU GPL v2 #============================================================================= parse_cmdline() { # extract long options from variable declarations declare getopt_long=$(set | \ sed '/^opt_/!d; s/^opt_//; s/_/-/g; s/\(.*\)=false$/\1 no-\1/; s/\(.*\)=true$/\1 no-\1/; s/=.*/:/; s/()//;' | xargs | sed 's/ /,/g') # augment the shortopts array with takes-a-value colon; # for example f:file becomes f::file declare shortopts=( "${shortopts[@]}" ) declare i x for ((i=0; i<${#shortopts[@]}; i++)); do x=${shortopts[i]} if [[ ",$getopt_long," == *,"${x#?:}":,* ]]; then shortopts[i]=${x/:/::} fi done declare getopt_short=$(IFS=''; echo "${shortopts[*]%:*}") declare args args=$(getopt -o "$getopt_short" \ --long "$getopt_long" -n "$0" -- "$@") || return eval set -- "$args" declare opt var val parsed_opts=() while true; do [[ $1 == -- ]] && { shift; break; } # translate short options to long if [[ $1 == -? ]]; then opt=${1#-} for x in "${shortopts[@]}"; do if [[ $x == "$opt":* ]]; then opt=${x##*:} break fi done else opt=${1#--} fi # figure out $var and $val; shift positional params var=opt_${opt//-/_} case ",$getopt_long," in # make sure to handle opt_no_something (--no-something) # which has a (silly) negation of --no-no-something *",no-$opt,"*) val=true parsed_opts=( "${parsed_opts[@]}" "$1" ) shift ;; *",$opt,"*) if [[ $opt == no-* ]]; then var=${var/no_/} val=false else val=true fi parsed_opts=( "${parsed_opts[@]}" "$1" ) shift ;; *",$opt:,"*) val=$2 parsed_opts=( "${parsed_opts[@]}" "$1" "$2" ) shift 2 ;; *) echo "error processing $1: not in getopt_long?" >&2 return 1 ;; esac if [[ $(type -t "$var") == function ]]; then $var elif [[ $(type -t "$var:") == function ]]; then $var: "$val" elif is_array "$var"; then eval "$var=( \"\${$var[@]}\" $(printf %q "$val") )" elif is_var "$var"; then eval "$var=\$val" else echo "error processing $var: no func/array/var?" >&2 return 1 fi done parsed_params=( "$@" ) } getopt() { declare cmd=${cmd:-${0##*/}} if [[ $(command getopt --help 2>&1) == *--longoptions* ]]; then command getopt "$@" elif [[ $OSTYPE == darwin* ]]; then if [[ ! -x /opt/local/bin/getopt ]]; then echo "Error: $cmd requires GNU getopt" >&2 echo "Please install from http://getopt.darwinports.com/" >&2 exit 1 fi /opt/local/bin/getopt "$@" else echo "Error: $cmd requires GNU getopt" >&2 exit 1 fi } is_var() { declare -p "$1" &>/dev/null } is_array() { set -- $(declare -p "$1" 2>/dev/null) [[ $2 == -*a* ]] } main "$@"