2022-08-01 20:21:45 +00:00
|
|
|
#!/usr/bin/env sh
|
|
|
|
#
|
|
|
|
# tp: "Telodendria Patch"
|
|
|
|
#
|
|
|
|
# This script is used to manage the patch queue.
|
|
|
|
|
2022-11-11 01:07:49 +00:00
|
|
|
. "$(pwd)/tools/lib/common.sh"
|
2022-08-13 02:01:27 +00:00
|
|
|
|
2022-08-01 20:21:45 +00:00
|
|
|
if [ -z "$TELODENDRIA_PUB" ]; then
|
2023-03-08 22:47:40 +00:00
|
|
|
echo "TELODENDRIA_PUB not set."
|
|
|
|
exit 1
|
2022-08-01 20:21:45 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
TP_DIR="$TELODENDRIA_PUB/patches"
|
|
|
|
|
|
|
|
if [ ! -d "$TP_DIR" ]; then
|
2023-03-08 22:47:40 +00:00
|
|
|
echo "$TP_DIR does not exist."
|
|
|
|
exit 1
|
2022-08-01 20:21:45 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
matrix_send() {
|
2023-03-08 22:47:40 +00:00
|
|
|
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 '}'
|
2023-06-12 14:01:56 +00:00
|
|
|
) > /tmp/tp-$$
|
|
|
|
http -X PUT -d @/tmp/tp-$$ "$HOMESERVER/client/v3/rooms/$PATCHES_ROOM/send/m.room.message/$(date +%s)?access_token=$ACCESS_TOKEN"
|
|
|
|
rm /tmp/tp-$$
|
2023-03-08 22:47:40 +00:00
|
|
|
fi
|
2022-08-01 20:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case "$1" in
|
2023-03-08 22:47:40 +00:00
|
|
|
"ingress")
|
|
|
|
timeline="/tmp/timeline.json"
|
2023-04-29 23:28:33 +00:00
|
|
|
http "$HOMESERVER/client/v3/sync?access_token=$ACCESS_TOKEN" |
|
2023-03-08 22:47:40 +00:00
|
|
|
json -s "rooms->join->${PATCHES_ROOM}->timeline" >"$timeline"
|
|
|
|
|
|
|
|
length=$(cat "$timeline" | json -s "events->@length")
|
|
|
|
|
|
|
|
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
|
2022-08-01 20:21:45 +00:00
|
|
|
|
2023-03-08 22:47:40 +00:00
|
|
|
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)
|
2022-08-01 20:21:45 +00:00
|
|
|
|
2023-04-29 23:28:33 +00:00
|
|
|
if ! http "$HOMESERVER/media/v3/download/$server/$id" > "$TP_DIR/ingress/$id.patch"; then
|
2023-01-09 18:01:32 +00:00
|
|
|
rm "$TP_DIR/ingress/$id.patch"
|
2023-01-09 17:56:24 +00:00
|
|
|
echo "Failed to fetch mxc://$server/$id."
|
|
|
|
echo "Will try again next time."
|
|
|
|
continue
|
|
|
|
fi
|
2022-08-01 20:21:45 +00:00
|
|
|
|
2023-03-08 22:47:40 +00:00
|
|
|
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"
|
|
|
|
|
|
|
|
matrix_send "Queued <code>$file</code> as <a href=\"https://telodendria.io/patches/p/$count.patch\">#$count</a>" >/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 <a href=\"https://telodendria.io/patches/p/$2.patch\">#$2</a> was marked as ${1}ed."
|
|
|
|
msgFile="/tmp/patchmsg-$(date +%s).txt"
|
|
|
|
$EDITOR "$msgFile"
|
|
|
|
if [ -f "$msgFile" ]; then
|
|
|
|
msg="$msg<br><blockquote>$(cat $msgFile)<br>—$DISPLAY_NAME ($MXID)</blockquote>"
|
|
|
|
fi
|
|
|
|
matrix_send "$msg"
|
|
|
|
else
|
|
|
|
echo "Patch #$2 doesn't exist in the queue."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "No action specified."
|
|
|
|
exit 1
|
|
|
|
;;
|
2022-08-01 20:21:45 +00:00
|
|
|
esac
|