Make send-patch use json instead of jq.
This commit is contained in:
parent
20ebeb9c32
commit
012c334ee5
2 changed files with 132 additions and 124 deletions
|
@ -17,154 +17,147 @@ HS_NAME="$(echo "$MXID" | sed "s/\@\(.*\)\:\(.*\)/\2/")"
|
||||||
|
|
||||||
# Prompts the user for a password, while disabling echo-ing.
|
# Prompts the user for a password, while disabling echo-ing.
|
||||||
readpwd() {
|
readpwd() {
|
||||||
printf "$1"
|
printf "$1"
|
||||||
stty -echo -ctlecho
|
stty -echo -ctlecho
|
||||||
read -r "$2"; echo
|
read -r "$2"
|
||||||
stty echo ctlecho
|
echo
|
||||||
|
stty echo ctlecho
|
||||||
}
|
}
|
||||||
|
|
||||||
# Calls curl, setting the RETURN variable for the actual
|
# Calls curl, setting the RETURN variable for the actual
|
||||||
# reply from the server and the ERROR_CODE variable for a
|
# reply from the server and the ERROR_CODE variable for a
|
||||||
# HTTP error code.
|
# HTTP error code.
|
||||||
curlec() {
|
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)"
|
ERROR_CODE="$(echo "$RETURN" | tail -n1)"
|
||||||
RETURN="$(echo "$RETURN" | sed '$d')"
|
RETURN="$(echo "$RETURN" | sed '$d')"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Prompts user to login and gives out an access token to use
|
# Prompts user to login and gives out an access token to use
|
||||||
# in the ACCESS_TOKEN variable
|
# in the ACCESS_TOKEN variable
|
||||||
matrix_login() {
|
matrix_login() {
|
||||||
# Check authentication methods
|
# Check authentication methods
|
||||||
echo "Checking authentication methods..."
|
echo "Checking authentication methods..."
|
||||||
curlec "$HS_BASE/_matrix/client/v3/login"
|
curlec "$HS_BASE/_matrix/client/v3/login"
|
||||||
AUTH_METHODS=$RETURN
|
AUTH_METHODS=$RETURN
|
||||||
if [ $ERROR_CODE -ne 200 ]; then
|
if [ $ERROR_CODE -ne 200 ]; then
|
||||||
echo "Homeserver does not support login."
|
echo "Homeserver does not support login."
|
||||||
exit 1;
|
exit 1
|
||||||
fi;
|
fi
|
||||||
SUPPORTS_PASSWORD="$(echo "$AUTH_METHODS" | jq -r '.flows[].type' | grep "m.login.password" | wc -l)"
|
if ! echo "$AUTH_METHODS" | grep "m.login.password" >/dev/null; then
|
||||||
if [ $SUPPORTS_PASSWORD -lt 1 ]; then
|
echo "Homeserver does not support password authentication."
|
||||||
echo "Homeserver does not support password authentication."
|
exit 1
|
||||||
exit 1;
|
fi
|
||||||
fi;
|
# Homeserver does support password authentication, so request
|
||||||
# Homeserver does support password authentication, so request
|
# them one.
|
||||||
# them one.
|
if [ -z "$MXPW" ]; then
|
||||||
if [ -z "$MXPW" ]; then
|
readpwd "Enter your Matrix password: " MXPW
|
||||||
readpwd "Enter your Matrix password: " MXPW
|
fi
|
||||||
fi
|
# Tries to login using the "Telodendria Patch Script" device
|
||||||
# Tries to login using the "Telodendria Patch Script" device
|
# name
|
||||||
# name
|
JSON=$(
|
||||||
JSON="$(jq --null-input \
|
printf '{'
|
||||||
--arg username "$UR_NAME" \
|
printf ' "identifier": {'
|
||||||
--arg password "$MXPW" \
|
printf ' "type": "m.id.user",'
|
||||||
--arg idtype "m.id.user" \
|
printf ' "user": %s' "$(json -e $UR_NAME)"
|
||||||
--arg passwordtype "m.login.password" \
|
printf ' },'
|
||||||
'{
|
printf ' "initial_device_display_name": "Telodendria Patch Script",'
|
||||||
"identifier": {
|
printf ' "type": "m.login.password",'
|
||||||
"type": $idtype,
|
printf ' "password": %s' "$(json -e $MXPW)"
|
||||||
"user": $username
|
printf '}'
|
||||||
},
|
)
|
||||||
"initial_device_display_name": "Telodendria Patch Script",
|
curlec -X POST -d "$JSON" $HS_BASE/_matrix/client/v3/login
|
||||||
"type": $passwordtype,
|
LOGIN=$RETURN
|
||||||
"password": $password
|
if [ $ERROR_CODE -ne 200 ]; then
|
||||||
}')"
|
echo "Login failed."
|
||||||
curlec -X POST $HS_BASE/_matrix/client/v3/login --data "$JSON"
|
exit 1
|
||||||
LOGIN=$RETURN
|
fi
|
||||||
if [ $ERROR_CODE -ne 200 ]; then
|
ACCESS_TOKEN=$(echo "$LOGIN" | json -s "access_token->@decode")
|
||||||
echo "Login failed."
|
|
||||||
exit 1;
|
|
||||||
fi;
|
|
||||||
ACCESS_TOKEN="$(echo "$LOGIN" | jq -r .access_token)"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Logs out of Matrix using the ACFESS_TOKEN environment variable
|
# Logs out of Matrix using the ACFESS_TOKEN environment variable
|
||||||
matrix_logout() {
|
matrix_logout() {
|
||||||
if [ -z "$ACCESS_TOKEN" ]; then
|
if [ -z "$ACCESS_TOKEN" ]; then
|
||||||
echo "No access token"
|
echo "No access token"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
curlec -X POST $HS_BASE/_matrix/client/v3/logout -H "Authorization: Bearer $ACCESS_TOKEN"
|
curlec -X POST $HS_BASE/_matrix/client/v3/logout -H "Authorization: Bearer $ACCESS_TOKEN"
|
||||||
LOGOUT=$RETURN
|
LOGOUT=$RETURN
|
||||||
if [ $ERROR_CODE -ne 200 ]; then
|
if [ $ERROR_CODE -ne 200 ]; then
|
||||||
echo "Logout failed."
|
echo "Logout failed."
|
||||||
exit 1
|
exit 1
|
||||||
fi;
|
fi
|
||||||
echo "Logged out."
|
echo "Logged out."
|
||||||
}
|
}
|
||||||
|
|
||||||
send_patch() {
|
send_patch() {
|
||||||
if [ -z "$ACCESS_TOKEN" ]; then
|
if [ -z "$ACCESS_TOKEN" ]; then
|
||||||
matrix_login
|
matrix_login
|
||||||
DO_LOGOUT=1
|
DO_LOGOUT=1
|
||||||
fi
|
fi
|
||||||
# We are sucessfully logged in as our user, now let's
|
# We are sucessfully logged in as our user, now let's
|
||||||
# try to upload and post our patch
|
# try to upload and post our patch
|
||||||
echo "$PATCHFILE"
|
echo "$PATCHFILE"
|
||||||
curlec -X POST "$HS_BASE/_matrix/media/v3/upload" \
|
curlec -X POST "$HS_BASE/_matrix/media/v3/upload" \
|
||||||
-H "Content-Type: text/x-patch" \
|
-H "Content-Type: text/x-patch" \
|
||||||
-H "Authorization: Bearer $ACCESS_TOKEN" \
|
-H "Authorization: Bearer $ACCESS_TOKEN" \
|
||||||
-T "$PATCHFILE"
|
-T "$PATCHFILE"
|
||||||
MXCID=$RETURN
|
MXCID=$RETURN
|
||||||
if [ $ERROR_CODE -ne 200 ]; then
|
if [ $ERROR_CODE -ne 200 ]; then
|
||||||
echo "Upload failed."
|
echo "Upload failed."
|
||||||
matrix_logout
|
matrix_logout
|
||||||
exit 1;
|
exit 1
|
||||||
fi;
|
fi
|
||||||
MXCID="$(echo "$MXCID" | jq -r .content_uri)"
|
MXCID=$(echo "$MXCID" | json -s "content_uri->@decode")
|
||||||
echo "MXC ID: $MXCID"
|
echo "MXC ID: $MXCID"
|
||||||
JSON="$(jq --null-input \
|
JSON=$(
|
||||||
--arg name "$(basename "$PATCHFILE")" \
|
base=$(basename "$PATCHFILE")
|
||||||
--arg mxc "$MXCID" \
|
printf '{'
|
||||||
--arg msgtype "m.file" \
|
printf ' "body": %s,' "$(json -e $base)"
|
||||||
--argjson size "$(wc -c "$PATCHFILE" | cut -d" " -f1)" \
|
printf ' "filename": %s,' "$(json -e $base)"
|
||||||
'{
|
printf ' "info": {'
|
||||||
"body": $name,
|
printf ' "mimetype": "text/x-patch",'
|
||||||
"filename": $name,
|
printf ' "size": %d' $(wc -c "$PATCHFILE" | awk '{print $1}')
|
||||||
"info": {
|
printf ' },'
|
||||||
"mimetype": "text/x-patch",
|
printf ' "msgtype": "m.file",'
|
||||||
"size": $size
|
printf ' "url": %s' "$(json -e $MXCID)"
|
||||||
},
|
printf '}'
|
||||||
"msgtype": $msgtype,
|
)
|
||||||
"url": $mxc
|
curl -X PUT -d "$JSON" -H "Authorization: Bearer $ACCESS_TOKEN" \
|
||||||
}')"
|
"$HS_BASE/_matrix/client/v3/rooms/$PATCHES_ROOM/send/m.room.message/$(date +%s)" \
|
||||||
curl \
|
2>/dev/null >/dev/null && echo "Patch sent."
|
||||||
-X PUT \
|
|
||||||
-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."
|
|
||||||
|
|
||||||
# Log out if we generated an access token
|
# Log out if we generated an access token
|
||||||
if [ "$DO_LOGOUT" -eq "1" ]; then
|
if [ "$DO_LOGOUT" -eq "1" ]; then
|
||||||
matrix_logout
|
matrix_logout
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check if the patch file is valid.
|
# Check if the patch file is valid.
|
||||||
if [ "$(basename "$PATCHFILE" .patch)" = "$PATCHFILE" ] || [ ! -f "$PATCHFILE" ]; then
|
if [ "$(basename "$PATCHFILE" .patch)" = "$PATCHFILE" ] || [ ! -f "$PATCHFILE" ]; then
|
||||||
echo "Format: $0 file.patch"
|
echo "Format: $0 file.patch"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
echo "Sending file '$PATCHFILE'"
|
echo "Sending file '$PATCHFILE'"
|
||||||
echo "Checking homeserver's real address using .well-known..."
|
echo "Checking homeserver's real address using .well-known..."
|
||||||
curlec "https://$HS_NAME/.well-known/matrix/client"
|
curlec "https://$HS_NAME/.well-known/matrix/client"
|
||||||
case "$ERROR_CODE" in
|
case "$ERROR_CODE" in
|
||||||
"200")
|
"200")
|
||||||
WELL_KNOWN=$RETURN
|
WELL_KNOWN=$RETURN
|
||||||
if [ -z "$WELL_KNOWN" ]; then
|
if [ -z "$WELL_KNOWN" ]; then
|
||||||
echo "well-known test returned 200 but no correct input was given."
|
echo "well-known test returned 200 but no correct input was given."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
# well-known entry is correct, we can now store our base endpoint
|
# 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 "$ERROR_CODE"
|
||||||
|
|
||||||
echo "well-known test failed."
|
echo "well-known test failed."
|
||||||
printf "Please enter your homeserver base URL: "
|
printf "Please enter your homeserver base URL: "
|
||||||
read -r HS_BASE; echo
|
read -r HS_BASE
|
||||||
send_patch
|
echo
|
||||||
;;
|
send_patch
|
||||||
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
|
|
19
tools/env.sh
19
tools/env.sh
|
@ -8,10 +8,25 @@ find tools/bin -type f | grep -v CVS | grep -v '#' | while IFS= read -r tool; do
|
||||||
echo "- $(basename $tool)"
|
echo "- $(basename $tool)"
|
||||||
chmod +x "$tool"
|
chmod +x "$tool"
|
||||||
done
|
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
|
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
|
if which makewhatis 2>&1 > /dev/null; then
|
||||||
makewhatis "$(pwd)/man"
|
makewhatis "$(pwd)/man"
|
||||||
fi
|
fi
|
||||||
|
|
Loading…
Reference in a new issue