Make send-patch use json instead of jq.

This commit is contained in:
Jordan Bancino 2023-03-09 00:06:15 +00:00
parent 20ebeb9c32
commit 012c334ee5
2 changed files with 132 additions and 124 deletions

View file

@ -19,7 +19,8 @@ HS_NAME="$(echo "$MXID" | sed "s/\@\(.*\)\:\(.*\)/\2/")"
readpwd() {
printf "$1"
stty -echo -ctlecho
read -r "$2"; echo
read -r "$2"
echo
stty echo ctlecho
}
@ -27,7 +28,7 @@ readpwd() {
# reply from the server and the ERROR_CODE variable for a
# HTTP error code.
curlec() {
RETURN="$(curl -w "\n%{http_code}" "$@" 2> /dev/null)"
RETURN="$(curl -w "\n%{http_code}" "$@" 2>/dev/null)"
ERROR_CODE="$(echo "$RETURN" | tail -n1)"
RETURN="$(echo "$RETURN" | sed '$d')"
}
@ -41,13 +42,12 @@ matrix_login() {
AUTH_METHODS=$RETURN
if [ $ERROR_CODE -ne 200 ]; then
echo "Homeserver does not support login."
exit 1;
fi;
SUPPORTS_PASSWORD="$(echo "$AUTH_METHODS" | jq -r '.flows[].type' | grep "m.login.password" | wc -l)"
if [ $SUPPORTS_PASSWORD -lt 1 ]; then
exit 1
fi
if ! echo "$AUTH_METHODS" | grep "m.login.password" >/dev/null; then
echo "Homeserver does not support password authentication."
exit 1;
fi;
exit 1
fi
# Homeserver does support password authentication, so request
# them one.
if [ -z "$MXPW" ]; then
@ -55,27 +55,24 @@ matrix_login() {
fi
# Tries to login using the "Telodendria Patch Script" device
# name
JSON="$(jq --null-input \
--arg username "$UR_NAME" \
--arg password "$MXPW" \
--arg idtype "m.id.user" \
--arg passwordtype "m.login.password" \
'{
"identifier": {
"type": $idtype,
"user": $username
},
"initial_device_display_name": "Telodendria Patch Script",
"type": $passwordtype,
"password": $password
}')"
curlec -X POST $HS_BASE/_matrix/client/v3/login --data "$JSON"
JSON=$(
printf '{'
printf ' "identifier": {'
printf ' "type": "m.id.user",'
printf ' "user": %s' "$(json -e $UR_NAME)"
printf ' },'
printf ' "initial_device_display_name": "Telodendria Patch Script",'
printf ' "type": "m.login.password",'
printf ' "password": %s' "$(json -e $MXPW)"
printf '}'
)
curlec -X POST -d "$JSON" $HS_BASE/_matrix/client/v3/login
LOGIN=$RETURN
if [ $ERROR_CODE -ne 200 ]; then
echo "Login failed."
exit 1;
fi;
ACCESS_TOKEN="$(echo "$LOGIN" | jq -r .access_token)"
exit 1
fi
ACCESS_TOKEN=$(echo "$LOGIN" | json -s "access_token->@decode")
}
# Logs out of Matrix using the ACFESS_TOKEN environment variable
@ -89,7 +86,7 @@ matrix_logout() {
if [ $ERROR_CODE -ne 200 ]; then
echo "Logout failed."
exit 1
fi;
fi
echo "Logged out."
}
@ -109,30 +106,26 @@ send_patch() {
if [ $ERROR_CODE -ne 200 ]; then
echo "Upload failed."
matrix_logout
exit 1;
fi;
MXCID="$(echo "$MXCID" | jq -r .content_uri)"
exit 1
fi
MXCID=$(echo "$MXCID" | json -s "content_uri->@decode")
echo "MXC ID: $MXCID"
JSON="$(jq --null-input \
--arg name "$(basename "$PATCHFILE")" \
--arg mxc "$MXCID" \
--arg msgtype "m.file" \
--argjson size "$(wc -c "$PATCHFILE" | cut -d" " -f1)" \
'{
"body": $name,
"filename": $name,
"info": {
"mimetype": "text/x-patch",
"size": $size
},
"msgtype": $msgtype,
"url": $mxc
}')"
curl \
-X PUT \
-H "Authorization: Bearer $ACCESS_TOKEN" \
JSON=$(
base=$(basename "$PATCHFILE")
printf '{'
printf ' "body": %s,' "$(json -e $base)"
printf ' "filename": %s,' "$(json -e $base)"
printf ' "info": {'
printf ' "mimetype": "text/x-patch",'
printf ' "size": %d' $(wc -c "$PATCHFILE" | awk '{print $1}')
printf ' },'
printf ' "msgtype": "m.file",'
printf ' "url": %s' "$(json -e $MXCID)"
printf '}'
)
curl -X PUT -d "$JSON" -H "Authorization: Bearer $ACCESS_TOKEN" \
"$HS_BASE/_matrix/client/v3/rooms/$PATCHES_ROOM/send/m.room.message/$(date +%s)" \
--data "$JSON" 2> /dev/null > /dev/null && echo "Patch sent."
2>/dev/null >/dev/null && echo "Patch sent."
# Log out if we generated an access token
if [ "$DO_LOGOUT" -eq "1" ]; then
@ -156,15 +149,15 @@ case "$ERROR_CODE" in
exit 1
fi
# well-known entry is correct, we can now store our base endpoint
HS_BASE="$(printf "$WELL_KNOWN" | jq -r '.["m.homeserver"].base_url')" && send_patch
HS_BASE=$(printf "$WELL_KNOWN" | json -s "m.homeserver->base_url->@decode") && send_patch
;;
* )
*)
echo "$ERROR_CODE"
echo "well-known test failed."
printf "Please enter your homeserver base URL: "
read -r HS_BASE; echo
read -r HS_BASE
echo
send_patch
;;
esac

View file

@ -8,10 +8,25 @@ find tools/bin -type f | grep -v CVS | grep -v '#' | while IFS= read -r tool; do
echo "- $(basename $tool)"
chmod +x "$tool"
done
find tools/src -type f -name '*.c' | while IFS= read -r tool; do
echo "- $(basename $tool .c)"
missing=0
for tool in $(find tools/src -type f -name '*.c'); do
base=$(basename "$tool" .c)
printf "- $base"
if [ ! -f "build/tools/$base" ]; then
printf ' (missing)'
missing=1
fi
echo
done
if [ "$missing" -eq "1" ]; then
echo
echo "Warning: Some tools are missing, which means others"
echo "may not work properly. Build missing tools with td."
fi
if which makewhatis 2>&1 > /dev/null; then
makewhatis "$(pwd)/man"
fi