+ // get the event type\r
+ STATIC char eventType[128];\r
+ memset(eventType, 0, sizeof(eventType));\r
+ mjson_get_string(event, eventLen, "$.type", eventType, 128);\r
+\r
+ // static variables for verification\r
+ // since verification takes multiple requests\r
+ // data is cleared when verification is finished or started\r
+ static char transactionId[64];\r
+ static char verifyFromDeviceId[DEVICE_ID_SIZE];\r
+ static OlmSAS * olmSas = NULL;\r
+\r
+ // initial verification request, reply that we are ready to verify\r
+ if (strcmp(eventType, "m.key.verification.request") == 0) {\r
+ // reset static data\r
+ memset(transactionId, 0, 64);\r
+ memset(verifyFromDeviceId, 0, DEVICE_ID_SIZE);\r
+ if (olmSas != NULL)\r
+ free(olmSas);\r
+ \r
+ mjson_get_string(event, eventLen, "$.content.transaction_id", transactionId, 64);\r
+ mjson_get_string(event, eventLen, "$.content.from_device", verifyFromDeviceId, DEVICE_ID_SIZE);\r
+ \r
+ char verificationReadyBuffer[2048];\r
+ snprintf(verificationReadyBuffer, 2048,\r
+ "{"\r
+ "\"from_device\":\"%s\","\r
+ "\"methods\":[\"m.sas.v1\"],"\r
+ "\"transaction_id\":\"%s\""\r
+ "}",\r
+ client->deviceId,\r
+ transactionId);\r
+ \r
+ MatrixClientSendToDevice(client,\r
+ client->userId,\r
+ verifyFromDeviceId,\r
+ verificationReadyBuffer,\r
+ "m.key.verification.ready");\r
+ }\r
+ else if (strcmp(eventType, "m.key.verification.start") == 0) {\r
+ olmSas = olm_sas(malloc(olm_sas_size()));\r
+ void * sasRandomBytes = malloc(olm_create_sas_random_length(olmSas));\r
+ olm_create_sas(olmSas,\r
+ sasRandomBytes,\r
+ olm_create_sas_random_length(olmSas));\r
+ \r
+ OlmUtility * olmUtil = olm_utility(malloc(olm_utility_size()));\r
+ \r
+ // calculate commitment according to \r
+ // https://spec.matrix.org/v1.8/client-server-api/#mkeyverificationaccept\r
+ STATIC char publicKey[64];\r
+ STATIC char keyStartJsonCanonical[512];\r
+ STATIC char concat[512+64];\r
+ STATIC char commitment[1024];\r
+ olm_sas_get_pubkey(olmSas,\r
+ publicKey,\r
+ 64);\r
+\r
+ const char * keyStartJson;\r
+ int keyStartJsonLen;\r
+ mjson_find(event, eventLen, "$.content", &keyStartJson, &keyStartJsonLen);\r
+ JsonCanonicalize(keyStartJson, keyStartJsonLen, keyStartJsonCanonical, 512);\r
+\r
+ int concatLen =\r
+ snprintf(concat, 512+64, "%.*s%s", (int)olm_sas_pubkey_length(olmSas), publicKey, keyStartJsonCanonical);\r
+\r
+ int commitmentLen =\r
+ olm_sha256(olmUtil, concat, concatLen, commitment, 1024);\r
+ olm_clear_utility(olmUtil);\r
+ free(olmUtil);\r
+ \r
+ STATIC char verificationAcceptBuffer[512];\r
+ snprintf(verificationAcceptBuffer, 512,\r
+ "{"\r
+ "\"commitment\":\"%.*s\","\r
+ "\"hash\":\"sha256\","\r
+ "\"key_agreement_protocol\":\"curve25519\","\r
+ "\"message_authentication_code\":\"hkdf-hmac-sha256.v2\","\r
+ "\"method\":\"m.sas.v1\","\r
+ "\"short_authentication_string\":[\"decimal\"],"\r
+ "\"transaction_id\":\"%s\""\r
+ "}",\r
+ commitmentLen, commitment,\r
+ transactionId);\r
+ \r
+ MatrixClientSendToDevice(client,\r
+ client->userId,\r
+ verifyFromDeviceId,\r
+ verificationAcceptBuffer,\r
+ "m.key.verification.accept");\r
+ }\r
+ // send our sas key and calculate sas using their received key\r
+ else if (strcmp(eventType, "m.key.verification.key") == 0) {\r
+ STATIC char publicKey[128];\r
+ olm_sas_get_pubkey(olmSas,\r
+ publicKey,\r
+ 128);\r
+\r
+ STATIC char theirPublicKey[128];\r
+ int theirPublicKeyLen =\r
+ mjson_get_string(event, eventLen, "$.content.key", theirPublicKey, 128);\r
+\r
+ olm_sas_set_their_key(olmSas, theirPublicKey, theirPublicKeyLen);\r
+ \r
+ STATIC char verificationKeyBuffer[256];\r
+ snprintf(verificationKeyBuffer, 256,\r
+ "{"\r
+ "\"key\":\"%.*s\","\r
+ "\"transaction_id\":\"%s\""\r
+ "}",\r
+ (int)olm_sas_pubkey_length(olmSas), publicKey,\r
+ transactionId);\r
+ \r
+ MatrixClientSendToDevice(client,\r
+ client->userId,\r
+ verifyFromDeviceId,\r
+ verificationKeyBuffer,\r
+ "m.key.verification.key");\r
+ \r
+ // sas\r
+ STATIC char hkdfInfo[1024];\r
+ int hkdfInfoLen =\r
+ snprintf(hkdfInfo, 1024,\r
+ "MATRIX_KEY_VERIFICATION_SAS%s%s%s%s%s",\r
+ client->userId,\r
+ verifyFromDeviceId,\r
+ client->userId,\r
+ client->deviceId,\r
+ transactionId);\r
+\r
+ unsigned char sasBytes[5];\r
+ olm_sas_generate_bytes(olmSas,\r
+ hkdfInfo, hkdfInfoLen,\r
+ sasBytes, 5);\r
+ int b0 = sasBytes[0];\r
+ int b1 = sasBytes[1];\r
+ int b2 = sasBytes[2];\r
+ int b3 = sasBytes[3];\r
+ int b4 = sasBytes[4];\r
+\r
+ // for now just printf SAS numbers\r
+ // https://spec.matrix.org/v1.8/client-server-api/#sas-method-decimal\r
+ printf("%d | %d | %d\n",\r
+ ((b0 << 5) | (b1 >> 3)) + 1000,\r
+ (((b1 & 0x7) << 10) | (b2 << 2) | (b3 >> 6)) + 1000,\r
+ (((b3 & 0x3F) << 7) | (b4 >> 1)) + 1000);\r
+ }\r
+ // calculate MACs for signing key, master key and the key list\r
+ else if (strcmp(eventType, "m.key.verification.mac") == 0) { \r
+ // mac\r
+ STATIC char masterKey[123];\r
+ MatrixClientRequestMasterKey(client, masterKey, 123);\r
+\r
+ STATIC char keyList[256];\r
+ STATIC char keyListMac[256];\r
+ STATIC char key1Id[128];\r
+ STATIC char key1[128];\r
+ STATIC char key1Mac[128];\r
+ STATIC char key2Id[128];\r
+ STATIC char key2[128];\r
+ STATIC char key2Mac[128];\r
+\r
+ // keys have to be sorted so write keys/key IDs into key1/key2\r
+ // depending on lexicographical order\r
+ if (strcmp(masterKey, client->deviceId) < 0) {\r
+ snprintf(key1Id, 1024, "ed25519:%s", masterKey);\r
+ strcpy(key1, masterKey);\r
+ snprintf(key2Id, 1024, "ed25519:%s", client->deviceId);\r
+ MatrixOlmAccountGetSigningKey(&client->olmAccount, key2, 1024);\r
+ }\r
+ else {\r
+ snprintf(key1Id, 1024, "ed25519:%s", client->deviceId);\r
+ MatrixOlmAccountGetSigningKey(&client->olmAccount, key1, 1024);\r
+ snprintf(key2Id, 1024, "ed25519:%s", masterKey);\r
+ strcpy(key2, masterKey);\r
+ }\r
+\r
+ // create key list\r
+ snprintf(keyList, 1024,\r
+ "%s,%s", key1Id, key2Id);\r
+ \r
+ // generate MAC info for both keys and key list\r
+ // https://spec.matrix.org/v1.8/client-server-api/#mac-calculation\r
+ STATIC char macInfo[1024];\r
+ int macInfoLen;\r
+ {\r
+ macInfoLen =\r
+ snprintf(macInfo, 1024,\r
+ "MATRIX_KEY_VERIFICATION_MAC%s%s%s%s%s%s",\r
+ client->userId,\r
+ client->deviceId,\r
+ client->userId,\r
+ verifyFromDeviceId,\r
+ transactionId,\r
+ "KEY_IDS");\r
+ olm_sas_calculate_mac_fixed_base64(olmSas, keyList, strlen(keyList), macInfo, macInfoLen, keyListMac, 1024);\r
+ }\r
+ {\r
+ macInfoLen =\r
+ snprintf(macInfo, 1024,\r
+ "MATRIX_KEY_VERIFICATION_MAC%s%s%s%s%s%s",\r
+ client->userId,\r
+ client->deviceId,\r
+ client->userId,\r
+ verifyFromDeviceId,\r
+ transactionId,\r
+ key1Id);\r
+ olm_sas_calculate_mac_fixed_base64(olmSas, key1, strlen(key1), macInfo, macInfoLen, key1Mac, 1024);\r
+ }\r
+ {\r
+ macInfoLen =\r
+ snprintf(macInfo, 1024,\r
+ "MATRIX_KEY_VERIFICATION_MAC%s%s%s%s%s%s",\r
+ client->userId,\r
+ client->deviceId,\r
+ client->userId,\r
+ verifyFromDeviceId,\r
+ transactionId,\r
+ key2Id);\r
+ olm_sas_calculate_mac_fixed_base64(olmSas, key2, strlen(key2), macInfo, macInfoLen, key2Mac, 1024);\r
+ }\r
+\r
+ // construct message and send\r
+ STATIC char verificationMacBuffer[1024];\r
+ snprintf(verificationMacBuffer, 1024,\r
+ "{"\r
+ "\"keys\":\"%s\","\r
+ "\"mac\":{"\r
+ "\"%s\":\"%s\","\r
+ "\"%s\":\"%s\""\r
+ "},"\r
+ "\"transaction_id\":\"%s\""\r
+ "}",\r
+ keyListMac,\r
+ key1Id,\r
+ key1Mac,\r
+ key2Id,\r
+ key2Mac,\r
+ transactionId);\r
+ \r
+ MatrixClientSendToDevice(client,\r
+ client->userId,\r
+ verifyFromDeviceId,\r
+ verificationMacBuffer,\r
+ "m.key.verification.mac");\r
+\r
+ // send 'done' message\r
+ STATIC char verificationDoneBuffer[128];\r
+ snprintf(verificationDoneBuffer, 128,\r
+ "{"\r
+ "\"transaction_id\":\"%s\""\r
+ "}",\r
+ transactionId);\r
+ \r
+ MatrixClientSendToDevice(client,\r
+ client->userId,\r
+ verifyFromDeviceId,\r
+ verificationDoneBuffer,\r
+ "m.key.verification.done");\r
+ \r
+ free(olmSas);\r
+\r
+ client->verified = true;\r
+ }\r
+ else if (strcmp(eventType, "m.room.encrypted") == 0) {\r
+ STATIC char algorithm[128];\r
+ mjson_get_string(event, eventLen, "$.content.algorithm", algorithm, 128);\r
+\r
+ // since this only handles to_device messages algorithm should always be olm\r
+ if (strcmp(algorithm, "m.olm.v1.curve25519-aes-sha2") == 0) {\r
+ STATIC char thisDeviceKey[DEVICE_KEY_SIZE];\r
+ MatrixOlmAccountGetDeviceKey(&client->olmAccount, thisDeviceKey, DEVICE_KEY_SIZE);\r
+\r
+ STATIC char jp[128];\r
+ snprintf(jp, 128, "$.content.ciphertext.%s.type", thisDeviceKey);\r
+\r
+ double messageType;\r
+ mjson_get_number(event, eventLen, jp, &messageType);\r
+ int messageTypeInt = (int)messageType;\r
+\r
+ snprintf(jp, 128, "$.content.ciphertext.%s.body", thisDeviceKey);\r
+\r
+ mjson_get_string(event, eventLen, jp, g_EncryptedEventBuffer, 2048);\r
+\r
+ MatrixOlmSession * olmSession;\r
+ \r
+ // depending on message type create new incoming\r
+ // (type 0 indicates a new session so we dont check locally)\r
+ if (messageTypeInt == 0) {\r
+ MatrixClientNewOlmSessionIn(client,\r
+ client->userId,\r
+ verifyFromDeviceId,\r
+ g_EncryptedEventBuffer,\r
+ &olmSession);\r
+ }\r
+ // or new outgoing, checking for known sessions first\r
+ else {\r
+ if (! MatrixClientGetOlmSession(client, client->userId, verifyFromDeviceId, &olmSession))\r
+ {\r
+ MatrixClientNewOlmSessionOut(client,\r
+ client->userId,\r
+ verifyFromDeviceId,\r
+ &olmSession);\r
+ }\r
+ }\r
+ \r
+ STATIC char decrypted[2048];\r
+ MatrixOlmSessionDecrypt(olmSession,\r
+ messageTypeInt, g_EncryptedEventBuffer, decrypted, 2048);\r
+ \r
+ MatrixClientHandleEvent(client, decrypted, strlen(decrypted));\r
+ }\r
+ }\r
+ else if (strcmp(eventType, "m.room_key") == 0 ||\r
+ strcmp(eventType, "m.forwarded_room_key") == 0) {\r
+ // store session information\r
+ STATIC char roomId[128];\r
+ STATIC char sessionId[128];\r
+ STATIC char sessionKey[1024];\r
+ mjson_get_string(event, eventLen, "$.content.room_id", roomId, 128);\r
+ mjson_get_string(event, eventLen, "$.content.session_id", sessionId, 128);\r
+ mjson_get_string(event, eventLen, "$.content.session_key", sessionKey, 1024);\r
+\r
+ MatrixMegolmInSession * megolmInSession;\r
+ MatrixClientNewMegolmInSession(client, roomId, sessionId, sessionKey, &megolmInSession);\r
+ }\r
+}\r
+\r
+void\r
+MatrixClientHandleRoomEvent(\r
+ MatrixClient * client,\r
+ const char * room, int roomLen,\r
+ const char * event, int eventLen)\r
+{\r
+ STATIC char eventType[128];\r
+ memset(eventType, 0, sizeof(eventType));\r
+ mjson_get_string(event, eventLen, "$.type", eventType, 128);\r
+\r
+ if (strcmp(eventType, "m.room.encrypted") == 0) {\r
+ STATIC char algorithm[128];\r
+ mjson_get_string(event, eventLen, "$.content.algorithm", algorithm, 128);\r
+\r
+ // only room specific message type is encrypted\r
+ // since this is only room messages, algorithm should always be megolm\r
+ if (strcmp(algorithm, "m.megolm.v1.aes-sha2") == 0) {\r
+ STATIC char sessionId[128];\r
+ int sessionIdLen =\r
+ mjson_get_string(event, eventLen, "$.content.session_id", sessionId, 128);\r
+\r
+ bool res;\r
+\r
+ MatrixMegolmInSession * megolmInSession;\r
+ res = MatrixClientGetMegolmInSession(client,\r
+ room, roomLen,\r
+ sessionId, sessionIdLen,\r
+ &megolmInSession);\r
+\r
+ if (res) {\r
+ mjson_get_string(event, eventLen, "$.content.ciphertext", g_EncryptedEventBuffer, 2048);\r
+\r
+ STATIC char decrypted[2048];\r
+ MatrixMegolmInSessionDecrypt(megolmInSession, g_EncryptedEventBuffer, strlen(g_EncryptedEventBuffer), decrypted, 2048);\r
+\r
+ MatrixClientHandleEvent(client, decrypted, strlen(decrypted));\r
+ }\r
+ else {\r
+ printf("error: megolm session not known\n");\r
+ }\r
+ }\r
+ }\r
+ MatrixClientHandleEvent(client, event, eventLen);\r
+}\r
+\r
+// pass the response from sync to Handle(Room)Event\r
+void\r
+MatrixClientHandleSync(\r
+ MatrixClient * client,\r
+ char * syncBuffer, int syncBufferLen,\r
+ char * nextBatch, int nextBatchCap)\r
+{ \r
+ int res;\r
+\r
+ const char * s = syncBuffer;\r
+ int slen = syncBufferLen;\r
+\r
+ // read next_batch\r
+ mjson_get_string(s, slen, "$.next_batch", nextBatch, nextBatchCap);\r
+\r
+ // to_device\r
+ const char * events;\r
+ int eventsLen;\r
+ res =\r
+ mjson_find(s, slen, "$.to_device.events", &events, &eventsLen);\r
+ \r
+ // iterate event and pass to HandleEvent\r
+ if (res != MJSON_TOK_INVALID) {\r
+ {\r
+ int koff, klen, voff, vlen, vtype, off = 0;\r
+ for (off = 0; (off = mjson_next(events, eventsLen, off, &koff, &klen,\r
+ &voff, &vlen, &vtype)) != 0; ) {\r
+ const char * v = events + voff;\r
+\r
+ MatrixClientHandleEvent(client, v, vlen);\r
+ }\r
+ }\r
+ }\r
+\r
+ // rooms\r
+ const char * rooms;\r
+ int roomsLen;\r
+ res =\r
+ mjson_find(s, slen, "$.rooms.join", &rooms, &roomsLen);\r
+ \r
+ if (res != MJSON_TOK_INVALID) {\r
+ // iterate rooms\r
+ int koff, klen, voff, vlen, vtype, off = 0;\r
+ for (off = 0; (off = mjson_next(rooms, roomsLen, off, &koff, &klen,\r
+ &voff, &vlen, &vtype)) != 0; ) {\r
+ const char * k = rooms + koff;\r
+ const char * v = rooms + voff;\r
+\r
+ const char * events;\r
+ int eventsLen;\r
+ res =\r
+ mjson_find(v, vlen, "$.timeline.events", &events, &eventsLen);\r
+ \r
+ if (res != MJSON_TOK_INVALID) {\r
+ // iterate messages in that room\r
+ int koff2, klen2, voff2, vlen2, vtype2, off2 = 0;\r
+ for (off2 = 0; (off2 = mjson_next(events, eventsLen, off2, &koff2, &klen2,\r
+ &voff2, &vlen2, &vtype2)) != 0; ) {\r
+ const char * v2 = events + voff2;\r
+\r
+ MatrixClientHandleRoomEvent(client,\r
+ k+1, klen-2,\r
+ v2, vlen2);\r
+ }\r
+ }\r
+ }\r
+ }\r
+}\r
+\r
+// https://spec.matrix.org/v1.8/client-server-api/#get_matrixclientv3sync\r
+bool\r
+MatrixClientSync(\r
+ MatrixClient * client,\r
+ char * outSyncBuffer, int outSyncCap,\r
+ char * nextBatch, int nextBatchCap)\r
+{\r
+ // filter={\"event_fields\":[\"to_device\"]}\r
+ STATIC char url[MAX_URL_LEN];\r
+ snprintf(url, MAX_URL_LEN,\r
+ "/_matrix/client/v3/sync?timeout=%d" "%s" "%s",\r
+ SYNC_TIMEOUT,\r
+ "",\r
+ // "&filter={\"event_fields\":[\"to_device\"]}",\r
+ strlen(nextBatch) > 0 ? "&since=" : "");\r
+ \r
+ int index = strlen(url);\r
+\r
+ // URL encode next_batch parameter since it can include ~\r
+ for (size_t i = 0; i < strlen(nextBatch); i++) {\r
+ char c = nextBatch[i];\r
+\r
+ if (c == '~') {\r
+ url[index++] = '%';\r
+ url[index++] = '7';\r
+ url[index++] = 'E';\r
+ }\r
+ else {\r
+ url[index++] = c;\r
+ }\r
+ }\r
+ url[index] = '\0';\r
+\r
+ bool result =\r
+ MatrixHttpGet(client->hc,\r
+ url,\r
+ outSyncBuffer, outSyncCap,\r
+ true);\r
+ \r
+ MatrixClientHandleSync(client,\r
+ outSyncBuffer, strlen(outSyncBuffer),\r
+ nextBatch, nextBatchCap);\r
+ \r
+ return result;\r
+}\r
+\r
+// https://spec.matrix.org/v1.8/client-server-api/#get_matrixclientv3roomsroomideventeventid\r
+bool\r
+MatrixClientGetRoomEvent(\r
+ MatrixClient * client,\r
+ const char * roomId,\r
+ const char * eventId,\r
+ char * outEvent, int outEventCap)\r
+{\r
+ STATIC char url[MAX_URL_LEN];\r
+ snprintf(url, MAX_URL_LEN,\r
+ "/_matrix/client/v3/rooms/%s/event/%s",\r
+ roomId,\r
+ eventId);\r
+\r
+ return\r
+ MatrixHttpGet(client->hc,\r
+ url,\r
+ outEvent, outEventCap,\r
+ true);\r
+}\r
+\r
+bool\r
+MatrixClientShareMegolmOutSession(\r
+ MatrixClient * client,\r
+ const char * userId,\r
+ const char * deviceId,\r
+ MatrixMegolmOutSession * session)\r
+{\r
+ // generate room key event\r
+ STATIC char eventBuffer[KEY_SHARE_EVENT_LEN];\r
+ snprintf(eventBuffer, KEY_SHARE_EVENT_LEN,\r
+ "{"\r
+ "\"algorithm\":\"m.megolm.v1.aes-sha2\","\r
+ "\"room_id\":\"%s\","\r
+ "\"session_id\":\"%s\","\r
+ "\"session_key\":\"%s\""\r
+ "}",\r
+ session->roomId,\r
+ session->id,\r
+ session->key\r
+ );\r
+\r
+ // send\r
+ MatrixClientSendToDeviceEncrypted(client,\r
+ userId,\r
+ deviceId,\r
+ eventBuffer,\r
+ "m.room_key");\r
+\r
+ return true;\r
+}\r
+\r
+bool\r
+MatrixClientGetMegolmOutSession(\r
+ MatrixClient * client,\r
+ const char * roomId,\r
+ MatrixMegolmOutSession ** outSession)\r
+{\r
+ for (int i = 0; i < client->numMegolmOutSessions; i++)\r
+ {\r
+ if (strcmp(client->megolmOutSessions[i].roomId, roomId) == 0)\r
+ {\r
+ *outSession = &client->megolmOutSessions[i];\r
+ return true;\r
+ }\r
+ }\r
+\r
+ return false;\r
+}\r
+\r
+bool\r
+MatrixClientNewMegolmOutSession(\r
+ MatrixClient * client,\r
+ const char * roomId,\r
+ MatrixMegolmOutSession ** outSession)\r
+{\r
+ if (client->numMegolmOutSessions < NUM_MEGOLM_SESSIONS)\r
+ {\r
+ MatrixMegolmOutSession * result =\r
+ &client->megolmOutSessions[client->numMegolmOutSessions];\r
+ \r
+ MatrixMegolmOutSessionInit(result,\r
+ roomId);\r
+\r
+ *outSession = result;\r
+\r
+ client->numMegolmOutSessions++;\r
+\r
+ return true;\r
+ }\r
+\r
+ return false;\r
+}\r
+\r
+bool\r
+MatrixClientGetMegolmInSession(\r
+ MatrixClient * client,\r
+ const char * roomId, int roomIdLen,\r
+ const char * sessionId, int sessionIdLen,\r
+ MatrixMegolmInSession ** outSession)\r
+{\r
+ for (int i = 0; i < client->numMegolmInSessions; i++)\r
+ {\r
+ if (strncmp(client->megolmInSessions[i].roomId, roomId, roomIdLen) == 0 &&\r
+ strncmp(client->megolmInSessions[i].id, sessionId, sessionIdLen) == 0)\r
+ {\r
+ *outSession = &client->megolmInSessions[i];\r
+ return true;\r
+ }\r
+ }\r
+\r
+ return false;\r
+}\r
+\r
+bool\r
+MatrixClientNewMegolmInSession(\r
+ MatrixClient * client,\r
+ const char * roomId,\r
+ const char * sessionId,\r
+ const char * sessionKey,\r
+ MatrixMegolmInSession ** outSession)\r
+{\r
+ if (client->numMegolmInSessions < NUM_MEGOLM_SESSIONS)\r
+ {\r
+ MatrixMegolmInSession * result =\r
+ &client->megolmInSessions[client->numMegolmInSessions];\r
+ \r
+ MatrixMegolmInSessionInit(result,\r
+ roomId,\r
+ sessionId,\r
+ sessionKey, strlen(sessionKey));\r
+ \r
+ *outSession = result;\r
+\r
+ client->numMegolmInSessions++;\r
+\r
+ return true;\r
+ }\r
+\r
+ return false;\r
+}\r
+\r
+bool\r
+MatrixClientRequestMegolmInSession(\r
+ MatrixClient * client,\r
+ const char * roomId,\r
+ const char * sessionId,\r
+ const char * senderKey,\r
+ const char * userId,\r
+ const char * deviceId)\r
+{\r
+ // TODO: cancel requests\r
+ MatrixClientSendDummy(client, userId, deviceId);\r
+\r
+ STATIC char event[ROOMKEY_REQUEST_SIZE];\r
+ snprintf(event, ROOMKEY_REQUEST_SIZE,\r
+ "{"\r
+ "\"action\":\"request\","\r
+ "\"body\":{"\r
+ "\"algorithm\":\"m.megolm.v1.aes-sha2\","\r
+ "\"room_id\":\"%s\","\r
+ "\"sender_key\":\"%s\","\r
+ "\"session_id\":\"%s\""\r
+ "},"\r
+ "\"request_id\":\"%lld\","\r
+ "\"requesting_device_id\":\"%s\""\r
+ "}",\r
+ roomId,\r
+ senderKey,\r
+ sessionId,\r
+ time(NULL),\r
+ client->deviceId);\r