Raw paste - Back to the pastebin
  1. #!/bin/sh
  2. # This software is released into the public domain.
  3. # It is provided "as is", without warranties or conditions of any kind.
  4. # Anyone is free to use, modify, redistribute and do anything with this software.
  5. # requires: POSIX sh, curl, tr
  6. # To use service other than https://sicp.me/, set the environmental variable
  7. # SICPLOAD_SERVICE to the upload/paste endpoint URL for that service or
  8. # uncomment and set the following line:
  9. #SICPLOAD_SERVICE=https://[your service here]/
  10. # To auto-authenticate without having to use the -U flag, either set the
  11. # environmental variable SICPLOAD_UUID or uncomment and set the following line:
  12. #SICPLOAD_UUID=[your UUID here]
  13. ###############################################################
  14. # do not edit below this line unless you know what you're doing
  15. ###############################################################
  16. default_service="${SICPLOAD_SERVICE:-"https://sicp.me/"}"
  17. service="$default_service"
  18. uuid="${SICPLOAD_UUID:-}"
  19. prog="$(basename "$0")"
  20. paste=0
  21. stdin=1
  22. lang=
  23. usage() {
  24. printf "Usage: %s < file\n" "$prog"
  25. printf " command | %s\n" "$prog"
  26. printf " %s file1 file2 file3 ...\n" "$prog"
  27. printf "\n"
  28. printf "Options (applies for all files following the flag):\n"
  29. printf " -S, --service=ADDR uses ADDR as the service URL instead of %s\n" "$default_service"
  30. printf " -U, --uuid=UUID uses UUID for authentication with the service\n"
  31. printf " -u, --upload upload files from this flag on to the file hosting service (default)\n"
  32. printf " -p, --paste paste files from this flag on to the pastebin\n"
  33. printf " -l, --lang=LANG syntax highlight using language LANG (implies -p)\n"
  34. printf " -l-, --lang=- guess language to highlight (inaccurate) (implies -p)\n"
  35. printf " -L, --lang= do not highlight paste, serve raw plaintext (implies -p)\n"
  36. printf " -h, --help display this help and exit\n"
  37. printf " -- stop processing options and treat all following arguments as filenames\n"
  38. printf "\n"
  39. printf "Example: %s foo -lsh bar -u baz -p quux -L qwe asd\n" "$prog"
  40. printf " will post 'foo' and 'baz' as files, 'bar' and 'quux' as .sh pastes and 'qwe' and 'asd' as raw pastes\n"
  41. }
  42. upload() {
  43. file=""
  44. auth=""
  45. if [ $# -ge 1 ]; then
  46. filename="$(basename "$1" | tr -c -d 'a-zA-Z0-9_?.+=!@#$%^&*()[]{} -')"
  47. if [ -n "$filename" ]; then
  48. file=";filename=$filename"
  49. fi
  50. fi
  51. if [ -n "$uuid" ]; then
  52. auth="-F uuid=$uuid"
  53. fi
  54. #shellcheck disable=SC2086
  55. if [ "$paste" -ne 0 ]; then
  56. if [ -n "$lang" ]; then
  57. url="$(curl -sS $auth -F "lang=$lang" -F "paste=<-" "$service")"
  58. else
  59. url="$(curl -sS $auth -F "paste=<-" "$service")"
  60. fi
  61. else
  62. url="$(curl --progress-bar $auth -F "file=@-$file" "$service")"
  63. fi
  64. case "$url" in
  65. http*)
  66. printf "%s\n" "$url"
  67. ;;
  68. ?*)
  69. printf "%s: error: %s\n" "$prog" "$url" >&2
  70. return 1
  71. ;;
  72. "")
  73. printf "%s: error: received an empty response from the service\n" "$prog" >&2
  74. return 1
  75. ;;
  76. esac
  77. }
  78. upload_file() {
  79. #shellcheck disable=SC2094
  80. if ! url="$(upload "$1" < "$1")"; then
  81. exit 1
  82. fi
  83. printf "%s: %s\n" "$1" "$url"
  84. }
  85. while [ $# -gt 0 ]; do
  86. case "$1" in
  87. -h|--help)
  88. usage
  89. exit
  90. ;;
  91. -u|--upload)
  92. paste=0
  93. ;;
  94. -p|--paste)
  95. paste=1
  96. ;;
  97. -l|--lang)
  98. if [ $# -ge 2 ]; then
  99. lang="$2"
  100. shift
  101. else
  102. printf "%s: option '%s' requires an argument\n" "$prog" "$1" >&2
  103. exit 2
  104. fi
  105. paste=1
  106. ;;
  107. -l?*)
  108. lang="${1#??}"
  109. paste=1
  110. ;;
  111. --lang=*)
  112. lang="${1#*=}"
  113. paste=1
  114. ;;
  115. -L)
  116. lang=
  117. paste=1
  118. ;;
  119. -U|--uuid)
  120. if [ $# -ge 2 ]; then
  121. uuid="$2"
  122. shift
  123. else
  124. printf "%s: option '%s' requires an argument\n" "$prog" "$1" >&2
  125. exit 2
  126. fi
  127. ;;
  128. -U?*)
  129. uuid="${1#??}"
  130. ;;
  131. --uuid=*)
  132. uuid="${1#*=}"
  133. ;;
  134. -S|--service)
  135. if [ $# -ge 2 ]; then
  136. service="$2"
  137. shift
  138. else
  139. printf "%s: option '%s' requires an argument\n" "$prog" "$1" >&2
  140. exit 2
  141. fi
  142. ;;
  143. -S?*)
  144. service="${1#??}"
  145. ;;
  146. --service=*)
  147. service="${1#*=}"
  148. ;;
  149. --)
  150. shift
  151. break
  152. ;;
  153. -?*)
  154. printf "%s: unrecognized option '%s'\n" "$prog" "$1" >&2
  155. exit 2
  156. ;;
  157. *)
  158. stdin=0
  159. upload_file "$1"
  160. ;;
  161. esac
  162. shift
  163. done
  164. while [ $# -gt 0 ]; do
  165. stdin=0
  166. upload_file "$1"
  167. shift
  168. done
  169. if [ "$stdin" -ne 0 ]; then
  170. upload
  171. exit $?
  172. fi