5 void MX_Nick_(entity fh, entity pass, int status);
6 void MX_Nick(string name)
8 if (!matrix_access_token) return;
9 entity pass = new_pure(mx);
12 sprintf("%s/_matrix/client/r0/profile/%s/displayname?access_token=%s", autocvar_matrix_server, matrix_user, matrix_access_token),
18 void MX_Nick_(entity fh, entity pass, int status)
21 case URL_READY_CANWRITE: {
23 fh.url_content_type = "application/json";
24 url_fputs(fh, sprintf("{\"displayname\": \"%s\"}", pass.message));
33 void MX_Messages_(entity fh, entity pass, int status);
34 void MX_Messages(string from)
36 if (!matrix_access_token) return;
37 string s = sprintf("%s/_matrix/client/r0/events?room_id=%s&limit=50&timeout=30000&from=%s&access_token=%s", autocvar_matrix_server, matrix_room, from, matrix_access_token);
45 void MX_Messages_(entity fh, entity pass, int status)
49 LOG_WARNINGF("status: %d", status);
52 case URL_READY_CLOSED: break;
53 case URL_READY_CANREAD: {
55 for (string s; (s = url_fgets(fh)); ) { json = strcat(json, s, "\n"); }
57 int buf = json_parse(json, _json_parse_object);
59 for (int i = 0, n = stof(json_get(buf, "chunk.length")); i < n; ++i) {
60 MX_Handle(buf, sprintf("chunk.%d", i));
62 MX_Messages(json_get(buf, "end"));
69 void MX_Sync_(entity fh, entity pass, int status);
70 void MX_Sync(string since)
72 if (!matrix_access_token) return;
73 string s = strcat(autocvar_matrix_server, "/_matrix/client/r0/sync?");
78 sprintf("filter={\"account_data\":{\"types\":[]},\"presence\":{\"types\":[]},\"room\":{\"rooms\":[\"%s\"]}}&", matrix_room)
83 "filter={\"account_data\":{\"types\":[]},\"presence\":{\"types\":[]},\"room\":{\"rooms\":[]}}&"
86 s = strcat(s, "access_token=", matrix_access_token);
87 url_single_fopen(s, FILE_READ, MX_Sync_, NULL);
89 void MX_Sync_(entity fh, entity pass, int status)
93 LOG_WARNINGF("status: %d", status);
96 case URL_READY_CLOSED: break;
97 case URL_READY_CANREAD: {
99 for (string s; (s = url_fgets(fh)); ) { json = strcat(json, s, "\n"); }
101 int buf = json_parse(json, _json_parse_object);
103 string arr = sprintf("rooms.join.%s.timeline.events", matrix_room);
104 for (int i = 0, n = stof(json_get(buf, sprintf("%s.length", arr))); i < n; ++i) {
105 MX_Handle(buf, sprintf("%s.%d", arr, i));
107 MX_Sync(json_get(buf, "next_batch"));
114 void MX_JLF_(entity fh, entity pass, int status);
115 void MX_Join(string room)
117 if (!matrix_access_token) return;
119 sprintf("%s/_matrix/client/r0/rooms/%s/join?access_token=%s", autocvar_matrix_server, matrix_room, matrix_access_token),
125 void MX_Leave(string room)
127 if (!matrix_access_token) return;
129 sprintf("%s/_matrix/client/r0/rooms/%s/leave?access_token=%s", autocvar_matrix_server, matrix_room, matrix_access_token),
135 void MX_Forget(string room)
137 if (!matrix_access_token) return;
139 sprintf("%s/_matrix/client/r0/rooms/%s/forget?access_token=%s", autocvar_matrix_server, matrix_room, matrix_access_token),
145 void MX_JLF_(entity fh, entity pass, int status)
148 case URL_READY_CANWRITE: {
149 fh.url_content_type = "application/json";
150 url_fputs(fh, sprintf("{}", pass.message));
158 void MX_Typing_(entity fh, entity pass, int status);
159 void MX_Typing(bool state)
161 if (!matrix_access_token) return;
162 entity pass = new_pure(mx);
163 pass.message = state ? "true" : "false";
165 sprintf("%s/_matrix/client/r0/rooms/%s/typing/%s?access_token=%s", autocvar_matrix_server, matrix_room, matrix_user, matrix_access_token),
171 void MX_Typing_(entity fh, entity pass, int status)
174 case URL_READY_CANWRITE: {
176 fh.url_content_type = "application/json";
177 url_fputs(fh, sprintf("{\"typing\": %s, \"timeout\": 30000}", pass.message));
186 void MX_Say_(entity fh, entity pass, int status);
187 void MX_Say(string body)
189 if (!matrix_access_token) return;
191 entity pass = new_pure(mx);
192 pass.message = strzone(body);
194 sprintf("%s/_matrix/client/r0/rooms/%s/send/m.room.message/%d?access_token=%s", autocvar_matrix_server, matrix_room, ++txnid, matrix_access_token),
200 void MX_Say_(entity fh, entity pass, int status)
203 case URL_READY_CANWRITE: {
205 fh.url_content_type = "application/json";
206 url_fputs(fh, sprintf("{\"msgtype\": \"m.text\", \"body\": \"%s\"}", pass.message));
207 strunzone(pass.message); delete(pass);