From d382d193cb2d550cc769afa76e55823865a39023 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sun, 28 May 2023 17:27:25 +0200 Subject: [PATCH] send example, http PUT --- examples/Send.c | 12 ++++--- src/matrix.c | 46 +++++++++++++++++++++++- src/matrix.h | 28 +++++++++++++-- src/matrix_http_mongoose.c | 71 +++++++++++++++++++++++++++++++++++--- 4 files changed, 144 insertions(+), 13 deletions(-) diff --git a/examples/Send.c b/examples/Send.c index 8786be9..e595ba7 100644 --- a/examples/Send.c +++ b/examples/Send.c @@ -1,26 +1,28 @@ #include -#define SERVER "matrix.org" +#define SERVER "https://matrix.org" #define ACCESS_TOKEN "syt_cHNjaG8_yBvTjVTquGCikvsAenOJ_49mBMO" #define DEVICE_ID "MAZNCCZLBR" -#define ROOM_ID "!jhpZBTbckszblMYjMK:matrix.org" +#define ROOM_ID "!koVStwyiiKcBVbXZYz:matrix.org" int main() { MatrixClient client; - MatrixClientCreate(&client, - SERVER, strlen(SERVER)); + MatrixClientInit(&client, + SERVER); MatrixHttpInit(&client); MatrixClientSetAccessToken(&client, - ACCESS_TOKEN, strlen(ACCESS_TOKEN)); + ACCESS_TOKEN); MatrixClientSendEvent(&client, ROOM_ID, "m.room.message", "{\"body\":\"Hello\",\"msgtype\":\"m.text\"}"); + + MatrixHttpDeinit(&client); return 0; } \ No newline at end of file diff --git a/src/matrix.c b/src/matrix.c index 5759ba2..7cc4de9 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -1,5 +1,6 @@ #include "matrix.h" +#include #include #include @@ -8,6 +9,10 @@ #define LOGIN_RESPONSE_SIZE 1024 #define LOGIN_URL "/_matrix/client/v3/login" +#define ROOMEVENT_REQUEST_SIZE 1024 +#define ROOMEVENT_RESPONSE_SIZE 1024 +#define ROOMEVENT_URL "/_matrix/client/v3/rooms/%s/send/%s/%d" + bool MatrixClientInit( @@ -23,6 +28,22 @@ MatrixClientInit( return true; } +bool +MatrixClientSetAccessToken( + MatrixClient * client, + const char * accessToken) +{ + int accessTokenLen = strlen(accessToken); + + if (accessTokenLen < ACCESS_TOKEN_SIZE - 1) + return false; + + for (int i = 0; i < accessTokenLen; i++) + client->accessTokenBuffer[i] = accessToken[i]; + + return true; +} + // https://spec.matrix.org/v1.6/client-server-api/#post_matrixclientv3login bool MatrixClientLoginPassword( @@ -52,7 +73,8 @@ MatrixClientLoginPassword( MatrixHttpPost(client, LOGIN_URL, requestBuffer, - responseBuffer, LOGIN_RESPONSE_SIZE); + responseBuffer, LOGIN_RESPONSE_SIZE, + false); int responseLen = strlen(responseBuffer); @@ -74,4 +96,26 @@ MatrixClientLoginPassword( return true; } + +bool +MatrixClientSendEvent( + MatrixClient * client, + const char * roomId, + const char * msgType, + const char * msgBody) +{ + static char requestUrl[MAX_URL_LEN]; + sprintf_s(requestUrl, MAX_URL_LEN, + ROOMEVENT_URL, roomId, msgType, time(NULL)); + + static char responseBuffer[ROOMEVENT_RESPONSE_SIZE]; + bool result = + MatrixHttpPut(client, + requestUrl, + msgBody, + responseBuffer, ROOMEVENT_RESPONSE_SIZE, + true); + + return result; +} diff --git a/src/matrix.h b/src/matrix.h index cd109ce..2e7f6e0 100644 --- a/src/matrix.h +++ b/src/matrix.h @@ -21,7 +21,7 @@ typedef struct MatrixClient { OlmAccount * olmAccount; OlmSession * olmSession; - char server[SERVER_SIZE]; + char server[SERVER_SIZE+1]; char accessTokenBuffer[ACCESS_TOKEN_SIZE]; char deviceIdBuffer[DEVICE_ID_SIZE]; char expireMsBuffer[EXPIRE_MS_SIZE]; @@ -35,12 +35,24 @@ MatrixClientInit( MatrixClient * client, const char * server); +bool +MatrixClientSetAccessToken( + MatrixClient * client, + const char * accessToken); + bool MatrixClientLoginPassword( MatrixClient * client, const char * username, const char * password, const char * displayName); + +bool +MatrixClientSendEvent( + MatrixClient * client, + const char * roomId, + const char * msgType, + const char * msgBody); bool MatrixHttpInit( @@ -54,13 +66,23 @@ bool MatrixHttpGet( MatrixClient * client, const char * url, - char * outResponseBuffer, int outResponseCap); + char * outResponseBuffer, int outResponseCap, + bool authenticated); bool MatrixHttpPost( MatrixClient * client, const char * url, const char * requestBuffer, - char * outResponseBuffer, int outResponseCap); + char * outResponseBuffer, int outResponseCap, + bool authenticated); + +bool +MatrixHttpPut( + MatrixClient * client, + const char * url, + const char * requestBuffer, + char * outResponseBuffer, int outResponseCap, + bool authenticated); #endif diff --git a/src/matrix_http_mongoose.c b/src/matrix_http_mongoose.c index d44787c..8d575e5 100644 --- a/src/matrix_http_mongoose.c +++ b/src/matrix_http_mongoose.c @@ -6,6 +6,7 @@ #include //#define HTTP_DATA_SIZE 1024 +#define AUTHORIZATION_HEADER_LEN 64 typedef struct MatrixHttpConnection { struct mg_mgr mgr; @@ -48,6 +49,7 @@ MatrixHttpCallback( // memcpy_s(client->data, 1024, hm->message.ptr, hm->message.len); // client->dataLen = hm->message.len; memcpy_s(conn->data, conn->dataCap, hm->body.ptr, hm->body.len); + conn->data[hm->body.len] = '\0'; conn->dataLen = hm->body.len; conn->dataReceived = true; } @@ -89,7 +91,8 @@ bool MatrixHttpGet( MatrixClient * client, const char * url, - char * outResponseBuffer, int outResponseCap) + char * outResponseBuffer, int outResponseCap, + bool authenticated) { MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData; @@ -97,12 +100,19 @@ MatrixHttpGet( struct mg_str host = mg_url_host(client->server); + static char authorizationHeader[AUTHORIZATION_HEADER_LEN] = "\0"; + if (authenticated) + sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN, + "Authorization: Bearer %s\r\n", client->accessTokenBuffer); + mg_printf(conn->connection, "GET %s HTTP/1.1\r\n" "Host: %.*s\r\n" + "%s" "\r\n", url, - host.len, host.ptr); + host.len, host.ptr, + authorizationHeader); conn->data = outResponseBuffer; conn->dataCap = outResponseCap; @@ -118,7 +128,8 @@ MatrixHttpPost( MatrixClient * client, const char * url, const char * requestBuffer, - char * outResponseBuffer, int outResponseCap) + char * outResponseBuffer, int outResponseCap, + bool authenticated) { MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData; @@ -126,9 +137,60 @@ MatrixHttpPost( struct mg_str host = mg_url_host(client->server); + static char authorizationHeader[AUTHORIZATION_HEADER_LEN] = "\0"; + if (authenticated) + sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN, + "Authorization: Bearer %s\r\n", client->accessTokenBuffer); + mg_printf(conn->connection, "POST %s HTTP/1.0\r\n" "Host: %.*s\r\n" + "%s" + "Content-Type: application/json\r\n" + "Content-Length: %d\r\n" + "\r\n" + "%s" + "\r\n", + url, + host.len, host.ptr, + authorizationHeader, + strlen(requestBuffer), + requestBuffer); + + conn->data = outResponseBuffer; + conn->dataCap = outResponseCap; + + while (! conn->dataReceived) + mg_mgr_poll(&conn->mgr, 1000); + + return conn->dataReceived; +} + +bool +MatrixHttpPut( + MatrixClient * client, + const char * url, + const char * requestBuffer, + char * outResponseBuffer, int outResponseCap, + bool authenticated) +{ + MatrixHttpConnection * conn = (MatrixHttpConnection *)client->httpUserData; + + conn->dataReceived = false; + + struct mg_str host = mg_url_host(client->server); + + static char authorizationHeader[AUTHORIZATION_HEADER_LEN]; + if (authenticated) + sprintf_s(authorizationHeader, AUTHORIZATION_HEADER_LEN, + "Authorization: Bearer %s\r\n", client->accessTokenBuffer); + else + authorizationHeader[0] = '\0'; + + mg_printf(conn->connection, + "PUT %s HTTP/1.0\r\n" + "Host: %.*s\r\n" + "%s" "Content-Type: application/json\r\n" "Content-Length: %d\r\n" "\r\n" @@ -136,6 +198,7 @@ MatrixHttpPost( "\r\n", url, host.len, host.ptr, + authorizationHeader, strlen(requestBuffer), requestBuffer); @@ -146,4 +209,4 @@ MatrixHttpPost( mg_mgr_poll(&conn->mgr, 1000); return conn->dataReceived; -} \ No newline at end of file +} -- 2.50.1