SERVER_URL='http://127.0.0.1:8008' CURL='curl -g -s' req() { # req "$@" local _out=$(mktemp) local method="$1" local url="$2" shift 2 ${CURL} "-X$method" -o "$_out" "$url" "$@" echo "$_out" } matrix_set_server() { SERVER_URL="$1" } matrix_login_flow() { ${CURL} -XGET "$SERVER_URL/_matrix/client/r0/login" } matrix_login_password() { local _user="$1" local _pass="$2" local _out=$(mktemp) ${CURL} -XPOST -o "$_out" -d \ "{\"type\":\"m.login.password\",\"user\":\"$_user\",\"password\":\"$_pass\"}" \ "$SERVER_URL/_matrix/client/r0/login" local _err="$(jq .errcode $_out)" if [[ "$_err" == null ]] then echo "Login success" TOKEN="$(jq -r .access_token $_out)" MXID="$(jq -r .user_id $_out)" DEVID="$(jq -r .device_id $_out)" HOMESERVER="$(jq -r .home_server $_out)" echo "MXID: $MXID, device: $DEVID, server: $HOMESERVER" else echo "Fail with $_err, $(jq .error $_out)" fi } # to sync events, call matrix_sync and the the next_batch # then continue calling it with `since=$NEXT_BATCH` # # to get the full state of all joined rooms, use `full_state=true` matrix_sync() { local _out=$(mktemp) local _url="$SERVER_URL/_matrix/client/r0/sync?access_token=$TOKEN" for arg in "$@"; do _url="$_url&$arg" done ${CURL} -XGET -o "$_out" "$_url" jq . "$_out" } matrix_join() { # arg1: room id (not alias) local _out=$(mktemp) local _url="$SERVER_URL/_matrix/client/r0/rooms/$1/join?access_token=$TOKEN" ${CURL} -XPOST -o "$_out" "$_url" jq . "$_out" } matrix_joined_rooms() { local _out="$(req GET "$SERVER_URL/_matrix/client/r0/joined_rooms?access_token=$TOKEN")" jq . "$_out" } # 6.3.1 ~ 6.3.3 # GET /_matrix/client/r0/rooms/{roomId}/state/{eventType}/{stateKey} # GET /_matrix/client/r0/rooms/{roomId}/state/{eventType} # GET /_matrix/client/r0/rooms/{roomId}/state matrix_lookup_room_state() { # args: roomid, [event type], [state key] local _url="$SERVER_URL/_matrix/client/r0/rooms/$1/state" if [[ -n "$2" ]]; then _url="$_url/$2" if [[ -n "$3" ]]; then _url="$_url/$3" fi fi _url="$_url?access_token=$TOKEN" local _out="$(req GET "$_url")" jq . "$_out" } # 6.3.4 GET /_matrix/client/r0/rooms/{roomId}/members matrix_members() { # arg: room local _out="$(req GET "$SERVER_URL/_matrix/client/r0/rooms/$1/members?access_token=$TOKEN")" jq . "$_out" } # 6.3.5 GET /_matrix/client/r0/rooms/{roomId}/joined_members matrix_joined_members() { # arg: room local _out="$(req GET "$SERVER_URL/_matrix/client/r0/rooms/$1/joined_members?access_token=$TOKEN")" jq . "$_out" } # 6.3.6 GET /_matrix/client/r0/rooms/{roomId}/messages matrix_get_room_message() { # args: room, from=..., to=..., dir=..., limit=..., filter=... local _url="$SERVER_URL/_matrix/client/r0/rooms/$1/messages?access_token=$TOKEN" for arg in "$@"; do _url="$_url&$arg" done local _out="$(req GET "$_url")" jq . "$_out" } # 6.4.3 PUT /_matrix/client/r0/rooms/{roomId}/send/{eventType}/{txnId} matrix_send() { # args: roomid, eventType(m.room.message), txn, msg local _url="$SERVER_URL/_matrix/client/r0/rooms/$1/send/$2/$3?access_token=$TOKEN" local _out="$(req PUT "$_url" -d "$4")" jq . "$_out" } # 6.5.2.1 PUT /_matrix/client/r0/rooms/{roomId}/redact/{eventId}/{txnId} matrix_redact() { # args: roomid, event id, txn, reason local _url="$SERVER_URL/_matrix/client/r0/rooms/$1/redact/$2/$3?access_token=$TOKEN" local _out="$(req PUT "$_url" -d "{\"reason\":\"$4\"}")" jq . "$_out" }