| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # |
| 3 | # Creates a test room #test:localhost on the Synapse Matrix server. |
| 4 | # Reads credentials from environment variables. |
| 5 | # |
| 6 | # Required environment variables: |
| 7 | # SHOUTRRR_MATRIX_HOST - Matrix server host (e.g., localhost:8008) |
| 8 | # SHOUTRRR_MATRIX_USER - Matrix username |
| 9 | # SHOUTRRR_MATRIX_PASSWORD - Matrix password |
| 10 | # SHOUTRRR_MATRIX_DISABLE_TLS (optional) - Set to "true" to disable TLS |
| 11 | # |
| 12 | |
| 13 | set -euo pipefail |
| 14 | |
| 15 | # Color codes for output |
| 16 | RED='\033[0;31m' |
| 17 | GREEN='\033[0;32m' |
| 18 | YELLOW='\033[1;33m' |
| 19 | NC='\033[0m' # No Color |
| 20 | |
| 21 | # Error handling function |
| 22 | errorscip-shell shell shell-project 0.0.0 error().() { |
| 23 | echoscip-shell system . . echo. -e "${RED}Error: $1${NC}" >&2 |
| 24 | exit 1 |
| 25 | } |
| 26 | |
| 27 | # Info output |
| 28 | infoscip-shell shell shell-project 0.0.0 info().() { |
| 29 | echoscip-shell system . . echo. -e "${GREEN}[INFO]${NC} $1" |
| 30 | } |
| 31 | |
| 32 | # Warning output |
| 33 | warnscip-shell shell shell-project 0.0.0 warn().() { |
| 34 | echoscip-shell system . . echo. -e "${YELLOW}[WARN]${NC} $1" |
| 35 | } |
| 36 | |
| 37 | # Get the script's directory to locate .env file |
| 38 | SCRIPT_DIR="$(cd "$(dirnamescip-shell system . . dirname. "$0")" && pwdscip-shell system . . pwd.)" |
| 39 | |
| 40 | # Load .env file from script's directory if it exists |
| 41 | if [[ -f "${SCRIPT_DIR}/.env" ]]; then |
| 42 | infoscip-shell shell shell-project 0.0.0 info(). "Loading environment variables from ${SCRIPT_DIR}/.env" |
| 43 | source "${SCRIPT_DIR}/.env" |
| 44 | else |
| 45 | warnscip-shell shell shell-project 0.0.0 warn(). "No .env file found in script directory" |
| 46 | fi |
| 47 | |
| 48 | # Check required environment variables |
| 49 | check_env_varsscip-shell shell shell-project 0.0.0 check_env_vars().() { |
| 50 | local missing_vars=() |
| 51 | |
| 52 | if [[ -z "${SHOUTRRR_MATRIX_HOST:-}" ]]; then |
| 53 | missing_vars+=("SHOUTRRR_MATRIX_HOST") |
| 54 | fi |
| 55 | if [[ -z "${SHOUTRRR_MATRIX_USER:-}" ]]; then |
| 56 | missing_vars+=("SHOUTRRR_MATRIX_USER") |
| 57 | fi |
| 58 | if [[ -z "${SHOUTRRR_MATRIX_PASSWORD:-}" ]]; then |
| 59 | missing_vars+=("SHOUTRRR_MATRIX_PASSWORD") |
| 60 | fi |
| 61 | |
| 62 | if [[ ${#missing_vars[@]} -gt 0 ]]; then |
| 63 | errorscip-shell shell shell-project 0.0.0 error(). "Missing required environment variables: ${missing_vars[*]}" |
| 64 | fi |
| 65 | } |
| 66 | |
| 67 | # Determine the base URL for Matrix API |
| 68 | # Default to HTTP (TLS disabled) for local Synapse development |
| 69 | get_base_urlscip-shell shell shell-project 0.0.0 get_base_url().() { |
| 70 | local host="$1" |
| 71 | local disable_tls="${SHOUTRRR_MATRIX_DISABLE_TLS:-true}" |
| 72 | |
| 73 | if [[ "$disable_tls" == "true" ]]; then |
| 74 | echoscip-shell system . . echo. "http://${host}" |
| 75 | else |
| 76 | echoscip-shell system . . echo. "https://${host}" |
| 77 | fi |
| 78 | } |
| 79 | |
| 80 | # Login to Matrix and get access token |
| 81 | loginscip-shell shell shell-project 0.0.0 login().() { |
| 82 | local base_url="$1" |
| 83 | local user="$2" |
| 84 | local password="$3" |
| 85 | |
| 86 | infoscip-shell shell shell-project 0.0.0 info(). "Logging in as ${user}..." |
| 87 | |
| 88 | local response |
| 89 | response=$( |
| 90 | curlscip-shell system . . curl. -s -X POST "${base_url}/_matrix/client/v3/login" \ |
| 91 | -H "Content-Type: application/json" \ |
| 92 | -d "{ |
| 93 | \"type\": \"m.login.password\", |
| 94 | \"identifier\": { |
| 95 | \"type\": \"m.id.user\", |
| 96 | \"user\": \"${user}\" |
| 97 | }, |
| 98 | \"password\": \"${password}\" |
| 99 | }" 2>&1 |
| 100 | ) || errorscip-shell shell shell-project 0.0.0 error(). "Failed to connect to Matrix server at ${base_url}" |
| 101 | |
| 102 | infoscip-shell shell shell-project 0.0.0 info(). "Login response received" |
| 103 | |
| 104 | # Check for errors in response |
| 105 | local error_msg |
| 106 | error_msg=$(echoscip-shell system . . echo. "$response" | jqscip-shell system . . jq. -r '.errcode // empty' 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.) |
| 107 | if [[ -n "$error_msg" && "$error_msg" != "null" ]]; then |
| 108 | local error_desc |
| 109 | error_desc=$(echoscip-shell system . . echo. "$response" | jqscip-shell system . . jq. -r '.error // "Unknown error"' 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.) |
| 110 | errorscip-shell shell shell-project 0.0.0 error(). "Login failed: ${error_msg} - ${error_desc}" |
| 111 | fi |
| 112 | |
| 113 | local access_token |
| 114 | access_token=$(echoscip-shell system . . echo. "$response" | jqscip-shell system . . jq. -r '.access_token' 2>/dev/nullscip-shell filesystem . . `/dev/null`.) || errorscip-shell shell shell-project 0.0.0 error(). "Failed to parse login response" |
| 115 | |
| 116 | infoscip-shell shell shell-project 0.0.0 info(). "Access token received successfully" |
| 117 | |
| 118 | if [[ -z "$access_token" || "$access_token" == "null" ]]; then |
| 119 | errorscip-shell shell shell-project 0.0.0 error(). "No access token received from login" |
| 120 | fi |
| 121 | |
| 122 | infoscip-shell shell shell-project 0.0.0 info(). "Login successful" |
| 123 | echoscip-shell system . . echo. "$access_token" |
| 124 | } |
| 125 | |
| 126 | # Create a room with the specified alias |
| 127 | create_roomscip-shell shell shell-project 0.0.0 create_room().() { |
| 128 | local base_url="$1" |
| 129 | local access_token="$2" |
| 130 | local room_alias="$3" |
| 131 | |
| 132 | infoscip-shell shell shell-project 0.0.0 info(). "Creating room with alias ${room_alias}..." |
| 133 | |
| 134 | # Extract local part of the alias (e.g., "test" from "#test:localhost") |
| 135 | local local_alias |
| 136 | local_alias=$(echoscip-shell system . . echo. "$room_alias" | sedscip-shell system . . sed. 's/^#//' | cutscip-shell system . . cut. -d':' -f1) |
| 137 | |
| 138 | local response |
| 139 | response=$( |
| 140 | curlscip-shell system . . curl. -s -X POST "${base_url}/_matrix/client/v3/createRoom" \ |
| 141 | -H "Content-Type: application/json" \ |
| 142 | -H "Authorization: Bearer ${access_token}" \ |
| 143 | -d "{ |
| 144 | \"room_alias_name\": \"${local_alias}\", |
| 145 | \"name\": \"Test Room\", |
| 146 | \"topic\": \"Test room for shoutrrr e2e tests\", |
| 147 | \"visibility\": \"public\" |
| 148 | }" 2>&1 |
| 149 | ) || errorscip-shell shell shell-project 0.0.0 error(). "Failed to create room" |
| 150 | |
| 151 | # Check for errors in response |
| 152 | local error_msg |
| 153 | error_msg=$(echoscip-shell system . . echo. "$response" | jqscip-shell system . . jq. -r '.errcode // empty' 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.) |
| 154 | if [[ -n "$error_msg" && "$error_msg" != "null" ]]; then |
| 155 | local error_desc |
| 156 | error_desc=$(echoscip-shell system . . echo. "$response" | jqscip-shell system . . jq. -r '.error // "Unknown error"' 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true.) |
| 157 | |
| 158 | # Room might already exist - that's okay |
| 159 | if [[ "$error_msg" == "ROOM_ALIAS_EXISTS" ]]; then |
| 160 | warnscip-shell shell shell-project 0.0.0 warn(). "Room alias already exists: ${room_alias}" |
| 161 | return 0 |
| 162 | fi |
| 163 | |
| 164 | errorscip-shell shell shell-project 0.0.0 error(). "Failed to create room: ${error_msg} - ${error_desc}" |
| 165 | fi |
| 166 | |
| 167 | local room_id |
| 168 | room_id=$(echoscip-shell system . . echo. "$response" | jqscip-shell system . . jq. -r '.room_id' 2>/dev/nullscip-shell filesystem . . `/dev/null`.) || errorscip-shell shell shell-project 0.0.0 error(). "Failed to parse room creation response" |
| 169 | |
| 170 | if [[ -z "$room_id" || "$room_id" == "null" ]]; then |
| 171 | errorscip-shell shell shell-project 0.0.0 error(). "No room_id received from room creation" |
| 172 | fi |
| 173 | |
| 174 | infoscip-shell shell shell-project 0.0.0 info(). "Room created successfully with ID: ${room_id}" |
| 175 | echoscip-shell system . . echo. "$room_id" |
| 176 | } |
| 177 | |
| 178 | # Main function |
| 179 | mainscip-shell shell shell-project 0.0.0 main().() { |
| 180 | infoscip-shell shell shell-project 0.0.0 info(). "Starting Matrix test room creation..." |
| 181 | |
| 182 | # Check environment variables |
| 183 | check_env_varsscip-shell shell shell-project 0.0.0 check_env_vars(). |
| 184 | |
| 185 | # Get configuration |
| 186 | local host="${SHOUTRRR_MATRIX_HOST}" |
| 187 | local user="${SHOUTRRR_MATRIX_USER}" |
| 188 | local password="${SHOUTRRR_MATRIX_PASSWORD}" |
| 189 | local room_alias="${SHOUTRRR_MATRIX_ROOM:-#test:localhost}" |
| 190 | |
| 191 | infoscip-shell shell shell-project 0.0.0 info(). "Matrix host: ${host}" |
| 192 | infoscip-shell shell shell-project 0.0.0 info(). "Matrix user: ${user}" |
| 193 | infoscip-shell shell shell-project 0.0.0 info(). "Room alias: ${room_alias}" |
| 194 | |
| 195 | # Get base URL |
| 196 | local base_url |
| 197 | base_url=$(get_base_urlscip-shell shell shell-project 0.0.0 get_base_url(). "$host") |
| 198 | |
| 199 | # Login to get access token |
| 200 | local access_token |
| 201 | access_token=$(loginscip-shell shell shell-project 0.0.0 login(). "$base_url" "$user" "$password") |
| 202 | |
| 203 | # Create room |
| 204 | local room_id |
| 205 | room_id=$(create_roomscip-shell shell shell-project 0.0.0 create_room(). "$base_url" "$access_token" "$room_alias") |
| 206 | |
| 207 | infoscip-shell shell shell-project 0.0.0 info(). "Successfully created test room ${room_alias} (${room_id})" |
| 208 | infoscip-shell shell shell-project 0.0.0 info(). "Room creation complete!" |
| 209 | } |
| 210 | |
| 211 | # Run main function |
| 212 | mainscip-shell shell shell-project 0.0.0 main(). "$@" |