Matrix Docker Ansible eploy
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

92 строки
3.3 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. REQUEST_TIMEOUT='{{ matrix_bot_meowlnir_api_request_timeout_seconds }}'
  21. if [ $# -lt 2 ]; then
  22. echo "Usage: $(basename "$0") <bot_localpart> <initial_manager_mxid>..." >&2
  23. exit 2
  24. fi
  25. bot_localpart="$1"
  26. shift
  27. bot_mxid="@$bot_localpart:$HOMESERVER_DOMAIN"
  28. as_token="$(awk '$1 == "as_token:" { print $2; exit }' "$CONFIG_FILE" | sed 's/^"//; s/"$//')"
  29. if [ -z "$as_token" ]; then
  30. echo "Could not read as_token from $CONFIG_FILE" >&2
  31. exit 1
  32. fi
  33. urlencode() {
  34. printf '%s' "$1" | sed 's/%/%25/g; s/!/%21/g; s/:/%3A/g; s/@/%40/g; s/\$/%24/g; s/\//%2F/g'
  35. }
  36. jq_run() {
  37. {{ devture_systemd_docker_base_host_command_docker }} exec -i "$CONTAINER_NAME" jq "$@"
  38. }
  39. if [ "$ENCRYPTED" = 'true' ]; then
  40. initial_state='[{"type": "m.room.encryption", "state_key": "", "content": {"algorithm": "m.megolm.v1.aes-sha2"}}]'
  41. else
  42. initial_state='[]'
  43. fi
  44. # Matrix user IDs cannot contain newlines, so splitting on them is safe here.
  45. invitees="$(printf '%s\n' "$@" | jq_run -R -s 'split("\n") | map(select(length > 0))')"
  46. create_body="$(jq_run -n \
  47. --arg name "$ROOM_NAME" \
  48. --arg topic "$ROOM_TOPIC" \
  49. --argjson invitees "$invitees" \
  50. --argjson initial_state "$initial_state" \
  51. '{preset: "trusted_private_chat", name: $name, topic: $topic, invite: $invitees, initial_state: $initial_state}')"
  52. user_id_param="$(urlencode "$bot_mxid")"
  53. # Runs curl inside the container, because the homeserver is only reachable over the container network.
  54. # Prints the body, with the HTTP status code on the final line.
  55. response="$({{ devture_systemd_docker_base_host_command_docker }} exec "$CONTAINER_NAME" \
  56. curl -sS --max-time "$REQUEST_TIMEOUT" -X POST \
  57. -H "Authorization: Bearer $as_token" \
  58. -H 'Content-Type: application/json' \
  59. -d "$create_body" \
  60. -w '\n%{http_code}' \
  61. "$HOMESERVER_ADDRESS/_matrix/client/v3/createRoom?user_id=$user_id_param")"
  62. status="$(printf '%s\n' "$response" | tail -n 1)"
  63. if [ "$status" != '200' ]; then
  64. echo "Creating the management room failed with HTTP $status:" >&2
  65. printf '%s\n' "$response" | sed '$d' >&2
  66. exit 1
  67. fi
  68. room_id="$(printf '%s\n' "$response" | sed '$d' | jq_run -r '.room_id')"
  69. if [ -z "$room_id" ] || [ "$room_id" = 'null' ]; then
  70. echo 'The homeserver did not return a room ID' >&2
  71. exit 1
  72. fi
  73. printf '%s\n' "$room_id"