]> gitweb.ps.run Git - matrix_esp_thesis/commitdiff
share, save, load, init, print megolm out sessions
authorPatrick <patrick.schoenberger@posteo.de>
Fri, 21 Jul 2023 21:04:48 +0000 (23:04 +0200)
committerPatrick <patrick.schoenberger@posteo.de>
Fri, 21 Jul 2023 21:04:48 +0000 (23:04 +0200)
examples/Cli.c
src/matrix.c
src/matrix.h

index 4a8e571c9df413bc74b39663511562f25f6c3545..daf79e9c21ca6ccc2ec35678a14d4194b5c3360d 100644 (file)
@@ -160,28 +160,56 @@ ExecuteCommand(
             body);\r
     }\r
     else if (CheckCommand(cmd, "sharesession")) {\r
-        CHECK_ARGS(2, "<user_id> <device_id>")\r
+        CHECK_ARGS(3, "<session_index> <user_id> <device_id>")\r
 \r
-        MatrixClientShareMegolmOutSession(&client,\r
-            args[0],\r
+        int sessionIndex = atoi(args[0]);\r
+\r
+        MatrixClientShareMegolmOutSession(client,\r
             args[1],\r
-            &client->megolmOutSessions[0]);\r
+            args[2],\r
+            &client->megolmOutSessions[sessionIndex]);\r
     }\r
     else if (CheckCommand(cmd, "savesession")) {\r
-        CHECK_ARGS(2, "<filename> <key>")\r
+        CHECK_ARGS(3, "<session_index> <filename> <key>")\r
+\r
+        int sessionIndex = atoi(args[0]);\r
 \r
         MatrixMegolmOutSessionSave(\r
-            &client->megolmOutSessions[0],\r
-            args[0],\r
-            args[1]);\r
+            &client->megolmOutSessions[sessionIndex],\r
+            args[1],\r
+            args[2]);\r
     }\r
     else if (CheckCommand(cmd, "loadsession")) {\r
-        CHECK_ARGS(2, "<filename> <key>")\r
+        CHECK_ARGS(3, "<session_index> <filename> <key>")\r
+\r
+        int sessionIndex = atoi(args[0]);\r
 \r
         MatrixMegolmOutSessionLoad(\r
-            &client->megolmOutSessions[0],\r
-            args[0],\r
-            args[1]);\r
+            &client->megolmOutSessions[sessionIndex],\r
+            args[1],\r
+            args[2]);\r
+    }\r
+    else if (CheckCommand(cmd, "printsessions")) {\r
+        for (int i = 0; i < client->numMegolmOutSessions; i++) {\r
+            printf("%d: %s\t%s\t%s\n", i,\r
+                client->megolmOutSessions[i].roomId,\r
+                client->megolmOutSessions[i].id,\r
+                client->megolmOutSessions[i].key);\r
+        }\r
+    }\r
+    else if (CheckCommand(cmd, "initsession")) {\r
+        CHECK_ARGS(1, "<room_id>")\r
+\r
+        if (! MatrixClientInitMegolmOutSession(client,\r
+            args[0]))\r
+        {\r
+            printf("Maximum number of Megolm sessions reached (%d)\n", NUM_MEGOLM_SESSIONS);\r
+        }\r
+    }\r
+    \r
+    \r
+    else {\r
+        printf("Unknown command\n");\r
     }\r
 #undef CHECK_ARGS\r
 }\r
index fa7303f891e250dfc5d1349e6293ee8bf4d5a7ff..de936de0d47bc31247d4e5a5f4dba95a4bd808ce 100644 (file)
@@ -192,7 +192,7 @@ MatrixMegolmOutSessionInit(
     static uint8_t random[MEGOLM_INIT_RANDOM_SIZE];\r
     Randomize(random, MEGOLM_INIT_RANDOM_SIZE);\r
 \r
-    session->roomId = roomId;\r
+    strncpy(session->roomId, roomId, ROOM_ID_SIZE);\r
 \r
     session->session =\r
         olm_outbound_group_session(session->memory);\r
@@ -268,6 +268,8 @@ MatrixMegolmOutSessionLoad(
     size_t roomIdLen;\r
     fread(&roomIdLen, sizeof(size_t), 1, f);\r
     fread(session->roomId, 1, roomIdLen, f);\r
+    for (int i = roomIdLen; i < ROOM_ID_SIZE; i++)\r
+        session->roomId[i] = '\0';\r
 \r
     size_t pickleBufferLen;\r
     fread(&pickleBufferLen, sizeof(size_t), 1, f);\r
@@ -282,6 +284,9 @@ MatrixMegolmOutSessionLoad(
     \r
     free(pickleBuffer);\r
 \r
+    olm_outbound_group_session_id(session->session, (uint8_t *)session->id, MEGOLM_SESSION_ID_SIZE);\r
+    olm_outbound_group_session_key(session->session, (uint8_t *)session->key, MEGOLM_SESSION_KEY_SIZE);\r
+\r
     fclose(f);\r
 \r
     return true;\r
@@ -816,19 +821,29 @@ MatrixClientGetMegolmOutSession(
         }\r
     }\r
 \r
+    if (MatrixClientInitMegolmOutSession(client, roomId)) {\r
+        *outSession = &client->megolmOutSessions[client->numMegolmOutSessions-1];\r
+        return true;\r
+    }\r
+\r
+    return false;\r
+}\r
+\r
+bool\r
+MatrixClientInitMegolmOutSession(\r
+    MatrixClient * client,\r
+    const char * roomId)\r
+{\r
     if (client->numMegolmOutSessions < NUM_MEGOLM_SESSIONS)\r
     {\r
         MatrixMegolmOutSessionInit(\r
             &client->megolmOutSessions[client->numMegolmOutSessions],\r
             roomId);\r
-\r
-        *outSession = &client->megolmOutSessions[client->numMegolmOutSessions];\r
         \r
         client->numMegolmOutSessions++;\r
 \r
         return true;\r
     }\r
-\r
     return false;\r
 }\r
 \r
index 073f61002d6c2486cd64d0580ef8199230d04d4b..3614b6a74c4f89c91f268d47f8fbc041e654e962 100644 (file)
@@ -11,6 +11,7 @@
 \r
 \r
 #define USER_ID_SIZE 64\r
+#define ROOM_ID_SIZE 128\r
 #define SERVER_SIZE 20\r
 #define ACCESS_TOKEN_SIZE 40\r
 #define DEVICE_ID_SIZE 20\r
@@ -102,7 +103,7 @@ typedef struct MatrixMegolmInSession {
 } MatrixMegolmInSession;\r
 \r
 typedef struct MatrixMegolmOutSession {\r
-    const char * roomId;\r
+    char roomId[ROOM_ID_SIZE];\r
 \r
     OlmOutboundGroupSession * session;\r
     char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE];\r
@@ -264,6 +265,11 @@ MatrixClientSetMegolmOutSession(
     const char * roomId,\r
     MatrixMegolmOutSession session);\r
 \r
+bool\r
+MatrixClientInitMegolmOutSession(\r
+    MatrixClient * client,\r
+    const char * roomId);\r
+\r
 bool\r
 MatrixClientGetOlmSession(\r
     MatrixClient * client,\r