X-Git-Url: https://gitweb.ps.run/matrix_esp_thesis/blobdiff_plain/c7aba5979c820958aa08947903afb47ace496a16..HEAD:/examples/Cli.c?ds=sidebyside diff --git a/examples/Cli.c b/examples/Cli.c index daf79e9..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,29 +70,59 @@ Usage( printf("Usage: %s %s\n", cmd, args); } -void +bool ExecuteCommand( MatrixClient * client, 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); +#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")) { 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++) @@ -102,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, @@ -115,15 +146,13 @@ ExecuteCommand( " ", mjson_print_fixed_buf, &fb); printf("%.*s\n", fb.len, fb.ptr); } - else if (CheckCommand(cmd, "save")) { - CHECK_ARGS(1, "") + else if (CheckCommand(cmd, "login")) { + CHECK_ARGS(3, " ") - MatrixClientSave(client, args[0]); - } - else if (CheckCommand(cmd, "load")) { - CHECK_ARGS(1, "") - - MatrixClientLoad(client, args[0]); + MatrixClientLoginPassword(client, + args[0], + args[1], + args[2]); } else if (CheckCommand(cmd, "send")) { CHECK_ARGS(2, " ") @@ -138,6 +167,19 @@ ExecuteCommand( "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, "") @@ -169,26 +211,6 @@ ExecuteCommand( args[2], &client->megolmOutSessions[sessionIndex]); } - else if (CheckCommand(cmd, "savesession")) { - CHECK_ARGS(3, " ") - - int sessionIndex = atoi(args[0]); - - MatrixMegolmOutSessionSave( - &client->megolmOutSessions[sessionIndex], - args[1], - args[2]); - } - else if (CheckCommand(cmd, "loadsession")) { - CHECK_ARGS(3, " ") - - int sessionIndex = atoi(args[0]); - - MatrixMegolmOutSessionLoad( - &client->megolmOutSessions[sessionIndex], - args[1], - args[2]); - } else if (CheckCommand(cmd, "printsessions")) { for (int i = 0; i < client->numMegolmOutSessions; i++) { printf("%d: %s\t%s\t%s\n", i, @@ -200,8 +222,10 @@ ExecuteCommand( else if (CheckCommand(cmd, "initsession")) { CHECK_ARGS(1, "") - if (! MatrixClientInitMegolmOutSession(client, - args[0])) + MatrixMegolmOutSession * megolmOutSession; + if (! MatrixClientNewMegolmOutSession(client, + args[0], + &megolmOutSession)) { printf("Maximum number of Megolm sessions reached (%d)\n", NUM_MEGOLM_SESSIONS); } @@ -212,23 +236,25 @@ ExecuteCommand( 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_[NUMBER_ARGS][BUFFER_SIZE]; @@ -236,14 +262,19 @@ main(void) 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; }