1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
SERVER_URL='http://127.0.0.1:8008'
CURL='curl -g -s'
req() { # req <method> <url> "$@"
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"
}
|