From 03b8fdd80931bd9da17db245f97ecc7f42fe3b0e Mon Sep 17 00:00:00 2001 From: 3wc <3wc.git@doesthisthing.work> Date: Sat, 20 Mar 2021 23:15:44 +0200 Subject: [PATCH] Add `... api login` subcommand --- abra.sh | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 abra.sh diff --git a/abra.sh b/abra.sh new file mode 100644 index 0000000..05741ba --- /dev/null +++ b/abra.sh @@ -0,0 +1,66 @@ +sub_api_login() { + require_binary jq + + if [ -n $1 ]; then + USERID="$1" + else + read -rp "User ID (format: @bot:example.com): " USERID + fi + if [ -n $2 ]; then + DISPLAY_NAME="$2" + else + read -rp "Display name (e.g. Matrix Bot): " DISPLAY_NAME + fi + if [ -n $3 ]; then + PASSWORD="$3" + else + read -rp "Password: " PASSWORD + fi + if [ -n $4 ]; then + USER_HOMESERVER="$4" + else + read -rp "User homeserver (e.g. matrix.example.com): " USER_HOMESERVER + fi + + USERNAME=$(echo "${USERID/@/}" | cut -d':' -f1) + + AUTH_DATA="{ + \"identifier\": { \"type\": \"m.id.user\", \"user\": \"$USERNAME\" }, + \"password\": \"$PASSWORD\", + \"type\": \"m.login.password\" +}" + + debug "Auth data: +$AUTH_DATA" + + AUTH_RESPONSE=$(curl -s -X POST --header 'Content-Type: application/json' -d "$AUTH_DATA" "https://$USER_HOMESERVER/_matrix/client/r0/login") + + TOKEN=$(echo "$AUTH_RESPONSE" | jq -r '.access_token') + DEVICE_ID=$(echo "$AUTH_RESPONSE" | jq -r '.device_id') + + CONFIGURE_DATA="{ + \"UserId\": \"$USERID\", + \"AccessToken\": \"$TOKEN\", + \"HomeServerURL\": \"$DOMAIN\", + \"DeviceId\": \"LSWJCPGUAZ\", + \"Sync\": true, + \"AutoJoinRooms\": true, + \"DisplayName\": \"autono-bot\" +}" + + debug "Auth data: +$CONFIGURE_DATA" + + curl -s -X POST "https://$DOMAIN/admin/configureClient" --data-binary $CONFIGURE_DATA | jq +} + +sub_api() { + CMD="$1" + if type "sub_api_$CMD" > /dev/null 2>&1; then + # shellcheck disable=SC2086 + shift + "sub_api_$CMD" "$@" + else + error "Subcommand 'sub_api_$CMD' not found" + fi +}