add login
This commit is contained in:
parent
0dd2cc867a
commit
2fab919532
2 changed files with 44 additions and 7 deletions
|
@ -22,12 +22,15 @@ public:
|
||||||
std::string userIdCache = "";
|
std::string userIdCache = "";
|
||||||
int requestId = 0;
|
int requestId = 0;
|
||||||
bool stopSyncing = false;
|
bool stopSyncing = false;
|
||||||
|
bool isSyncing = false;
|
||||||
Thread syncThread;
|
Thread syncThread;
|
||||||
void processSync(json_t* sync);
|
void processSync(json_t* sync);
|
||||||
json_t* doSync(std::string token);
|
json_t* doSync(std::string token);
|
||||||
json_t* doRequest(const char* method, std::string path, json_t* body = NULL);
|
json_t* doRequest(const char* method, std::string path, json_t* body = NULL);
|
||||||
public:
|
public:
|
||||||
Client(std::string homeserverUrl, std::string matrixToken, Store* clientStore = NULL);
|
Client(std::string homeserverUrl, std::string matrixToken = "", Store* clientStore = NULL);
|
||||||
|
std::string getToken();
|
||||||
|
bool login(std::string username, std::string password);
|
||||||
std::string getUserId();
|
std::string getUserId();
|
||||||
std::string resolveRoom(std::string alias);
|
std::string resolveRoom(std::string alias);
|
||||||
std::string sendEmote(std::string roomId, std::string text);
|
std::string sendEmote(std::string roomId, std::string text);
|
||||||
|
|
|
@ -28,6 +28,36 @@ Client::Client(std::string homeserverUrl, std::string matrixToken, Store* client
|
||||||
store = clientStore;
|
store = clientStore;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string Client::getToken() {
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Client::login(std::string username, std::string password) {
|
||||||
|
json_t* request = json_object();
|
||||||
|
json_object_set_new(request, "type", json_string("m.login.password"));
|
||||||
|
json_t* identifier = json_object();
|
||||||
|
json_object_set_new(identifier, "type", json_string("m.id.user"));
|
||||||
|
json_object_set_new(identifier, "user", json_string(username.c_str()));
|
||||||
|
json_object_set_new(request, "identifier", identifier);
|
||||||
|
json_object_set_new(request, "password", json_string(password.c_str()));
|
||||||
|
json_object_set_new(request, "initial_device_display_name", json_string("Nintendo 3DS"));
|
||||||
|
|
||||||
|
json_t* ret = doRequest("POST", "/_matrix/client/r0/login", request);
|
||||||
|
json_decref(request);
|
||||||
|
|
||||||
|
if (!ret) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
json_t* accessToken = json_object_get(ret, "access_token");
|
||||||
|
if (!accessToken) {
|
||||||
|
json_decref(ret);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
token = json_string_value(accessToken);
|
||||||
|
json_decref(ret);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
std::string Client::getUserId() {
|
std::string Client::getUserId() {
|
||||||
if (userIdCache != "") {
|
if (userIdCache != "") {
|
||||||
return userIdCache;
|
return userIdCache;
|
||||||
|
@ -160,9 +190,9 @@ void startSyncLoopWithoutClass(void* arg) {
|
||||||
|
|
||||||
void Client::startSyncLoop() {
|
void Client::startSyncLoop() {
|
||||||
stopSyncLoop(); // first we stop an already running sync loop
|
stopSyncLoop(); // first we stop an already running sync loop
|
||||||
|
isSyncing = true;
|
||||||
stopSyncing = false;
|
stopSyncing = false;
|
||||||
s32 prio = 0;
|
s32 prio = 0;
|
||||||
// startSyncLoopWithoutClass(this);
|
|
||||||
printf("%lld\n", (u64)this);
|
printf("%lld\n", (u64)this);
|
||||||
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
|
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
|
||||||
syncThread = threadCreate(startSyncLoopWithoutClass, this, 8*1024, prio-1, -2, true);
|
syncThread = threadCreate(startSyncLoopWithoutClass, this, 8*1024, prio-1, -2, true);
|
||||||
|
@ -170,8 +200,11 @@ void Client::startSyncLoop() {
|
||||||
|
|
||||||
void Client::stopSyncLoop() {
|
void Client::stopSyncLoop() {
|
||||||
stopSyncing = true;
|
stopSyncing = true;
|
||||||
//threadJoin(syncThread, U64_MAX);
|
if (isSyncing) {
|
||||||
//threadFree(syncThread);
|
threadJoin(syncThread, U64_MAX);
|
||||||
|
threadFree(syncThread);
|
||||||
|
}
|
||||||
|
isSyncing = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::processSync(json_t* sync) {
|
void Client::processSync(json_t* sync) {
|
||||||
|
@ -286,7 +319,9 @@ json_t* Client::doRequest(const char* method, std::string path, json_t* body) {
|
||||||
}
|
}
|
||||||
std::string readBuffer;
|
std::string readBuffer;
|
||||||
struct curl_slist* headers = NULL;
|
struct curl_slist* headers = NULL;
|
||||||
headers = curl_slist_append(headers, ("Authorization: Bearer " + token).c_str());
|
if (token != "") {
|
||||||
|
headers = curl_slist_append(headers, ("Authorization: Bearer " + token).c_str());
|
||||||
|
}
|
||||||
if (body) {
|
if (body) {
|
||||||
headers = curl_slist_append(headers, "Content-Type: application/json");
|
headers = curl_slist_append(headers, "Content-Type: application/json");
|
||||||
const char* bodyStr = json_dumps(body, JSON_ENSURE_ASCII | JSON_ESCAPE_SLASH);
|
const char* bodyStr = json_dumps(body, JSON_ENSURE_ASCII | JSON_ESCAPE_SLASH);
|
||||||
|
@ -315,8 +350,7 @@ json_t* Client::doRequest(const char* method, std::string path, json_t* body) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// printf(readBuffer.c_str());
|
printf("%s\n", readBuffer.c_str());
|
||||||
// printf("\n");
|
|
||||||
json_error_t error;
|
json_error_t error;
|
||||||
json_t* content = json_loads(readBuffer.c_str(), 0, &error);
|
json_t* content = json_loads(readBuffer.c_str(), 0, &error);
|
||||||
if (!content) {
|
if (!content) {
|
||||||
|
|
Loading…
Reference in a new issue