Move main down + Use Encryption keygen

This commit is contained in:
timoreo 2023-12-09 15:44:09 +01:00
parent 3a35dadf3d
commit c2d0114aec
Signed by: timoreo
GPG key ID: 121A72C3512BA288
2 changed files with 76 additions and 71 deletions

@ -1 +1 @@
Subproject commit 833de44d81c7a79bb1327cd829116c1cce3861f6
Subproject commit b055b4a6630b06f3fc243dedac2951dc361b72f4

View file

@ -6,6 +6,7 @@
#include <filesystem>
#include <fstream>
#include <iostream>
#include <memorystore.h>
#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<OlmAccount*>(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();
}