From: Patrick Date: Fri, 21 Jul 2023 21:04:48 +0000 (+0200) Subject: share, save, load, init, print megolm out sessions X-Git-Url: https://gitweb.ps.run/matrix_esp_thesis/commitdiff_plain/c7aba5979c820958aa08947903afb47ace496a16 share, save, load, init, print megolm out sessions --- diff --git a/examples/Cli.c b/examples/Cli.c index 4a8e571..daf79e9 100644 --- a/examples/Cli.c +++ b/examples/Cli.c @@ -160,28 +160,56 @@ ExecuteCommand( body); } else if (CheckCommand(cmd, "sharesession")) { - CHECK_ARGS(2, " ") + CHECK_ARGS(3, " ") - MatrixClientShareMegolmOutSession(&client, - args[0], + int sessionIndex = atoi(args[0]); + + MatrixClientShareMegolmOutSession(client, args[1], - &client->megolmOutSessions[0]); + args[2], + &client->megolmOutSessions[sessionIndex]); } else if (CheckCommand(cmd, "savesession")) { - CHECK_ARGS(2, " ") + CHECK_ARGS(3, " ") + + int sessionIndex = atoi(args[0]); MatrixMegolmOutSessionSave( - &client->megolmOutSessions[0], - args[0], - args[1]); + &client->megolmOutSessions[sessionIndex], + args[1], + args[2]); } else if (CheckCommand(cmd, "loadsession")) { - CHECK_ARGS(2, " ") + CHECK_ARGS(3, " ") + + int sessionIndex = atoi(args[0]); MatrixMegolmOutSessionLoad( - &client->megolmOutSessions[0], - args[0], - args[1]); + &client->megolmOutSessions[sessionIndex], + args[1], + args[2]); + } + else if (CheckCommand(cmd, "printsessions")) { + for (int i = 0; i < client->numMegolmOutSessions; i++) { + printf("%d: %s\t%s\t%s\n", i, + client->megolmOutSessions[i].roomId, + client->megolmOutSessions[i].id, + client->megolmOutSessions[i].key); + } + } + else if (CheckCommand(cmd, "initsession")) { + CHECK_ARGS(1, "") + + if (! MatrixClientInitMegolmOutSession(client, + args[0])) + { + printf("Maximum number of Megolm sessions reached (%d)\n", NUM_MEGOLM_SESSIONS); + } + } + + + else { + printf("Unknown command\n"); } #undef CHECK_ARGS } diff --git a/src/matrix.c b/src/matrix.c index fa7303f..de936de 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -192,7 +192,7 @@ MatrixMegolmOutSessionInit( static uint8_t random[MEGOLM_INIT_RANDOM_SIZE]; Randomize(random, MEGOLM_INIT_RANDOM_SIZE); - session->roomId = roomId; + strncpy(session->roomId, roomId, ROOM_ID_SIZE); session->session = olm_outbound_group_session(session->memory); @@ -268,6 +268,8 @@ MatrixMegolmOutSessionLoad( size_t roomIdLen; fread(&roomIdLen, sizeof(size_t), 1, f); fread(session->roomId, 1, roomIdLen, f); + for (int i = roomIdLen; i < ROOM_ID_SIZE; i++) + session->roomId[i] = '\0'; size_t pickleBufferLen; fread(&pickleBufferLen, sizeof(size_t), 1, f); @@ -282,6 +284,9 @@ MatrixMegolmOutSessionLoad( free(pickleBuffer); + olm_outbound_group_session_id(session->session, (uint8_t *)session->id, MEGOLM_SESSION_ID_SIZE); + olm_outbound_group_session_key(session->session, (uint8_t *)session->key, MEGOLM_SESSION_KEY_SIZE); + fclose(f); return true; @@ -816,19 +821,29 @@ MatrixClientGetMegolmOutSession( } } + if (MatrixClientInitMegolmOutSession(client, roomId)) { + *outSession = &client->megolmOutSessions[client->numMegolmOutSessions-1]; + return true; + } + + return false; +} + +bool +MatrixClientInitMegolmOutSession( + MatrixClient * client, + const char * roomId) +{ if (client->numMegolmOutSessions < NUM_MEGOLM_SESSIONS) { MatrixMegolmOutSessionInit( &client->megolmOutSessions[client->numMegolmOutSessions], roomId); - - *outSession = &client->megolmOutSessions[client->numMegolmOutSessions]; client->numMegolmOutSessions++; return true; } - return false; } diff --git a/src/matrix.h b/src/matrix.h index 073f610..3614b6a 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -11,6 +11,7 @@ #define USER_ID_SIZE 64 +#define ROOM_ID_SIZE 128 #define SERVER_SIZE 20 #define ACCESS_TOKEN_SIZE 40 #define DEVICE_ID_SIZE 20 @@ -102,7 +103,7 @@ typedef struct MatrixMegolmInSession { } MatrixMegolmInSession; typedef struct MatrixMegolmOutSession { - const char * roomId; + char roomId[ROOM_ID_SIZE]; OlmOutboundGroupSession * session; char memory[MEGOLM_OUTBOUND_SESSION_MEMORY_SIZE]; @@ -264,6 +265,11 @@ MatrixClientSetMegolmOutSession( const char * roomId, MatrixMegolmOutSession session); +bool +MatrixClientInitMegolmOutSession( + MatrixClient * client, + const char * roomId); + bool MatrixClientGetOlmSession( MatrixClient * client,