X-Git-Url: https://gitweb.ps.run/matrix_esp_thesis/blobdiff_plain/4c72c6901e007414aebb4cb6534c1a49d63558b0..HEAD:/examples/Cli.c?ds=sidebyside diff --git a/examples/Cli.c b/examples/Cli.c index af1d6b1..bcd3a12 100644 --- a/examples/Cli.c +++ b/examples/Cli.c @@ -4,11 +4,11 @@ #include #include -#define SERVER "https://matrix.org" -#define ACCESS_TOKEN "syt_cHNjaG8_yBvTjVTquGCikvsAenOJ_49mBMO" -#define DEVICE_ID "MAZNCCZLBR" -#define USER_ID "@pscho:matrix.org" -#define ROOM_ID "!XKFUjAsGrSSrpDFIxB:matrix.org" +#define SERVER "https://matrix.org" +#define USER_ID "@example:matrix.org" +#define USERNAME "" +#define PASSWORD "" +#define DEVICE_NAME "" #define BUFFER_SIZE 1024 #define NUMBER_ARGS 10 @@ -70,30 +70,59 @@ Usage( printf("Usage: %s %s\n", cmd, args); } -void +bool ExecuteCommand( MatrixClient * client, const char * cmd, int nargs, char ** args ) { - /**/ if (CheckCommand(cmd, "devicekey")) { - printf("%s\n", client->deviceKey); +#define CHECK_ARGS(N, ARGS) if (nargs != N) { Usage(cmd, ARGS); return true; } + + /**/ if (CheckCommand(cmd, "exit")) { + return false; + } + else if (CheckCommand(cmd, "devicekey")) { + static char key[DEVICE_KEY_SIZE]; + if (MatrixOlmAccountGetDeviceKey(&client->olmAccount, key, DEVICE_KEY_SIZE)) + printf("%s\n", key); + } + else if (CheckCommand(cmd, "accesstoken")) { + printf("%s\n", client->accessToken); } else if (CheckCommand(cmd, "genkeys")) { - if (nargs != 1) { - Usage(cmd, ""); - return; - } + CHECK_ARGS(1, "") + MatrixClientGenerateOnetimeKeys(client, atoi(args[0])); } - else if (CheckCommand(cmd, "uploadkeys")) { + else if (CheckCommand(cmd, "uploadonetimekeys")) { MatrixClientUploadOnetimeKeys(client); } + else if (CheckCommand(cmd, "uploaddevicekey")) { + MatrixClientUploadDeviceKeys(client); + } else if (CheckCommand(cmd, "onetimekeys")) { static char buffer[1024]; olm_account_one_time_keys(client->olmAccount.account, buffer, 1024); printf("%s\n", buffer); } + else if (CheckCommand(cmd, "sendto")) { + CHECK_ARGS(3, " ") + + MatrixClientSendToDevice(client, + USER_ID, + args[0], + args[2], + args[1]); + } + else if (CheckCommand(cmd, "sendtoe")) { + CHECK_ARGS(3, " ") + + MatrixClientSendToDeviceEncrypted(client, + USER_ID, + args[0], + args[2], + args[1]); + } else if (CheckCommand(cmd, "getkeys")) { MatrixClientRequestDeviceKeys(client); for (int i = 0; i < client->numDevices; i++) @@ -103,8 +132,9 @@ ExecuteCommand( } else if (CheckCommand(cmd, "todevice")) { static char buffer[30000]; + static char nextBatch[128]; MatrixClientSync(client, - buffer, 30000); + buffer, 30000, nextBatch, 128); const char * todevice; int todeviceLen; mjson_find(buffer, 30000, @@ -116,38 +146,135 @@ ExecuteCommand( " ", mjson_print_fixed_buf, &fb); printf("%.*s\n", fb.len, fb.ptr); } + else if (CheckCommand(cmd, "login")) { + CHECK_ARGS(3, " ") + + MatrixClientLoginPassword(client, + args[0], + args[1], + args[2]); + } + else if (CheckCommand(cmd, "send")) { + CHECK_ARGS(2, " ") + + static char body[1024]; + snprintf(body, 1024, + "{\"body\":\"%s\",\"msgtype\":\"m.text\"}", + args[1]); + + MatrixClientSendEvent(client, + args[0], + "m.room.message", + body); + } + else if (CheckCommand(cmd, "sendencrypted")) { + CHECK_ARGS(2, " ") + + static char body[1024]; + snprintf(body, 1024, + "{\"body\":\"%s\",\"msgtype\":\"m.text\"}", + args[1]); + + MatrixClientSendEventEncrypted(client, + args[0], + "m.room.message", + body); + } + else if (CheckCommand(cmd, "setuserid")) { + CHECK_ARGS(1, "") + + MatrixClientSetUserId(client, args[0]); + } + else if (CheckCommand(cmd, "getuserid")) { + printf("User ID: %s\n", client->userId); + } + else if (CheckCommand(cmd, "sendencrypted")) { + CHECK_ARGS(2, " ") + + static char body[1024]; + snprintf(body, 1024, + "{\"body\":\"%s\",\"msgtype\":\"m.text\"}", + args[1]); + + MatrixClientSendEventEncrypted(client, + args[0], + "m.room.message", + body); + } + else if (CheckCommand(cmd, "sharesession")) { + CHECK_ARGS(3, " ") + + int sessionIndex = atoi(args[0]); + + MatrixClientShareMegolmOutSession(client, + args[1], + args[2], + &client->megolmOutSessions[sessionIndex]); + } + else if (CheckCommand(cmd, "printsessions")) { + for (int i = 0; i < client->numMegolmOutSessions; i++) { + printf("%d: %s\t%s\t%s\n", i, + client->megolmOutSessions[i].roomId, + client->megolmOutSessions[i].id, + client->megolmOutSessions[i].key); + } + } + else if (CheckCommand(cmd, "initsession")) { + CHECK_ARGS(1, "") + + MatrixMegolmOutSession * megolmOutSession; + if (! MatrixClientNewMegolmOutSession(client, + args[0], + &megolmOutSession)) + { + printf("Maximum number of Megolm sessions reached (%d)\n", NUM_MEGOLM_SESSIONS); + } + } + + + else { + printf("Unknown command\n"); + } +#undef CHECK_ARGS + + return true; } int main(void) { MatrixClient client; - MatrixClientInit(&client, - SERVER); + MatrixClientInit(&client); - MatrixHttpInit(&client); + MatrixHttpInit(&client.hc, SERVER); + + + MatrixClientSetUserId(&client, USER_ID); + MatrixClientLoginPassword(&client, USERNAME, PASSWORD, DEVICE_NAME); + MatrixClientGenerateOnetimeKeys(&client, 10); + MatrixClientUploadDeviceKeys(&client); + MatrixClientUploadOnetimeKeys(&client); - MatrixClientSetAccessToken(&client, - ACCESS_TOKEN); - MatrixClientSetDeviceId(&client, - DEVICE_ID); - MatrixClientSetUserId(&client, - 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]; int nargs; - do { + while (1) { GetCommand(cmd, &nargs, args); - ExecuteCommand(&client, cmd, nargs, args); - - } while (strcmp(cmd, "exit") != 0); + bool res = + ExecuteCommand(&client, cmd, nargs, args); - MatrixHttpDeinit(&client); + if (! res) + break; + } + + MatrixClientDeleteDevice(&client); + + MatrixHttpDeinit(&client.hc); return 0; }