abouttreesummaryrefslogcommitdiff
diff options
context:
space:
mode:
authorPatrick2023-07-17 17:37:49 +0200
committerPatrick2023-07-17 17:37:49 +0200
commit464bfb1912d0806143386f61c33dd45fbafc38e8 (patch)
tree0c6c64c8c144d262e64c14e5b3434b146928220d
parent4c72c6901e007414aebb4cb6534c1a49d63558b0 (diff)
downloadmatrix_esp_thesis-464bfb1912d0806143386f61c33dd45fbafc38e8.tar.gz
matrix_esp_thesis-464bfb1912d0806143386f61c33dd45fbafc38e8.zip
cli send, save and load
-rw-r--r--examples/Cli.c43
-rw-r--r--src/matrix.c55
-rw-r--r--src/matrix.h10
3 files changed, 103 insertions, 5 deletions
diff --git a/examples/Cli.c b/examples/Cli.c
index af1d6b1..b2fbe45 100644
--- a/examples/Cli.c
+++ b/examples/Cli.c
@@ -76,14 +76,13 @@ ExecuteCommand(
const char * cmd,
int nargs, char ** args
) {
+#define CHECK_ARGS(N, ARGS) if (nargs != N) { Usage(cmd, ARGS); return; }
/**/ if (CheckCommand(cmd, "devicekey")) {
printf("%s\n", client->deviceKey);
}
else if (CheckCommand(cmd, "genkeys")) {
- if (nargs != 1) {
- Usage(cmd, "<number of keys>");
- return;
- }
+ CHECK_ARGS(1, "<number of keys>")
+
MatrixClientGenerateOnetimeKeys(client, atoi(args[0]));
}
else if (CheckCommand(cmd, "uploadkeys")) {
@@ -116,6 +115,40 @@ ExecuteCommand(
" ", mjson_print_fixed_buf, &fb);
printf("%.*s\n", fb.len, fb.ptr);
}
+ else if (CheckCommand(cmd, "save")) {
+ CHECK_ARGS(1, "<filename>")
+
+ MatrixClientSave(client, args[0]);
+ }
+ else if (CheckCommand(cmd, "load")) {
+ CHECK_ARGS(1, "<filename>")
+
+ MatrixClientLoad(client, args[0]);
+ }
+ else if (CheckCommand(cmd, "send")) {
+ CHECK_ARGS(2, "<room_id> <message>")
+
+ static char body[1024];
+ snprintf(body, 1024,
+ "{\"body\":\"%s\",\"msgtype\":\"m.text\"}",
+ args[1]);
+
+ printf("Sending %s to %s\n", body, args[0]);
+
+ MatrixClientSendEvent(client,
+ args[0],
+ "m.room.message",
+ body);
+ }
+ else if (CheckCommand(cmd, "setuserid")) {
+ CHECK_ARGS(1, "<user_id>")
+
+ MatrixClientSetUserId(client, args[0]);
+ }
+ else if (CheckCommand(cmd, "getuserid")) {
+ printf("User ID: %s\n", client->userId);
+ }
+#undef CHECK_ARGS
}
int
@@ -135,7 +168,7 @@ main(void)
USER_ID);
static char cmd[BUFFER_SIZE];
- static char args_[BUFFER_SIZE][NUMBER_ARGS];
+ static char args_[NUMBER_ARGS][BUFFER_SIZE];
char * args[NUMBER_ARGS];
for (int i = 0; i < NUMBER_ARGS; i++)
args[i] = args_[i];
diff --git a/src/matrix.c b/src/matrix.c
index fbcf97e..80802c3 100644
--- a/src/matrix.c
+++ b/src/matrix.c
@@ -259,6 +259,58 @@ MatrixClientInit(
}
bool
+MatrixClientSave(
+ MatrixClient * client,
+ const char * filename)
+{
+ FILE * f = fopen(filename, "w");
+
+ fwrite(client->deviceKey, 1, DEVICE_KEY_SIZE, f);
+ fwrite(client->signingKey, 1, DEVICE_KEY_SIZE, f);
+ fwrite(client->userId, 1, USER_ID_SIZE, f);
+ fwrite(client->server, 1, SERVER_SIZE, f);
+ fwrite(client->accessToken, 1, ACCESS_TOKEN_SIZE, f);
+ fwrite(client->deviceId, 1, DEVICE_ID_SIZE, f);
+ fwrite(client->expireMs, 1, EXPIRE_MS_SIZE, f);
+ fwrite(client->refreshToken, 1, REFRESH_TOKEN_SIZE, f);
+
+ fwrite(&client->numDevices, sizeof(int), 1, f);
+ for (int i = 0; i < client->numDevices; i++) {
+ fwrite(client->devices[i].deviceId, 1, DEVICE_ID_SIZE, f);
+ fwrite(client->devices[i].deviceKey, 1, DEVICE_KEY_SIZE, f);
+ }
+
+ fclose(f);
+ return true;
+}
+
+bool
+MatrixClientLoad(
+ MatrixClient * client,
+ const char * filename)
+{
+ FILE * f = fopen(filename, "r");
+
+ fread(client->deviceKey, 1, DEVICE_KEY_SIZE, f);
+ fread(client->signingKey, 1, DEVICE_KEY_SIZE, f);
+ fread(client->userId, 1, USER_ID_SIZE, f);
+ fread(client->server, 1, SERVER_SIZE, f);
+ fread(client->accessToken, 1, ACCESS_TOKEN_SIZE, f);
+ fread(client->deviceId, 1, DEVICE_ID_SIZE, f);
+ fread(client->expireMs, 1, EXPIRE_MS_SIZE, f);
+ fread(client->refreshToken, 1, REFRESH_TOKEN_SIZE, f);
+
+ fread(&client->numDevices, sizeof(int), 1, f);
+ for (int i = 0; i < client->numDevices; i++) {
+ fread(client->devices[i].deviceId, 1, DEVICE_ID_SIZE, f);
+ fread(client->devices[i].deviceKey, 1, DEVICE_KEY_SIZE, f);
+ }
+
+ fclose(f);
+ return true;
+}
+
+bool
MatrixClientSetAccessToken(
MatrixClient * client,
const char * accessToken)
@@ -270,6 +322,7 @@ MatrixClientSetAccessToken(
for (int i = 0; i < accessTokenLen; i++)
client->accessToken[i] = accessToken[i];
+ client->accessToken[accessTokenLen] = '\0';
return true;
}
@@ -286,6 +339,7 @@ MatrixClientSetDeviceId(
for (int i = 0; i < deviceIdLen; i++)
client->deviceId[i] = deviceId[i];
+ client->deviceId[deviceIdLen] = '\0';
return true;
}
@@ -302,6 +356,7 @@ MatrixClientSetUserId(
for (int i = 0; i < userIdLen; i++)
client->userId[i] = userId[i];
+ client->userId[userIdLen] = '\0';
return true;
}
diff --git a/src/matrix.h b/src/matrix.h
index 32b8294..91ed208 100644
--- a/src/matrix.h
+++ b/src/matrix.h
@@ -157,6 +157,16 @@ MatrixClientInit(
const char * server);
bool
+MatrixClientSave(
+ MatrixClient * client,
+ const char * filename);
+
+bool
+MatrixClientLoad(
+ MatrixClient * client,
+ const char * filename);
+
+bool
MatrixClientSetAccessToken(
MatrixClient * client,
const char * accessToken);