#!/bin/bash # # ompa -- quick script in the spirit of eatpaste, to generate omnipaste urls. # # Copyright 2005,2009,2010 Aron Griffis # Released under the GNU General Public License v2 # ompa_version=2.2 omnipaste_url=${OMNIPASTE_URL:-http://n01se.net/paste/} betapaste_url=${omnipaste_url}betapaste/ main() { declare opt_nick=${OMNIPASTE_USER:-${USER:-$(whoami)}} declare opt_pass=$OMNIPASTE_PASS declare opt_desc= declare opt_mimetype= declare opt_xcut=false declare private # set by opt_private() declare shortopts=( b:beta d:desc m:mimetype n:nick p:pass x:xcut ) declare -a parsed_opts parsed_params parse_cmdline "$@" || exit set -- "${parsed_params[@]}" declare -a urls if [[ $# -gt 0 ]]; then declare f for f; do urls+=( "$(opt_desc=${opt_desc:-$f} \ opt_mimetype=${opt_mimetype:-$(guess_mimetype "$f")} \ docurl -F "nugget!=@$f")" ) done else declare buf if $opt_xcut; then buf=$(get_x_clipboard) || exit 1 : ${opt_desc:=xcut} else read -rd '' buf [[ -n $buf ]] || exit 1 : ${opt_desc:=stdin} fi if [[ -z $opt_mimetype ]]; then opt_mimetype=text/plain case $buf in '<'*'>'*) opt_mimetype=text/html ;; http://*|https://*|ftp://*) opt_mimetype=application/x-url ;; esac fi urls=( "$(docurl -F "nugget=<-" <<<"$buf")" ) fi put_x_clipboard <<<"${urls[*]}" printf "%s\n" "${urls[@]}" } docurl() { curl --silent --show-error -o /dev/null -w "%{redirect_url}\n" \ --form-string "username=$opt_nick" --form-string "passwd=$opt_pass" \ --form-string "mimeType=$opt_mimetype" --form-string "descrip=$opt_desc" \ ${private:+--form-string "private=1"} \ -F cmd=create "$@" "$omnipaste_url" | sed 's/.pretty=yes$//' } guess_mimetype() { declare ext=${1##*.} declare db t e for db in ~/.mime.types /etc/mime.types; do [[ -r $db ]] || continue while read t e; do [[ $t == '#'* || $t != */* ]] && continue [[ " $e " == *" $ext "* ]] || continue echo "$t" return 0 done < "$db" done echo text/plain return 1 } get_x_clipboard() { if type -P xclip &>/dev/null; then xclip -o elif type -P xcut &>/dev/null; then xcut -p else echo "ompa: xclip or xcut required for --xcut" >&2 return 1 fi } put_x_clipboard() { xclip 2>/dev/null || xcut 2>/dev/null } opt_beta() { omnipaste_url=$betapaste_url; } opt_private() { private=1; } opt_help() { usage; exit; } opt_version() { echo "ompa $ompa_version"; exit; } usage() { cat <<'EOT' usage: ompa [options] [file] -b --beta Use betapaste instead of production paste -d --desc Set description (defaults to filename) -m --mimetype Set mimetype -n --nick Set nick (honors OMNIPASTE_USER) -p --pass Set password (honors OMNIPASTE_PASS) --private Set private flag -x --xcut Paste from X selection (using xclip or xcut) --help Show this help --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 "$@"