Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 13th, 2012  |  syntax: None  |  size: 23.27 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # cat $(which wgetpaste )
  2. #!/usr/bin/env bash
  3. # A Script that automates pasting to a number of pastebin services
  4. # relying only on bash, sed, coreutils (mktemp/sort/tr/wc/whoami/tee) and wget
  5. # Copyright (c) 2007-2009 Bo ��rsted Andresen <bo.andresen@zlin.dk>
  6. # Distributed in the public domain. Do with it whatever you want.
  7.  
  8. VERSION="2.17"
  9.  
  10. # don't inherit LANGUAGE from the env
  11. unset LANGUAGE
  12.  
  13. # escape and new line characters
  14. E=$'\e'
  15. N=$'\n'
  16.  
  17. ### services
  18. SERVICES="ca codepad dpaste osl pocoo"
  19. # ca
  20. ENGINE_ca=ca
  21. URL_ca="http://pastebin.ca/"
  22. SIZE_ca="1024000 1%MB"
  23. # codepad
  24. ENGINE_codepad=codepad
  25. URL_codepad="http://codepad.org/"
  26. SIZE_codepad="64000 64%KB"
  27. # dpaste
  28. ENGINE_dpaste=dpaste
  29. URL_dpaste="http://dpaste.com/"
  30. SIZE_dpaste="25000 25%kB"
  31. DESCRIPTION_SIZE_dpaste="50"
  32. DEFAULT_EXPIRATION_dpaste="30 days"
  33. # osl
  34. ENGINE_osl=osl
  35. URL_osl="http://pastebin.osuosl.org/ pastebin.php"
  36. # pocoo
  37. ENGINE_pocoo=pocoo
  38. URL_pocoo="http://paste.pocoo.org/"
  39. # tinyurl
  40. ENGINE_tinyurl=tinyurl
  41. URL_tinyurl="http://tinyurl.com/ api-create.php"
  42. REGEX_RAW_tinyurl='s|^\(http://[^/]*/\)\([[:alnum:]]*\)$|\1\2|'
  43.  
  44. ### engines
  45. # ca
  46. LANGUAGES_ca="Plain%Text Asterisk%Configuration C C++ PHP Perl Java VB C# Ruby Python Pascal \
  47. mIRC PL/I XML SQL Scheme ActionScript Ada Apache%Configuration Assembly%(NASM) ASP Bash CSS \
  48. Delphi HTML%4.0%Strict JavaScript LISP Lua Microprocessor%ASM Objective%C VB.NET"
  49. LANGUAGE_COUNT_ca=0
  50. EXPIRATIONS_ca="Never 5%minutes 10%minutes 15%minutes 30%minutes 45%minutes 1%hour 2%hours \
  51. 4%hours 8%hours 12%hours 1%day 2%days 3%days 1%week 2%weeks 3%weeks 1%month 2%months \
  52. 3%months 4%months 5%months 6%months 1%year"
  53. POST_ca="s=Submit+Post name description type expiry % content"
  54. REGEX_URL_ca='s|^.*content="[0-9]*;\(http://[^/]*/[0-9]*\)".*$|\1|p'
  55. REGEX_RAW_ca='s|^\(http://[^/]*/\)\([0-9]*\)$|\1raw/\2|'
  56. # codepad
  57. LANGUAGES_codepad="C C++ D Haskell Lua OCaml PHP Perl Plain%Text Python Ruby Scheme Tcl"
  58. POST_codepad="submit % % lang % % code"
  59. REGEX_URL_codepad='s|^--.*\(http://codepad.org/[^ ]\+\)|\1|p'
  60. REGEX_RAW_codepad='s|^\(http://[^/]*/\)\([[:alnum:]]*\)$|\1\2/raw.rb|'
  61. # dpaste
  62. LANGUAGES_dpaste="Plain%Text Apache%Config Bash CSS Diff Django%Template/HTML Haskell JavaScript \
  63. Python Python%Interactive/Traceback Ruby Ruby%HTML%(ERB) SQL XML"
  64. LANGUAGE_VALUES_dpaste="% Apache Bash Css Diff DjangoTemplate Haskell JScript Python PythonConsole \
  65. Ruby Rhtml Sql Xml"
  66. EXPIRATIONS_dpaste="30%days 30%days%after%last%view"
  67. EXPIRATION_VALUES_dpaste="off on"
  68. POST_dpaste="submit=Paste+it poster title language hold % content"
  69. REGEX_RAW_dpaste='s|^\(http://[^/]*/\)[^0-9]*\([0-9]*/\)$|\1\2plain/|'
  70. # osl
  71. LANGUAGES_osl="Plain%Text ActionScript Ada Apache%Log%File AppleScript Assembly%(NASM) \
  72. ASP Bash C C%for%Macs CAD%DCL CAD%Lisp C++ C# ColdFusion CSS D Delphi Diff DOS Eiffel Fortran \
  73. FreeBasic Game%Maker HTML%4.0%Strict INI%file Java Javascript Lisp Lua MatLab Microprocessor%ASM \
  74. MySQL NullSoft%Installer Objective%C OCaml Openoffice.org%BASIC Oracle%8 Pascal Perl PHP Python \
  75. QBasic Robots.txt Ruby Scheme Smarty SQL TCL VB VB.NET VisualFoxPro XML"
  76. LANGUAGE_VALUES_osl="text actionscript ada apache applescript asm asp bash c c_mac caddcl \
  77. cadlisp cpp csharp cfm css d delphi diff dos eiffel fortran freebasic gml html4strict ini java \
  78. javascript lisp lua matlab mpasm mysql nsis objc ocaml oobas oracle8 pascal perl php python \
  79. qbasic robots ruby scheme smarty sql tcl vb vbnet visualfoxpro xml"
  80. EXPIRATIONS_osl="Never 1%day 1%month"
  81. EXPIRATION_VALUES_osl="f d m"
  82. POST_osl="paste=Send poster % format expiry % code2"
  83. REGEX_RAW_osl='s|^\(http://[^/]*/\)\([0-9]*\)$|\1pastebin.php?dl=\2|'
  84. # pocoo
  85. LANGUAGES_pocoo="Apache%Config%(.htaccess) Bash Batch%(.bat) Boo C C# C++ Clojure Creole%Wiki CSS \
  86. CSV D Debian%control-files Django%/%Jinja%Templates Dylan Erlang eRuby%/%rhtml GAS GCC%Messages \
  87. Genshi%Templates Gettext%catalogs GL%Shader%language Haskell HTML INI%File Interactive%Ruby IO \
  88. IRC%Logs Java javac%Messages JavaScript JSP Lighttpd Literate%Haskell LLVM Lua Mako%Templates Matlab \
  89. Matlab%Session MiniD Multi-File Myghty%Templates MySQL Nasm Nginx Object-Pascal OCaml Perl PHP \
  90. PHP%(inline) Povray Python Python%Console%Sessions Python%Tracebacks reStructuredText Ruby Scala \
  91. Scheme Smalltalk Smarty sources.list SQL SquidConf TeX%/%LaTeX Plain%Text Unified%Diff Vim XML XSLT YAML"
  92. LANGUAGE_VALUES_pocoo="apache bash bat boo c csharp cpp clojure creole css csv d control html+django \
  93. dylan erlang rhtml gas gcc-messages html+genshi gettext glsl haskell html ini irb io irc java \
  94. javac-messages js jsp lighttpd literate-haskell llvm lua html+mako matlab matlabsession minid multi \
  95. html+myghty mysql nasm nginx objectpascal ocaml perl html+php php povray python pycon pytb rst ruby \
  96. scala scheme smalltalk smarty sourceslist sql squidconf tex text diff vim xml xslt yaml"
  97. POST_pocoo="submit=Paste! % % language % % code"
  98. REGEX_RAW_pocoo='s|^\(http://[^/]*/\)show\(/[0-9]*/\)$|\1raw\2|'
  99.  
  100. ### errors
  101. die() {
  102.         echo "$@" >&2
  103.         exit 1
  104. }
  105.  
  106. requiredarg() {
  107.         [[ -z $2 ]] && die "$0: option $1 requires an argument"
  108.         ((args++))
  109. }
  110.  
  111. notreadable() {
  112.         die "The input source: \"$1\" is not readable. Please specify a readable input source."
  113. }
  114.  
  115. noxclip() {
  116.         cat <<EOF >&2
  117. Could not find xclip on your system. In order to use --x$1 you must either
  118. emerge x11-misc/xclip or define x_$1() globally in /etc/wgetpaste.conf or
  119. per user in ~/.wgetpaste.conf to use another program (such as e.g. xcut or
  120. klipper) to $2 your clipboard.
  121.  
  122. EOF
  123.         exit 1
  124. }
  125.  
  126. ### conversions
  127.  
  128. # escape % (used for escaping), & (used as separator in POST data), + (used as space in POST data), space and ;
  129. escape() {
  130.         sed -e 's|%|%25|g' -e 's|&|%26|g' -e 's|+|%2b|g' -e 's|;|%3b|g' -e 's| |+|g' <<< "$*" || die "sed failed"
  131. }
  132.  
  133. # if possible convert URL to raw
  134. converttoraw() {
  135.         local regex
  136.         regex=REGEX_RAW_$ENGINE
  137.         if [[ -n ${!regex} ]]; then
  138.                 RAWURL=$(sed -e "${!regex}" <<< "$URL")
  139.                 [[ -n $RAWURL ]] && return 0
  140.                 echo "Convertion to raw url failed." >&2
  141.         else
  142.                 echo "Raw download of pastes is not supported by $(getrecipient)." >&2
  143.         fi
  144.         return 1
  145. }
  146.  
  147. ### verification
  148. verifyservice() {
  149.         for s in $SERVICES; do
  150.                 [[ $s == $* ]] && return 0
  151.         done
  152.         echo "\"$*\" is not a supported service.$N" >&2
  153.         showservices >&2
  154.         exit 1
  155. }
  156.  
  157. verifylanguage() {
  158.         local i j l lang count v values
  159.         lang=LANGUAGES_$ENGINE
  160.         count=LANGUAGE_COUNT_$ENGINE
  161.         values=LANGUAGE_VALUES_$ENGINE
  162.         if [[ -n ${!lang} ]]; then
  163.                 ((i=0))
  164.                 for l in ${!lang}; do
  165.                         if [[ $LANGUAGE == ${l//\%/ } ]]; then
  166.                                 if [[ -n ${!count} ]]; then
  167.                                         ((LANGUAGE=i+1))
  168.                                 elif [[ -n ${!values} ]]; then
  169.                                         ((j=0))
  170.                                         for v in ${!values}; do
  171.                                                 if [[ i -eq j ]]; then
  172.                                                         if [[ ${v} == \% ]]; then
  173.                                                                 LANGUAGE=
  174.                                                         else
  175.                                                                 LANGUAGE=${v//\%/ }
  176.                                                         fi
  177.                                                         break
  178.                                                 fi
  179.                                                 ((j++))
  180.                                         done
  181.                                 fi
  182.                                 return 0
  183.                         fi
  184.                         ((i++))
  185.                 done
  186.         else
  187.                 [[ $LANGUAGESET = 0 ]] || return 0
  188.         fi
  189.         echo "\"$LANGUAGE\" is not a supported language for $(getrecipient).$N" >&2
  190.         showlanguages >&2
  191.         exit 1
  192. }
  193.  
  194. verifyexpiration() {
  195.         local i j e expiration count v values
  196.         expiration=EXPIRATIONS_$ENGINE
  197.         count=EXPIRATION_COUNT_$ENGINE
  198.         values=EXPIRATION_VALUES_$ENGINE
  199.         if [[ -n ${!expiration} ]]; then
  200.                 ((i=0))
  201.                 for e in ${!expiration}; do
  202.                         if [[ ${EXPIRATION} == ${e//\%/ } ]]; then
  203.                                 if [[ -n ${!count} ]]; then
  204.                                         ((EXPIRATION=i+1))
  205.                                 elif [[ -n {!values} ]]; then
  206.                                         ((j=0))
  207.                                         for v in ${!values}; do
  208.                                                 if [[ i -eq j ]]; then
  209.                                                         if [[ ${v} == \% ]]; then
  210.                                                                 EXPIRATION=
  211.                                                         else
  212.                                                                 EXPIRATION=${v//\%/ }
  213.                                                         fi
  214.                                                         break
  215.                                                 fi
  216.                                                 ((j++))
  217.                                         done
  218.                                 fi
  219.                                 return 0
  220.                         fi
  221.                         ((i++))
  222.                 done
  223.         else
  224.                 [[ $EXPIRATIONSET = 0 ]] || return 0
  225.         fi
  226.         echo "\"$EXPIRATION\" is not a supported expiration option for $(getrecipient).$N" >&2
  227.         showexpirations >&2
  228.         exit 1
  229. }
  230.  
  231. # verify that the pastebin service did not return a known error url. otherwise print a helpful error message
  232. verifyurl() {
  233.         dieifknown() {
  234.                 [[ -n ${!1%% *} && ${!1%% *} == $URL ]] && die "${!1#* }"
  235.         }
  236.         local t
  237.         for t in ${!TOO*}; do
  238.                 [[ $t == TOO*_$SERVICE ]] && dieifknown "$t"
  239.         done
  240. }
  241.  
  242. # print a warning if failure is predictable due to the mere size of the paste. note that this is only a warning
  243. # printed. it does not abort.
  244. warnings() {
  245.         warn() {
  246.                 if [[ -n $2 && $1 -gt $2 ]]; then
  247.                         echo "Pasting > ${3//\%/ } often tend to fail with $SERVICE. Use --verbose or --debug to see the"
  248.                         echo "error output from wget if it fails. Alternatively use another pastebin service."
  249.                 fi
  250.         }
  251.         local size lines
  252.         size=SIZE_$SERVICE
  253.         warn "$SIZE" "${!size% *}" "${!size#* }"
  254.         lines=LINES_$SERVICE
  255.         warn "$LINES" "${!lines}" "${!lines} lines"
  256. }
  257.  
  258. ### input
  259. getfilenames() {
  260.         for f in "$@"; do
  261.                 [[ -f $f ]] || die "$0: $f No such file found."
  262.                 SOURCE="files"
  263.                 FILES[${#FILES[*]}]="$f"
  264.         done
  265. }
  266.  
  267. x_cut() {
  268.         if [[ -x $(type -P xclip) ]]; then
  269.                 xclip -o || die "xclip failed."
  270.         else
  271.                 noxclip cut "read from"
  272.         fi
  273. }
  274.  
  275. ### output
  276. usage() {
  277.         cat <<EOF
  278. Usage: $0 [options] [file[s]]
  279.  
  280. Options:
  281.     -l, --language LANG           set language (defaults to "$DEFAULT_LANGUAGE")
  282.     -d, --description DESCRIPTION set description (defaults to "stdin" or filename)
  283.     -n, --nick NICK               set nick (defaults to your username)
  284.     -s, --service SERVICE         set service to use (defaults to "$DEFAULT_SERVICE")
  285.     -e, --expiration EXPIRATION   set when it should expire (defaults to "$DEFAULT_EXPIRATION")
  286.  
  287.     -S, --list-services           list supported pastebin services
  288.     -L, --list-languages          list languages supported by the specified service
  289.     -E, --list-expiration         list expiration setting supported by the specified service
  290.  
  291.     -u, --tinyurl URL             convert input url to tinyurl
  292.  
  293.     -c, --command COMMAND         paste COMMAND and the output of COMMAND
  294.     -i, --info                    append the output of \`$INFO_COMMAND\`
  295.     -I, --info-only               paste the output of \`$INFO_COMMAND\` only
  296.     -x, --xcut                    read input from clipboard (requires x11-misc/xclip)
  297.     -X, --xpaste                  write resulting url to the X primary selection buffer (requires x11-misc/xclip)
  298.     -C, --xclippaste              write resulting url to the X clipboard selection buffer (requires x11-misc/xclip)
  299.  
  300.     -r, --raw                     show url for the raw paste (no syntax highlighting or html)
  301.     -t, --tee                     use tee to show what is being pasted
  302.     -v, --verbose                 show wget stderr output if no url is received
  303.         --completions             emit output suitable for shell completions (only affects --list-*)
  304.         --debug                   be *very* verbose (implies -v)
  305.  
  306.     -h, --help                    show this help
  307.     -g, --ignore-configs          ignore /etc/wgetpaste.conf, ~/.wgetpaste.conf etc.
  308.         --version                 show version information
  309.  
  310. Defaults (DEFAULT_{NICK,LANGUAGE,EXPIRATION}[_\${SERVICE}] and DEFAULT_SERVICE)
  311. can be overridden globally in /etc/wgetpaste.conf or /etc/wgetpaste.d/*.conf or
  312. per user in any of ~/.wgetpaste.conf or ~/.wgetpaste.d/*.conf.
  313. EOF
  314. }
  315.  
  316. showservices() {
  317.         local max s IND INDV engine url d
  318.         if [[ -n $COMPLETIONS ]]; then
  319.                 for s in $SERVICES; do
  320.                         if [[ -n $VERBOSE ]]; then
  321.                                 d=URL_$s && echo "$s:${!d% *}"
  322.                         else
  323.                                 echo "$s"
  324.                         fi
  325.                 done
  326.                 exit 0
  327.         fi
  328.         echo "Services supported: (case sensitive):"
  329.         max=4
  330.         for s in $SERVICES; do
  331.                 [[ ${#s} -gt $max ]] && max=${#s}
  332.         done
  333.         ((IND=6+max))
  334.         if [[ $VERBOSE ]]; then
  335.                 max=0
  336.                 for s in $SERVICES; do
  337.                         s=URL_$s
  338.                         s=${!s% *}
  339.                         [[ ${#s} -gt $max ]] && max=${#s}
  340.                 done
  341.                 ((INDV=3+max+IND))
  342.                 engine=" $E[${INDV}G| Pastebin engine:"
  343.         fi
  344.         echo "   Name: $E[${IND}G| Url:$engine"
  345.         echo -ne "   "; for((s=3;s<${INDV:-${IND}}+17;s++)); do (( $s == IND-1 || $s == INDV-1 )) && echo -ne "|" || echo -ne "="; done; echo
  346.         for s in $SERVICES; do
  347.                 [[ $s = $DEFAULT_SERVICE ]] && d="*" || d=" "
  348.                 [[ $VERBOSE ]] && engine=ENGINE_$s && engine="$E[${INDV}G| ${!engine}"
  349.                 url=URL_$s
  350.                 url=${!url% *}
  351.                 echo "   $d$s $E[${IND}G| $url$engine"
  352.         done | sort
  353. }
  354.  
  355. printlist() {
  356.         while [[ -n $1 ]]; do
  357.                 echo "${1//\%/ }"
  358.                 shift
  359.         done
  360. }
  361.  
  362. showlanguages() {
  363.         local l lang d
  364.         lang=LANGUAGES_$ENGINE
  365.         [[ -n $COMPLETIONS ]] && printlist ${!lang} | sort && exit 0
  366.         echo "Languages supported by $(getrecipient) (case sensitive):"
  367.         [[ -z ${!lang} ]] && echo "$N\"$ENGINE\" has no support for setting language." >&2 && exit 1
  368.         for l in ${!lang}; do
  369.                 [[ ${l//\%/ } = $DEFAULT_LANGUAGE ]] && d="*" || d=" "
  370.                 echo "   $d${l//\%/ }"
  371.         done | sort
  372. }
  373.  
  374. showexpirations() {
  375.         local e expiration info d
  376.         expiration=EXPIRATIONS_$ENGINE
  377.         [[ -n $COMPLETIONS ]] && printlist ${!expiration} && exit 0
  378.         echo "Expiration options supported by $(getrecipient) (case sensitive):"
  379.         info=EXPIRATION_INFO_$SERVICE
  380.         [[ -z ${!expiration} ]] && echo "$N${!info}\"$ENGINE\" has no support for setting expiration." >&2 && exit 1
  381.         for e in ${!expiration}; do
  382.                 [[ ${e//\%/ } = $DEFAULT_EXPIRATION ]] && d="*" || d=" "
  383.                 echo "   $d${e//\%/ }"
  384.         done
  385. }
  386.  
  387. showurl() {
  388.         echo "Your ${2}paste can be seen here: $1"
  389.         [[ $XPASTE ]] && x_paste "$1" primary
  390.         [[ $XCLIPPASTE ]] && x_paste "$1" clipboard
  391. }
  392.  
  393. x_paste() {
  394.         if [[ -x $(type -P xclip) ]]; then
  395.                 echo -n "$1" | xclip -selection $2 -loops 10 &>/dev/null || die "xclip failed."
  396.         else
  397.                 noxclip paste "write to"
  398.         fi
  399. }
  400.  
  401. ### Posting helper functions
  402.  
  403. # get the url to post to
  404. getrecipient() {
  405.         local urls target serv
  406.         for s in $SERVICES tinyurl; do
  407.                 if [[ $s == $SERVICE ]]; then
  408.                         urls=URL_$SERVICE
  409.                         if [[ RAW == $1 ]]; then
  410.                                 [[ ${!urls} = ${!urls#* } ]] || target=${!urls#* }
  411.                         else
  412.                                 serv="$SERVICE: "
  413.                         fi
  414.                         echo "${serv}${!urls% *}${target}"
  415.                         return 0
  416.                 fi
  417.         done
  418.         die "Failed to get url for \"$SERVICE\"."
  419. }
  420.  
  421. # generate POST data
  422. postdata() {
  423.         local post nr extra f
  424.         post=POST_$ENGINE
  425.         nr=${!post//[^ ]}
  426.         [[ 6 = ${#nr} ]] || die "\"${SERVICE}\" is not supported by ${FUNCNAME}()."
  427.         extra=${!post%% *}
  428.         [[ '%' = $extra ]] || echo -n "$extra&"
  429.         e() {
  430.                 post="$1"
  431.                 shift
  432.                 while [[ -n $1 ]]; do
  433.                         f=${post%% *}
  434.                         [[ '%' != $f ]] && echo -n "$f=${!1}" && [[ $# -gt 1 ]] && echo -n "&"
  435.                         shift
  436.                         post=${post#$f }
  437.                 done
  438.         }
  439.         e "${!post#$extra }" NICK DESCRIPTION LANGUAGE EXPIRATION CVT_TABS INPUT
  440. }
  441.  
  442. # get url from response from server
  443. geturl() {
  444.         local regex
  445.         regex=REGEX_URL_$ENGINE
  446.         if [[ -n ${!regex} ]]; then
  447.                 [[ needstdout = $1 ]] && return 0
  448.                 sed -n -e "${!regex}" <<< "$*"
  449.         else
  450.                 [[ needstdout = $1 ]] && return 1
  451.                 sed -n -e 's|^.*Location: \(http://[^ ]*\).*$|\1|p' <<< "$*"
  452.         fi
  453. }
  454.  
  455. ### read cli options
  456.  
  457. # separate groups of short options. replace --foo=bar with --foo bar
  458. while [[ -n $1 ]]; do
  459.         case "$1" in
  460.                 -- )
  461.                 for arg in "$@"; do
  462.                         ARGS[${#ARGS[*]}]="$arg"
  463.                 done
  464.                 break
  465.                 ;;
  466.                 --debug )
  467.                 set -x
  468.                 DEBUG=0
  469.                 ;;
  470.                 --*=* )
  471.                 ARGS[${#ARGS[*]}]="${1%%=*}"
  472.                 ARGS[${#ARGS[*]}]="${1#*=}"
  473.                 ;;
  474.                 --* )
  475.                 ARGS[${#ARGS[*]}]="$1"
  476.                 ;;
  477.                 -* )
  478.                 for shortarg in $(sed -e 's|.| -&|g' <<< "${1#-}"); do
  479.                         ARGS[${#ARGS[*]}]="$shortarg"
  480.                 done
  481.                 ;;
  482.                 * )
  483.                 ARGS[${#ARGS[*]}]="$1"
  484.         esac
  485.         shift
  486. done
  487.  
  488. # set the separated options as input options.
  489. set -- "${ARGS[@]}"
  490.  
  491. while [[ -n $1 ]]; do
  492.         ((args=1))
  493.         case "$1" in
  494.                 -- )
  495.                 shift && getfilenames "$@" && break
  496.                 ;;
  497.                 -c | --command )
  498.                 requiredarg "$@"
  499.                 SOURCE="command"
  500.                 COMMANDS[${#COMMANDS[*]}]="$2"
  501.                 ;;
  502.                 --completions )
  503.                 COMPLETIONS=0
  504.                 ;;
  505.                 -d | --description )
  506.                 requiredarg "$@"
  507.                 DESCRIPTION="$2"
  508.                 ;;
  509.                 -e | --expiration )
  510.                 requiredarg "$@"
  511.                 EXPIRATIONSET=0
  512.                 EXPIRATION="$2"
  513.                 ;;
  514.                 -E | --list-expiration )
  515.                 LISTEXPIRATION=0
  516.                 ;;
  517.                 -h | --help )
  518.                 USAGE=0
  519.                 ;;
  520.                 -g | --ignore-configs )
  521.                 IGNORECONFIGS=0
  522.                 ;;
  523.                 -i | --info )
  524.                 INFO=0
  525.                 ;;
  526.                 -I | --info-only )
  527.                 SOURCE=info
  528.                 ;;
  529.                 -l | --language )
  530.                 requiredarg "$@"
  531.                 LANGUAGESET=0
  532.                 LANGUAGE="$2"
  533.                 ;;
  534.                 -L | --list-languages )
  535.                 LISTLANGUAGES=0
  536.                 ;;
  537.                 -n | --nick )
  538.                 requiredarg "$@"
  539.                 NICK=$(escape "$2")
  540.                 ;;
  541.                 -r | --raw )
  542.                 RAW=0
  543.                 ;;
  544.                 -s | --service )
  545.                 requiredarg "$@"
  546.                 SERVICESET="$2"
  547.                 ;;
  548.                 -S | --list-services )
  549.                 SHOWSERVICES=0
  550.                 ;;
  551.                 -t | --tee )
  552.                 TEE=0
  553.                 ;;
  554.                 -u | --tinyurl )
  555.                 requiredarg "$@"
  556.                 SERVICE=tinyurl
  557.                 SOURCE="url"
  558.                 INPUTURL="$2"
  559.                 ;;
  560.                 -v | --verbose )
  561.                 VERBOSE=0
  562.                 ;;
  563.                 --version )
  564.                 echo "$0, version $VERSION" && exit 0
  565.                 ;;
  566.                 -x | --xcut )
  567.                 SOURCE=xcut
  568.                 ;;
  569.                 -X | --xpaste )
  570.                 XPASTE=0
  571.                 ;;
  572.                 -C | --xclippaste )
  573.                 XCLIPPASTE=0
  574.                 ;;
  575.                 -* )
  576.                 die "$0: unrecognized option \`$1'"
  577.                 ;;
  578.                 *)
  579.                 getfilenames "$1"
  580.                 ;;
  581.         esac
  582.         shift $args
  583. done
  584.  
  585. ### defaults
  586. load_configs() {
  587.         if [[ ! $IGNORECONFIGS ]]; then
  588.                 # compatibility code
  589.                 local f deprecated=
  590.                 for f in {/etc/,~/.}wgetpaste{.d/*.bash,}; do
  591.                         if [[ -f $f ]]; then
  592.                                 if [[ -z $deprecated ]]; then
  593.                                         echo "The config files for wgetpaste have changed to *.conf.$N" >&2
  594.                                         deprecated=0
  595.                                 fi
  596.                                 echo "Please move ${f} to ${f/%.bash/.conf}" >&2
  597.                                 source "$f" || die "Failed to source $f"
  598.                         fi
  599.                 done
  600.                 [[ -n $deprecated ]] && echo >&2
  601.                 # new locations override old ones in case they collide
  602.                 for f in {/etc/,~/.}wgetpaste{.d/*,}.conf; do
  603.                         if [[ -f $f ]]; then
  604.                                 source "$f" || die "Failed to source $f"
  605.                         fi
  606.                 done
  607.         fi
  608. }
  609. load_configs
  610. [[ $SERVICESET ]] && verifyservice "$SERVICESET" && SERVICE=$(escape "$SERVICESET")
  611. DEFAULT_NICK=${DEFAULT_NICK:-$(whoami)} || die "whoami failed"
  612. DEFAULT_SERVICE=${DEFAULT_SERVICE:-pocoo}
  613. DEFAULT_LANGUAGE=${DEFAULT_LANGUAGE:-Plain Text}
  614. DEFAULT_EXPIRATION=${DEFAULT_EXPIRATION:-1 month}
  615. SERVICE=${SERVICE:-${DEFAULT_SERVICE}}
  616. ENGINE=ENGINE_$SERVICE
  617. ENGINE="${!ENGINE}"
  618. default="DEFAULT_NICK_$SERVICE" && [[ -n ${!default} ]] && DEFAULT_NICK=${!default}
  619. default="DEFAULT_LANGUAGE_$SERVICE" && [[ -n ${!default} ]] && DEFAULT_LANGUAGE=${!default}
  620. default="DEFAULT_EXPIRATION_$SERVICE" && [[ -n ${!default} ]] && DEFAULT_EXPIRATION=${!default}
  621. NICK=${NICK:-$(escape "${DEFAULT_NICK}")}
  622. [[ -z $SOURCE ]] && SOURCE="stdin" && FILES[${#FILES[*]}]="/dev/stdin"
  623. CVT_TABS=No
  624.  
  625. INFO_COMMAND=${INFO_COMMAND:-"emerge --info"}
  626. INFO_ARGS=${INFO_ARGS:-"--ignore-default-opts"}
  627.  
  628. ### everything below this should be independent of which service is being used...
  629.  
  630. # show listings if requested
  631. [[ $USAGE ]] && usage && exit 0
  632. [[ $SHOWSERVICES ]] && showservices && exit 0
  633. [[ $LISTLANGUAGES ]] && showlanguages && exit 0
  634. [[ $LISTEXPIRATION ]] && showexpirations && exit 0
  635.  
  636. # language and expiration need to be verified before they are escaped but after service and defaults
  637. # have been selected
  638. LANGUAGE=${LANGUAGE:-${DEFAULT_LANGUAGE}}
  639. verifylanguage
  640. LANGUAGE=$(escape "$LANGUAGE")
  641. EXPIRATION=${EXPIRATION:-${DEFAULT_EXPIRATION}}
  642. verifyexpiration
  643. EXPIRATION=$(escape "$EXPIRATION")
  644.  
  645. # set prompt
  646. if [[ 0 -eq $UID ]]; then
  647.         PS1="#"
  648. else
  649.         PS1=$
  650. fi
  651.  
  652. # set default description
  653. size=DESCRIPTION_SIZE_$SERVICE
  654. if [[ -z $DESCRIPTION ]]; then
  655.         case "$SOURCE" in
  656.                 info )
  657.                 DESCRIPTION="$PS1 $INFO_COMMAND;"
  658.                 ;;
  659.                 command )
  660.                 DESCRIPTION="$PS1"
  661.                 for c in "${COMMANDS[@]}"; do
  662.                         DESCRIPTION="$DESCRIPTION $c;"
  663.                 done
  664.                 ;;
  665.                 files )
  666.                 DESCRIPTION="${FILES[@]}"
  667.                 ;;
  668.                 * )
  669.                 DESCRIPTION="$SOURCE"
  670.                 ;;
  671.         esac
  672.         if [[ -n ${!size} && ${#DESCRIPTION} -gt ${!size} ]]; then
  673.                 DESCRIPTION="${DESCRIPTION: -${!size}}"
  674.         fi
  675. else
  676.         if [[ -n ${!size} && ${#DESCRIPTION} -gt ${!size} ]]; then
  677.                 die "Your description (${#DESCRIPTION} bytes) is too long. Shorten it to fit within ${!size} bytes."
  678.         fi
  679. fi
  680.  
  681. # create tmpfile for use with tee
  682. if [[ $TEE ]]; then
  683.         TMPF=$(mktemp /tmp/wgetpaste.XXXXXX)
  684.         [[ -f $TMPF ]] || die "Could not create a temporary file for use with tee."
  685. fi
  686.  
  687. # read input
  688. case "$SOURCE" in
  689.         url )
  690.         INPUT="${INPUTURL}"
  691.         ;;
  692.         command )
  693.         for c in "${COMMANDS[@]}"; do
  694.                 if [[ $TEE ]]; then
  695.                         echo "$PS1 $c$N$(bash -c "$c" 2>&1)$N" | tee -a "$TMPF"
  696.                 else
  697.                         INPUT="$INPUT$PS1 $c$N$(bash -c "$c" 2>&1)$N$N"
  698.                 fi
  699.         done
  700.         ;;
  701.         info )
  702.         if [[ $TEE ]]; then
  703.                 echo "$PS1 $INFO_COMMAND$N$($INFO_COMMAND $INFO_ARGS 2>&1)" | tee "$TMPF"
  704.         else
  705.                 INPUT="$PS1 $INFO_COMMAND$N$($INFO_COMMAND $INFO_ARGS 2>&1)"
  706.         fi
  707.         ;;
  708.         xcut )
  709.         if [[ $TEE ]]; then
  710.                 x_cut | tee "$TMPF"
  711.         else
  712.                 INPUT="$(x_cut)"
  713.         fi
  714.         ;;
  715.         files | stdin )
  716.         if [[ ${#FILES[@]} -gt 1 ]]; then
  717.                 for f in "${FILES[@]}"; do
  718.                         [[ -r $f ]] || notreadable "$f"
  719.                         if [[ $TEE ]]; then
  720.                                 echo "$PS1 cat $f$N$(<"$f")$N" | tee -a "$TMPF"
  721.                         else
  722.                                 INPUT="$INPUT$PS1 cat $f$N$(<"$f")$N$N"
  723.                         fi
  724.                 done
  725.         else
  726.                 [[ -r $FILES ]] || notreadable "$FILES"
  727.                 if [[ $TEE ]]; then
  728.                         tee "$TMPF" < "$FILES"
  729.                 else
  730.                         INPUT=$(<"$FILES")
  731.                 fi
  732.         fi
  733.         ;;
  734. esac
  735. NOINPUT="No input read. Nothing to paste. Aborting."
  736. if [[ $TEE ]]; then
  737.         [[ 0 -eq $(wc -c < "$TMPF") ]] && die "$NOINPUT"
  738. else
  739.         [[ -z $INPUT ]] && die "$NOINPUT"
  740. fi
  741.  
  742. # append info if needed
  743. if [[ $INFO ]]; then
  744.         DESCRIPTION="$DESCRIPTION $PS1 $INFO_COMMAND;"
  745.         if [[ $TEE ]]; then
  746.                 echo "$N$PS1 $INFO_COMMAND$N$($INFO_COMMAND $INFO_ARGS 2>&1)" | tee -a "$TMPF"
  747.         else
  748.                 INPUT="$INPUT$N$PS1 $INFO_COMMAND$N$($INFO_COMMAND $INFO_ARGS 2>&1)"
  749.         fi
  750. fi
  751.  
  752. # now that tee has done its job read data into INPUT
  753. [[ $TEE ]] && INPUT=$(<"$TMPF") && echo
  754.  
  755. # escape DESCRIPTION and INPUT
  756. DESCRIPTION=$(escape "$DESCRIPTION")
  757. INPUT=$(escape "$INPUT")
  758.  
  759. # print friendly warnings if max sizes have been specified for the pastebin service and the size exceeds that
  760. SIZE=$(wc -c <<< "$INPUT")
  761. LINES=$(wc -l <<< "$INPUT")
  762. warnings >&2
  763.  
  764. # set recipient
  765. RECIPIENT=$(getrecipient RAW)
  766.  
  767. if [[ $SERVICE == tinyurl ]]; then
  768.         URL=$(LC_ALL=C wget -qO - "$RECIPIENT?url=$INPUT")
  769. else
  770.         # create temp file (wget is much more reliable reading
  771.         # large input via --post-file rather than --post-data)
  772.         [[ -f $TMPF ]] || TMPF=$(mktemp /tmp/wgetpaste.XXXXXX)
  773.         if [[ -f $TMPF ]]; then
  774.                 postdata > "$TMPF" || die "Failed to write to temporary file: \"$TMPF\"."
  775.                 WGETARGS="--post-file=$TMPF"
  776.         else
  777.                 # fall back to using --post-data if the temporary file could not be created
  778.                 # TABs and new lines need to be escaped for wget to interpret it as one string
  779.                 WGETARGS="--post-data=$(postdata | sed -e 's|$|%0a|g' -e 's|\t|%09|g' | tr -d '\n')"
  780.         fi
  781.  
  782.         # paste it
  783.         WGETARGS="--tries=5 --timeout=60 $WGETARGS"
  784.         if geturl needstdout || [[ $DEBUG || ! -w /dev/null ]]; then
  785.                 OUTPUT=$(LC_ALL=C wget -O - $WGETARGS $RECIPIENT 2>&1)
  786.         else
  787.                 OUTPUT=$(LC_ALL=C wget -O /dev/null $WGETARGS $RECIPIENT 2>&1)
  788.         fi
  789.  
  790.         # clean temporary file if it was created
  791.         if [[ -f $TMPF ]]; then
  792.                 if [[ $DEBUG ]]; then
  793.                         echo "Left temporary file: \"$TMPF\" alone for debugging purposes."
  794.                 else
  795.                         rm "$TMPF" || echo "Failed to remove temporary file: \"$TMPF\"." >&2
  796.                 fi
  797.         fi
  798.  
  799.         # get the url
  800.         URL=$(geturl "$OUTPUT")
  801. fi
  802.  
  803. # verify that the pastebin service did not return a known error url such as toofast.html from rafb
  804. verifyurl
  805.  
  806. # handle the case when there was no location returned
  807. if [[ -z $URL ]]; then
  808.         if [[ $DEBUG || $VERBOSE ]]; then
  809.                 die "Apparently nothing was received. Perhaps the connection failed.$N$OUTPUT"
  810.         else
  811.                 echo "Apparently nothing was received. Perhaps the connection failed. Enable --verbose or" >&2
  812.                 die "--debug to get the output from wget that can help diagnose it correctly."
  813.         fi
  814. fi
  815.  
  816. # converttoraw() sets RAWURL upon success.
  817. if [[ $RAW ]] && converttoraw; then
  818.         showurl "$RAWURL" "raw "
  819. else
  820.         showurl "$URL"
  821. fi
  822.  
  823. exit 0