diff --git a/include/matrixclient.h b/include/matrixclient.h index 84abeee..3cb968e 100644 --- a/include/matrixclient.h +++ b/include/matrixclient.h @@ -51,26 +51,27 @@ private: uint32_t requestId = 0; //TODO make atomic bool stopSyncing = false; bool isSyncing = false; - Thread syncThread; + Thread syncThread = nullptr; olm::Account acc; struct { - eventCallback event = NULL; - eventCallback leaveRoom = NULL; - eventCallback inviteRoom = NULL; - roomInfoCallback roomInfo = NULL; - roomLimitedCallback roomLimited = NULL; + eventCallback event = nullptr; + eventCallback leaveRoom = nullptr; + eventCallback inviteRoom = nullptr; + roomInfoCallback roomInfo = nullptr; + roomLimitedCallback roomLimited = nullptr; } callbacks; void processSync(json_t* sync); void registerFilter(); - json_t* doSync(std::string token, std::string filter, u32 timeout, CURLcode* res); - json_t* doRequest(const char* method, std::string path, json_t* body = nullptr, u32 timeout = 5, CURLcode* retRes = nullptr, bool doRequest = true); - json_t* doRequestCurl(const char* method, std::string url, json_t* body, u32 timeout, CURLcode* retRes); + json_t* doSync(const std::string& syncToken, const std::string& filter, u32 timeout, CURLcode* res); + json_t* doRequest(const char* method, + const std::string& path, json_t* body = nullptr, u32 timeout = 5, CURLcode* retRes = nullptr, bool needsRequest = true); + json_t* doRequestCurl(const char* method, const std::string& url, json_t* body, u32 timeout, CURLcode* retRes); static void print_json(json_t *json); public: Client(std::string homeserverUrl, std::string matrixToken = "", Store* clientStore = NULL); std::string getToken() const; - bool login(std::string username, std::string password, std::string device_id=""); + bool login(const std::string& username, const std::string& password, const std::string& device_id=""); void logout(); std::string getUserId(); std::string getDeviceId(); @@ -78,18 +79,18 @@ public: std::vector getJoinedRooms(); RoomInfo getRoomInfo(std::string roomId); ExtraRoomInfo getExtraRoomInfo(std::string roomId); - MemberInfo getMemberInfo(std::string userId, std::string roomId = ""); - std::string getRoomName(std::string roomId); - std::string getRoomTopic(std::string roomId); - std::string getRoomAvatar(std::string roomId); - std::string getCanonicalAlias(std::string roomId); - void sendReadReceipt(std::string roomId, std::string eventId); + MemberInfo getMemberInfo(const std::string& userId, const std::string& roomId = ""); + std::string getRoomName(const std::string& roomId); + std::string getRoomTopic(const std::string& roomId); + std::string getRoomAvatar(const std::string& roomId); + std::string getCanonicalAlias(const std::string& roomId); + void sendReadReceipt(const std::string& roomId, const std::string& eventId); void setTyping(std::string roomId, bool typing, u32 timeout = 30000); - std::string sendEmote(std::string roomId, std::string text); - std::string sendNotice(std::string roomId, std::string text); - std::string sendText(std::string roomId, std::string text); - std::string sendMessage(std::string roomId, json_t* content); - std::string sendEvent(std::string roomId, std::string eventType, json_t* content); + std::string sendEmote(const std::string& roomId, const std::string& text); + std::string sendNotice(const std::string& roomId, const std::string& text); + std::string sendText(const std::string& roomId, const std::string& text); + std::string sendMessage(const std::string& roomId, json_t* content); + std::string sendEvent(const std::string& roomId, const std::string& eventType, json_t* content); // Format Json in shape // { @@ -108,9 +109,9 @@ public: // } // } void sendEventToDevice(const std::string& eventType, json_t* content); - json_t* getStateEvent(std::string roomId, std::string type, std::string stateKey); - std::string sendStateEvent(std::string roomId, std::string type, std::string stateKey, json_t* content); - std::string redactEvent(std::string roomId, std::string eventId, std::string reason = ""); + json_t* getStateEvent(const std::string& roomId, const std::string& type, const std::string& stateKey); + std::string sendStateEvent(const std::string& roomId, const std::string& type, const std::string& stateKey, json_t* content); + std::string redactEvent(const std::string& roomId, const std::string& eventId, const std::string& reason = ""); void startSyncLoop(); void stopSyncLoop(); void setEventCallback(eventCallback cb); diff --git a/source/matrixclient.cpp b/source/matrixclient.cpp index 56e23cf..07da512 100644 --- a/source/matrixclient.cpp +++ b/source/matrixclient.cpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include #include @@ -12,6 +12,7 @@ #include #include #include +#include #include #include "memorystore.h" #include "olm/base64.hh" @@ -30,7 +31,7 @@ #endif #if DEBUG -PrintConsole* topScreenDebugConsole = NULL; +PrintConsole* topScreenDebugConsole = nullptr; #endif #if DEBUG @@ -43,13 +44,13 @@ namespace Matrix { #define POST_BUFFERSIZE 0x100000 -static u32 *SOC_buffer = NULL; +static u32 *SOC_buffer = nullptr; bool HTTPC_inited = false; bool haveHttpcSupport = false; Client::Client(std::string homeserverUrl, std::string matrixToken, Store* clientStore) { - hsUrl = homeserverUrl; - token = matrixToken; + hsUrl = std::move(homeserverUrl); + token = std::move(matrixToken); if (!clientStore) { clientStore = new MemoryStore; } @@ -66,7 +67,7 @@ std::string Client::getToken() const { return token; } -bool Client::login(std::string username, std::string password, std::string device_id) { +bool Client::login(const std::string& username, const std::string& password, const std::string& device_id) { json_t* request = json_object(); json_object_set_new(request, "type", json_string("m.login.password")); json_t* identifier = json_object(); @@ -164,7 +165,7 @@ std::vector Client::getJoinedRooms() { json_array_foreach(roomsArr, index, value) { const char* val = json_string_value(value); if (val) { - rooms.push_back(val); + rooms.emplace_back(val); } } json_decref(ret); @@ -217,11 +218,11 @@ ExtraRoomInfo Client::getExtraRoomInfo(std::string roomId) { return info; } -MemberInfo Client::getMemberInfo(std::string userId, std::string roomId) { - std::string displayname = ""; - std::string avatarUrl = ""; - if (roomId != "") { - // first try fetching fro the room +MemberInfo Client::getMemberInfo(const std::string& userId, const std::string& roomId) { + std::string displayname; + std::string avatarUrl; + if (!roomId.empty()) { + // first try fetching from the room json_t* ret = getStateEvent(roomId, "m.room.member", userId); if (ret) { char* valCStr; @@ -236,7 +237,7 @@ MemberInfo Client::getMemberInfo(std::string userId, std::string roomId) { json_decref(ret); } } - if (displayname == "") { + if (displayname.empty()) { // then attempt the account std::string path = "/_matrix/client/v3/profile/" + urlencode(userId); json_t* ret = doRequest("GET", path); @@ -260,7 +261,7 @@ MemberInfo Client::getMemberInfo(std::string userId, std::string roomId) { return info; } -std::string Client::getRoomName(std::string roomId) { +std::string Client::getRoomName(const std::string& roomId) { json_t* ret = getStateEvent(roomId, "m.room.name", ""); const char* nameCStr = json_object_get_string_value(ret, "name"); if (!nameCStr) { @@ -272,7 +273,7 @@ std::string Client::getRoomName(std::string roomId) { return nameStr; } -std::string Client::getRoomTopic(std::string roomId) { +std::string Client::getRoomTopic(const std::string& roomId) { json_t* ret = getStateEvent(roomId, "m.room.topic", ""); const char* topicCStr = json_object_get_string_value(ret, "topic"); if (!topicCStr) { @@ -284,7 +285,7 @@ std::string Client::getRoomTopic(std::string roomId) { return topicStr; } -std::string Client::getRoomAvatar(std::string roomId) { +std::string Client::getRoomAvatar(const std::string& roomId) { json_t* ret = getStateEvent(roomId, "m.room.avatar", ""); const char* urlCStr = json_object_get_string_value(ret, "url"); if (!urlCStr) { @@ -296,7 +297,7 @@ std::string Client::getRoomAvatar(std::string roomId) { return urlStr; } -std::string Client::getCanonicalAlias(std::string roomId) { +std::string Client::getCanonicalAlias(const std::string& roomId) { json_t* ret = getStateEvent(roomId, "m.room.canonical_alias", ""); const char* aliasCStr = json_object_get_string_value(ret, "alias"); if (!aliasCStr) { @@ -308,9 +309,9 @@ std::string Client::getCanonicalAlias(std::string roomId) { return aliasStr; } -void Client::sendReadReceipt(std::string roomId, std::string eventId) { - roomId = resolveRoom(roomId); - std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomId) + "/receipt/m.read/" + urlencode(eventId); +void Client::sendReadReceipt(const std::string& roomId, const std::string& eventId) { + std::string roomDecoded = resolveRoom(roomId); + std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomDecoded) + "/receipt/m.read/" + urlencode(eventId); json_t* ret = doRequest("POST", path); if (ret) { json_decref(ret); @@ -319,14 +320,8 @@ void Client::sendReadReceipt(std::string roomId, std::string eventId) { void Client::uploadKeys(json_t* body) { 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); if (ret) { - /*char* j = json_dumps(ret, 0); - printf_top("%s\n",j); - free(j);*/ json_decref(ret); } } @@ -343,7 +338,7 @@ void Client::setTyping(std::string roomId, bool typing, u32 timeout) { json_decref(ret); } -std::string Client::sendEmote(std::string roomId, std::string text) { +std::string Client::sendEmote(const std::string& roomId, const std::string& text) { json_t* request = json_object(); json_object_set_new(request, "msgtype", json_string("m.emote")); json_object_set_new(request, "body", json_string(text.c_str())); @@ -352,7 +347,7 @@ std::string Client::sendEmote(std::string roomId, std::string text) { return eventId; } -std::string Client::sendNotice(std::string roomId, std::string text) { +std::string Client::sendNotice(const std::string& roomId, const std::string& text) { json_t* request = json_object(); json_object_set_new(request, "msgtype", json_string("m.notice")); json_object_set_new(request, "body", json_string(text.c_str())); @@ -361,7 +356,7 @@ std::string Client::sendNotice(std::string roomId, std::string text) { return eventId; } -std::string Client::sendText(std::string roomId, std::string text) { +std::string Client::sendText(const std::string& roomId, const std::string& text) { json_t* request = json_object(); json_object_set_new(request, "msgtype", json_string("m.text")); json_object_set_new(request, "body", json_string(text.c_str())); @@ -370,7 +365,7 @@ std::string Client::sendText(std::string roomId, std::string text) { return eventId; } -std::string Client::sendMessage(std::string roomId, json_t* content) { +std::string Client::sendMessage(const std::string& roomId, json_t* content) { return sendEvent(roomId, "m.room.message", content); } @@ -385,10 +380,10 @@ void Client::sendEventToDevice(const std::string& eventType, json_t* content){ if (ret) json_decref(ret); } -std::string Client::sendEvent(std::string roomId, std::string eventType, json_t* content) { - roomId = resolveRoom(roomId); - std::string txid = std::to_string(time(NULL)) + "_REQ_" + std::to_string(requestId++); - std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomId) + "/send/" + urlencode(eventType) + "/" + urlencode(txid); +std::string Client::sendEvent(const std::string& roomId, const std::string& eventType, json_t* content) { + std::string resolvedId = resolveRoom(roomId); + std::string txid = std::to_string(time(nullptr)) + "_REQ_" + std::to_string(requestId++); + std::string path = "/_matrix/client/v3/rooms/" + urlencode(resolvedId) + "/send/" + urlencode(eventType) + "/" + urlencode(txid); json_t* ret = doRequest("PUT", path, content, 5, nullptr, false); const char* eventIdCStr = json_object_get_string_value(ret, "event_id"); if (!eventIdCStr) { @@ -400,15 +395,15 @@ std::string Client::sendEvent(std::string roomId, std::string eventType, json_t* return eventIdStr; } -json_t* Client::getStateEvent(std::string roomId, std::string type, std::string stateKey) { - roomId = resolveRoom(roomId); - std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomId) + "/state/" + urlencode(type) + "/" + urlencode(stateKey); +json_t* Client::getStateEvent(const std::string& roomId, const std::string& type, const std::string& stateKey) { + std::string resolvedId = resolveRoom(roomId); + std::string path = "/_matrix/client/v3/rooms/" + urlencode(resolvedId) + "/state/" + urlencode(type) + "/" + urlencode(stateKey); return doRequest("GET", path); } -std::string Client::sendStateEvent(std::string roomId, std::string type, std::string stateKey, json_t* content) { - roomId = resolveRoom(roomId); - std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomId) + "/state/" + urlencode(type) + "/" + urlencode(stateKey); +std::string Client::sendStateEvent(const std::string& roomId, const std::string& type, const std::string& stateKey, json_t* content) { + std::string resolvedId = resolveRoom(roomId); + std::string path = "/_matrix/client/v3/rooms/" + urlencode(resolvedId) + "/state/" + urlencode(type) + "/" + urlencode(stateKey); json_t* ret = doRequest("PUT", path, content); const char* eventIdCStr = json_object_get_string_value(ret, "event_id"); if (!eventIdCStr) { @@ -420,14 +415,14 @@ std::string Client::sendStateEvent(std::string roomId, std::string type, std::st return eventIdStr; } -std::string Client::redactEvent(std::string roomId, std::string eventId, std::string reason) { - roomId = resolveRoom(roomId); - std::string txid = std::to_string(time(NULL)) + "_REQ_" + std::to_string(requestId++); +std::string Client::redactEvent(const std::string& roomId, const std::string& eventId, const std::string& reason) { + std::string resolvedId = resolveRoom(roomId); + std::string txid = std::to_string(time(nullptr)) + "_REQ_" + std::to_string(requestId++); json_t* content = json_object(); if (!reason.empty()) { json_object_set_new(content, "reason", json_string(reason.c_str())); } - std::string path = "/_matrix/client/v3/rooms/" + urlencode(roomId) + "/redact/" + urlencode(eventId) + "/" + txid; + std::string path = "/_matrix/client/v3/rooms/" + urlencode(resolvedId) + "/redact/" + urlencode(eventId) + "/" + txid; json_t* ret = doRequest("PUT", path, content, 5, nullptr, false); json_decref(content); const char* eventIdCStr = json_object_get_string_value(ret, "event_id"); @@ -511,7 +506,7 @@ void Client::processSync(json_t* sync) { if (!events) { continue; } - json_t* leaveEvent = NULL; + json_t* leaveEvent = nullptr; json_array_foreach(events, index, event) { // check if the event type is m.room.member char* val; @@ -553,7 +548,7 @@ void Client::processSync(json_t* sync) { if (!events) { continue; } - json_t* inviteEvent = NULL; + json_t* inviteEvent = nullptr; json_array_foreach(events, index, event) { // check if the event type is m.room.member char* val; @@ -732,14 +727,14 @@ void Client::syncLoop() { if (stopSyncing) { return; } - std::string token = store->getSyncToken(); + std::string syncToken = store->getSyncToken(); std::string filterId = store->getFilterId(); - if (filterId == "") { + if (filterId.empty()) { registerFilter(); filterId = store->getFilterId(); } CURLcode res; - json_t* ret = doSync(token, filterId, timeout, &res); + json_t* ret = doSync(syncToken, filterId, timeout, &res); if (ret) { timeout = 60; // set the token for the next batch @@ -761,14 +756,14 @@ void Client::syncLoop() { } } -json_t* Client::doSync(std::string token, std::string filter, u32 timeout, CURLcode* res) { +json_t* Client::doSync(const std::string& syncToken, const std::string& filter, u32 timeout, CURLcode* res) { // printf_top("Doing sync with token %s\n", token.c_str()); std::string query = "?full_state=false&timeout=" + std::to_string(SYNC_TIMEOUT) + "&filter=" + urlencode(filter); - if (token != "") { - query += "&since=" + token; + if (!syncToken.empty()) { + query += "&since=" + syncToken; } - return doRequest("GET", "/_matrix/client/v3/sync" + query, NULL, timeout, res); + return doRequest("GET", "/_matrix/client/v3/sync" + query, nullptr, timeout, res); } size_t DoRequestWriteCallback(char *contents, size_t size, size_t nmemb, void *userp) { @@ -780,7 +775,7 @@ size_t DoRequestWriteCallback(char *contents, size_t size, size_t nmemb, void *u bool doingCurlRequest = false; bool doingHttpcRequest = false; -json_t* Client::doRequest(const char* method, std::string path, json_t* body, u32 timeout, CURLcode* retRes, bool needsRequest) { +json_t* Client::doRequest(const char* method, const std::string& path, json_t* body, u32 timeout, CURLcode* retRes, bool needsRequest) { std::string url = hsUrl + path; if(needsRequest) { requestId++; @@ -792,8 +787,7 @@ CURLM* curl_multi_handle; std::map curl_handles_done; Thread curl_multi_loop_thread; - -void curl_multi_loop(void* p) { +[[noreturn]] void curl_multi_loop(void* p) { int openHandles = 0; while(true) { CURLMcode mc = curl_multi_perform(curl_multi_handle, &openHandles); @@ -814,34 +808,34 @@ 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, const std::string& url, json_t* body, u32 timeout, CURLcode* retRes) { // printf_top("Opening Request %d with CURL\n%s\n", requestId, url.c_str()); if (!SOC_buffer) { SOC_buffer = (u32*)memalign(0x1000, POST_BUFFERSIZE); if (!SOC_buffer) { - return NULL; + return nullptr; } if (socInit(SOC_buffer, POST_BUFFERSIZE) != 0) { - return NULL; + return nullptr; } curl_multi_handle = curl_multi_init(); s32 prio = 0; svcGetThreadPriority(&prio, CUR_THREAD_HANDLE); - curl_multi_loop_thread = threadCreate(curl_multi_loop, NULL, 8*1024, prio-1, -2, true); + curl_multi_loop_thread = threadCreate(curl_multi_loop, nullptr, 8*1024, prio-1, -2, true); } CURL* curl = curl_easy_init(); if (!curl) { printf_top("curl init failed\n"); - return NULL; + return nullptr; } std::string readBuffer; - struct curl_slist* headers = NULL; - if (token != "") { + struct curl_slist* headers = nullptr; + if (!token.empty()) { headers = curl_slist_append(headers, ("Authorization: Bearer " + token).c_str()); } - char* bodyStr = NULL; + char* bodyStr = nullptr; if (body) { headers = curl_slist_append(headers, "Content-Type: application/json"); bodyStr = json_dumps(body, JSON_ENSURE_ASCII | JSON_ESCAPE_SLASH); @@ -878,7 +872,7 @@ json_t* Client::doRequestCurl(const char* method, std::string url, json_t* body, if (retRes) *retRes = res; if (res != CURLE_OK) { printf_top("curl res not ok %d\n", res); - return NULL; + return nullptr; } // printf_top("++++\n%s\n", readBuffer.c_str()); @@ -887,7 +881,7 @@ json_t* Client::doRequestCurl(const char* method, std::string url, json_t* body, json_t* content = json_loads(readBuffer.c_str(), 0, &error); if (!content) { printf_top("Failed to parse json\n"); - return NULL; + return nullptr; } return content; }