Xonotic
matrix.qc
Go to the documentation of this file.
1 #include "matrix.qh"
2 
3 .string message;
4 
5 void MX_Nick_(entity fh, entity pass, int status);
6 void MX_Nick(string name)
7 {
8  if (!matrix_access_token) return;
9  entity pass = new_pure(mx);
10  pass.message = name;
12  sprintf("%s/_matrix/client/r0/profile/%s/displayname?access_token=%s", autocvar_matrix_server, matrix_user, matrix_access_token),
13  FILE_WRITE,
14  MX_Nick_,
15  pass
16  );
17 }
18 void MX_Nick_(entity fh, entity pass, int status)
19 {
20  switch (status) {
21  case URL_READY_CANWRITE: {
22  fh.url_verb = "PUT";
23  fh.url_content_type = "application/json";
24  url_fputs(fh, sprintf("{\"displayname\": \"%s\"}", pass.message));
25  delete(pass);
26  url_fclose(fh);
27  break;
28  }
29  }
30 }
31 
32 
33 void MX_Messages_(entity fh, entity pass, int status);
34 void MX_Messages(string from)
35 {
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);
39  s,
40  FILE_READ,
42  NULL
43  );
44 }
45 void MX_Messages_(entity fh, entity pass, int status)
46 {
47  switch (status) {
48  default: {
49  LOG_WARNF("status: %d", status);
50  break;
51  }
52  case URL_READY_CLOSED: break;
53  case URL_READY_CANREAD: {
54  string json = "";
55  for (string s; (s = url_fgets(fh)); ) { json = strcat(json, s, "\n"); }
56  url_fclose(fh);
57  int buf = json_parse(json, _json_parse_object);
58  EXPECT_NE(-1, buf);
59  for (int i = 0, n = stof(json_get(buf, "chunk.length")); i < n; ++i) {
60  MX_Handle(buf, sprintf("chunk.%d", i));
61  }
62  MX_Messages(json_get(buf, "end"));
63  break;
64  }
65  }
66 }
67 
68 
69 void MX_Sync_(entity fh, entity pass, int status);
70 void MX_Sync(string since)
71 {
72  if (!matrix_access_token) return;
73  string s = strcat(autocvar_matrix_server, "/_matrix/client/r0/sync?");
74  if (since) {
75  s = strcat(s,
76  "since=", since, "&",
77  "timeout=30000&",
78  sprintf("filter={\"account_data\":{\"types\":[]},\"presence\":{\"types\":[]},\"room\":{\"rooms\":[\"%s\"]}}&", matrix_room)
79  );
80  } else {
81  s = strcat(s,
82  "timeout=0&",
83  "filter={\"account_data\":{\"types\":[]},\"presence\":{\"types\":[]},\"room\":{\"rooms\":[]}}&"
84  );
85  }
86  s = strcat(s, "access_token=", matrix_access_token);
88 }
89 void MX_Sync_(entity fh, entity pass, int status)
90 {
91  switch (status) {
92  default: {
93  LOG_WARNF("status: %d", status);
94  break;
95  }
96  case URL_READY_CLOSED: break;
97  case URL_READY_CANREAD: {
98  string json = "";
99  for (string s; (s = url_fgets(fh)); ) { json = strcat(json, s, "\n"); }
100  url_fclose(fh);
101  int buf = json_parse(json, _json_parse_object);
102  EXPECT_NE(-1, buf);
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));
106  }
107  MX_Sync(json_get(buf, "next_batch"));
108  break;
109  }
110  }
111 }
112 
113 
114 void MX_JLF_(entity fh, entity pass, int status);
115 void MX_Join(string room)
116 {
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),
120  FILE_WRITE,
121  MX_JLF_,
122  NULL
123  );
124 }
125 void MX_Leave(string room)
126 {
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),
130  FILE_WRITE,
131  MX_JLF_,
132  NULL
133  );
134 }
135 void MX_Forget(string room)
136 {
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),
140  FILE_WRITE,
141  MX_JLF_,
142  NULL
143  );
144 }
145 void MX_JLF_(entity fh, entity pass, int status)
146 {
147  switch (status) {
148  case URL_READY_CANWRITE: {
149  fh.url_content_type = "application/json";
150  url_fputs(fh, sprintf("{%s}", pass.message));
151  url_fclose(fh);
152  break;
153  }
154  }
155 }
156 
157 
158 void MX_Typing_(entity fh, entity pass, int status);
159 void MX_Typing(bool state)
160 {
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),
166  FILE_WRITE,
167  MX_Typing_,
168  pass
169  );
170 }
171 void MX_Typing_(entity fh, entity pass, int status)
172 {
173  switch (status) {
174  case URL_READY_CANWRITE: {
175  fh.url_verb = "PUT";
176  fh.url_content_type = "application/json";
177  url_fputs(fh, sprintf("{\"typing\": %s, \"timeout\": 30000}", pass.message));
178  delete(pass);
179  url_fclose(fh);
180  break;
181  }
182  }
183 }
184 
185 
186 void MX_Say_(entity fh, entity pass, int status);
187 void MX_Say(string body)
188 {
189  if (!matrix_access_token) return;
190  static int txnid;
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),
195  FILE_WRITE,
196  MX_Say_,
197  pass
198  );
199 }
200 void MX_Say_(entity fh, entity pass, int status)
201 {
202  switch (status) {
203  case URL_READY_CANWRITE: {
204  fh.url_verb = "PUT";
205  fh.url_content_type = "application/json";
206  url_fputs(fh, sprintf("{\"msgtype\": \"m.text\", \"body\": \"%s\"}", pass.message));
207  strfree(pass.message);
208  delete(pass);
209  url_fclose(fh);
210  break;
211  }
212  }
213 }
float state
Definition: subs.qh:32
const float URL_READY_CANREAD
Definition: urllib.qh:17
void MX_Sync_(entity fh, entity pass, int status)
Definition: matrix.qc:89
const float URL_READY_CLOSED
Definition: urllib.qh:15
ERASEABLE int json_parse(string in, bool() func)
Definition: json.qc:230
const float URL_READY_CANWRITE
Definition: urllib.qh:16
entity() spawn
string matrix_access_token
Definition: matrix.qh:5
const float FILE_READ
Definition: csprogsdefs.qc:231
void MX_Typing(bool state)
Definition: matrix.qc:159
#define EXPECT_NE(val1, val2)
Definition: test.qh:52
string matrix_room
Definition: matrix.qh:6
#define LOG_WARNF(...)
Definition: log.qh:67
void MX_Join(string room)
Definition: matrix.qc:115
void MX_Nick(string name)
Definition: matrix.qc:6
ERASEABLE string json_get(int buf, string key)
Definition: json.qc:276
spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 f1 s1 strcat(_("Level %s: "), "^BG%s\3\, _("^BGPress ^F2%s^BG to enter the game"))
ERASEABLE void url_fputs(entity e, string s)
Definition: urllib.qc:312
ERASEABLE void url_fclose(entity e)
Definition: urllib.qc:207
void MX_Say(string body)
Definition: matrix.qc:187
#define NULL
Definition: post.qh:17
void MX_Leave(string room)
Definition: matrix.qc:125
string matrix_user
Definition: matrix.qh:4
void MX_Typing_(entity fh, entity pass, int status)
Definition: matrix.qc:171
void MX_Messages(string from)
Definition: matrix.qc:34
const float FILE_WRITE
Definition: csprogsdefs.qc:233
ERASEABLE void url_single_fopen(string url, int mode, url_ready_func rdy, entity pass)
Definition: urllib.qc:87
bool _json_parse_object()
parse a json object
Definition: json.qc:36
void MX_Forget(string room)
Definition: matrix.qc:135
string message
Definition: matrix.qc:3
void MX_Sync(string since)
Definition: matrix.qc:70
void MX_JLF_(entity fh, entity pass, int status)
Definition: matrix.qc:145
#define new_pure(class)
purely logical entities (.origin doesn&#39;t work)
Definition: oo.qh:62
#define strfree(this)
Definition: string.qh:56
void MX_Nick_(entity fh, entity pass, int status)
Definition: matrix.qc:18
#define pass(name, colormin, colormax)
void MX_Messages_(entity fh, entity pass, int status)
Definition: matrix.qc:45
void MX_Say_(entity fh, entity pass, int status)
Definition: matrix.qc:200
ERASEABLE string url_fgets(entity e)
Definition: urllib.qc:287
string autocvar_matrix_server
Definition: matrix.qh:3