From 20ebeb9c3216d7e9a682bcb1b82487b16f96f0d4 Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Wed, 8 Mar 2023 22:47:40 +0000 Subject: [PATCH] Switch tp from using jq to json; format td --- tools/bin/td | 33 ++++---- tools/bin/tp | 207 ++++++++++++++++++++++++++--------------------- tools/src/json.c | 2 +- 3 files changed, 132 insertions(+), 110 deletions(-) diff --git a/tools/bin/td b/tools/bin/td index c67d26e..da56f52 100644 --- a/tools/bin/td +++ b/tools/bin/td @@ -49,21 +49,21 @@ fi mod_time() { if [ -n "$1" ] && [ -f "$1" ]; then case "$(uname)" in - Linux|CYGWIN_NT*|Haiku) - stat -c %Y "$1" + Linux | CYGWIN_NT* | Haiku) + stat -c %Y "$1" ;; - *BSD|DragonFly|Minix) - stat -f %m "$1" + *BSD | DragonFly | Minix) + stat -f %m "$1" ;; *) # Platform unknown, force rebuilding the whole # project every time. - echo "0" + echo "0" ;; - esac + esac else - echo "0" - fi + echo "0" + fi } # Substitute shell variables in a stream with their actual value @@ -79,7 +79,7 @@ setsubst() { echo "s|\\\${[a-zA-Z_]*}||g" echo "s|'''|'|g" - ) > "$SED" + ) >"$SED" sed -f "$SED" $@ rm "$SED" @@ -104,7 +104,7 @@ recipe_build() { if [ $(basename "$obj" .o) != "$MAIN" ]; then objs="$objs $obj" fi - + if [ $(mod_time "$src") -ge $(mod_time "../$obj") ]; then echo "CC $(basename $obj)" obj_dir=$(dirname "../$obj") @@ -195,9 +195,9 @@ recipe_site() { echo "$dest" mkdir -p "$dir" - setsubst "$file" > "$dest" + setsubst "$file" >"$dest" done - cd - > /dev/null + cd - >/dev/null find man/ -name '*.[1-9]' | while IFS= read -r man; do dir=$(dirname "$man") @@ -206,7 +206,7 @@ recipe_site() { mkdir -p "$TELODENDRIA_PUB/$dir/" mandoc -Thtml \ -O style=/style.css,man=/man/man%S/%N.%S.html "$man" \ - > "$TELODENDRIA_PUB/$dir/$html.html" + >"$TELODENDRIA_PUB/$dir/$html.html" echo "$TELODENDRIA_PUB/$dir/$html.html" done } @@ -235,7 +235,7 @@ recipe_release() { # Checksum the release tarball. sha256 "Telodendria-v$TELODENDRIA_VERSION.tar.gz" \ - > "Telodendria-v$TELODENDRIA_VERSION.tar.gz.sha256" + >"Telodendria-v$TELODENDRIA_VERSION.tar.gz.sha256" # Sign the release tarball. if [ ! -z "$TELODENDRIA_SIGNIFY_SECRET" ]; then @@ -280,7 +280,7 @@ recipe_patch() { echo " this patch under the conditions specified in it." echo cvs -q diff -uNp $PATCHSET | grep -v '^\? ' - ) > "$PATCH_FILE" + ) >"$PATCH_FILE" "$EDITOR" "$PATCH_FILE" echo "$PATCH_FILE" @@ -288,7 +288,7 @@ recipe_patch() { recipe_diff() { tmp_patch="/tmp/telodendria-$(date +%s).patch" - cvs -q diff -uNp $PATCHSET > "$tmp_patch" + cvs -q diff -uNp $PATCHSET >"$tmp_patch" if [ -z "$PAGER" ]; then PAGER="less -F" fi @@ -306,4 +306,3 @@ done if [ -z "$1" ]; then recipe_build fi - diff --git a/tools/bin/tp b/tools/bin/tp index 21f61ba..d0e069c 100755 --- a/tools/bin/tp +++ b/tools/bin/tp @@ -7,8 +7,8 @@ . "$(pwd)/tools/lib/common.sh" if [ -z "$TELODENDRIA_PUB" ]; then - echo "TELODENDRIA_PUB not set." - exit 1 + echo "TELODENDRIA_PUB not set." + exit 1 fi TP_DIR="$TELODENDRIA_PUB/patches" @@ -16,110 +16,133 @@ TP_DIR="$TELODENDRIA_PUB/patches" CURL="curl -s" if [ ! -d "$TP_DIR" ]; then - echo "$TP_DIR does not exist." - exit 1 + echo "$TP_DIR does not exist." + exit 1 fi matrix_send() { - msg="$1" - if [ ! -z "$msg" ]; then - jq --null-input \ - --arg body "$msg" \ - --arg formatted_body "$msg" \ - --arg format "org.matrix.custom.html" \ - --arg msgtype "m.text" \ - '{"body":$body,"formatted_body":$formatted_body,"format":$format,"msgtype":$msgtype}' | - $CURL -X PUT -d @- "$HOMESERVER/client/v3/rooms/$PATCHES_ROOM/send/m.room.message/$(date +%s)?access_token=$ACCESS_TOKEN" - fi -} - -matrix_get_files() { - $CURL "$HOMESERVER/client/v3/sync?access_token=$ACCESS_TOKEN" | - jq ".rooms.join.\"$PATCHES_ROOM\".timeline.events[] | select(.type==\"m.room.message\") | .content | select(.msgtype==\"m.file\") | [.body,.info.size,.url] | @tsv" | - cut -d '"' -f 2 | - sed 's/\\t/,/g' + msg="$1" + if [ ! -z "$msg" ]; then + ( + printf '{' + printf '"body":' + json -e "$msg" + printf ',"formatted_body":' + json -e "$msg" + printf ',"format":"org.matrix.custom.html",' + printf '"msgtype":"m.text"' + printf '}' + ) | $CURL -X PUT -d @- "$HOMESERVER/client/v3/rooms/$PATCHES_ROOM/send/m.room.message/$(date +%s)?access_token=$ACCESS_TOKEN" + fi } case "$1" in - "ingress") - matrix_get_files | while IFS="," read -r file size url; do - server=$(echo "$url" | cut -d '/' -f 3) - id=$(echo "$url" | cut -d '/' -f 4) - ext=$(echo "$file" | rev | cut -d '.' -f 1 | rev) + "ingress") + timeline="/tmp/timeline.json" + $CURL "$HOMESERVER/client/v3/sync?access_token=$ACCESS_TOKEN" | + json -s "rooms->join->${PATCHES_ROOM}->timeline" >"$timeline" - if [ "$ext" != "patch" ] || [ "$size" -gt "$MAX_SIZE" ] || [ -f "$TP_DIR/ingress/$id.patch" ]; then - continue - fi + length=$(cat "$timeline" | json -s "events->@length") - if ! $CURL -o "$TP_DIR/ingress/$id.patch" "$HOMESERVER/media/v3/download/$server/$id"; then + i=0 + while [ $i -lt $length ]; do + content=$(cat "$timeline" | json -s "events[$i]->content->^body->^formatted_body") + i=$((i + 1)) + + type=$(echo "$content" | json -s "msgtype->@decode") + if [ "$type" != "m.file" ]; then + continue + fi + + size=$(echo "$content" | json -s "info->size") + if [ "$size" -gt "$MAX_SIZE" ]; then + continue + fi + + file=$(echo "$content" | json -s "filename->@decode") + ext=$(echo "$file" | rev | cut -d '.' -f 1 | rev) + if [ "$ext" != "patch" ]; then + continue + fi + + url=$(echo "$content" | json -s "url->@decode") + id=$(echo "$url" | cut -d '/' -f 4) + + if [ -f "$TP_DIR/ingress/$id.patch" ]; then + continue + fi + + server=$(echo "$url" | cut -d '/' -f 3) + + if ! $CURL -o "$TP_DIR/ingress/$id.patch" "$HOMESERVER/media/v3/download/$server/$id"; then rm "$TP_DIR/ingress/$id.patch" echo "Failed to fetch mxc://$server/$id." echo "Will try again next time." continue fi - count=$(cat "$TP_DIR/count.txt") - count=$((count + 1)) - cp "$TP_DIR/ingress/$id.patch" "$TP_DIR/p/$count.patch" - ( - cd "$TP_DIR/queued" - ln -s "../p/$count.patch" "$count.patch" - ) + count=$(cat "$TP_DIR/count.txt") + count=$((count + 1)) + cp "$TP_DIR/ingress/$id.patch" "$TP_DIR/p/$count.patch" + ( + cd "$TP_DIR/queued" + ln -s "../p/$count.patch" "$count.patch" + ) - echo "$count" > "$TP_DIR/count.txt" + echo "$count" >"$TP_DIR/count.txt" - matrix_send "Queued $file as #$count" > /dev/null - done - ;; - "queue") - find "$TP_DIR/queued" -name '*.patch' | while IFS= read -r patch; do - n=$(basename "$patch" .patch) - echo "Patch #$n:" - head -n3 "$patch" - echo - done - ;; - "view") - if [ -f "$TP_DIR/queued/$2.patch" ]; then - less "$TP_DIR/queued/$2.patch" - else - echo "Patch #$2 doesn't exist in the queue." - exit 1 - fi - ;; - "apply") - if [ -f "$TP_DIR/queued/$2.patch" ]; then - patch < "$TP_DIR/queued/$2.patch" - else - echo "Patch #$2 doesn't exist in the queue." - exit 1 - fi - ;; - "reverse") - if [ -f "$TP_DIR/queued/$2.patch" ]; then - patch -R < "$TP_DIR/queued/$2.patch" - else - echo "Patch #$2 doesn't exist in the queue." - exit 1 - fi - ;; - "accept"|"reject") - if [ -f "$TP_DIR/queued/$2.patch" ]; then - mv "$TP_DIR/queued/$2.patch" "$TP_DIR/${1}ed/$2.patch" - msg="Patch #$2 was marked as ${1}ed." - msgFile="/tmp/patchmsg-$(date +%s).txt" - $EDITOR "$msgFile" - if [ -f "$msgFile" ]; then - msg="$msg
$(cat $msgFile)
—$DISPLAY_NAME ($MXID)
" - fi - matrix_send "$msg" - else - echo "Patch #$2 doesn't exist in the queue." - exit 1 - fi - ;; - *) - echo "No action specified." - exit 1 - ;; + matrix_send "Queued $file as #$count" >/dev/null + done + ;; + "queue") + find "$TP_DIR/queued" -name '*.patch' | while IFS= read -r patch; do + n=$(basename "$patch" .patch) + echo "Patch #$n:" + head -n3 "$patch" + echo + done + ;; + "view") + if [ -f "$TP_DIR/queued/$2.patch" ]; then + less "$TP_DIR/queued/$2.patch" + else + echo "Patch #$2 doesn't exist in the queue." + exit 1 + fi + ;; + "apply") + if [ -f "$TP_DIR/queued/$2.patch" ]; then + patch <"$TP_DIR/queued/$2.patch" + else + echo "Patch #$2 doesn't exist in the queue." + exit 1 + fi + ;; + "reverse") + if [ -f "$TP_DIR/queued/$2.patch" ]; then + patch -R <"$TP_DIR/queued/$2.patch" + else + echo "Patch #$2 doesn't exist in the queue." + exit 1 + fi + ;; + "accept" | "reject") + if [ -f "$TP_DIR/queued/$2.patch" ]; then + mv "$TP_DIR/queued/$2.patch" "$TP_DIR/${1}ed/$2.patch" + msg="Patch #$2 was marked as ${1}ed." + msgFile="/tmp/patchmsg-$(date +%s).txt" + $EDITOR "$msgFile" + if [ -f "$msgFile" ]; then + msg="$msg
$(cat $msgFile)
—$DISPLAY_NAME ($MXID)
" + fi + matrix_send "$msg" + else + echo "Patch #$2 doesn't exist in the queue." + exit 1 + fi + ;; + *) + echo "No action specified." + exit 1 + ;; esac diff --git a/tools/src/json.c b/tools/src/json.c index 3aab262..1f98cfb 100644 --- a/tools/src/json.c +++ b/tools/src/json.c @@ -101,7 +101,7 @@ query(char *select, HashMap * json) func = 1; } - else if (keyName[0] == '!') + else if (keyName[0] == '^') { if (JsonValueType(val) == JSON_OBJECT) {