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
|
|
|
|
echo "TELODENDRIA_PUB not set."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
TP_DIR="$TELODENDRIA_PUB/patches"
|
|
|
|
|
|
|
|
CURL="curl -s"
|
|
|
|
|
|
|
|
if [ ! -d "$TP_DIR" ]; then
|
|
|
|
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}' |
|
2022-11-11 01:07:49 +00:00
|
|
|
$CURL -X PUT -d @- "$HOMESERVER/client/v3/rooms/$PATCHES_ROOM/send/m.room.message/$(date +%s)?access_token=$ACCESS_TOKEN"
|
2022-08-01 20:21:45 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
matrix_get_files() {
|
|
|
|
$CURL "$HOMESERVER/client/v3/sync?access_token=$ACCESS_TOKEN" |
|
2022-11-11 01:07:49 +00:00
|
|
|
jq ".rooms.join.\"$PATCHES_ROOM\".timeline.events[] | select(.type==\"m.room.message\") | .content | select(.msgtype==\"m.file\") | [.body,.info.size,.url] | @tsv" |
|
2022-08-01 20:21:45 +00:00
|
|
|
cut -d '"' -f 2 |
|
|
|
|
sed 's/\\t/,/g'
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
if [ "$ext" != "patch" ] || [ "$size" -gt "$MAX_SIZE" ] || [ -f "$TP_DIR/ingress/$id.patch" ]; then
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2023-01-09 17:56:24 +00:00
|
|
|
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
|
2022-08-01 20:21:45 +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>"
|
|
|
|
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"
|
2022-08-12 23:08:27 +00:00
|
|
|
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
|
2022-08-13 02:01:27 +00:00
|
|
|
msg="$msg<br><blockquote>$(cat $msgFile)<br>—$DISPLAY_NAME ($MXID)</blockquote>"
|
2022-08-12 23:08:27 +00:00
|
|
|
fi
|
|
|
|
matrix_send "$msg"
|
2022-08-01 20:21:45 +00:00
|
|
|
else
|
|
|
|
echo "Patch #$2 doesn't exist in the queue."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "No action specified."
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|