diff options
| -rw-r--r-- | .gitmodules | 3 | ||||
| -rw-r--r-- | Makefile | 14 | ||||
| -rw-r--r-- | Readme.md | 1 | ||||
| -rw-r--r-- | examples/Login.c | 27 | ||||
| -rw-r--r-- | examples/Send.c | 25 | ||||
| -rw-r--r-- | examples/SendEncrypted.c | 32 | ||||
| -rw-r--r-- | examples/Sync.c | 31 | ||||
| m--------- | ext/olm | 0 | ||||
| -rw-r--r-- | out/examples/keepme | 0 | ||||
| -rw-r--r-- | src/matrix.c | 38 | ||||
| -rw-r--r-- | src/matrix.h | 50 |
11 files changed, 221 insertions, 0 deletions
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 <stdio.h>
+#include <matrix.h>
+
+#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 <matrix.h>
+
+#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 <matrix.h>
+
+#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 <matrix.h>
+
+#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 +Subproject 66294cf7f66ae380683dbb7f43a16a8922249fc diff --git a/out/examples/keepme b/out/examples/keepme new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/out/examples/keepme 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 <stdbool.h>
+#include <string.h>
+
+#include <olm/olm.h>
+
+
+
+
+
+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
|
