diclish 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. #!/bin/bash
  2. #
  3. # diclish - Diaspora* cli bash script.
  4. # Version 1.2
  5. # Source: https://notabug.org/uzver/diclish
  6. #
  7. # Based on:
  8. # podwrapper Version 1.5.4
  9. #
  10. # https://whird.jpope.org/podwrapper/
  11. # https://code.jpope.org/jpope/podwrapper
  12. # https://pod.jpope.org/posts/1da068b01d2b01336bd10e5b4d50b4e6
  13. #
  14. # https://kentshikama.com/posts/6c4b0ff01c090133da7b04015765ae01
  15. # https://kentshikama.com/posts/68f70c801c0d0133da7b04015765ae01
  16. #
  17. # License: WTFPL2 http://wtfpl2.com/
  18. # uzver(at)protonmail.ch
  19. #
  20. # Deps: curl, jq, grep, sed, awk, tr, mkdir, mktemp, rm, wc, which
  21. thispath="$(realpath $0)"
  22. thisdir="$(dirname "$thispath")"
  23. dotdir=~/'.diclish'
  24. verbose=0
  25. ssl=1
  26. useragent='Mozilla/5.0 (Windows NT 6.1; rv:41.0) Gecko/20100101 Firefox/41.0'
  27. # FUNCTIONS
  28. USAGE(){
  29. exec=$(basename "$0"); [[ "$exec" ]] || exec="${0##*/}"
  30. cat << EOF
  31. $exec - CLI bash wrapper script for the Diaspora* API
  32. USAGE: $exec [OPTIONS] [<<<"MESSAGE"]
  33. OPTIONS:
  34. -a aspect Specify aspect (public, all, or aspect id number)
  35. -c config Specify config file
  36. (use different configs for different accounts)
  37. -d config Specify config file and set it as default auth
  38. -D Delete config and cookie files
  39. -g id|guid Get post of given id or guid
  40. -h, --help Show this help
  41. -i Post from stdin
  42. -p Post message
  43. -q Less verbose printing
  44. -r Revalidate login token
  45. -s Set new authentification
  46. (register script, save login password)
  47. -v Verbose mode
  48. Notice: For now length of message limited by bash variable size limit.
  49. EXAMPLES:
  50. Set authentification to config file:
  51. $exec -vs
  52. or to custom place:
  53. $exec -vc myConfig -s # save in "$dotdir"
  54. $exec -vc ~/myConfig -s # save in home directory
  55. or to custom place and set it default:
  56. $exec -vd myNewDefaultConfig -s # save in "$dotdir"
  57. $exec -vd ~/myNewDefaultConfig -s # save in home directory
  58. Post message:
  59. $exec
  60. $exec -vp
  61. $exec -a 5173 -p
  62. $exec -c myConfig # with custom config
  63. $exec -d myNewDefaultConfig # with custom config & set it default
  64. Post message from stdin:
  65. $exec <<<"Hello world!"
  66. echo "Hello world!" | $exec
  67. cat myPost.md | $exec
  68. Get post by id:
  69. $exec -g fb2c9e20b2270134e39f451eccdf6dc9
  70. $exec -qg 123456
  71. $exec -c myConfig -g 123456 # with custom config
  72. $exec -d myNewDefaultConfig -g 123456 # with custom config & set it default
  73. EOF
  74. }
  75. CONFIG(){
  76. # Check for config file and create it if it does not exist
  77. if ! (($custconfig)) && ! (($setnew)); then
  78. confdir="$dotdir"
  79. [[ -f "${confdir}/defaultconfig" ]] && source "${confdir}/defaultconfig"
  80. else
  81. ! (($quiet)) && (($verbose)) && [[ $config ]] && echo -e "\e[0;36m""Using config: $config""\e[m"
  82. [[ -f "$config" ]] && confdir=$(dirname "$config")
  83. [[ -d "$confdir" ]] || confdir="$dotdir"
  84. if [[ -f "$config" ]]; then
  85. true
  86. else
  87. if [[ -f "${confdir}/$config" ]]; then
  88. config="${confdir}/$config"
  89. elif [[ -f "${confdir}/${config}.config" ]]; then
  90. config="${confdir}/${config}.config"
  91. fi
  92. fi
  93. fi
  94. if [[ -f "$config" ]]; then
  95. ! (($quiet)) && (($verbose)) && echo -e "\e[0;36m""Using config: $config""\e[m"
  96. # (($custconfig)) && confdir=$(dirname "$config")
  97. if (($setdefconf)); then
  98. ! (($quiet)) && (($verbose)) && echo -e "\e[0;36m""Setting default config: $config""\e[m"
  99. echo "config=\"$config\"">"${confdir}/defaultconfig" && [[ -f "${confdir}/defaultconfig" ]] && chmod 600 "${confdir}/defaultconfig"
  100. fi
  101. else
  102. mkdir -p "$confdir" && chmod 700 "$confdir"
  103. echo -e '\E[37;34m'"\033[1m[notice]\033[0m Creating new config file:"
  104. read -p "What is your Diaspora* handle? (user@pod.example.com): " handle
  105. if ! (($sslsetted)); then
  106. validsslres=0
  107. sslres=y
  108. while ! (($validsslres)); do
  109. read -n 1 -p "Does your pod use SSL? (Y/n): " sslres
  110. if [[ "${sslres,,}" == [Nn] ]]; then
  111. validsslres=1
  112. ssl=0
  113. pod="http://${handle##*@}"
  114. elif [[ "${sslres,,}" == [Yy] || -z "$sslres" ]]; then
  115. validsslres=1
  116. ssl=1
  117. pod="https://${handle##*@}"
  118. else
  119. validsslres=0
  120. echo -e "\e[0;31m\nResponse not valid, please answer y or n\e[m"
  121. fi
  122. done
  123. echo
  124. fi
  125. if (($ssl)); then
  126. pod="https://${handle##*@}"
  127. else
  128. pod="http://${handle##*@}"
  129. fi
  130. username="${handle%@*}"
  131. echo ""
  132. echo "Do you prefer to post publicly or privately?"
  133. echo "1) Public"
  134. echo "2) Private"
  135. echo ""
  136. echo "Or, you can enter the specific aspect id number that you want to use as default"
  137. echo ""
  138. read -p "Preference: (1/2) " aspresponse
  139. if [[ "$aspresponse" == 1 ]]
  140. then
  141. aspect='public'
  142. elif [[ "$aspresponse" == 2 ]]
  143. then
  144. aspect='all'
  145. else
  146. aspect="$aspresponse"
  147. fi
  148. if ! (($custconfig)); then
  149. config="${confdir}/${handle}.config"
  150. fi
  151. source "$config"
  152. if [[ "$clientname" ]]; then
  153. echo -e '\E[37;31m'"\033[1m[error]\033[0m" "Script appears to be registered previously" >&2
  154. echo "Use -D option to delete auth and then -s to try again" >&2
  155. (($verbose)) && echo -e "\e[0;36m""Exiting""\e[m"
  156. exit 1
  157. fi
  158. if [[ ! -f "${confdir}/defaultconfig" || "$(wc -m "${confdir}/defaultconfig"|cut -d ' ' -f 1)" == 0 ]] || (($setdefconf)); then
  159. ! (($quiet)) && (($verbose)) && echo -e "\e[0;36m""Setting default config: $config""\e[m"
  160. echo "config=\"$config\"">"${confdir}/defaultconfig" && [[ -f "${confdir}/defaultconfig" ]] && chmod 600 "${confdir}/defaultconfig"
  161. fi
  162. cookie="${handle}.cookie"
  163. {
  164. cat << EOF
  165. pod="$pod"
  166. username="$username"
  167. handle="$handle"
  168. aspect="$aspect"
  169. useragent="$useragent"
  170. cookie="\${handle}.cookie"
  171. EOF
  172. }>"$config"
  173. [[ -f "$config" ]] && chmod 600 "$config"
  174. if ! (($quiet)) && (($verbose)); then
  175. echo -e "\e[0;36m""These variables have been saved to ${config}:\n"; cat "$config"; echo -e "\e[m"
  176. fi
  177. [[ -f "$config" ]] && chmod 600 "$config"
  178. if ! (($quiet)) && (($verbose)); then
  179. echo -e "\e[0;36m""These variables have been saved to ${config}:""\e[m"
  180. echo -e "\e[0;36m"; cat "$config"; echo -e "\e[m"
  181. fi
  182. fi
  183. source "$config"
  184. [[ -f "${confdir}/${handle}.cookie" ]] || (touch "${confdir}/${handle}.cookie" && chmod 600 "${confdir}/${handle}.cookie")
  185. if [[ "$(which jq >& /dev/null; echo $?)" == 0 ]]; then
  186. jq="jq"
  187. elif [[ -f ~/"bin/jq" ]]; then
  188. jq=~/"bin/jq"
  189. elif [[ -f "${thisdir}/jq" ]]; then
  190. jq="jq"
  191. elif [[ -f "${confdir}/jq" ]]; then
  192. ="jq"
  193. elif [[ -f "${dotdir}/jq" ]]; then
  194. jq="jq"
  195. fi
  196. }
  197. PASSWORD(){
  198. unset password
  199. if [[ "$config" ]] && [[ "$(grep 'password=' "$config")" ]]; then
  200. source "$config"
  201. else
  202. echo ""
  203. prompt="What is your password?: "
  204. while IFS= read -p "$prompt" -r -s -n 1 char
  205. do
  206. if [[ $char == $'\0' ]]; then
  207. break
  208. fi
  209. prompt='*'
  210. password+="$char"
  211. done
  212. echo -e "\n"
  213. fi
  214. }
  215. TOKENS(){
  216. CONFIG
  217. source "$config" && unset password
  218. # cookie="${confdir}/${handle}.cookie"
  219. # [[ -f "$cookie" ]] || (touch "$cookie" && chmod 600 "$cookie")
  220. local tempauth=$(curl -ksS \
  221. -H "User-Agent: $useragent" \
  222. -d "grant_type=authorization_code&client_id=${clientid}&client_secret=${clientsecret}&redirect_uri=${pod}/&code=${authcode}" \
  223. "${pod}"/api/openid_connect/access_tokens \
  224. --cookie-jar "${confdir}/$cookie" --cookie "${confdir}/$cookie")
  225. sed -i '/accesstoken=/d' "$config"
  226. accesstoken=$(echo "$tempauth" | awk -F'access_token":"' '{print $2}' | awk -F'"' '{print $1}') && echo "accesstoken=\"$accesstoken\"">>"$config"
  227. ! (($quiet)) && (($verbose)) && echo -e "\e[0;36m""accesstoken = $accesstoken""\e[m\n"
  228. sed -i '/idtoken=/d' "$config"
  229. idtoken=$(echo "$tempauth" | awk -F'id_token":"' '{print $2}' | awk -F'"' '{print $1}') && echo "idtoken=\"$idtoken\"">>"$config"
  230. ! (($quiet)) && (($verbose)) && echo -e "\e[0;36m""idtoken = $idtoken""\e[m\n"
  231. unset tempauth
  232. unset csrf authtoken authcode accesstoken idtoken
  233. }
  234. REREGTOKENS(){
  235. CONFIG
  236. source "$config"
  237. # cookie="${confdir}/${handle}.cookie"
  238. # [[ -f "$cookie" ]] || (touch "$cookie" && chmod 600 "$cookie")
  239. # tempfile=$(mktemp /tmp/pwtemp.XXXXXX)
  240. # chmod 600 "$tempfile"
  241. # curl -ksS -H "User-Agent: $useragent" "${pod}"/api/openid_connect/clients -d "redirect_uris[]=${pod}/&client_name=$clientname" >"$tempfile"
  242. # clientid=$(cat "$tempfile" | awk -F'client_id":"' '{print $2}' | awk -F'"' '{print $1}')
  243. # clientsecret=$(cat "$tempfile" | awk -F'client_secret":"' '{print $2}' | awk -F'"' '{print $1}')
  244. # if ! (($quiet)) && (($verbose)); then
  245. # echo -e "\e[0;36m""clientid = $clientid""\e[m\n"
  246. # echo -e "\e[0;36m""clientsecret = $clientsecret""\e[m\n"
  247. # fi
  248. csrf=$(curl -ksS -L \
  249. -H "User-Agent: $useragent" \
  250. "${pod}"/api/openid_connect/authorizations/new?client_id="${clientid}"\&redirect_uri="${pod}"/\&response_type=code\&scope=openid%20read%20write\&nonce=hi\&state=hi \
  251. --cookie-jar "${confdir}/$cookie" | tr '\n' ' ' | awk -F'block-form' '{print $2}' | awk -F'authenticity_token" value="' '{print $2}' | awk -F'"' '{print $1}')
  252. ! (($quiet)) && (($verbose)) && echo -e "\e[0;36m""csrf = $csrf""\e[m\n"
  253. # [[ "$clientname" ]] && sed -i '/clientname=/d' "$config"; echo "clientname=\"$clientname\"">>"$config"
  254. # [[ "$clientid" ]] && sed -i '/clientid=/d' "$config"; echo "clientid=\"$clientid\"">>"$config"
  255. # [[ "$clientsecret" ]] && sed -i '/clientsecret=/d' "$config"; echo "clientsecret=\"$clientsecret\"">>"$config"
  256. [[ "$csrf" ]] && sed -i '/csrf=/d' "$config"; echo "csrf=\"$csrf\"">>"$config"
  257. # /bin/rm -f "$tempfile"
  258. if ! [[ "$password" ]]; then
  259. echo -e '\E[37;34m'"\033[1m[notice]\033[0m" "This will be the only time you will be asked for your password"
  260. echo "Your password will be stored plain in config file"
  261. PASSWORD
  262. ## Store password in config file:
  263. sed -i '/password=/d' "$config"
  264. echo password=\"$(sed 's/\"/\\"/g'<<<"$password")\">>"$config"
  265. if (($verbose)); then
  266. echo -e "\e[0;36m""Password have been saved to $config""\e[m"
  267. fi
  268. fi
  269. #'
  270. authtoken=$(curl -ksS -L \
  271. -H "User-Agent: $useragent" \
  272. -H "X-CSRF-TOKEN: $csrf" \
  273. -d "user[username]=${username}&user[password]=${password}&user[remember_me]=1" \
  274. "${pod}"/users/sign_in \
  275. --cookie-jar "${confdir}/$cookie" --cookie "${confdir}/$cookie" | tr '\n' ' ' | awk -F'authenticity_token" value="' '{print $2}' | awk -F'"' '{print $1}')
  276. ! (($quiet)) && (($verbose)) && echo -e "\e[0;36m""authtoken = $authtoken""\e[m\n"
  277. authcode=$(curl -ksS \
  278. -H "User-Agent: $useragent" \
  279. -H "X-CSRF-TOKEN: $authtoken" \
  280. -d "approve=true" \
  281. "${pod}"/api/openid_connect/authorizations \
  282. --cookie-jar "${confdir}/$cookie" --cookie "${confdir}/$cookie" | awk -F'code=' '{print $2}' | awk -F'"' '{print $1}' | awk -F'&' '{print $1}')
  283. ! (($quiet)) && (($verbose)) && echo -e "\e[0;36m""authcode = $authcode""\e[m\n"
  284. [[ "$authtoken" ]] && sed -i '/authtoken=/d' "$config"; echo "authtoken=\"$authtoken\"">>"$config"
  285. [[ "$authcode" ]] && sed -i '/authcode=/d' "$config"; echo "authcode=\"$authcode\"">>"$config"
  286. source "$config"
  287. TOKENS
  288. source "$config"
  289. unset password
  290. # VALIDTOKEN # Infinity retries
  291. if [[ "$accesstoken" ]]; then
  292. if ! (($quiet)); then
  293. (($verbose)) && echo -e '\E[37;32m'"\033[1m[success]\033[0m" "Config updated"
  294. fi
  295. else
  296. echo -e '\E[37;31m'"\033[1m[error]\033[0m" "Did not recieve an access token" >&2
  297. (($verbose)) && echo -e "\e[0;36m""Exiting""\e[m"
  298. exit 1
  299. fi
  300. }
  301. VALIDTOKEN(){
  302. CONFIG
  303. source "$config" && unset password
  304. # cookie="${confdir}/${handle}.cookie"
  305. # [[ -f "$cookie" ]] || (touch "$cookie" && chmod 600 "$cookie")
  306. if ! (($quiet)) && (($verbose)); then
  307. echo -e "\e[0;36m""Validating login tokens...""\e[m"
  308. fi
  309. local tmpauth=$(curl -ksS \
  310. -H "User-Agent: $useragent" \
  311. -H "Authorization: Bearer $accesstoken" \
  312. "${pod}"/api/openid_connect/user_info)
  313. if [[ "$(echo "$tmpauth" | grep error)" ]]; then
  314. if ! (($quiet)); then
  315. # echo -e '\E[37;31m'"\033[1m[error]\033[0m" "Token not valid"
  316. echo -e '\E[37;32m'"\033[1m[warning]\033[0m" "Token not valid" >&2
  317. (($verbose)) && echo -e "\e[0;36""m$(echo -n "$tmpauth" | "$jq" -r '.error_description')""\e[m" >&2
  318. echo -e "Requesting new one..." >&2
  319. fi
  320. # sed -i '/accesstoken=/d' "$config"
  321. # sed -i '/idtoken=/d' "$config"
  322. unset tmpauth
  323. REREGTOKENS
  324. local tmpauth=$(curl -ksS \
  325. -H "User-Agent: $useragent" \
  326. -H "Authorization: Bearer $accesstoken" \
  327. "${pod}"/api/openid_connect/user_info)
  328. fi
  329. source "$config" && unset password
  330. local user=$(echo "$tmpauth" | awk -F'sub":"' '{print $2}' | awk -F'"' '{print $1}')
  331. unset tmpauth
  332. if ! [[ "$user" ]]; then
  333. echo -e '\E[37;31m'"\033[1m[error]\033[0m" "Error validating login token.\nCheck/set your auth and try again" >&2
  334. (($verbose)) && echo -e "\e[0;36m""Exiting""\e[m"
  335. else
  336. if ! (($quiet)); then
  337. (($verbose)) && echo -e '\E[37;32m'"\033[1m[success]\033[0m" "Token valid for user: $user"
  338. (($verbose)) && echo -e "\e[0;36""muser = $user""\e[m\n"
  339. fi
  340. fi
  341. }
  342. REGISTER(){
  343. CONFIG
  344. source "$config"
  345. # cookie="${confdir}/${handle}.cookie"
  346. # [[ -f "$cookie" ]] || (touch "$cookie" && chmod 600 "$cookie")
  347. echo "Registering script"
  348. clientname="$(basename "$0")_$handle"
  349. ! (($quiet)) && (($verbose)) && echo -e "\e[0;36m""clientname = $clientname""\e[m\n"
  350. local tempfile=$(mktemp /tmp/pwtemp.XXXXXX)
  351. [[ -f "$tempfile" ]] && chmod 600 "$tempfile"
  352. curl -ksS \
  353. -H "User-Agent: $useragent" \
  354. "${pod}"/api/openid_connect/clients \
  355. -d "redirect_uris[]=${pod}/&client_name=$clientname" >"$tempfile"
  356. clientid=$(awk -F'client_id":"' '{print $2}' "$tempfile" | awk -F'"' '{print $1}')
  357. clientsecret=$(awk -F'client_secret":"' '{print $2}' "$tempfile" | awk -F'"' '{print $1}')
  358. if ! (($quiet)) && (($verbose)); then
  359. echo -e "\e[0;36m""clientid = $clientid""\e[m\n"
  360. echo -e "\e[0;36m""clientsecret = $clientsecret""\e[m\n"
  361. fi
  362. csrf=$(curl -ksS -L \
  363. -H "User-Agent: $useragent" \
  364. "${pod}"/api/openid_connect/authorizations/new?client_id=${clientid}\&redirect_uri=${pod}/\&response_type=code\&scope=openid%20read%20write\&nonce=hi\&state=hi \
  365. --cookie-jar "${confdir}/$cookie" | tr '\n' ' ' | awk -F'block-form' '{print $2}' | awk -F'authenticity_token" value="' '{print $2}' | awk -F'"' '{print $1}')
  366. ! (($quiet)) && (($verbose)) && echo -e "\e[0;36m""csrf = $csrf""\e[m\n"
  367. echo "clientname=\"$clientname\"">>"$config"
  368. echo "clientid=\"$clientid\"">>"$config"
  369. echo "clientsecret=\"$clientsecret\"">>"$config"
  370. echo "csrf=\"$csrf\"">>"$config"
  371. /bin/rm -f "$tempfile"
  372. if ! [[ "$password" ]]; then
  373. echo -e '\E[37;34m'"\033[1m[notice]\033[0m" "This will be the only time you will be asked for your password"
  374. echo "Your password will be stored plain in config file"
  375. PASSWORD
  376. fi
  377. sed -i '/password=/d' "$config"
  378. echo password=\"$(sed 's/\"/\\"/g'<<<"$password")\">>"$config"
  379. #'
  380. if (($verbose)); then
  381. echo -e "\e[0;36m""Password have been saved to $config""\e[m"
  382. fi
  383. authtoken=$(curl -ksS -L \
  384. -H "User-Agent: $useragent" \
  385. -H "X-CSRF-TOKEN: $csrf" \
  386. -d "user[username]=${username}&user[password]=${password}&user[remember_me]=1" \
  387. "${pod}"/users/sign_in \
  388. --cookie-jar "${confdir}/$cookie" --cookie "${confdir}/$cookie" | tr '\n' ' ' | awk -F'authenticity_token" value="' '{print $2}' | awk -F'"' '{print $1}')
  389. unset password
  390. ! (($quiet)) && (($verbose)) && echo -e "\e[0;36m""authtoken = $authtoken""\e[m\n"
  391. authcode=$(curl -ksS \
  392. -H "User-Agent: $useragent" \
  393. -H "X-CSRF-TOKEN: $authtoken" \
  394. -d "approve=true" \
  395. "${pod}"/api/openid_connect/authorizations \
  396. --cookie-jar "${confdir}/$cookie" --cookie "${confdir}/$cookie" | awk -F'code=' '{print $2}' | awk -F'"' '{print $1}' | awk -F'&' '{print $1}')
  397. ! (($quiet)) && (($verbose)) && echo -e "\e[0;36m""authcode = $authcode""\e[m\n"
  398. echo "authtoken=\"$authtoken\"">>"$config"
  399. echo "authcode=\"$authcode\"">>"$config"
  400. source "$config"
  401. TOKENS
  402. source "$config"
  403. VALIDTOKEN
  404. if [[ "$accesstoken" ]]; then
  405. if ! (($quiet)); then
  406. (($verbose)) && echo -e '\E[37;32m'"\033[1m[success]\033[0m" "Config updated"
  407. fi
  408. else
  409. echo -e '\E[37;31m'"\033[1m[error]\033[0m" "Did not recieve an access token"
  410. (($verbose)) && echo -e "\e[0;36m""Exiting""\e[m"
  411. exit 1
  412. fi
  413. unset csrf authtoken authcode accesstoken
  414. }
  415. GETPOST(){
  416. VALIDTOKEN
  417. local post=$(mktemp /tmp/dipost.XXXXXX)
  418. (($verbose)) && echo -e "\e[0;36m""Getting post ${postid}...""\e[m"
  419. curl -ksS \
  420. -H "User-Agent: $useragent" \
  421. -H "Authorization: Bearer $accesstoken" \
  422. -H "X-CSRF-TOKEN: $authtoken" \
  423. -H "Content-Type: application/json" \
  424. -H 'Accept: application/json' \
  425. "${pod}/posts/${postid}.json" \
  426. --cookie-jar "${confdir}/$cookie" --cookie "${confdir}/$cookie" >"$post"
  427. unset password csrf authtoken authcode accesstoken idtoken
  428. local error=$(grep '{"error' "$post")
  429. if [[ "$error" ]]; then
  430. echo -e '\E[37;31m'"\033[1m[error]\033[0m" "Did not recieve post" >&2
  431. if (($verbose)); then
  432. echo -e "\e[0;36m""$error""\e[m" >&2
  433. echo -e "\e[0;36m""Exiting""\e[m"
  434. fi
  435. exit 1
  436. fi
  437. local postlength=$(wc -m "$post"|cut -d ' ' -f 1)
  438. if (($postlength)); then
  439. # pid=$("$jq" -r '.id' "$post")
  440. local pguid=$("$jq" -r '.guid' "$post")
  441. local posturl="${pod}/posts/$pguid"
  442. local author=$("$jq" -r '.author.name' "$post")
  443. local authorhandle=$("$jq" -r '.author.diaspora_id' "$post")
  444. echo -en "\e[0;36m""${author}"; (($verbose)) && echo -n " (${authorhandle})"; echo -e ":\e[m\n"
  445. local ptxt=$("$jq" -r '.text' "$post")
  446. fi
  447. if [[ "$pguid" ]]; then
  448. echo "$ptxt"
  449. if ! (($quiet)); then
  450. echo -e "\n\e[0;36m""post url: $posturl""\e[m"
  451. fi
  452. else
  453. echo -e '\E[37;31m'"\033[1m[error]\033[0m" "No post returned, please try again or give up" >&2
  454. (($verbose)) && echo -e "\e[0;36m""Exiting""\e[m"
  455. exit 1
  456. fi
  457. /bin/rm -f "$post"
  458. }
  459. POST(){
  460. VALIDTOKEN
  461. local post=$(mktemp /tmp/hzpost.XXXXXX)
  462. jsonstart='{"status_message": {"text": "'
  463. if [[ "$aspectoveride" ]]; then
  464. aspect="$aspectoveride"
  465. fi
  466. jsonend='"},"aspect_ids":"'"$aspect"'"}'
  467. postfile=$(mktemp /tmp/dipost.XXXXXX)
  468. message=$(mktemp /tmp/dimessage.XXXXXX)
  469. [[ -f "$postfile" && -f "$message" ]] && chmod 600 "$postfile" "$message"
  470. echo -n "$jsonstart" >"$postfile"
  471. if ! [[ -t 0 ]] || (($stdin)); then
  472. eval 'stdin=$(cat); echo "$stdin"' >"$message"
  473. else
  474. $EDITOR "$message"
  475. fi
  476. ## Delete leading blank lines
  477. ## & trailing double spaces on none blank lines (Diaspora markdown uses hard line breaks)
  478. sed -i '/./,$!d; s/\(.\+\)[[:space:]]\{2\}$/\1/g' "$message"
  479. msglength=$(wc -m "$message"|cut -d ' ' -f 1)
  480. if ! (($msglength)); then
  481. echo -e '\E[37;31m'"\033[1m[error]\033[0m" "The message is empty. Not posting message" >&2
  482. echo "Not deleting the file \"$message\" in case it is not empty" >&2
  483. /bin/rm -f "$postfile"
  484. (($verbose)) && echo -e "\e[0;36m""Exiting""\e[m"
  485. exit 1
  486. fi
  487. sed 's/\\/\\\\/g; s/\"/\\"/g; s/\($\)/\1\\r\\n/g' "$message" | tr -d "\n" | sed 's/\\r\\n$//'>>"$postfile"
  488. echo -n "$jsonend" >>"$postfile"
  489. (($verbose)) && echo -e "\e[0;36m""Posting...""\e[m"
  490. curl -ksS \
  491. -H "User-Agent: $useragent" \
  492. -H "Authorization: Bearer $accesstoken" \
  493. -H "X-CSRF-TOKEN: $authtoken" \
  494. -H "Content-Type: application/json" \
  495. -H 'Accept: application/json' \
  496. -X POST \
  497. -d @"$postfile" \
  498. "${pod}/status_messages" \
  499. --cookie-jar "${confdir}/$cookie" --cookie "${confdir}/$cookie" >"$post"
  500. unset password csrf authtoken authcode accesstoken idtoken
  501. local pid=$("$jq" -r '.id' "$post")
  502. local pguid=$("$jq" -r '.guid' "$post")
  503. if [[ "$pid" || "$pguid" ]]; then
  504. if ! (($quiet)); then
  505. if [[ "$pid" ]]; then
  506. local id="$pid"
  507. elif [[ "$pguid" ]]; then
  508. local id="$pguid"
  509. fi
  510. # echo -e '\E[37;32m'"\033[1m[success]\033[0m" "Message posted at ${pod}/posts/$pguid"
  511. echo -e '\E[37;32m'"\033[1m[success]\033[0m" "Message "$id" posted"
  512. echo -en "\e[0;36m""post url: ""\e[m"
  513. fi
  514. echo -e "\e[0;36m""${pod}/posts/$pguid""\e[m"
  515. /bin/rm -f "$postfile" "$message"
  516. else
  517. echo -e '\E[37;31m'"\033[1m[error]\033[0m" "No post guid returned, please try again or give up" >&2
  518. echo "Message should still be saved in the file $message" >&2
  519. /bin/rm -f "$postfile"
  520. if (($verbose)); then
  521. ! (($quiet)) && echo -e "\e[0;36m" && cat "$post" && "\e[m" >&2
  522. echo -e "\e[0;36m""Exiting""\e[m"
  523. fi
  524. exit 1
  525. fi
  526. /bin/rm -f "$post"
  527. }
  528. # DELETE CONFIG & COOKIE
  529. DELETE(){
  530. CONFIG
  531. ! (($quiet)) && echo Deleting: cookie: "${confdir}/$cookie", config: "$config"
  532. [[ "$confdir" && "$handle" ]] && [[ -f "${confdir}/${handle}.cookie" ]] && /bin/rm -f "${confdir}/${handle}.cookie"
  533. [[ "$config" ]] && [[ -f "$config" ]] && /bin/rm -f "$config"
  534. }
  535. if [[ $# -lt 1 ]]; then
  536. exe=1
  537. POST
  538. elif [[ "$1" == '--help' ]]; then
  539. USAGE
  540. exit 0
  541. fi
  542. # RUN OPTIONS
  543. while getopts "a:c:d:Dg:hipqrsS:u:v" OPTION
  544. do
  545. case $OPTION in
  546. a)
  547. if [[ "$OPTARG" ]]; then
  548. aspectoveride="$OPTARG"
  549. fi
  550. ;;
  551. c)
  552. if [[ "$OPTARG" ]]; then
  553. config="$OPTARG"
  554. custconfig=1
  555. fi
  556. ;;
  557. d)
  558. if [[ "$OPTARG" ]]; then
  559. config="$OPTARG"
  560. custconfig=1
  561. fi
  562. setdefconf=1
  563. ;;
  564. D)
  565. exe=1
  566. DELETE
  567. ;;
  568. g)
  569. exe=1
  570. postid="$OPTARG"
  571. GETPOST
  572. ;;
  573. h)
  574. exe=1
  575. USAGE
  576. ;;
  577. i)
  578. stdin=1
  579. ;;
  580. p)
  581. exe=1
  582. POST
  583. ;;
  584. q)
  585. quiet=1
  586. ;;
  587. r)
  588. exe=1
  589. VALIDTOKEN
  590. ;;
  591. s)
  592. exe=1
  593. setnew=1
  594. REGISTER
  595. ;;
  596. S)
  597. sslarg="$OPTARG"
  598. if [[ "$sslarg" ]]; then
  599. if [[ "$sslarg" == [nN][oO] ]]; then
  600. ssl=0
  601. sslsetted=1
  602. elif [[ "$sslarg" == [yY][eE][sS] ]]; then
  603. ssl=1
  604. sslsetted=1
  605. fi
  606. else
  607. ssl=1
  608. sslsetted=1
  609. fi
  610. ;;
  611. u)
  612. if [[ "$OPTARG" ]]; then
  613. "user@example.com:password"
  614. haspass=$(echo "$OPTARG" | awk -F':' '{print $3}')
  615. handle="${OPTARG%%:*}"
  616. if [[ "$haspass" ]]; then
  617. password="${OPTARG#*:}"
  618. unset haspass
  619. fi
  620. username="${handle%@*}"
  621. domain="${handle##*@}"
  622. if (($ssl)); then
  623. pod="https://${domain}"
  624. else
  625. pod="http://${domain}"
  626. fi
  627. inlineauth=1
  628. fi
  629. ;;
  630. v)
  631. verbose=1
  632. ;;
  633. \?)
  634. # echo -e '\e[37;34m'"\e[1m[notice]\e[0m" "Invalid option: -$OPTARG" >&2
  635. echo -e '\e[37;34m'"\e[1m[notice]\e[0m" "Invalid option" >&2
  636. (($verbose)) && echo -e "\e[0;36m""Exiting""\e[m"
  637. exit 1
  638. ;;
  639. :)
  640. echo -e '\e[37;34m'"\e[1m[notice]\e[0m" "Option -$OPTARG requires an argument" >&2
  641. (($verbose)) && echo -e "\e[0;36m""Exiting""\e[m"
  642. exit 1
  643. ;;
  644. esac
  645. done
  646. shift $(($OPTIND -1))
  647. ## Post if no other functional commands was given
  648. if ! (($exe)); then
  649. exe=1
  650. POST
  651. fi
  652. exit 0
  653. ### TODO:
  654. # inline auth
  655. # use keyring
  656. # delete post
  657. # comment post
  658. # delete comment
  659. # reshare post
  660. # like, dislike post
  661. # attach image to post
  662. # read post + comments
  663. # notifications
  664. # debug
  665. # version option
  666. # more errors handle