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

@ -35,9 +35,15 @@ 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 {
@ -84,7 +90,8 @@ bool Client::login(const std::string& username, const std::string& password, con
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");
@ -124,7 +132,8 @@ std::string Client::getDeviceId() {
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,7 +383,6 @@ 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);
@ -377,7 +390,8 @@ void Client::sendEventToDevice(const std::string& eventType, json_t* content){
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;
@ -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;
@ -868,8 +886,10 @@ json_t* Client::doRequestCurl(const char* method, const std::string& url, json_t
// 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;
@ -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);
@ -1086,10 +1107,7 @@ 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,8 +1,8 @@
#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 {
@ -10,6 +10,7 @@ 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();

View file

@ -1,8 +1,8 @@
#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) {
@ -13,8 +13,7 @@ std::string urlencode(std::string s) {
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 << '%';

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);