]> Git in Space - tootsh/commitdiff
Write 'toot' script
authorMira Ayre <mi@boxin.space>
Mon, 9 Mar 2020 21:04:00 +0000 (21:04 +0000)
committerMira Ayre <mi@boxin.space>
Fri, 9 Jul 2021 19:44:46 +0000 (20:44 +0100)
toot [new file with mode: 0755]

diff --git a/toot b/toot
new file mode 100755 (executable)
index 0000000..721db09
--- /dev/null
+++ b/toot
@@ -0,0 +1,61 @@
+#!/bin/bash
+
+# send a toot
+
+# If any arguments are provided, they form the toot, otherwise stdin is used.
+# If $1 is one of "PUBLIC", "UNLISTED", "PRIVATE", or "DIRECT" (case-sensitive),
+# then the post visibility is set to that, otherwise $1 stays part of the
+# resulting toot.
+
+# config file
+config="${XDG_CONFIG_HOME:-$HOME/.config}/tootsh-toot"
+
+# source the "config file"
+# this needs to set $token and $instance
+[[ -e "$config" ]] || {
+       echo "Missing config file at $config"
+       exit 1
+}
+. "$config"
+
+# verify config
+[[ "$token" == "" ]] && {
+       echo '$token'" needs to be set in config file at $config"
+       exit 1
+}
+[[ "$instance" == "" ]] && {
+       echo '$instance'" needs to be set in config file at $config"
+       exit 1
+}
+
+# post visibility
+visibility="public"
+if [[ "$1" == "PUBLIC" ]] ; then
+       shift
+elif [[ "$1" == "UNLISTED" ]] ; then
+       visibility="unlisted"
+       shift
+elif [[ "$1" == "PRIVATE" ]] ; then
+       visibility="private"
+       shift
+elif [[ "$1" == "DIRECT" ]] ; then
+       visibility="direct"
+       shift
+fi
+
+# get toot content
+toot="$@"
+if [[ "$toot" == "" ]] ; then
+       while read l ; do
+               [[ "$l" == "." ]] && break || toot+="$l\\n"
+       done
+fi
+
+echo -n "tooting... "
+
+itoken="$(echo "$toot" | base64)"
+
+curl -H "Authorization: Bearer $token" -H "Content-Type: application/json" \
+       -H "Idempotency-Key: $itoken" \
+       -d "{\"status\":\"$toot\",\"visibility\":\"$visibility\"}" \
+       "https://$instance/api/v1/statuses" 2>/dev/null | jq '.url'