]> gitweb.ps.run Git - matrix_esp_thesis/commitdiff
send example, http PUT
authorPatrick <patrick.schoenberger@posteo.de>
Sun, 28 May 2023 15:27:25 +0000 (17:27 +0200)
committerPatrick <patrick.schoenberger@posteo.de>
Sun, 28 May 2023 15:27:25 +0000 (17:27 +0200)
examples/Send.c
src/matrix.c
src/matrix.h
src/matrix_http_mongoose.c

index 8786be910ab32dac20bd5a7dd5bead220de83fa9..e595ba7011688c537882233ef977e7da1072761c 100644 (file)
@@ -1,26 +1,28 @@
 #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
index 5759ba24334a3d1ee8aca71e927e3cef50f248ad..7cc4de9fe11d25a054d6e62a44563d3f97682b82 100644 (file)
@@ -1,5 +1,6 @@
 #include "matrix.h"\r
 \r
+#include <time.h>\r
 #include <stdio.h>\r
 #include <mjson.h>\r
 \r
@@ -8,6 +9,10 @@
 #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
@@ -23,6 +28,22 @@ MatrixClientInit(
     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
@@ -52,7 +73,8 @@ MatrixClientLoginPassword(
         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
@@ -74,4 +96,26 @@ MatrixClientLoginPassword(
 \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
index cd109ce87d81cb863404628606f70fd34beb320f..2e7f6e0e9a6861fa70839a18b2c8dd8ca27b19b4 100644 (file)
@@ -21,7 +21,7 @@ typedef struct MatrixClient {
     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
@@ -35,12 +35,24 @@ MatrixClientInit(
     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
@@ -54,13 +66,23 @@ bool
 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
index d44787c7760137dd3fb78aa1744f9d5849040813..8d575e5213bf37407c5590a85c03ce72123e74be 100644 (file)
@@ -6,6 +6,7 @@
 #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
@@ -48,6 +49,7 @@ MatrixHttpCallback(
         // 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
@@ -89,7 +91,8 @@ bool
 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
@@ -97,12 +100,19 @@ MatrixHttpGet(
 \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
@@ -118,7 +128,8 @@ MatrixHttpPost(
     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
@@ -126,9 +137,60 @@ MatrixHttpPost(
 \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
@@ -136,6 +198,7 @@ MatrixHttpPost(
             "\r\n",\r
             url,\r
             host.len, host.ptr,\r
+            authorizationHeader,\r
             strlen(requestBuffer),\r
             requestBuffer);\r
 \r
@@ -146,4 +209,4 @@ MatrixHttpPost(
         mg_mgr_poll(&conn->mgr, 1000);\r
 \r
     return conn->dataReceived;\r
-}
\ No newline at end of file
+}\r