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
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
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
\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
}\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
\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
} 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
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