65 lines
2.0 KiB
Bash
65 lines
2.0 KiB
Bash
#!/bin/sh
|
|
# usage: define the variables below and add a cron job to run it every hour
|
|
|
|
roomid=''
|
|
mods_roomid='' # for notifications
|
|
bot_user_token='syt_...'
|
|
morg_user_token='mct_...'
|
|
user_ids_to_tag='["@user:domain.tld","@other_user.domain.tld"]'
|
|
|
|
curl -s \
|
|
-X GET "https://xmr.mx/_matrix/client/r0/rooms/$roomid/members" \
|
|
-H "Authorization: Bearer $bot_user_token" \
|
|
> /tmp/members.xmrmx.json
|
|
|
|
curl -s \
|
|
-X GET "https://matrix.org/_matrix/client/r0/rooms/$roomid/members" \
|
|
-H "Authorization: Bearer $morg_user_token" \
|
|
> /tmp/members.matrixorg.json
|
|
|
|
jq '.chunk.[] | {membership: .content.membership, user_id: .state_key}' /tmp/members.matrixorg.json \
|
|
| grep -A 1 'membership": "join' \
|
|
| grep user_id \
|
|
| perl -lpe 's/ "user_id": "([^"]+)"/$1/' \
|
|
| sort -u > /tmp/members.matrixorg.csv
|
|
|
|
jq '.chunk.[] | {membership: .content.membership, user_id: .state_key}' /tmp/members.xmrmx.json \
|
|
| grep -A 1 'membership": "join' \
|
|
| grep user_id \
|
|
| perl -lpe 's/ "user_id": "([^"]+)"/$1/' \
|
|
| sort -u > /tmp/members.xmrmx.csv
|
|
|
|
diff /tmp/members.xmrmx.csv /tmp/members.matrixorg.csv | grep '@' > /tmp/discrepancies.txt
|
|
is_positive=$?
|
|
|
|
header="Membership discrepancies between xmr.mx and matrix.org detected:"
|
|
body="$header\n$(cat /tmp/discrepancies.txt | sed 's/$/\\n/' | tr -d '\n')"
|
|
formatted_body="<strong>$header</strong><pre>$(cat /tmp/discrepancies.txt | sed 's/$/\<br\>/' | tr -d '\n')</pre>"
|
|
json_data=$(cat <<EOF
|
|
{
|
|
"msgtype": "m.text",
|
|
"format":"org.matrix.custom.html",
|
|
"body": "$body",
|
|
"formatted_body": "$formatted_body",
|
|
"m.mentions": {
|
|
"user_ids": $user_ids_to_tag
|
|
}
|
|
}
|
|
EOF
|
|
)
|
|
|
|
if [ $is_positive -eq 0 ]; then
|
|
echo Discrepancies found.
|
|
txnId=$(date +%s%N | cut -b1-13)$(shuf -i 1000-9999 -n 1)
|
|
curl -s -X PUT \
|
|
"https://xmr.mx/_matrix/client/v3/rooms/$mods_roomid/send/m.room.message/m$txnId.00" \
|
|
-H "Authorization: Bearer $bot_user_token" \
|
|
-H "Content-Type: application/json" \
|
|
-d "$json_data" \
|
|
-o /dev/null \
|
|
-w "%{http_code}"
|
|
echo ""
|
|
else
|
|
echo No discrepancies found.
|
|
fi
|