ℹ️ Your data is safe here... unless you make the SNIP gods angry. And let's just say they have a really bad sense of humor.

From AAA, 5 Years ago, written in Bash.
Embed
  1. #!/usr/bin/env bash
  2. # paste!
  3. # Spencer Butler <spencerb>
  4.  
  5. if [ ! $(type -p jq) ] || [ ! $(type -p curl) ]; then
  6.   echo "jq and curl are required to run this script."
  7.   echo "[pkg install jq curl] [apt install jq curl] [yum install jq curl]"
  8.   exit
  9. fi
  10.  
  11. host=p.libren.ms
  12. host_url="https://${host}"
  13. api_create='/api/create'
  14. api_get_paste='/api/paste'  # [pasteid]
  15. api_random='/api/random'
  16. api_recent='/api/recent'
  17. api_trending='/api/trending'
  18. api_langs='/api/langs'
  19.  
  20. api_check="$(curl -s ${host_url}${api_langs} | jq '.text' 2>/dev/null)"
  21. if [ "$api_check" != '"Plain Text"' ]; then
  22.   api_err="$(curl -sD - -o /dev/null ${host_url}${api_langs} | head -n1)"
  23.   echo -e "Could not connect to ${host_url}${api_langs}, the server responded with\n${api_err}"
  24.   exit
  25. fi
  26.  
  27. usage() {
  28.   echo -e "USAGE:
  29.  $(basename $0) [-o (read from STDIN)] [-s fqdn.host.name (no proto)] [-t title] [-n name] [-p private (bool)] [-l lang] [-e expire (min)|burn] [-p pasteid]" file
  30.   echo "All arguments are optional. A file or STDOUT is all that is required."
  31.   echo -e "\tfrom STDIN:\techo foo | $(basename $0) -o"
  32.   echo -e "\twith file:\t$(basename $0) file.txt"
  33.   echo -e "\twith options:\t$(basename $0) -l text -t 'my title' -n 'my name' -e burn -s p.libren.ms file.txt"
  34.   if [ -n "$langerr" ]; then
  35.     echo -e "\nlang \"${curlopts[lang]}\" is not known to us.\n${langerr}"
  36.     echo -e "The API at: ${host_url}${api_langs}\nreports the following available languages:\n"
  37.     echo "$(curl -s ${host_url}${api_langs} | jq --join-output '.| @sh \"\(.|keys)\"')"
  38.   fi
  39.   exit
  40. }
  41.  
  42. if [ "$#" -lt 1 ]; then
  43.   usage
  44. fi
  45.  
  46.  
  47. # default api data options
  48. declare -A curlopts
  49. # snipurl is undocumented and just needs to be set (server must have shorturls enabled)
  50. curlopts[snipurl]=true
  51. curlopts[name]="$(hostname -s)"
  52. curlopts[title]="$USER says..."
  53. curlopts[private]=0
  54. curlopts[lang]=autoit
  55. curlopts[expire]=30       # minutes or burn
  56. curlopts[text]=''
  57. curlopts[reply]=''        # requires pasteid
  58. curlopts[pasteid]=''      # /pasteid suffix for future use
  59.  
  60. snipin() {
  61.   curl -s "${curl_opts[@]}" --data-urlencode text@"$file" "$api_url"
  62. }
  63. snipout() {
  64.   curl -s "${curl_opts[@]}" --data-urlencode text@- "$api_url"
  65. }
  66.  
  67. while getopts 's:ot:n:x:l:e:p:h' dopt; do
  68.   case "$dopt" in
  69.     s)
  70.       host="$OPTARG"
  71.       ;;
  72.     o)
  73.       stdout=true
  74.       ;;
  75.     t)
  76.       curlopts[title]="$OPTARG"
  77.       ;;
  78.     n)
  79.       curlopts[name]="$OPTARG"
  80.       ;;
  81.     x)
  82.       curlopts[private]="$OPTARG"
  83.       ;;
  84.     l)
  85.       good_lang="$(curl -s ${host_url}${api_langs} | jq --raw-output ".\"${OPTARG}\"")"
  86.       if [[ "$good_lang" == null ]]; then
  87.         curlopts[lang]="$OPTARG"
  88.         langerr="The server responded \\\\"$good_lang\" to your requested \"$OPTARG\" language."
  89.       else
  90.         curlopts[lang]="$OPTARG"
  91.       fi
  92.       ;;
  93.     e)
  94.       curlopts[expire]="$OPTARG"
  95.       ;;
  96.     p)
  97.       curlopts[pasteid]="$OPTARG"
  98.       ;;
  99.     ?)
  100.       usage
  101.       ;;
  102.   esac
  103. done
  104. shift "$(($OPTIND -1))"
  105. # we only want the file after getopts, any args after that are lost
  106. file="$1"
  107.  
  108. # prepare all the options to send to curl
  109. declare -a curl_opts
  110. for copt in "${!curlopts[@]}"; do
  111.   if [ -n "${curlopts[$copt]}" ]; then
  112.     curl_opts+=(-d "$copt"="${curlopts[$copt]}" )
  113.   fi
  114. done
  115.  
  116. # we set this here so we can change the api endpoint
  117. api="${api:-"$api_create"}"
  118. api_url="https://${host}${api}"
  119.  
  120. # if there is not lang error, decide if we are in or out and run
  121. if [ -n "$stdout" ] && [ ! "$langerr" ]; then
  122.   snipout
  123. elif [ -z "$stdout" ] && [ ! "$langerr" ]; then
  124.   snipin
  125. else
  126.   usage
  127. fi
  128.  
captcha