Raw paste - Back to the pastebin
  1. #!/bin/sh
  2. # This software is released into 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. service="${SICPLOAD_SERVICE:-https://sicp.me/}"
  17. uuid="${SICPLOAD_UUID:-}"
  18. prog="$(basename "$0")"
  19. paste=0
  20. stdin=1
  21. lang=
  22. usage() {
  23. printf "Usage: %s < file\n" "$prog"
  24. printf " command | %s\n" "$prog"
  25. printf " %s file1 file2 file3 ...\n" "$prog"
  26. printf "\n"
  27. printf "Options (applies for all files following the flag):\n"
  28. printf " -S, --service=ADDR uses ADDR as the service URL instead of https://sicp.me/\n"
  29. printf " -U, --uuid=UUID uses UUID for authentication with the service\n"
  30. printf " -u, --upload upload files from this flag on to the file hosting service (default)\n"
  31. printf " -p, --paste paste files from this flag on to the pastebin\n"
  32. printf " -l, --lang=LANG syntax highlight using language LANG (implies -p)\n"
  33. printf " -l-, --lang=- guess language to highlight (inaccurate) (implies -p)\n"
  34. printf " -L, --lang= do not highlight paste, serve raw plaintext (implies -p)\n"
  35. printf " -h, --help display this help and exit\n"
  36. printf " -- stop processing options and treat all following arguments as filenames\n"
  37. printf "\n"
  38. printf "Example: %s foo -lsh bar -u baz -p quux -L qwe asd\n" "$prog"
  39. printf " will post 'foo' and 'baz' as files, 'bar' and 'quux' as .sh pastes and 'qwe' and 'asd' as raw pastes\n"
  40. exit
  41. }
  42. upload() {
  43. file=''
  44. auth=''
  45. if [ $# -ge 1 ]; then
  46. filename="$(printf "%s" "$(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. if [ "$paste" -ne 0 ]; then
  55. if [ -n "$lang" ]; then
  56. url="$(curl -sS $auth -F "lang=$lang" -F 'paste=<-' "$service")"
  57. else
  58. url="$(curl -sS $auth -F 'paste=<-' "$service")"
  59. fi
  60. else
  61. url="$(curl --progress-bar $auth -F "file=@-$file" "$service")"
  62. fi
  63. case "$url" in
  64. http*)
  65. printf "%s\n" "$url"
  66. ;;
  67. ?*)
  68. printf "%s: error: %s\n" "$prog" "$url" >&2
  69. return 1
  70. ;;
  71. '')
  72. return 1
  73. ;;
  74. esac
  75. }
  76. upload_file() {
  77. url="$(upload "$1" < "$1")"
  78. if [ $? -ne 0 ]; then
  79. exit 1
  80. fi
  81. printf "%s: %s\n" "$1" "$url"
  82. }
  83. while [ $# -gt 0 ]; do
  84. case "$1" in
  85. -h|--help)
  86. usage
  87. ;;
  88. -u|--upload)
  89. paste=0
  90. ;;
  91. -p|--paste)
  92. paste=1
  93. ;;
  94. -l|--lang)
  95. if [ $# -ge 2 ]; then
  96. lang="$2"
  97. shift
  98. else
  99. printf "%s: option '%s' requires an argument\n" "$prog" "$1" >&2
  100. exit 2
  101. fi
  102. paste=1
  103. ;;
  104. -l?*)
  105. lang="${1#-l}"
  106. paste=1
  107. ;;
  108. --lang=*)
  109. lang="${1#*=}"
  110. paste=1
  111. ;;
  112. -L)
  113. lang=
  114. paste=1
  115. ;;
  116. -U|--uuid)
  117. if [ $# -ge 2 ]; then
  118. uuid="$2"
  119. shift
  120. else
  121. printf "%s: option '%s' requires an argument\n" "$prog" "$1" >&2
  122. exit 2
  123. fi
  124. ;;
  125. -U?*)
  126. uuid="${1#-U}"
  127. ;;
  128. --uuid=*)
  129. uuid="${1#*=}"
  130. ;;
  131. -S|--service)
  132. if [ $# -ge 2 ]; then
  133. service="$2"
  134. shift
  135. else
  136. printf "%s: option '%s' requires an argument\n" "$prog" "$1" >&2
  137. exit 2
  138. fi
  139. ;;
  140. -S?*)
  141. service="${1#-S}"
  142. ;;
  143. --service=*)
  144. service="${1#*=}"
  145. ;;
  146. --)
  147. shift
  148. break
  149. ;;
  150. -?*)
  151. printf "%s: unrecognized option '%s'\n" "$prog" "$1" >&2
  152. exit 2
  153. ;;
  154. *)
  155. stdin=0
  156. upload_file "$1"
  157. ;;
  158. esac
  159. shift
  160. done
  161. while [ $# -gt 0 ]; do
  162. stdin=0
  163. upload_file "$1"
  164. shift
  165. done
  166. if [ "$stdin" -ne 0 ]; then
  167. upload
  168. exit $?
  169. fi