+ const char * username,\r
+ const char * password,\r
+ const char * displayName)\r
+{\r
+ static char requestBuffer[LOGIN_REQUEST_SIZE];\r
+\r
+ mjson_snprintf(requestBuffer, LOGIN_REQUEST_SIZE,\r
+ "{"\r
+ "\"type\": \"m.login.password\","\r
+ "\"identifier\": {"\r
+ "\"type\": \"m.id.user\","\r
+ "\"user\": \"%s\""\r
+ "},"\r
+ "\"password\": \"%s\","\r
+ "\"initial_device_display_name\": \"%s\""\r
+ "}",\r
+ username,\r
+ password,\r
+ displayName);\r
+ \r
+ static char responseBuffer[LOGIN_RESPONSE_SIZE];\r
+ bool result =\r
+ MatrixHttpPost(client,\r
+ LOGIN_URL,\r
+ requestBuffer,\r
+ responseBuffer, LOGIN_RESPONSE_SIZE,\r
+ false);\r
+ \r
+ int responseLen = strlen(responseBuffer);\r
+ \r
+ if (!result)\r
+ return false;\r
+\r
+ mjson_get_string(responseBuffer, responseLen,\r
+ "$.access_token",\r
+ client->accessToken, ACCESS_TOKEN_SIZE);\r
+ mjson_get_string(responseBuffer, responseLen,\r
+ "$.device_id",\r
+ client->deviceId, DEVICE_ID_SIZE);\r
+ mjson_get_string(responseBuffer, responseLen,\r
+ "$.expires_in_ms",\r
+ client->expireMs, EXPIRE_MS_SIZE);\r
+ mjson_get_string(responseBuffer, responseLen,\r
+ "$.refresh_token",\r
+ client->refreshToken, REFRESH_TOKEN_SIZE);\r
+\r
+ return true;\r
+}\r
+\r
+// https://spec.matrix.org/v1.6/client-server-api/#put_matrixclientv3roomsroomidsendeventtypetxnid\r
+bool\r
+MatrixClientSendEvent(\r
+ MatrixClient * client,\r
+ const char * roomId,\r
+ const char * msgType,\r
+ const char * msgBody)\r
+{ \r
+ static char requestUrl[MAX_URL_LEN];\r
+ sprintf(requestUrl,\r
+ ROOMEVENT_URL, roomId, msgType, (int)time(NULL));\r
+\r
+ static char responseBuffer[ROOMEVENT_RESPONSE_SIZE];\r
+ bool result =\r
+ MatrixHttpPut(client,\r
+ requestUrl,\r
+ msgBody,\r
+ responseBuffer, ROOMEVENT_RESPONSE_SIZE,\r
+ true);\r
+ \r
+ return result;\r
+}\r
+\r
+// https://spec.matrix.org/v1.6/client-server-api/#mroomencrypted\r
+// https://matrix.org/docs/guides/end-to-end-encryption-implementation-guide#sending-an-encrypted-message-event\r
+bool\r
+MatrixClientSendEventEncrypted(\r
+ MatrixClient * client,\r
+ const char * roomId,\r
+ const char * msgType,\r
+ const char * msgBody)\r
+{\r
+ // event json\r
+ static char requestBuffer[ROOMEVENT_REQUEST_SIZE];\r
+ sprintf(requestBuffer,\r
+ "{"\r
+ "\"type\":\"%s\","\r
+ "\"content\":%s,"\r
+ "\"room_id\":\"%s\""\r
+ "}",\r
+ msgType,\r
+ msgBody,\r
+ roomId);\r
+\r
+ // get megolm session\r
+ MatrixMegolmOutSession * outSession;\r
+ MatrixClientGetMegolmOutSession(client, roomId, &outSession);\r
+ \r
+ // encrypt\r
+ static char encryptedBuffer[ENCRYPTED_REQUEST_SIZE];\r
+ MatrixMegolmOutSessionEncrypt(outSession,\r
+ requestBuffer,\r
+ encryptedBuffer, ENCRYPTED_REQUEST_SIZE);\r
+\r
+ // encrypted event json\r
+ const char * senderKey = client->deviceKey;\r
+ const char * sessionId = outSession->id;\r
+ const char * deviceId = client->deviceId;\r
+\r
+ static char encryptedEventBuffer[ENCRYPTED_EVENT_SIZE];\r
+ sprintf(encryptedEventBuffer,\r
+ "{"\r
+ "\"algorithm\":\"m.megolm.v1.aes-sha2\","\r
+ "\"ciphertext\":\"%s\","\r
+ "\"device_id\":\"%s\","\r
+ "\"sender_key\":\"%s\","\r
+ "\"session_id\":\"%s\""\r
+ "}",\r
+ encryptedBuffer,\r
+ deviceId,\r
+ senderKey,\r
+ sessionId);\r
+\r
+ // send\r
+ return MatrixClientSendEvent(client,\r
+ roomId,\r
+ "m.room.encrypted",\r
+ encryptedEventBuffer);\r
+}\r
+\r
+// https://spec.matrix.org/v1.6/client-server-api/#get_matrixclientv3sync\r
+bool\r
+MatrixClientSync(\r
+ MatrixClient * client,\r
+ char * outSyncBuffer, int outSyncCap)\r
+{\r
+ return\r
+ MatrixHttpGet(client,\r
+ "/_matrix/client/v3/sync",\r
+ outSyncBuffer, outSyncCap,\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
+ sprintf(eventBuffer,\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
+ // // get olm session\r
+ // MatrixOlmSession * olmSession;\r
+ // MatrixClientGetOlmSession(client, userId, deviceId, &olmSession);\r
+\r
+ // // encrypt\r
+ // char encryptedBuffer[KEY_SHARE_EVENT_LEN];\r
+ // MatrixOlmSessionEncrypt(olmSession,\r
+ // eventBuffer,\r
+ // encryptedBuffer, KEY_SHARE_EVENT_LEN);\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
+MatrixClientShareMegolmOutSessionTest(\r
+ MatrixClient * client,\r
+ const char * deviceId,\r
+ MatrixMegolmOutSession * session)\r
+{\r
+ // generate room key event\r
+ char eventBuffer[KEY_SHARE_EVENT_LEN];\r
+ sprintf(eventBuffer,\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
+ MatrixClientSendToDevice(client,\r
+ client->userId,\r
+ deviceId,\r
+ eventBuffer,\r
+ "m.room_key");\r
+\r
+ return true;\r
+}\r
+\r
+// bool\r
+// MatrixClientSetMegolmOutSession(\r
+// MatrixClient * client,\r
+// const char * roomId,\r
+// MatrixMegolmOutSession session)\r
+// {\r
+// if (client->numMegolmOutSessions < 10)\r
+// {\r
+// session.roomId = roomId;\r
+// client->megolmOutSessions[client->numMegolmOutSessions] = session;\r
+// client->numMegolmOutSessions++;\r
+\r
+// return true;\r
+// }\r
+// return false;\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
+ 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
+bool\r
+MatrixClientGetOlmSession(\r
+ MatrixClient * client,\r
+ const char * userId,\r
+ const char * deviceId,\r
+ MatrixOlmSession ** outSession)\r
+{\r
+ for (int i = 0; i < client->numOlmSessions; i++)\r
+ {\r
+ if (strcmp(client->olmSessions[i].deviceId, deviceId) == 0)\r
+ {\r
+ *outSession = &client->olmSessions[i];\r
+ return true;\r
+ }\r
+ }\r
+\r
+ if (client->numOlmSessions < NUM_OLM_SESSIONS)\r
+ {\r
+ static char deviceKey[DEVICE_KEY_SIZE];\r
+ MatrixClientGetDeviceKey(client,\r
+ deviceId,\r
+ deviceKey, DEVICE_KEY_SIZE);\r
+\r
+ char onetimeKey[ONETIME_KEY_SIZE];\r
+ MatrixClientClaimOnetimeKey(client,\r
+ userId,\r
+ deviceId,\r
+ onetimeKey, ONETIME_KEY_SIZE);\r
+\r
+ MatrixOlmSessionTo(\r
+ &client->olmSessions[client->numOlmSessions],\r
+ client->olmAccount.account,\r
+ deviceId,\r
+ deviceKey,\r
+ onetimeKey);\r
+\r
+ *outSession = &client->olmSessions[client->numOlmSessions];\r
+ \r
+ client->numOlmSessions++;\r
+\r
+ return true;\r
+ }\r
+\r
+ return false;\r
+}\r
+\r
+// https://spec.matrix.org/v1.6/client-server-api/#put_matrixclientv3sendtodeviceeventtypetxnid\r
+bool\r
+MatrixClientSendToDevice(\r
+ MatrixClient * client,\r
+ const char * userId,\r
+ const char * deviceId,\r
+ const char * message,\r
+ const char * msgType)\r
+{\r
+ static char requestUrl[MAX_URL_LEN];\r
+ sprintf(requestUrl,\r
+ TODEVICE_URL, msgType, (int)time(NULL));\r