matrix-3ds-sdk/include/matrixclient.h

141 lines
4.5 KiB
C
Raw Normal View History

2019-10-16 18:39:57 +02:00
#ifndef _matrixclient_h_
#define _matrixclient_h_
#include <3ds.h>
2023-12-10 20:24:28 +01:00
#include <curl/curl.h>
2019-10-16 18:39:57 +02:00
#include <jansson.h>
2019-10-24 14:24:02 +02:00
#include <map>
2023-12-10 20:24:28 +01:00
#include <memory>
#include <string>
#include <vector>
2023-12-11 16:06:09 +01:00
#include <atomic>
2023-12-10 20:24:28 +01:00
#include "olm/account.hh"
2019-10-16 18:39:57 +02:00
2019-10-17 10:45:55 +02:00
namespace Matrix {
class Store {
public:
virtual void setSyncToken(std::string token) = 0;
virtual std::string getSyncToken() = 0;
2019-10-19 18:12:10 +02:00
virtual void setFilterId(std::string filterId) = 0;
virtual std::string getFilterId() = 0;
2019-10-17 10:45:55 +02:00
};
2019-10-18 13:10:54 +02:00
struct RoomInfo {
std::string name;
std::string topic;
2019-10-18 13:26:52 +02:00
std::string avatarUrl;
};
2019-10-24 12:48:48 +02:00
struct MemberInfo {
2019-10-18 13:26:52 +02:00
std::string displayname;
std::string avatarUrl;
2019-10-18 13:10:54 +02:00
};
2019-10-24 14:24:02 +02:00
struct ExtraRoomInfo {
std::string canonicalAlias;
std::map<std::string, MemberInfo> members;
};
2019-10-20 23:49:08 +02:00
typedef void (*eventCallback)(std::string roomId, json_t* event);
typedef void (*roomInfoCallback)(std::string roomId, RoomInfo info);
2019-10-26 12:54:09 +02:00
typedef void (*roomLimitedCallback)(std::string roomId, std::string prevBatch);
2019-10-20 23:49:08 +02:00
2019-10-17 10:45:55 +02:00
class Client {
2019-10-16 18:39:57 +02:00
private:
std::string hsUrl;
std::string token;
2019-10-17 10:45:55 +02:00
Store* store;
2023-12-09 14:39:57 +01:00
std::string userIdCache;
std::string deviceIdCache;
2023-12-11 16:06:09 +01:00
std::atomic_uint32_t requestId = 0;
2019-10-17 12:54:49 +02:00
bool stopSyncing = false;
2019-10-17 14:00:02 +02:00
bool isSyncing = false;
2019-10-17 12:54:49 +02:00
Thread syncThread;
2023-12-10 20:24:28 +01:00
olm::Account acc;
struct {
eventCallback event = NULL;
eventCallback leaveRoom = NULL;
eventCallback inviteRoom = NULL;
2019-10-20 23:49:08 +02:00
roomInfoCallback roomInfo = NULL;
2019-10-26 12:54:09 +02:00
roomLimitedCallback roomLimited = NULL;
} callbacks;
2019-10-17 10:45:55 +02:00
void processSync(json_t* sync);
2019-10-19 18:12:10 +02:00
void registerFilter();
2019-10-28 19:47:52 +01:00
json_t* doSync(std::string token, std::string filter, u32 timeout, CURLcode* res);
2023-12-11 16:06:09 +01:00
json_t* doRequest(const char* method, std::string path, json_t* body = nullptr, u32 timeout = 5, CURLcode* retRes = nullptr, bool doRequest = true);
2019-10-28 19:47:52 +01:00
json_t* doRequestCurl(const char* method, std::string url, json_t* body, u32 timeout, CURLcode* retRes);
2023-12-10 20:24:28 +01:00
static void print_json(json_t *json);
2019-10-16 18:39:57 +02:00
public:
2019-10-17 14:00:02 +02:00
Client(std::string homeserverUrl, std::string matrixToken = "", Store* clientStore = NULL);
2023-12-09 14:39:57 +01:00
std::string getToken() const;
bool login(std::string username, std::string password, std::string device_id="");
2019-10-19 18:12:10 +02:00
void logout();
std::string getUserId();
2023-12-09 14:39:57 +01:00
std::string getDeviceId();
2019-10-17 13:22:39 +02:00
std::string resolveRoom(std::string alias);
2019-10-18 13:10:54 +02:00
std::vector<std::string> getJoinedRooms();
RoomInfo getRoomInfo(std::string roomId);
2019-10-24 14:24:02 +02:00
ExtraRoomInfo getExtraRoomInfo(std::string roomId);
2019-10-24 12:48:48 +02:00
MemberInfo getMemberInfo(std::string userId, std::string roomId = "");
2019-10-18 13:10:54 +02:00
std::string getRoomName(std::string roomId);
std::string getRoomTopic(std::string roomId);
std::string getRoomAvatar(std::string roomId);
2019-10-24 14:24:02 +02:00
std::string getCanonicalAlias(std::string roomId);
2019-10-26 14:24:09 +02:00
void sendReadReceipt(std::string roomId, std::string eventId);
2019-10-26 18:25:02 +02:00
void setTyping(std::string roomId, bool typing, u32 timeout = 30000);
2019-10-17 13:22:39 +02:00
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);
2019-10-16 22:57:55 +02:00
std::string sendMessage(std::string roomId, json_t* content);
std::string sendEvent(std::string roomId, std::string eventType, json_t* content);
2023-12-11 16:06:09 +01:00
// Format Json in shape
// {
// "@alice:example.com": {
// "TLLBEANAAG": {
// "example_content_key": "value"
// }
// }
// }
// OR, for all devices
// {
// "@alice:example.com": {
// "*": {
// "example_content_key": "value"
// }
// }
// }
void sendEventToDevice(const std::string& eventType, json_t* content);
2019-10-18 13:10:54 +02:00
json_t* getStateEvent(std::string roomId, std::string type, std::string stateKey);
2019-10-17 13:22:39 +02:00
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 = "");
2019-10-17 12:54:49 +02:00
void startSyncLoop();
void stopSyncLoop();
void setEventCallback(eventCallback cb);
void setLeaveRoomCallback(eventCallback cb);
void setInviteRoomCallback(eventCallback cb);
2019-10-20 23:49:08 +02:00
void setRoomInfoCallback(roomInfoCallback cb);
2019-10-26 12:54:09 +02:00
void setRoomLimitedCallback(roomLimitedCallback cb);
void syncLoop();
2023-12-09 15:36:46 +01:00
void uploadKeys(json_t* body);
2023-12-10 20:24:28 +01:00
// Crypto
void generate_otk(size_t otkcount);
void generate_otk();
void generate_device_key();
void generate_fallback_key();
void sign_json(json_t* json);
json_t* get_device_keys();
json_t* get_fallback_keys();
json_t* get_unpublished_otk();
void save_keys();
void upload_keys();
void start_encryption();
2019-10-16 18:39:57 +02:00
};
2019-10-17 10:45:55 +02:00
}; // namespace Matrix
2019-10-16 18:39:57 +02:00
#endif // _matrixclient_h_