#include <matrix.h>\r
\r
-#define SERVER "matrix.org"\r
+#define SERVER "https://matrix.org"\r
#define ACCESS_TOKEN "syt_cHNjaG8_yBvTjVTquGCikvsAenOJ_49mBMO"\r
#define DEVICE_ID "MAZNCCZLBR"\r
-#define ROOM_ID "!jhpZBTbckszblMYjMK:matrix.org"\r
+#define ROOM_ID "!koVStwyiiKcBVbXZYz:matrix.org"\r
\r
int\r
main()\r
{\r
MatrixClient client;\r
- MatrixClientCreate(&client,\r
- SERVER, strlen(SERVER));\r
+ MatrixClientInit(&client,\r
+ SERVER);\r
\r
MatrixHttpInit(&client);\r
\r
MatrixClientSetAccessToken(&client,\r
- ACCESS_TOKEN, strlen(ACCESS_TOKEN));\r
+ ACCESS_TOKEN);\r
\r
MatrixClientSendEvent(&client,\r
ROOM_ID,\r
"m.room.message",\r
"{\"body\":\"Hello\",\"msgtype\":\"m.text\"}");\r
+ \r
+ MatrixHttpDeinit(&client);\r
\r
return 0;\r
}
\ No newline at end of file
#include "matrix.h"\r
\r
+#include <time.h>\r
#include <stdio.h>\r
#include <mjson.h>\r
\r
#define LOGIN_RESPONSE_SIZE 1024\r
#define LOGIN_URL "/_matrix/client/v3/login"\r
\r
+#define ROOMEVENT_REQUEST_SIZE 1024\r
+#define ROOMEVENT_RESPONSE_SIZE 1024\r
+#define ROOMEVENT_URL "/_matrix/client/v3/rooms/%s/send/%s/%d"\r
+\r
\r
bool\r
MatrixClientInit(\r
return true;\r
}\r
\r
+bool\r
+MatrixClientSetAccessToken(\r
+ MatrixClient * client,\r
+ const char * accessToken)\r
+{\r
+ int accessTokenLen = strlen(accessToken);\r
+\r
+ if (accessTokenLen < ACCESS_TOKEN_SIZE - 1)\r
+ return false;\r
+\r
+ for (int i = 0; i < accessTokenLen; i++)\r
+ client->accessTokenBuffer[i] = accessToken[i];\r
+\r
+ return true;\r
+}\r
+\r
// https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3login\r
bool\r
MatrixClientLoginPassword(\r
MatrixHttpPost(client,\r
LOGIN_URL,\r
requestBuffer,\r
- responseBuffer, LOGIN_RESPONSE_SIZE);\r
+ responseBuffer, LOGIN_RESPONSE_SIZE,\r
+ false);\r
\r
int responseLen = strlen(responseBuffer);\r
\r
\r
return true;\r
}\r
+ \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_s(requestUrl, MAX_URL_LEN,\r
+ ROOMEVENT_URL, roomId, msgType, 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
OlmAccount * olmAccount;\r
OlmSession * olmSession;\r
\r
- char server[SERVER_SIZE];\r
+ char server[SERVER_SIZE+1];\r
char accessTokenBuffer[ACCESS_TOKEN_SIZE];\r
char deviceIdBuffer[DEVICE_ID_SIZE];\r
char expireMsBuffer[EXPIRE_MS_SIZE];\r
MatrixClient * client,\r
const char * server);\r
\r
+bool\r
+MatrixClientSetAccessToken(\r
+ MatrixClient * client,\r
+ const char * accessToken);\r
+\r
bool\r
MatrixClientLoginPassword(\r
MatrixClient * client,\r
const char * username,\r
const char * password,\r
const char * displayName);\r
+ \r
+bool\r
+MatrixClientSendEvent(\r
+ MatrixClient * client,\r
+ const char * roomId,\r
+ const char * msgType,\r
+ const char * msgBody);\r
\r
bool\r
MatrixHttpInit(\r
MatrixHttpGet(\r
MatrixClient * client,\r
const char * url,\r
- char * outResponseBuffer, int outResponseCap);\r
+ char * outResponseBuffer, int outResponseCap,\r
+ bool authenticated);\r
\r
bool\r
MatrixHttpPost(\r
MatrixClient * client,\r
const char * url,\r
const char * requestBuffer,\r
- char * outResponseBuffer, int outResponseCap);\r
+ char * outResponseBuffer, int outResponseCap,\r
+ bool authenticated);\r
+\r
+bool\r
+MatrixHttpPut(\r
+ MatrixClient * client,\r
+ const char * url,\r
+ const char * requestBuffer,\r
+ char * outResponseBuffer, int outResponseCap,\r
+ bool authenticated);\r
\r
#endif\r
#include <mongoose.h>\r
\r
//#define HTTP_DATA_SIZE 1024\r
+#define AUTHORIZATION_HEADER_LEN 64\r
\r
typedef struct MatrixHttpConnection {\r
struct mg_mgr mgr;\r
// memcpy_s(client->data, 1024, hm->message.ptr, hm->message.len);\r
// client->dataLen = hm->message.len;\r
memcpy_s(conn->data, conn->dataCap, hm->body.ptr, hm->body.len);\r
+ conn->data[hm->body.len] = '\0';\r
conn->dataLen = hm->body.len;\r
conn->dataReceived = true;\r
}\r
MatrixHttpGet(\r
MatrixClient * client,\r
const char * url,\r
- char * outResponseBuffer, int outResponseCap)\r
+ char * outResponseBuffer, int outResponseCap,\r
+ bool authenticated)\r
{\r
MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
\r
\r
struct mg_str host = mg_url_host(client->server);\r
\r
+ static char authorizationHeader[AUTHORIZATION_HEADER_LEN] = "\0";\r
+ if (authenticated)\r
+ sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN,\r
+ "Authorization: Bearer %s\r\n", client->accessTokenBuffer);\r
+\r
mg_printf(conn->connection,\r
"GET %s HTTP/1.1\r\n"\r
"Host: %.*s\r\n"\r
+ "%s"\r
"\r\n",\r
url,\r
- host.len, host.ptr);\r
+ host.len, host.ptr,\r
+ authorizationHeader);\r
\r
conn->data = outResponseBuffer;\r
conn->dataCap = outResponseCap;\r
MatrixClient * client,\r
const char * url,\r
const char * requestBuffer,\r
- char * outResponseBuffer, int outResponseCap)\r
+ char * outResponseBuffer, int outResponseCap,\r
+ bool authenticated)\r
{\r
MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
\r
\r
struct mg_str host = mg_url_host(client->server);\r
\r
+ static char authorizationHeader[AUTHORIZATION_HEADER_LEN] = "\0";\r
+ if (authenticated)\r
+ sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN,\r
+ "Authorization: Bearer %s\r\n", client->accessTokenBuffer);\r
+\r
mg_printf(conn->connection,\r
"POST %s HTTP/1.0\r\n"\r
"Host: %.*s\r\n"\r
+ "%s"\r
+ "Content-Type: application/json\r\n"\r
+ "Content-Length: %d\r\n"\r
+ "\r\n"\r
+ "%s"\r
+ "\r\n",\r
+ url,\r
+ host.len, host.ptr,\r
+ authorizationHeader,\r
+ strlen(requestBuffer),\r
+ requestBuffer);\r
+\r
+ conn->data = outResponseBuffer;\r
+ conn->dataCap = outResponseCap;\r
+ \r
+ while (! conn->dataReceived)\r
+ mg_mgr_poll(&conn->mgr, 1000);\r
+\r
+ return conn->dataReceived;\r
+}\r
+\r
+bool\r
+MatrixHttpPut(\r
+ MatrixClient * client,\r
+ const char * url,\r
+ const char * requestBuffer,\r
+ char * outResponseBuffer, int outResponseCap,\r
+ bool authenticated)\r
+{\r
+ MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData;\r
+\r
+ conn->dataReceived = false;\r
+\r
+ struct mg_str host = mg_url_host(client->server);\r
+\r
+ static char authorizationHeader[AUTHORIZATION_HEADER_LEN];\r
+ if (authenticated)\r
+ sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN,\r
+ "Authorization: Bearer %s\r\n", client->accessTokenBuffer);\r
+ else\r
+ authorizationHeader[0] = '\0';\r
+\r
+ mg_printf(conn->connection,\r
+ "PUT %s HTTP/1.0\r\n"\r
+ "Host: %.*s\r\n"\r
+ "%s"\r
"Content-Type: application/json\r\n"\r
"Content-Length: %d\r\n"\r
"\r\n"\r
"\r\n",\r
url,\r
host.len, host.ptr,\r
+ authorizationHeader,\r
strlen(requestBuffer),\r
requestBuffer);\r
\r
mg_mgr_poll(&conn->mgr, 1000);\r
\r
return conn->dataReceived;\r
-}
\ No newline at end of file
+}\r