abouttreesummaryrefslogcommitdiff
diff options
context:
space:
mode:
authorPatrick2023-09-05 17:08:25 +0200
committerPatrick2023-09-05 17:08:25 +0200
commit504241758d7b832af61939beaf61b0e0574174c4 (patch)
treee0956d4a52c1f9504896f93d92d57d9693d613e2
parent30bde47d1d5d9b6f0b59c318ff16caed6268d1a8 (diff)
downloadmatrix_esp_thesis-504241758d7b832af61939beaf61b0e0574174c4.tar.gz
matrix_esp_thesis-504241758d7b832af61939beaf61b0e0574174c4.zip
start working on Sync example (reply to to_device messages)
-rw-r--r--Todo.md3
-rw-r--r--examples/Sync.c82
-rw-r--r--src/matrix.c27
-rw-r--r--src/matrix.h5
-rw-r--r--src/matrix_http_mongoose.c4
5 files changed, 108 insertions, 13 deletions
diff --git a/Todo.md b/Todo.md
index 23734f7..b8e62c5 100644
--- a/Todo.md
+++ b/Todo.md
@@ -17,4 +17,5 @@
+ add client saving/loading
- overhaul client saving/loading
+ esp compatibility
-- http requests in chunks/dynamically allocated \ No newline at end of file
+- http requests in chunks/dynamically allocated
+- URL encode (sync next_batch) \ No newline at end of file
diff --git a/examples/Sync.c b/examples/Sync.c
index e845056..eafed27 100644
--- a/examples/Sync.c
+++ b/examples/Sync.c
@@ -1,8 +1,16 @@
+#include <mjson.h>
#include <matrix.h>
#include <stdio.h>
-#define SERVER "https://matrix.org"
-#define ACCESS_TOKEN "syt_cHNjaG8_yBvTjVTquGCikvsAenOJ_49mBMO"
+#define SERVER "https://matrix.org"
+#define USER_ID "@pscho:matrix.org"
+#define USERNAME "pscho"
+#define PASSWORD "Wc23EbmB9G3faMq"
+#define DISPLAYNAME "SyncClient"
+
+#define ROOM_ID "!XKFUjAsGrSSrpDFIxB:matrix.org"
+#define EVENT_ID "$_-y42DkC3OmJ_s40gYko7jMwrUQhoXfEut2pMV3E2J8"
+#define SESSION_ID "tzdnJbDrm82D/RpgkZKpILTifQ5Rads+tVzp3ax8+Ls"
int
main(void)
@@ -13,13 +21,71 @@ main(void)
MatrixHttpInit(&client);
- MatrixClientSetAccessToken(&client,
- ACCESS_TOKEN);
+ MatrixClientSetUserId(&client, USER_ID);
+
+ MatrixClientLoginPassword(&client,
+ USERNAME,
+ PASSWORD,
+ DISPLAYNAME);
+
+ MatrixClientGenerateOnetimeKeys(&client, 10);
+ MatrixClientUploadOnetimeKeys(&client);
+ MatrixClientUploadDeviceKey(&client);
+
+
+ static char eventBuffer[1024];
+ MatrixClientGetRoomEvent(&client,
+ ROOM_ID,
+ EVENT_ID,
+ eventBuffer, 1024);
+
+ printf("event: %s\n", eventBuffer);
+
+
+ while (getchar() != 'q') {
+ static char nextBatch[1024];
+
+ static char syncBuffer[1024*50];
+ MatrixClientSync(&client, syncBuffer, 1024*50, nextBatch);
+
+ int res;
+
+ const char * s = syncBuffer;
+ int slen = strlen(syncBuffer);
+
+ {
+ int koff, klen, voff, vlen, vtype, off = 0;
+ for (off = 0; (off = mjson_next(s, slen, off, &koff, &klen,
+ &voff, &vlen, &vtype)) != 0; ) {
+ const char * key = s + koff;
+ const char * val = s + voff;
+
+ printf("%.*s: %.100s\n", klen, key, val);
+ }
+ }
+
+ mjson_get_string(s, slen, "$.next_batch", nextBatch, 1024);
+
+ const char * events;
+ int eventsLen;
+ res =
+ mjson_find(s, slen, "$.to_device.events", &events, &eventsLen);
+
+ if (res != MJSON_TOK_INVALID) {
+ {
+ int koff, klen, voff, vlen, vtype, off = 0;
+ for (off = 0; (off = mjson_next(events, eventsLen, off, &koff, &klen,
+ &voff, &vlen, &vtype)) != 0; ) {
+ const char * val = events + voff;
+
+ printf("%.*s\n", vlen, val);
+ }
+ }
+ }
+ }
+
- static char syncBuffer[40000];
- MatrixClientSync(&client,
- syncBuffer, 40000);
- printf("%s", syncBuffer);
+ MatrixClientDeleteDevice(&client);
MatrixHttpDeinit(&client);
diff --git a/src/matrix.c b/src/matrix.c
index 18b700a..147d919 100644
--- a/src/matrix.c
+++ b/src/matrix.c
@@ -842,11 +842,34 @@ MatrixClientSendEventEncrypted(
bool
MatrixClientSync(
MatrixClient * client,
- char * outSyncBuffer, int outSyncCap)
+ char * outSyncBuffer, int outSyncCap,
+ const char * nextBatch)
{
+ // filter={\"event_fields\":[\"to_device\"]}
+ static char url[MAX_URL_LEN];
+ snprintf(url, MAX_URL_LEN,
+ "/_matrix/client/v3/sync%s",
+ strlen(nextBatch) > 0 ? "?since=" : "");
+
+ int index = strlen(url);
+
+ for (int i = 0; i < strlen(nextBatch); i++) {
+ char c = nextBatch[i];
+
+ if (c == '~') {
+ url[index++] = '%';
+ url[index++] = '7';
+ url[index++] = 'E';
+ }
+ else {
+ url[index++] = c;
+ }
+ }
+ url[index] = '\0';
+
return
MatrixHttpGet(client,
- "/_matrix/client/v3/sync",
+ url,
outSyncBuffer, outSyncCap,
true);
}
diff --git a/src/matrix.h b/src/matrix.h
index 20cdc8c..da0e859 100644
--- a/src/matrix.h
+++ b/src/matrix.h
@@ -17,7 +17,7 @@
#define DEVICE_ID_SIZE 20
#define EXPIRE_MS_SIZE 20
#define REFRESH_TOKEN_SIZE 20
-#define MAX_URL_LEN 128
+#define MAX_URL_LEN 1024
#define OLM_IDENTITY_KEYS_JSON_SIZE 128
#define DEVICE_KEY_SIZE 44
@@ -275,7 +275,8 @@ MatrixClientSendEventEncrypted(
bool
MatrixClientSync(
MatrixClient * client,
- char * outSync, int outSyncCap);
+ char * outSync, int outSyncCap,
+ const char * nextBatch);
bool
MatrixClientGetRoomEvent(
diff --git a/src/matrix_http_mongoose.c b/src/matrix_http_mongoose.c
index b9b2ca3..452e26c 100644
--- a/src/matrix_http_mongoose.c
+++ b/src/matrix_http_mongoose.c
@@ -44,6 +44,10 @@ MatrixHttpCallback(
conn->connection = c;
conn->connected = true;
}
+ if (ev == MG_EV_HTTP_CHUNK)
+ {
+
+ }
if (ev == MG_EV_HTTP_MSG)
{
// Response