clang-format : format sdk

This commit is contained in:
timoreo 2023-12-11 21:21:45 +01:00
parent edd1a90dec
commit 95deba1139
Signed by: timoreo
GPG key ID: 121A72C3512BA288
7 changed files with 765 additions and 755 deletions

View file

@ -1,5 +1 @@
//
// Created by timoreo on 11/12/23.
//
#include "devicestore.h" #include "devicestore.h"

View file

@ -1,7 +1,3 @@
//
// Created by timoreo on 11/12/23.
//
#ifndef MATRIX_3DS_CLIENT_DEVICESTORE_H #ifndef MATRIX_3DS_CLIENT_DEVICESTORE_H
#define MATRIX_3DS_CLIENT_DEVICESTORE_H #define MATRIX_3DS_CLIENT_DEVICESTORE_H

View file

@ -27,7 +27,7 @@
#if DEBUG #if DEBUG
#define D #define D
#else #else
#define D for(;0;) #define D for (; 0;)
#endif #endif
#if DEBUG #if DEBUG
@ -35,16 +35,22 @@ PrintConsole* topScreenDebugConsole = nullptr;
#endif #endif
#if DEBUG #if DEBUG
#define printf_top(f_, ...) do {consoleSelect(topScreenDebugConsole);printf((f_), ##__VA_ARGS__);} while(0) #define printf_top(f_, ...) \
do { \
consoleSelect(topScreenDebugConsole); \
printf((f_), ##__VA_ARGS__); \
} while (0)
#else #else
#define printf_top(f_, ...) do {} while(0) #define printf_top(f_, ...) \
do { \
} while (0)
#endif #endif
namespace Matrix { namespace Matrix {
#define POST_BUFFERSIZE 0x100000 #define POST_BUFFERSIZE 0x100000
static u32 *SOC_buffer = nullptr; static u32* SOC_buffer = nullptr;
bool HTTPC_inited = false; bool HTTPC_inited = false;
bool haveHttpcSupport = false; bool haveHttpcSupport = false;
@ -76,15 +82,16 @@ bool Client::login(const std::string& username, const std::string& password, con
json_object_set_new(request, "identifier", identifier); json_object_set_new(request, "identifier", identifier);
json_object_set_new(request, "password", json_string(password.c_str())); 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_object_set_new(request, "initial_device_display_name", json_string("Nintendo 3DS"));
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/v3/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");
if (!tokenCStr) { if (!tokenCStr) {
if (ret) json_decref(ret); if (ret)
json_decref(ret);
return false; return false;
} }
token = tokenCStr; token = tokenCStr;
@ -106,7 +113,8 @@ std::string Client::getUserId() {
json_t* ret = doRequest("GET", "/_matrix/client/v3/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);
return ""; return "";
} }
const char* deviceIdCStr = json_object_get_string_value(ret, "device_id"); const char* deviceIdCStr = json_object_get_string_value(ret, "device_id");
@ -118,13 +126,14 @@ std::string Client::getUserId() {
return userIdCache; return userIdCache;
} }
std::string Client::getDeviceId() { std::string Client::getDeviceId() {
if(!deviceIdCache.empty()){ if (!deviceIdCache.empty()) {
return deviceIdCache; return deviceIdCache;
} }
json_t* ret = doRequest("GET", "/_matrix/client/v3/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);
return ""; return "";
} }
const char* deviceIdCStr = json_object_get_string_value(ret, "device_id"); const char* deviceIdCStr = json_object_get_string_value(ret, "device_id");
@ -143,7 +152,8 @@ std::string Client::resolveRoom(std::string alias) {
json_t* ret = doRequest("GET", "/_matrix/client/v3/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);
return ""; return "";
} }
std::string roomIdStr = roomIdCStr; std::string roomIdStr = roomIdCStr;
@ -265,7 +275,8 @@ std::string Client::getRoomName(const std::string& roomId) {
json_t* ret = getStateEvent(roomId, "m.room.name", ""); json_t* ret = getStateEvent(roomId, "m.room.name", "");
const char* nameCStr = json_object_get_string_value(ret, "name"); const char* nameCStr = json_object_get_string_value(ret, "name");
if (!nameCStr) { if (!nameCStr) {
if (ret) json_decref(ret); if (ret)
json_decref(ret);
return ""; return "";
} }
std::string nameStr = nameCStr; std::string nameStr = nameCStr;
@ -277,7 +288,8 @@ std::string Client::getRoomTopic(const std::string& roomId) {
json_t* ret = getStateEvent(roomId, "m.room.topic", ""); json_t* ret = getStateEvent(roomId, "m.room.topic", "");
const char* topicCStr = json_object_get_string_value(ret, "topic"); const char* topicCStr = json_object_get_string_value(ret, "topic");
if (!topicCStr) { if (!topicCStr) {
if (ret) json_decref(ret); if (ret)
json_decref(ret);
return ""; return "";
} }
std::string topicStr = topicCStr; std::string topicStr = topicCStr;
@ -289,7 +301,8 @@ std::string Client::getRoomAvatar(const std::string& roomId) {
json_t* ret = getStateEvent(roomId, "m.room.avatar", ""); json_t* ret = getStateEvent(roomId, "m.room.avatar", "");
const char* urlCStr = json_object_get_string_value(ret, "url"); const char* urlCStr = json_object_get_string_value(ret, "url");
if (!urlCStr) { if (!urlCStr) {
if (ret) json_decref(ret); if (ret)
json_decref(ret);
return ""; return "";
} }
std::string urlStr = urlCStr; std::string urlStr = urlCStr;
@ -301,7 +314,8 @@ std::string Client::getCanonicalAlias(const std::string& roomId) {
json_t* ret = getStateEvent(roomId, "m.room.canonical_alias", ""); json_t* ret = getStateEvent(roomId, "m.room.canonical_alias", "");
const char* aliasCStr = json_object_get_string_value(ret, "alias"); const char* aliasCStr = json_object_get_string_value(ret, "alias");
if (!aliasCStr) { if (!aliasCStr) {
if (ret) json_decref(ret); if (ret)
json_decref(ret);
return ""; return "";
} }
std::string aliasStr = aliasCStr; std::string aliasStr = aliasCStr;
@ -369,15 +383,15 @@ std::string Client::sendMessage(const std::string& roomId, json_t* content) {
return sendEvent(roomId, "m.room.message", content); return sendEvent(roomId, "m.room.message", content);
} }
void Client::sendEventToDevice(const std::string& eventType, json_t* content) {
void Client::sendEventToDevice(const std::string& eventType, json_t* content){
std::string txid = std::to_string(time(nullptr)) + "_REQ_" + std::to_string(requestId++); std::string txid = std::to_string(time(nullptr)) + "_REQ_" + std::to_string(requestId++);
std::string path = "/_matrix/client/v3/sendToDevice/" + urlencode(eventType) + "/" + urlencode(txid); std::string path = "/_matrix/client/v3/sendToDevice/" + urlencode(eventType) + "/" + urlencode(txid);
json_t* messages = json_object(); json_t* messages = json_object();
json_object_set(messages, "messages", content); json_object_set(messages, "messages", content);
json_t* ret = doRequest("PUT", path, messages, 5, nullptr, false); json_t* ret = doRequest("PUT", path, messages, 5, nullptr, false);
json_decref(messages); json_decref(messages);
if (ret) json_decref(ret); if (ret)
json_decref(ret);
} }
std::string Client::sendEvent(const std::string& roomId, const std::string& eventType, json_t* content) { std::string Client::sendEvent(const std::string& roomId, const std::string& eventType, json_t* content) {
@ -387,7 +401,8 @@ std::string Client::sendEvent(const std::string& roomId, const std::string& even
json_t* ret = doRequest("PUT", path, content, 5, nullptr, false); json_t* ret = doRequest("PUT", path, content, 5, nullptr, false);
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) {
if (ret) json_decref(ret); if (ret)
json_decref(ret);
return ""; return "";
} }
std::string eventIdStr = eventIdCStr; std::string eventIdStr = eventIdCStr;
@ -407,7 +422,8 @@ std::string Client::sendStateEvent(const std::string& roomId, const std::string&
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) {
if (ret) json_decref(ret); if (ret)
json_decref(ret);
return ""; return "";
} }
std::string eventIdStr = eventIdCStr; std::string eventIdStr = eventIdCStr;
@ -427,7 +443,8 @@ std::string Client::redactEvent(const std::string& roomId, const std::string& ev
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");
if (!eventIdCStr) { if (!eventIdCStr) {
if (ret) json_decref(ret); if (ret)
json_decref(ret);
return ""; return "";
} }
std::string eventIdStr = eventIdCStr; std::string eventIdStr = eventIdCStr;
@ -445,7 +462,7 @@ void Client::startSyncLoop() {
stopSyncing = false; stopSyncing = false;
s32 prio = 0; s32 prio = 0;
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);
} }
void Client::stopSyncLoop() { void Client::stopSyncLoop() {
@ -479,7 +496,7 @@ void Client::setRoomLimitedCallback(roomLimitedCallback cb) {
void Client::processSync(json_t* sync) { void Client::processSync(json_t* sync) {
json_t* to_device = json_object_get(sync, "to_device"); json_t* to_device = json_object_get(sync, "to_device");
if(to_device){ if (to_device) {
processToDevice(to_device); processToDevice(to_device);
} }
json_t* rooms = json_object_get(sync, "rooms"); json_t* rooms = json_object_get(sync, "rooms");
@ -655,7 +672,7 @@ void Client::processSync(json_t* sync) {
} }
void Client::registerFilter() { void Client::registerFilter() {
static const char *json = static const char* json =
"{" "{"
" \"account_data\": {" " \"account_data\": {"
" \"types\": [" " \"types\": ["
@ -713,7 +730,8 @@ void Client::registerFilter() {
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) {
if (ret) json_decref(ret); if (ret)
json_decref(ret);
return; return;
} }
std::string filterIdStr = filterIdCStr; std::string filterIdStr = filterIdCStr;
@ -748,7 +766,7 @@ void Client::syncLoop() {
json_decref(ret); json_decref(ret);
} else { } else {
if (res == CURLE_OPERATION_TIMEDOUT) { if (res == CURLE_OPERATION_TIMEDOUT) {
timeout += 10*60; timeout += 10 * 60;
printf_top("Timeout reached, increasing it to %lu\n", timeout); printf_top("Timeout reached, increasing it to %lu\n", timeout);
} }
} }
@ -757,7 +775,7 @@ void Client::syncLoop() {
} }
json_t* Client::doSync(const std::string& syncToken, const 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()); // 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); std::string query = "?full_state=false&timeout=" + std::to_string(SYNC_TIMEOUT) + "&filter=" + urlencode(filter);
if (!syncToken.empty()) { if (!syncToken.empty()) {
@ -766,8 +784,8 @@ json_t* Client::doSync(const std::string& syncToken, const std::string& filter,
return doRequest("GET", "/_matrix/client/v3/sync" + query, nullptr, 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) { size_t DoRequestWriteCallback(char* contents, size_t size, size_t nmemb, void* userp) {
// printf_top("----\n%s\n", ((std::string*)userp)->c_str()); // printf_top("----\n%s\n", ((std::string*)userp)->c_str());
((std::string*)userp)->append((char*)contents, size * nmemb); ((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb; return size * nmemb;
} }
@ -777,7 +795,7 @@ bool doingHttpcRequest = false;
json_t* Client::doRequest(const char* method, const 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; std::string url = hsUrl + path;
if(needsRequest) { if (needsRequest) {
requestId++; requestId++;
} }
return doRequestCurl(method, url, body, timeout, retRes); return doRequestCurl(method, url, body, timeout, retRes);
@ -789,12 +807,12 @@ Thread curl_multi_loop_thread;
[[noreturn]] void curl_multi_loop(void* p) { [[noreturn]] void curl_multi_loop(void* p) {
int openHandles = 0; int openHandles = 0;
while(true) { while (true) {
CURLMcode mc = curl_multi_perform(curl_multi_handle, &openHandles); CURLMcode mc = curl_multi_perform(curl_multi_handle, &openHandles);
if (mc != CURLM_OK) { if (mc != CURLM_OK) {
printf_top("curl multi fail: %u\n", mc); printf_top("curl multi fail: %u\n", mc);
} }
// curl_multi_wait(curl_multi_handle, NULL, 0, 1000, &openHandles); // curl_multi_wait(curl_multi_handle, NULL, 0, 1000, &openHandles);
CURLMsg* msg; CURLMsg* msg;
int msgsLeft; int msgsLeft;
while ((msg = curl_multi_info_read(curl_multi_handle, &msgsLeft))) { while ((msg = curl_multi_info_read(curl_multi_handle, &msgsLeft))) {
@ -822,7 +840,7 @@ json_t* Client::doRequestCurl(const char* method, const std::string& url, json_t
curl_multi_handle = curl_multi_init(); curl_multi_handle = curl_multi_init();
s32 prio = 0; s32 prio = 0;
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE); svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
curl_multi_loop_thread = threadCreate(curl_multi_loop, nullptr, 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(); CURL* curl = curl_easy_init();
@ -865,17 +883,19 @@ json_t* Client::doRequestCurl(const char* method, const std::string& url, json_t
curl_handles_done.erase(curl); curl_handles_done.erase(curl);
curl_multi_remove_handle(curl_multi_handle, curl); curl_multi_remove_handle(curl_multi_handle, curl);
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
// curl_easy_setopt(curl, CURLOPT_STDERR, stdout); // curl_easy_setopt(curl, CURLOPT_STDERR, stdout);
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
if (bodyStr) free(bodyStr); if (bodyStr)
if (retRes) *retRes = res; free(bodyStr);
if (retRes)
*retRes = res;
if (res != CURLE_OK) { if (res != CURLE_OK) {
printf_top("curl res not ok %d\n", res); printf_top("curl res not ok %d\n", res);
return nullptr; return nullptr;
} }
// 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);
@ -886,7 +906,7 @@ json_t* Client::doRequestCurl(const char* method, const std::string& url, json_t
return content; return content;
} }
void Client::print_json(json_t *json){ void Client::print_json(json_t* json) {
char* data = json_dumps(json, 0); char* data = json_dumps(json, 0);
puts(data); puts(data);
free(data); free(data);
@ -928,7 +948,8 @@ void Client::sign_json(json_t* json) {
json_t* signitem = json_object(); json_t* signitem = json_object();
std::cout << std::string(reinterpret_cast<const char*>(signature.get()), ptrlen) << std::endl; std::cout << std::string(reinterpret_cast<const char*>(signature.get()), ptrlen) << std::endl;
json_object_set_new(signitem, ("ed25519:" + getDeviceId()).c_str(), json_stringn(reinterpret_cast<const char*>(signature.get()), ptrlen)); json_object_set_new(signitem, ("ed25519:" + getDeviceId()).c_str(),
json_stringn(reinterpret_cast<const char*>(signature.get()), ptrlen));
json_t* signobj = json_object(); json_t* signobj = json_object();
json_object_set_new(signobj, getUserId().c_str(), signitem); json_object_set_new(signobj, getUserId().c_str(), signitem);
json_object_set_new(json, "signatures", signobj); json_object_set_new(json, "signatures", signobj);
@ -1082,14 +1103,11 @@ void Client::start_encryption() {
olm::unpickle(data.get(), data.get() + fsize, acc); olm::unpickle(data.get(), data.get() + fsize, acc);
} }
} }
void Client::getDevices(){ void Client::getDevices() {
// To get the devices, we need to do a /keys/query req // To get the devices, we need to do a /keys/query req
// We then need to keep up to date using /sync to_device (see processToDevice) // We then need to keep up to date using /sync to_device (see processToDevice)
// This will need to all be cached // This will need to all be cached
} }
void Client::processToDevice(json_t* data) { void Client::processToDevice(json_t* data) {}
}
}; // namespace Matrix }; // namespace Matrix

View file

@ -1,16 +1,17 @@
#ifndef _memorystore_h_ #ifndef _memorystore_h_
#define _memorystore_h_ #define _memorystore_h_
#include "../include/matrixclient.h"
#include <string> #include <string>
#include "../include/matrixclient.h"
namespace Matrix { namespace Matrix {
class MemoryStore : public Store { class MemoryStore : public Store {
private: private:
std::string syncToken = ""; std::string syncToken = "";
std::string filterId = ""; std::string filterId = "";
public:
public:
void setSyncToken(std::string token); void setSyncToken(std::string token);
std::string getSyncToken(); std::string getSyncToken();
void setFilterId(std::string fid); void setFilterId(std::string fid);

View file

@ -1,25 +1,24 @@
#include "util.h" #include "util.h"
#include <string>
#include <sstream>
#include <3ds.h> #include <3ds.h>
#include <jansson.h> #include <jansson.h>
#include <sstream>
#include <string>
// from http://www.zedwood.com/article/cpp-urlencode-function // from http://www.zedwood.com/article/cpp-urlencode-function
std::string urlencode(std::string s) { std::string urlencode(std::string s) {
static const char lookup[]= "0123456789abcdef"; static const char lookup[] = "0123456789abcdef";
std::stringstream e; std::stringstream e;
for(int i = 0, ix = s.length(); i < ix; i++) { for (int i = 0, ix = s.length(); i < ix; i++) {
const char& c = s[i]; const char& c = s[i];
if ( (48 <= c && c <= 57) ||//0-9 if ((48 <= c && c <= 57) || // 0-9
(65 <= c && c <= 90) ||//abc...xyz (65 <= c && c <= 90) || // abc...xyz
(97 <= c && c <= 122) || //ABC...XYZ (97 <= c && c <= 122) || // ABC...XYZ
(c=='-' || c=='_' || c=='.' || c=='~') (c == '-' || c == '_' || c == '.' || c == '~')) {
) {
e << c; e << c;
} else { } else {
e << '%'; e << '%';
e << lookup[ (c&0xF0)>>4 ]; e << lookup[(c & 0xF0) >> 4];
e << lookup[ (c&0x0F) ]; e << lookup[(c & 0x0F)];
} }
} }
return e.str(); return e.str();

View file

@ -1,9 +1,9 @@
#ifndef _UTIL_H_ #ifndef _UTIL_H_
#define _UTIL_H_ #define _UTIL_H_
#include <string>
#include <3ds.h> #include <3ds.h>
#include <jansson.h> #include <jansson.h>
#include <string>
std::string urlencode(std::string str); std::string urlencode(std::string str);