Matrix Docker Ansible eploy
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

91 linhas
3.2 KiB

  1. #!/bin/sh
  2. # Creates a management room for a Meowlnir bot, with the given users able to command the bot there.
  3. #
  4. # The room is created by the bot itself, impersonated through the appservice token, so no human account's credentials are needed.
  5. #
  6. # The `trusted_private_chat` preset is what gives the invited users their standing, and it does the right thing on both old and new room versions: on rooms supporting MSC4289 every invitee becomes an additional creator, and on older ones each is given power level 100.
  7. # Either way there is nothing for us to adjust afterwards.
  8. #
  9. # Usage: meowlnir-create-management-room <bot_localpart> <initial_manager_mxid>...
  10. #
  11. # Prints the created room's ID on success.
  12. set -eu
  13. CONFIG_FILE='{{ matrix_bot_meowlnir_config_path }}/config.yaml'
  14. CONTAINER_NAME='matrix-bot-meowlnir'
  15. HOMESERVER_ADDRESS='{{ matrix_bot_meowlnir_config_homeserver_address }}'
  16. HOMESERVER_DOMAIN='{{ matrix_bot_meowlnir_config_homeserver_domain }}'
  17. ROOM_NAME='{{ matrix_bot_meowlnir_management_room_name }}'
  18. ROOM_TOPIC='{{ matrix_bot_meowlnir_management_room_topic | trim }}'
  19. ENCRYPTED='{{ 'true' if matrix_bot_meowlnir_config_encryption_enable else 'false' }}'
  20. if [ $# -lt 2 ]; then
  21. echo "Usage: $(basename "$0") <bot_localpart> <initial_manager_mxid>..." >&2
  22. exit 2
  23. fi
  24. bot_localpart="$1"
  25. shift
  26. bot_mxid="@$bot_localpart:$HOMESERVER_DOMAIN"
  27. as_token="$(awk '$1 == "as_token:" { print $2; exit }' "$CONFIG_FILE" | sed 's/^"//; s/"$//')"
  28. if [ -z "$as_token" ]; then
  29. echo "Could not read as_token from $CONFIG_FILE" >&2
  30. exit 1
  31. fi
  32. urlencode() {
  33. printf '%s' "$1" | sed 's/%/%25/g; s/!/%21/g; s/:/%3A/g; s/@/%40/g; s/\$/%24/g; s/\//%2F/g'
  34. }
  35. jq_run() {
  36. {{ devture_systemd_docker_base_host_command_docker }} exec -i "$CONTAINER_NAME" jq "$@"
  37. }
  38. if [ "$ENCRYPTED" = 'true' ]; then
  39. initial_state='[{"type": "m.room.encryption", "state_key": "", "content": {"algorithm": "m.megolm.v1.aes-sha2"}}]'
  40. else
  41. initial_state='[]'
  42. fi
  43. # Matrix user IDs cannot contain newlines, so splitting on them is safe here.
  44. invitees="$(printf '%s\n' "$@" | jq_run -R -s 'split("\n") | map(select(length > 0))')"
  45. create_body="$(jq_run -n \
  46. --arg name "$ROOM_NAME" \
  47. --arg topic "$ROOM_TOPIC" \
  48. --argjson invitees "$invitees" \
  49. --argjson initial_state "$initial_state" \
  50. '{preset: "trusted_private_chat", name: $name, topic: $topic, invite: $invitees, initial_state: $initial_state}')"
  51. user_id_param="$(urlencode "$bot_mxid")"
  52. # Runs curl inside the container, because the homeserver is only reachable over the container network.
  53. # Prints the body, with the HTTP status code on the final line.
  54. response="$({{ devture_systemd_docker_base_host_command_docker }} exec "$CONTAINER_NAME" \
  55. curl -sS -X POST \
  56. -H "Authorization: Bearer $as_token" \
  57. -H 'Content-Type: application/json' \
  58. -d "$create_body" \
  59. -w '\n%{http_code}' \
  60. "$HOMESERVER_ADDRESS/_matrix/client/v3/createRoom?user_id=$user_id_param")"
  61. status="$(printf '%s\n' "$response" | tail -n 1)"
  62. if [ "$status" != '200' ]; then
  63. echo "Creating the management room failed with HTTP $status:" >&2
  64. printf '%s\n' "$response" | sed '$d' >&2
  65. exit 1
  66. fi
  67. room_id="$(printf '%s\n' "$response" | sed '$d' | jq_run -r '.room_id')"
  68. if [ -z "$room_id" ] || [ "$room_id" = 'null' ]; then
  69. echo 'The homeserver did not return a room ID' >&2
  70. exit 1
  71. fi
  72. printf '%s\n' "$room_id"