Device refcouting

This commit is contained in:
timoreo 2023-12-10 17:59:13 +01:00
parent 4463d9b96b
commit 021274fbf6
Signed by: timoreo
GPG key ID: 121A72C3512BA288

View file

@ -73,7 +73,7 @@ bool Client::login(std::string username, std::string password, std::string devic
if(!device_id.empty()) { if(!device_id.empty()) {
json_object_set_new(request, "device_id", json_string(device_id.c_str())); json_object_set_new(request, "device_id", json_string(device_id.c_str()));
} }
json_t* ret = doRequest("POST", "/_matrix/client/r0/login", request); json_t* ret = doRequest("POST", "/_matrix/client/v3/login", request);
json_decref(request); json_decref(request);
printf_top("Result : %s\n" ,json_dumps(ret, JSON_ENSURE_ASCII | JSON_ESCAPE_SLASH)); printf_top("Result : %s\n" ,json_dumps(ret, JSON_ENSURE_ASCII | JSON_ESCAPE_SLASH));
const char* tokenCStr = json_object_get_string_value(ret, "access_token"); const char* tokenCStr = json_object_get_string_value(ret, "access_token");
@ -87,7 +87,7 @@ bool Client::login(std::string username, std::string password, std::string devic
} }
void Client::logout() { void Client::logout() {
json_t* ret = doRequest("POST", "/_matrix/client/r0/logout"); json_t* ret = doRequest("POST", "/_matrix/client/v3/logout");
if (ret) { if (ret) {
json_decref(ret); json_decref(ret);
} }
@ -97,7 +97,7 @@ std::string Client::getUserId() {
if (!userIdCache.empty()) { if (!userIdCache.empty()) {
return userIdCache; return userIdCache;
} }
json_t* ret = doRequest("GET", "/_matrix/client/r0/account/whoami"); json_t* ret = doRequest("GET", "/_matrix/client/v3/account/whoami");
const char* userIdCStr = json_object_get_string_value(ret, "user_id"); const char* userIdCStr = json_object_get_string_value(ret, "user_id");
if (!userIdCStr) { if (!userIdCStr) {
if (ret) json_decref(ret); if (ret) json_decref(ret);
@ -115,7 +115,7 @@ std::string Client::getDeviceId() {
if(!deviceIdCache.empty()){ if(!deviceIdCache.empty()){
return deviceIdCache; return deviceIdCache;
} }
json_t* ret = doRequest("GET", "/_matrix/client/r0/account/whoami"); json_t* ret = doRequest("GET", "/_matrix/client/v3/account/whoami");
const char* userIdCStr = json_object_get_string_value(ret, "user_id"); const char* userIdCStr = json_object_get_string_value(ret, "user_id");
if (!userIdCStr) { if (!userIdCStr) {
if (ret) json_decref(ret); if (ret) json_decref(ret);
@ -134,7 +134,7 @@ std::string Client::resolveRoom(std::string alias) {
if (alias[0] == '!') { if (alias[0] == '!') {
return alias; // this is already a room ID, nothing to do return alias; // this is already a room ID, nothing to do
} }
json_t* ret = doRequest("GET", "/_matrix/client/r0/directory/room/" + urlencode(alias)); json_t* ret = doRequest("GET", "/_matrix/client/v3/directory/room/" + urlencode(alias));
const char* roomIdCStr = json_object_get_string_value(ret, "room_id"); const char* roomIdCStr = json_object_get_string_value(ret, "room_id");
if (!roomIdCStr) { if (!roomIdCStr) {
if (ret) json_decref(ret); if (ret) json_decref(ret);
@ -148,7 +148,7 @@ std::string Client::resolveRoom(std::string alias) {
std::vector<std::string> Client::getJoinedRooms() { std::vector<std::string> Client::getJoinedRooms() {
std::vector<std::string> rooms; std::vector<std::string> rooms;
json_t* ret = doRequest("GET", "/_matrix/client/r0/joined_rooms"); json_t* ret = doRequest("GET", "/_matrix/client/v3/joined_rooms");
json_t* roomsArr = json_object_get(ret, "joined_rooms"); json_t* roomsArr = json_object_get(ret, "joined_rooms");
if (!roomsArr) { if (!roomsArr) {
json_decref(ret); json_decref(ret);
@ -183,7 +183,7 @@ ExtraRoomInfo Client::getExtraRoomInfo(std::string roomId) {
info.canonicalAlias = getCanonicalAlias(roomId); info.canonicalAlias = getCanonicalAlias(roomId);
// next fetch the members // next fetch the members
std::string path = "/_matrix/client/r0/rooms/" + urlencode(roomId) + "/joined_members"; std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomId) + "/joined_members";
json_t* ret = doRequest("GET", path); json_t* ret = doRequest("GET", path);
if (!ret) { if (!ret) {
return info; return info;
@ -233,7 +233,7 @@ MemberInfo Client::getMemberInfo(std::string userId, std::string roomId) {
} }
if (displayname == "") { if (displayname == "") {
// then attempt the account // then attempt the account
std::string path = "/_matrix/client/r0/profile/" + urlencode(userId); std::string path = "/_matrix/client/v3/profile/" + urlencode(userId);
json_t* ret = doRequest("GET", path); json_t* ret = doRequest("GET", path);
if (ret) { if (ret) {
char* valCStr; char* valCStr;
@ -305,7 +305,7 @@ std::string Client::getCanonicalAlias(std::string roomId) {
void Client::sendReadReceipt(std::string roomId, std::string eventId) { void Client::sendReadReceipt(std::string roomId, std::string eventId) {
roomId = resolveRoom(roomId); roomId = resolveRoom(roomId);
std::string path = "/_matrix/client/r0/rooms/" + urlencode(roomId) + "/receipt/m.read/" + urlencode(eventId); std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomId) + "/receipt/m.read/" + urlencode(eventId);
json_t* ret = doRequest("POST", path); json_t* ret = doRequest("POST", path);
if (ret) { if (ret) {
json_decref(ret); json_decref(ret);
@ -313,9 +313,15 @@ void Client::sendReadReceipt(std::string roomId, std::string eventId) {
} }
void Client::uploadKeys(json_t* body) { void Client::uploadKeys(json_t* body) {
std::string path = "/_matrix/client/v3/keys/upload/"; std::string path = "/_matrix/client/v3/keys/upload";
/*char* jj = json_dumps(body, 0);
printf_top("%s\n", jj);
free(jj);*/
json_t* ret = doRequest("POST", path, body); json_t* ret = doRequest("POST", path, body);
if (ret) { if (ret) {
/*char* j = json_dumps(ret, 0);
printf_top("%s\n",j);
free(j);*/
json_decref(ret); json_decref(ret);
} }
} }
@ -323,7 +329,7 @@ void Client::uploadKeys(json_t* body) {
void Client::setTyping(std::string roomId, bool typing, u32 timeout) { void Client::setTyping(std::string roomId, bool typing, u32 timeout) {
roomId = resolveRoom(roomId); roomId = resolveRoom(roomId);
std::string userId = getUserId(); std::string userId = getUserId();
std::string path = "/_matrix/client/r0/rooms/" + urlencode(roomId) + "/typing/" + urlencode(userId); std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomId) + "/typing/" + urlencode(userId);
json_t* request = json_object(); json_t* request = json_object();
json_object_set_new(request, "typing", json_boolean(typing)); json_object_set_new(request, "typing", json_boolean(typing));
json_object_set_new(request, "timeout", json_integer(timeout)); json_object_set_new(request, "timeout", json_integer(timeout));
@ -366,7 +372,7 @@ std::string Client::sendMessage(std::string roomId, json_t* content) {
std::string Client::sendEvent(std::string roomId, std::string eventType, json_t* content) { std::string Client::sendEvent(std::string roomId, std::string eventType, json_t* content) {
roomId = resolveRoom(roomId); roomId = resolveRoom(roomId);
std::string txid = std::to_string(time(NULL)) + "_REQ_" + std::to_string(requestId); std::string txid = std::to_string(time(NULL)) + "_REQ_" + std::to_string(requestId);
std::string path = "/_matrix/client/r0/rooms/" + urlencode(roomId) + "/send/" + urlencode(eventType) + "/" + urlencode(txid); std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomId) + "/send/" + urlencode(eventType) + "/" + urlencode(txid);
json_t* ret = doRequest("PUT", path, content); json_t* ret = doRequest("PUT", path, content);
const char* eventIdCStr = json_object_get_string_value(ret, "event_id"); const char* eventIdCStr = json_object_get_string_value(ret, "event_id");
if (!eventIdCStr) { if (!eventIdCStr) {
@ -380,13 +386,13 @@ std::string Client::sendEvent(std::string roomId, std::string eventType, json_t*
json_t* Client::getStateEvent(std::string roomId, std::string type, std::string stateKey) { json_t* Client::getStateEvent(std::string roomId, std::string type, std::string stateKey) {
roomId = resolveRoom(roomId); roomId = resolveRoom(roomId);
std::string path = "/_matrix/client/r0/rooms/" + urlencode(roomId) + "/state/" + urlencode(type) + "/" + urlencode(stateKey); std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomId) + "/state/" + urlencode(type) + "/" + urlencode(stateKey);
return doRequest("GET", path); return doRequest("GET", path);
} }
std::string Client::sendStateEvent(std::string roomId, std::string type, std::string stateKey, json_t* content) { std::string Client::sendStateEvent(std::string roomId, std::string type, std::string stateKey, json_t* content) {
roomId = resolveRoom(roomId); roomId = resolveRoom(roomId);
std::string path = "/_matrix/client/r0/rooms/" + urlencode(roomId) + "/state/" + urlencode(type) + "/" + urlencode(stateKey); std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomId) + "/state/" + urlencode(type) + "/" + urlencode(stateKey);
json_t* ret = doRequest("PUT", path, content); json_t* ret = doRequest("PUT", path, content);
const char* eventIdCStr = json_object_get_string_value(ret, "event_id"); const char* eventIdCStr = json_object_get_string_value(ret, "event_id");
if (!eventIdCStr) { if (!eventIdCStr) {
@ -405,7 +411,7 @@ std::string Client::redactEvent(std::string roomId, std::string eventId, std::st
if (reason != "") { if (reason != "") {
json_object_set_new(content, "reason", json_string(reason.c_str())); json_object_set_new(content, "reason", json_string(reason.c_str()));
} }
std::string path = "/_matrix/client/r0/rooms/" + urlencode(roomId) + "/redact/" + urlencode(eventId) + "/" + txid; std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomId) + "/redact/" + urlencode(eventId) + "/" + txid;
json_t* ret = doRequest("PUT", path, content); json_t* ret = doRequest("PUT", path, content);
json_decref(content); json_decref(content);
const char* eventIdCStr = json_object_get_string_value(ret, "event_id"); const char* eventIdCStr = json_object_get_string_value(ret, "event_id");
@ -688,7 +694,7 @@ void Client::registerFilter() {
return; return;
} }
std::string userId = getUserId(); std::string userId = getUserId();
json_t* ret = doRequest("POST", "/_matrix/client/r0/user/" + urlencode(userId) + "/filter", filter); json_t* ret = doRequest("POST", "/_matrix/client/v3/user/" + urlencode(userId) + "/filter", filter);
json_decref(filter); json_decref(filter);
const char* filterIdCStr = json_object_get_string_value(ret, "filter_id"); const char* filterIdCStr = json_object_get_string_value(ret, "filter_id");
if (!filterIdCStr) { if (!filterIdCStr) {
@ -742,7 +748,7 @@ json_t* Client::doSync(std::string token, std::string filter, u32 timeout, CURLc
if (token != "") { if (token != "") {
query += "&since=" + token; query += "&since=" + token;
} }
return doRequest("GET", "/_matrix/client/r0/sync" + query, NULL, timeout, res); return doRequest("GET", "/_matrix/client/v3/sync" + query, NULL, timeout, res);
} }
size_t DoRequestWriteCallback(char *contents, size_t size, size_t nmemb, void *userp) { size_t DoRequestWriteCallback(char *contents, size_t size, size_t nmemb, void *userp) {
@ -787,7 +793,7 @@ void curl_multi_loop(void* p) {
} }
json_t* Client::doRequestCurl(const char* method, std::string url, json_t* body, u32 timeout, CURLcode* retRes) { json_t* Client::doRequestCurl(const char* method, std::string url, json_t* body, u32 timeout, CURLcode* retRes) {
printf_top("Opening Request %d with CURL\n%s\n", requestId, url.c_str()); // printf_top("Opening Request %d with CURL\n%s\n", requestId, url.c_str());
if (!SOC_buffer) { if (!SOC_buffer) {
SOC_buffer = (u32*)memalign(0x1000, POST_BUFFERSIZE); SOC_buffer = (u32*)memalign(0x1000, POST_BUFFERSIZE);
@ -854,7 +860,7 @@ json_t* Client::doRequestCurl(const char* method, std::string url, json_t* body,
} }
// printf_top("++++\n%s\n", readBuffer.c_str()); // printf_top("++++\n%s\n", readBuffer.c_str());
printf_top("Body size: %d\n", readBuffer.length()); // printf_top("Body size: %d\n", readBuffer.length());
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) {