SERVER_URL='http://127.0.0.1:8008' CURL='curl -g -s' req() { # req "$@" local _out=$(mktemp) ${CURL} "-X$1" -o "$_out" "$2" 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 } 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" }