diff --git a/matrix-3ds-sdk b/matrix-3ds-sdk index 833de44..b055b4a 160000 --- a/matrix-3ds-sdk +++ b/matrix-3ds-sdk @@ -1 +1 @@ -Subproject commit 833de44d81c7a79bb1327cd829116c1cce3861f6 +Subproject commit b055b4a6630b06f3fc243dedac2951dc361b72f4 diff --git a/src/main.cpp b/src/main.cpp index c66eeb5..83b747b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #define PREFIX_DIRECTORY "./matrix-storage" #define TOKEN_FILENAME "token" @@ -23,75 +24,7 @@ class Store : public Matrix::Store { }; std::string ask_for_pass(); PrintConsole* bottom = new PrintConsole; -int main() { - fsInit(); - gfxInitDefault(); - consoleInit(GFX_BOTTOM, bottom); - uint8_t major, minor, patch; - olm_get_library_version(&major, &minor, &patch); - std::cout << "Using olm " << major << "." << minor << "." << patch << std::endl; - std::filesystem::create_directories(PREFIX_DIRECTORY); - chdir(PREFIX_DIRECTORY); - Matrix::Client* client = nullptr; - while (aptMainLoop()) { - gspWaitForVBlank(); - hidScanInput(); - u32 kDown = hidKeysDown(); - if (kDown & KEY_START) { - break; - } - if (kDown & KEY_SELECT) { - std::ifstream stream{TOKEN_FILENAME, std::fstream::in}; - if(!stream.is_open()){ - // File dosn't exist, log in - client = new Matrix::Client("https://matrix.timoreo.fr", "", nullptr); // &s - - if (client->login("timoreo-3ds", ask_for_pass(), DEVICE_ID)) { - // logged in - puts("Logged in !!"); - std::ofstream ostr{TOKEN_FILENAME, std::fstream::out}; - ostr << client->getToken() << std::endl; - ostr.close(); - } else { - // wrong endpoint or combo - puts("Wrong combo !"); - goto wrong; - } - }else{ - std::string line; - std::getline(stream, line); - client = new Matrix::Client("https://matrix.timoreo.fr", line, nullptr); // &s - stream.close(); - } - auto userid = client->getUserId(); - if(userid.empty()){ - puts("Userid is empty ! Wrong token ?"); - goto wrong; - } - // Load encryption here - - client->setRoomInfoCallback([](std::string roomId, Matrix::RoomInfo info){ - consoleSelect(bottom); - printf("Joined room %s named %s\n", roomId.c_str(), info.name.c_str()); - }); - client->setEventCallback([](std::string roomId, json_t* event){ - consoleSelect(bottom); - printf("Event in room %s\n", roomId.c_str()); - printf("%s\n", json_dumps(event, JSON_ENSURE_ASCII)); - }); - client->startSyncLoop(); - } - wrong: - ; - } - if(client != nullptr){ - client->stopSyncLoop(); - // DO NOT LOGOUT, KEEP ENCRYPTION KEYS - } - gfxExit(); - fsExit(); -} std::string ask_for_pass() { SwkbdState swkbd; swkbdInit(&swkbd, SWKBD_TYPE_WESTERN, 1, -1); @@ -190,7 +123,7 @@ json_t* get_fallback_keys(Matrix::Client& client, OlmAccount* acc) { return fallback_keys; } -json_t* get_unpublished_otk(Matrix::Client client, OlmAccount* acc) { +json_t* get_unpublished_otk(Matrix::Client& client, OlmAccount* acc) { json_t * otk_keys = json_object(); // Extract keys size_t acclen = olm_account_one_time_keys_length(acc); @@ -226,7 +159,7 @@ void save_keys(OlmAccount* acc) { free(m); fclose(file); } -OlmAccount* load_account(Matrix::Client client){ +OlmAccount* load_account(Matrix::Client& client){ auto* acc = static_cast(malloc(olm_account_size())); olm_account(acc); std::filebuf filestorage; @@ -282,4 +215,76 @@ OlmAccount* load_account(Matrix::Client client){ olm_unpickle_account(acc, nullptr, 0, data, overhead + fsize); } return acc; +} + + +int main() { + fsInit(); + gfxInitDefault(); + consoleInit(GFX_BOTTOM, bottom); + uint8_t major, minor, patch; + olm_get_library_version(&major, &minor, &patch); + std::cout << "Using olm " << major << "." << minor << "." << patch << std::endl; + std::filesystem::create_directories(PREFIX_DIRECTORY); + chdir(PREFIX_DIRECTORY); + Matrix::MemoryStore store = Matrix::MemoryStore(); + Matrix::Client client("https://matrix.timoreo.fr", "", &store); + OlmAccount* acc; + while (aptMainLoop()) { + gspWaitForVBlank(); + hidScanInput(); + u32 kDown = hidKeysDown(); + + if (kDown & KEY_START) { + break; + } + if (kDown & KEY_SELECT) { + std::ifstream stream{TOKEN_FILENAME, std::fstream::in}; + if(!stream.is_open()){ + // File dosn't exist, log in + + if (client.login("timoreo-3ds", ask_for_pass(), DEVICE_ID)) { + // logged in + puts("Logged in !!"); + std::ofstream ostr{TOKEN_FILENAME, std::fstream::out}; + ostr << client.getToken() << std::endl; + ostr.close(); + } else { + // wrong endpoint or combo + puts("Wrong combo !"); + goto wrong; + } + }else{ + std::string line; + std::getline(stream, line); + client = Matrix::Client("https://matrix.timoreo.fr", line, nullptr); // &s + stream.close(); + } + auto userid = client.getUserId(); + if(userid.empty()){ + puts("Userid is empty ! Wrong token ?"); + goto wrong; + } + std::cout << "Logged in as " << userid << std::endl; + // Load encryption here + acc = load_account(client); + client.setRoomInfoCallback([](std::string roomId, Matrix::RoomInfo info){ + consoleSelect(bottom); + printf("Joined room %s named %s\n", roomId.c_str(), info.name.c_str()); + }); + client.setEventCallback([](std::string roomId, json_t* event){ + consoleSelect(bottom); + printf("Event in room %s\n", roomId.c_str()); + printf("%s\n", json_dumps(event, JSON_ENSURE_ASCII)); + }); + client.startSyncLoop(); + } + wrong: + ; + } + client.stopSyncLoop(); + // DO NOT LOGOUT, KEEP ENCRYPTION KEYS + free(acc); + gfxExit(); + fsExit(); } \ No newline at end of file