From: Patrick Date: Mon, 15 May 2023 16:47:59 +0000 (+0200) Subject: Initial commit. Rudimentary readme, mockup examples without actual implementation... X-Git-Url: https://gitweb.ps.run/matrix_esp_thesis/commitdiff_plain/d43e8671acc5709c192e159e0d91626f0677cdf1 Initial commit. Rudimentary readme, mockup examples without actual implementation, Makefile, Olm submodule --- d43e8671acc5709c192e159e0d91626f0677cdf1 diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..3d6f22a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "ext/olm"] + path = ext/olm + url = https://gitlab.matrix.org/matrix-org/olm/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d19abaa --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +CC=gcc + +C_OPTS=-Wall -Wextra -pedantic +C_OPTS+=-I src/ +C_OPTS+=-I ext/olm/include/ +C_OPTS+=src/matrix.c + +out/examples/%: examples/%.c + $(CC) -o out/examples/$* examples/$*.c $(C_OPTS) + + +.PHONY: examples + +examples: out/examples/Login out/examples/Send out/examples/SendEncrypted out/examples/Sync \ No newline at end of file diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..606f53d --- /dev/null +++ b/Readme.md @@ -0,0 +1 @@ +# Matrix Client Library in C diff --git a/examples/Login.c b/examples/Login.c new file mode 100644 index 0000000..5f07e87 --- /dev/null +++ b/examples/Login.c @@ -0,0 +1,27 @@ +#include +#include + +#define SERVER FixedBuf("matrix.org") +#define USERNAME FixedBuf("@pscho:matrix.org") +#define PASSWORD FixedBuf("abcde") + + +int +main( + int argc, + char **argv) +{ + MatrixClient client; + MatrixClientInit(&client, SERVER); + + MatrixClientLoginPassword(&client, + USERNAME, + PASSWORD); + + static char accessTokenCharBuffer[ACCESS_TOKEN_LEN]; + FixedBuffer accessTokenBuffer = { accessTokenCharBuffer, ACCESS_TOKEN_LEN, 0 }; + MatrixClientGetAccessToken(&client, &accessTokenBuffer); + printf("Access Token: %.*s\n", accessTokenBuffer.len, (char *)accessTokenBuffer.ptr); + + return 0; +} \ No newline at end of file diff --git a/examples/Send.c b/examples/Send.c new file mode 100644 index 0000000..95167cc --- /dev/null +++ b/examples/Send.c @@ -0,0 +1,25 @@ +#include + +#define SERVER FixedBuf("matrix.org") +#define ACCESS_TOKEN FixedBuf("abc") +#define ROOM_ID FixedBuf("!jhpZBTbckszblMYjMK:matrix.org") + +int +main( + int argc, + char **argv) +{ + MatrixClient client; + MatrixClientCreate(&client, + SERVER); + + MatrixClientSetAccessToken(&client, + ACCESS_TOKEN); + + MatrixClientSendEvent(&client, + ROOM_ID, + FixedBuf("m.room.message"), + FixedBuf("{\"body\":\"Hello\",\"msgtype\":\"m.text\"}")); + + return 0; +} \ No newline at end of file diff --git a/examples/SendEncrypted.c b/examples/SendEncrypted.c new file mode 100644 index 0000000..2d3bd74 --- /dev/null +++ b/examples/SendEncrypted.c @@ -0,0 +1,32 @@ +#include + +#define SERVER FixedBuf("matrix.org") +#define ACCESS_TOKEN FixedBuf("abc") +#define ROOM_ID FixedBuf("!jhpZBTbckszblMYjMK:matrix.org") + +int +main( + int argc, + char **argv) +{ + MatrixClient client; + MatrixClientCreate(&client, + SERVER); + + MatrixClientSetAccessToken(&client, + ACCESS_TOKEN); + + MatrixMegolmSession megolm; + MatrixMegolmSessionInit(&megolm); + + MatrixRoomShareMegolmSession(&client, + ROOM_ID, + megolm); + + MatrixClientSendGroupEncrypted(&client, + ROOM_ID, + FixedBuf("m.room.message"), + FixedBuf("{\"body\":\"Hello\",\"msgtype\":\"m.text\"}")); + + return 0; +} \ No newline at end of file diff --git a/examples/Sync.c b/examples/Sync.c new file mode 100644 index 0000000..5043884 --- /dev/null +++ b/examples/Sync.c @@ -0,0 +1,31 @@ +#include + +#define SERVER "matrix.org" +#define ACCESS_TOKEN "abc" +#define ROOM_ID "!jhpZBTbckszblMYjMK:matrix.org" + +int +main( + int argc, + char **argv) +{ + MatrixClient client; + MatrixClientCreate(&client, + SERVER); + + MatrixClientSetAccessToken(&client, + ACCESS_TOKEN); + + static char syncCharBuffer[1024]; + FixedBuffer syncBuffer = { syncCharBuffer, 1024, 0 }; + int syncN = 1; + + while (syncN > 0) + { + MatrixClientSyncN(&client, &syncBuffer, &syncN); + printf("%.*s", syncBuffer.len, (char *)syncBuffer.ptr); + } + printf("\n"); + + return 0; +} \ No newline at end of file diff --git a/ext/olm b/ext/olm new file mode 160000 index 0000000..66294cf --- /dev/null +++ b/ext/olm @@ -0,0 +1 @@ +Subproject commit 66294cf7f66ae380683dbb7f43a16a8922249fc8 diff --git a/out/examples/keepme b/out/examples/keepme new file mode 100644 index 0000000..e69de29 diff --git a/src/matrix.c b/src/matrix.c new file mode 100644 index 0000000..bc0f1ca --- /dev/null +++ b/src/matrix.c @@ -0,0 +1,38 @@ +#include "matrix.h" + +FixedBuffer +FixedBuf(const char * str) +{ + int len = strlen(str); + FixedBuffer result; + result.ptr = (char *)str; + result.size = len; + result.len = len; + return result; +} + + +bool +MatrixClientInit( + MatrixClient * client, + FixedBuffer server +) { + +} + +bool +MatrixClientLoginPassword( + MatrixClient * client, + FixedBuffer username, + FixedBuffer password +) { + +} + +bool +MatrixClientGetAccessToken( + MatrixClient * client, + FixedBuffer * outBuffer +) { + +} diff --git a/src/matrix.h b/src/matrix.h new file mode 100644 index 0000000..d61c59b --- /dev/null +++ b/src/matrix.h @@ -0,0 +1,50 @@ +#ifndef MATRIX__H +#define MATRIX__H + +#include +#include + +#include + + + + + +typedef struct FixedBuffer { + void * ptr; + int size; + int len; +} FixedBuffer; + +FixedBuffer +FixedBuf(const char * str); + + + +#define ACCESS_TOKEN_LEN 20 // TODO: fix + +typedef struct MatrixClient { + OlmAccount * olmAcc; + char accessToken[ACCESS_TOKEN_LEN]; +} MatrixClient; + +bool +MatrixClientInit( + MatrixClient * client, + FixedBuffer server +); + +bool +MatrixClientLoginPassword( + MatrixClient * client, + FixedBuffer username, + FixedBuffer password +); + +bool +MatrixClientGetAccessToken( + MatrixClient * client, + FixedBuffer * outBuffer +); + +#endif