Reformat
This commit is contained in:
parent
aa512aa4af
commit
a4f6c72e1d
1 changed files with 37 additions and 44 deletions
81
src/main.cpp
81
src/main.cpp
|
@ -1,24 +1,20 @@
|
||||||
#include <3ds.h>
|
#include <3ds.h>
|
||||||
#include <matrixclient.h>
|
#include <matrixclient.h>
|
||||||
|
#include <memorystore.h>
|
||||||
#include <olm/olm.h>
|
#include <olm/olm.h>
|
||||||
#include <olm/account.hh>
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <memorystore.h>
|
#include <olm/account.hh>
|
||||||
|
|
||||||
#define PREFIX_DIRECTORY "./matrix-storage"
|
#define PREFIX_DIRECTORY "./matrix-storage"
|
||||||
#define TOKEN_FILENAME "token"
|
#define TOKEN_FILENAME "token"
|
||||||
#define DEVICE_ID "XREVXEDLHU"
|
#define DEVICE_ID "XREVXEDLHU"
|
||||||
|
|
||||||
class Store : public Matrix::Store {
|
class Store : public Matrix::Store {
|
||||||
void setSyncToken(std::string token) override {
|
void setSyncToken(std::string token) override {} //TODO store in file
|
||||||
|
std::string getSyncToken() override { return ""; }
|
||||||
}
|
|
||||||
std::string getSyncToken() override {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
void setFilterId(std::string filterId) override {}
|
void setFilterId(std::string filterId) override {}
|
||||||
std::string getFilterId() override { return ""; }
|
std::string getFilterId() override { return ""; }
|
||||||
};
|
};
|
||||||
|
@ -30,7 +26,7 @@ std::string ask_for_pass() {
|
||||||
swkbdSetValidation(&swkbd, SWKBD_NOTEMPTY_NOTBLANK, 0, 0);
|
swkbdSetValidation(&swkbd, SWKBD_NOTEMPTY_NOTBLANK, 0, 0);
|
||||||
swkbdSetPasswordMode(&swkbd, SWKBD_PASSWORD_HIDE_DELAY);
|
swkbdSetPasswordMode(&swkbd, SWKBD_PASSWORD_HIDE_DELAY);
|
||||||
swkbdSetHintText(&swkbd, "Enter password");
|
swkbdSetHintText(&swkbd, "Enter password");
|
||||||
char buf[256]{0};
|
char buf[256]{ 0 };
|
||||||
swkbdInputText(&swkbd, buf, sizeof(buf));
|
swkbdInputText(&swkbd, buf, sizeof(buf));
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
@ -40,12 +36,11 @@ void generate_otk(olm::Account& acc, size_t otkcount) {
|
||||||
PS_GenerateRandomBytes(otkrandom.get(), len);
|
PS_GenerateRandomBytes(otkrandom.get(), len);
|
||||||
acc.generate_one_time_keys(otkcount, otkrandom.get(), len);
|
acc.generate_one_time_keys(otkcount, otkrandom.get(), len);
|
||||||
}
|
}
|
||||||
void generate_otk(olm::Account& acc){
|
void generate_otk(olm::Account& acc) {
|
||||||
size_t otkcount = acc.max_number_of_one_time_keys()/2;
|
size_t otkcount = acc.max_number_of_one_time_keys() / 2;
|
||||||
generate_otk(acc, otkcount);
|
generate_otk(acc, otkcount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void generate_device_key(olm::Account& acc) {
|
void generate_device_key(olm::Account& acc) {
|
||||||
size_t len = acc.new_account_random_length();
|
size_t len = acc.new_account_random_length();
|
||||||
std::unique_ptr<uint8_t[]> random = std::make_unique<uint8_t[]>(len);
|
std::unique_ptr<uint8_t[]> random = std::make_unique<uint8_t[]>(len);
|
||||||
|
@ -67,15 +62,15 @@ void sign_json(Matrix::Client& client, olm::Account& acc, json_t* json) {
|
||||||
std::string signature(signlen, '\0');
|
std::string signature(signlen, '\0');
|
||||||
acc.sign(reinterpret_cast<const uint8_t*>(jsonStr), strlen(jsonStr), reinterpret_cast<uint8_t*>(signature.data()), signlen);
|
acc.sign(reinterpret_cast<const uint8_t*>(jsonStr), strlen(jsonStr), reinterpret_cast<uint8_t*>(signature.data()), signlen);
|
||||||
free(jsonStr);
|
free(jsonStr);
|
||||||
json_t * signitem = json_object();
|
json_t* signitem = json_object();
|
||||||
json_object_set(signitem, ("ed25519:" + client.getDeviceId()).c_str(), json_stringn(signature.data(), signlen));
|
json_object_set(signitem, ("ed25519:" + client.getDeviceId()).c_str(), json_stringn(signature.data(), signlen));
|
||||||
json_t * signobj = json_object();
|
json_t* signobj = json_object();
|
||||||
json_object_set(signobj, client.getUserId().c_str(), signitem);
|
json_object_set(signobj, client.getUserId().c_str(), signitem);
|
||||||
json_object_set(json, "signatures", signobj);
|
json_object_set(json, "signatures", signobj);
|
||||||
}
|
}
|
||||||
json_t* get_device_keys(Matrix::Client& client, olm::Account& acc) {
|
json_t* get_device_keys(Matrix::Client& client, olm::Account& acc) {
|
||||||
json_t * device_keys = json_object();
|
json_t* device_keys = json_object();
|
||||||
json_t * algorithms = json_array();
|
json_t* algorithms = json_array();
|
||||||
json_array_append_new(algorithms, json_string("m.olm.v1.curve25519-aes-sha2"));
|
json_array_append_new(algorithms, json_string("m.olm.v1.curve25519-aes-sha2"));
|
||||||
json_array_append_new(algorithms, json_string("m.megolm.v1.aes-sha2"));
|
json_array_append_new(algorithms, json_string("m.megolm.v1.aes-sha2"));
|
||||||
json_object_set_new(device_keys, "algorithms", algorithms);
|
json_object_set_new(device_keys, "algorithms", algorithms);
|
||||||
|
@ -90,9 +85,9 @@ json_t* get_device_keys(Matrix::Client& client, olm::Account& acc) {
|
||||||
json_error_t error;
|
json_error_t error;
|
||||||
sleep(5);
|
sleep(5);
|
||||||
std::cout << "Heading into it..." << std::endl;
|
std::cout << "Heading into it..." << std::endl;
|
||||||
json_t * keys = json_loadb(reinterpret_cast<const char*>(acckeys.get()), bytes, 0, &error);
|
json_t* keys = json_loadb(reinterpret_cast<const char*>(acckeys.get()), bytes, 0, &error);
|
||||||
if(keys == nullptr){
|
if (keys == nullptr) {
|
||||||
printf("error: on line %d at char %d: %s\n", error.line, error.column ,error.text);
|
printf("error: on line %d at char %d: %s\n", error.line, error.column, error.text);
|
||||||
sleep(10);
|
sleep(10);
|
||||||
}
|
}
|
||||||
std::cout << "Reloaded data" << std::endl;
|
std::cout << "Reloaded data" << std::endl;
|
||||||
|
@ -106,12 +101,12 @@ json_t* get_device_keys(Matrix::Client& client, olm::Account& acc) {
|
||||||
return device_keys;
|
return device_keys;
|
||||||
}
|
}
|
||||||
json_t* get_fallback_keys(Matrix::Client& client, olm::Account& acc) {
|
json_t* get_fallback_keys(Matrix::Client& client, olm::Account& acc) {
|
||||||
json_t * fallback_keys = json_object();
|
json_t* fallback_keys = json_object();
|
||||||
// Extract keys
|
// Extract keys
|
||||||
size_t acclen = acc.get_unpublished_fallback_key_json_length();
|
size_t acclen = acc.get_unpublished_fallback_key_json_length();
|
||||||
auto acckeys = std::make_unique<uint8_t[]>(acclen);
|
auto acckeys = std::make_unique<uint8_t[]>(acclen);
|
||||||
size_t bytes = acc.get_unpublished_fallback_key_json(acckeys.get(), acclen);
|
size_t bytes = acc.get_unpublished_fallback_key_json(acckeys.get(), acclen);
|
||||||
json_t * keys = json_loadb(reinterpret_cast<const char*>(acckeys.get()), bytes, 0, nullptr);
|
json_t* keys = json_loadb(reinterpret_cast<const char*>(acckeys.get()), bytes, 0, nullptr);
|
||||||
json_t* keyobj = json_object_get(keys, "curve25519");
|
json_t* keyobj = json_object_get(keys, "curve25519");
|
||||||
void* iter = json_object_iter(keyobj);
|
void* iter = json_object_iter(keyobj);
|
||||||
|
|
||||||
|
@ -124,7 +119,7 @@ json_t* get_fallback_keys(Matrix::Client& client, olm::Account& acc) {
|
||||||
json_object_set_new(tosign, "fallback", json_true());
|
json_object_set_new(tosign, "fallback", json_true());
|
||||||
|
|
||||||
sign_json(client, acc, tosign);
|
sign_json(client, acc, tosign);
|
||||||
json_object_set_new(fallback_keys, (std::string("signed_curve25519:") + keyid).c_str(), tosign );
|
json_object_set_new(fallback_keys, (std::string("signed_curve25519:") + keyid).c_str(), tosign);
|
||||||
iter = json_object_iter_next(keyobj, iter);
|
iter = json_object_iter_next(keyobj, iter);
|
||||||
}
|
}
|
||||||
json_decref(keys);
|
json_decref(keys);
|
||||||
|
@ -132,12 +127,12 @@ json_t* get_fallback_keys(Matrix::Client& client, olm::Account& acc) {
|
||||||
}
|
}
|
||||||
|
|
||||||
json_t* get_unpublished_otk(Matrix::Client& client, olm::Account& acc) {
|
json_t* get_unpublished_otk(Matrix::Client& client, olm::Account& acc) {
|
||||||
json_t * otk_keys = json_object();
|
json_t* otk_keys = json_object();
|
||||||
// Extract keys
|
// Extract keys
|
||||||
size_t acclen = acc.get_one_time_keys_json_length();
|
size_t acclen = acc.get_one_time_keys_json_length();
|
||||||
auto acckeys = std::make_unique<uint8_t []>(acclen);
|
auto acckeys = std::make_unique<uint8_t[]>(acclen);
|
||||||
size_t bytes = acc.get_one_time_keys_json(acckeys.get(), acclen);
|
size_t bytes = acc.get_one_time_keys_json(acckeys.get(), acclen);
|
||||||
json_t * keys = json_loadb(reinterpret_cast<const char*>(acckeys.get()), bytes, 0, nullptr);
|
json_t* keys = json_loadb(reinterpret_cast<const char*>(acckeys.get()), bytes, 0, nullptr);
|
||||||
|
|
||||||
json_t* keyobj = json_object_get(keys, "curve25519");
|
json_t* keyobj = json_object_get(keys, "curve25519");
|
||||||
void* iter = json_object_iter(keyobj);
|
void* iter = json_object_iter(keyobj);
|
||||||
|
@ -149,7 +144,7 @@ json_t* get_unpublished_otk(Matrix::Client& client, olm::Account& acc) {
|
||||||
json_object_set_new(tosign, "key", keyval);
|
json_object_set_new(tosign, "key", keyval);
|
||||||
|
|
||||||
sign_json(client, acc, tosign);
|
sign_json(client, acc, tosign);
|
||||||
json_object_set_new(otk_keys, (std::string("signed_curve25519:") + keyid).c_str(), tosign );
|
json_object_set_new(otk_keys, (std::string("signed_curve25519:") + keyid).c_str(), tosign);
|
||||||
iter = json_object_iter_next(keyobj, iter);
|
iter = json_object_iter_next(keyobj, iter);
|
||||||
}
|
}
|
||||||
json_decref(keys);
|
json_decref(keys);
|
||||||
|
@ -158,16 +153,16 @@ json_t* get_unpublished_otk(Matrix::Client& client, olm::Account& acc) {
|
||||||
|
|
||||||
void save_keys(olm::Account& acc) {
|
void save_keys(olm::Account& acc) {
|
||||||
size_t pacclen = olm::pickle_length(acc);
|
size_t pacclen = olm::pickle_length(acc);
|
||||||
auto m = std::make_unique<uint8_t []>(pacclen);
|
auto m = std::make_unique<uint8_t[]>(pacclen);
|
||||||
olm::pickle(m.get(), acc);
|
olm::pickle(m.get(), acc);
|
||||||
FILE* file = fopen("secret-keys", "w");
|
FILE* file = fopen("secret-keys", "w");
|
||||||
fwrite(m.get(), pacclen, 1, file);
|
fwrite(m.get(), pacclen, 1, file);
|
||||||
fclose(file);
|
fclose(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
void load_account(Matrix::Client& client, olm::Account& acc){
|
void load_account(Matrix::Client& client, olm::Account& acc) {
|
||||||
// std::FILE* file = std::fopen("secret-keys", "r");
|
// std::FILE* file = std::fopen("secret-keys", "r");
|
||||||
std::ifstream ifs("secret-keys", std::ios::binary|std::ios::ate);
|
std::ifstream ifs("secret-keys", std::ios::binary | std::ios::ate);
|
||||||
if (!ifs.is_open()) {
|
if (!ifs.is_open()) {
|
||||||
std::cout << "No secret keys found, generating " << std::endl;
|
std::cout << "No secret keys found, generating " << std::endl;
|
||||||
// Generate and save to file
|
// Generate and save to file
|
||||||
|
@ -186,7 +181,7 @@ void load_account(Matrix::Client& client, olm::Account& acc){
|
||||||
std::cout << "Saved keys postgen." << std::endl;
|
std::cout << "Saved keys postgen." << std::endl;
|
||||||
// Upload the keys ---
|
// Upload the keys ---
|
||||||
// Build device key json
|
// Build device key json
|
||||||
json_t * upload_keys = json_object();
|
json_t* upload_keys = json_object();
|
||||||
|
|
||||||
json_t* device_keys = get_device_keys(client, acc);
|
json_t* device_keys = get_device_keys(client, acc);
|
||||||
std::cout << "Gathered device keys" << std::endl;
|
std::cout << "Gathered device keys" << std::endl;
|
||||||
|
@ -215,18 +210,17 @@ void load_account(Matrix::Client& client, olm::Account& acc){
|
||||||
} else {
|
} else {
|
||||||
// Load from file
|
// Load from file
|
||||||
std::ifstream::pos_type fsize = ifs.tellg();
|
std::ifstream::pos_type fsize = ifs.tellg();
|
||||||
if(fsize == -1){
|
if (fsize == -1) {
|
||||||
std::cout << "What." << std::endl;
|
std::cout << "What." << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto data = std::make_unique<uint8_t []>(fsize);
|
auto data = std::make_unique<uint8_t[]>(fsize);
|
||||||
ifs.seekg(0, std::ios::beg);
|
ifs.seekg(0, std::ios::beg);
|
||||||
ifs.read(reinterpret_cast<char*>(data.get()), static_cast<std::streamsize>(fsize));
|
ifs.read(reinterpret_cast<char*>(data.get()), static_cast<std::streamsize>(fsize));
|
||||||
olm::unpickle(data.get(),data.get() + fsize, acc);
|
olm::unpickle(data.get(), data.get() + fsize, acc);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
fsInit();
|
fsInit();
|
||||||
gfxInitDefault();
|
gfxInitDefault();
|
||||||
|
@ -249,14 +243,14 @@ int main() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (kDown & KEY_SELECT) {
|
if (kDown & KEY_SELECT) {
|
||||||
std::ifstream stream{TOKEN_FILENAME, std::fstream::in};
|
std::ifstream stream{ TOKEN_FILENAME, std::fstream::in };
|
||||||
if(!stream.is_open()){
|
if (!stream.is_open()) {
|
||||||
// File dosn't exist, log in
|
// File dosn't exist, log in
|
||||||
|
|
||||||
if (client.login("timoreo-3ds", ask_for_pass(), DEVICE_ID)) {
|
if (client.login("timoreo-3ds", ask_for_pass(), DEVICE_ID)) {
|
||||||
// logged in
|
// logged in
|
||||||
puts("Logged in !!");
|
puts("Logged in !!");
|
||||||
std::ofstream ostr{TOKEN_FILENAME, std::fstream::out};
|
std::ofstream ostr{ TOKEN_FILENAME, std::fstream::out };
|
||||||
ostr << client.getToken() << std::endl;
|
ostr << client.getToken() << std::endl;
|
||||||
ostr.close();
|
ostr.close();
|
||||||
} else {
|
} else {
|
||||||
|
@ -264,33 +258,32 @@ int main() {
|
||||||
puts("Wrong combo !");
|
puts("Wrong combo !");
|
||||||
goto wrong;
|
goto wrong;
|
||||||
}
|
}
|
||||||
}else{
|
} else {
|
||||||
std::string line;
|
std::string line;
|
||||||
std::getline(stream, line);
|
std::getline(stream, line);
|
||||||
client = Matrix::Client("https://matrix.timoreo.fr", line, nullptr); // &s
|
client = Matrix::Client("https://matrix.timoreo.fr", line, nullptr); // &s
|
||||||
stream.close();
|
stream.close();
|
||||||
}
|
}
|
||||||
auto userid = client.getUserId();
|
auto userid = client.getUserId();
|
||||||
if(userid.empty()){
|
if (userid.empty()) {
|
||||||
puts("Userid is empty ! Wrong token ?");
|
puts("Userid is empty ! Wrong token ?");
|
||||||
goto wrong;
|
goto wrong;
|
||||||
}
|
}
|
||||||
std::cout << "Logged in as " << userid << std::endl;
|
std::cout << "Logged in as " << userid << std::endl;
|
||||||
// Load encryption here
|
// Load encryption here
|
||||||
load_account(client, *acc);
|
load_account(client, *acc);
|
||||||
client.setRoomInfoCallback([](std::string roomId, Matrix::RoomInfo info){
|
client.setRoomInfoCallback([](std::string roomId, Matrix::RoomInfo info) {
|
||||||
consoleSelect(bottom);
|
consoleSelect(bottom);
|
||||||
printf("Joined room %s named %s\n", roomId.c_str(), info.name.c_str());
|
printf("Joined room %s named %s\n", roomId.c_str(), info.name.c_str());
|
||||||
});
|
});
|
||||||
client.setEventCallback([](std::string roomId, json_t* event){
|
client.setEventCallback([](std::string roomId, json_t* event) {
|
||||||
consoleSelect(bottom);
|
consoleSelect(bottom);
|
||||||
printf("Event in room %s\n", roomId.c_str());
|
printf("Event in room %s\n", roomId.c_str());
|
||||||
printf("%s\n", json_dumps(event, JSON_ENSURE_ASCII));
|
printf("%s\n", json_dumps(event, JSON_ENSURE_ASCII));
|
||||||
});
|
});
|
||||||
client.startSyncLoop();
|
client.startSyncLoop();
|
||||||
}
|
}
|
||||||
wrong:
|
wrong:;
|
||||||
;
|
|
||||||
}
|
}
|
||||||
client.stopSyncLoop();
|
client.stopSyncLoop();
|
||||||
// DO NOT LOGOUT, KEEP ENCRYPTION KEYS
|
// DO NOT LOGOUT, KEEP ENCRYPTION KEYS
|
||||||
|
|
Loading…
Reference in a new issue