143 lines
4.9 KiB
C++
143 lines
4.9 KiB
C++
#ifndef _matrixclient_h_
|
|
#define _matrixclient_h_
|
|
|
|
#include <3ds.h>
|
|
#include <curl/curl.h>
|
|
#include <jansson.h>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <vector>
|
|
#include <atomic>
|
|
#include "olm/account.hh"
|
|
|
|
namespace Matrix {
|
|
|
|
class Store {
|
|
public:
|
|
virtual void setSyncToken(std::string token) = 0;
|
|
virtual std::string getSyncToken() = 0;
|
|
virtual void setFilterId(std::string filterId) = 0;
|
|
virtual std::string getFilterId() = 0;
|
|
};
|
|
|
|
struct RoomInfo {
|
|
std::string name;
|
|
std::string topic;
|
|
std::string avatarUrl;
|
|
};
|
|
|
|
struct MemberInfo {
|
|
std::string displayname;
|
|
std::string avatarUrl;
|
|
};
|
|
|
|
struct ExtraRoomInfo {
|
|
std::string canonicalAlias;
|
|
std::map<std::string, MemberInfo> members;
|
|
};
|
|
|
|
typedef void (*eventCallback)(std::string roomId, json_t* event);
|
|
typedef void (*roomInfoCallback)(std::string roomId, RoomInfo info);
|
|
typedef void (*roomLimitedCallback)(std::string roomId, std::string prevBatch);
|
|
|
|
class Client {
|
|
private:
|
|
std::string hsUrl;
|
|
std::string token;
|
|
Store* store;
|
|
std::string userIdCache;
|
|
std::string deviceIdCache;
|
|
uint32_t requestId = 0; //TODO make atomic
|
|
bool stopSyncing = false;
|
|
bool isSyncing = false;
|
|
Thread syncThread = nullptr;
|
|
olm::Account acc;
|
|
struct {
|
|
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(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(const std::string& username, const std::string& password, const std::string& device_id="");
|
|
void logout();
|
|
std::string getUserId();
|
|
std::string getDeviceId();
|
|
std::string resolveRoom(std::string alias);
|
|
std::vector<std::string> getJoinedRooms();
|
|
RoomInfo getRoomInfo(std::string roomId);
|
|
ExtraRoomInfo getExtraRoomInfo(std::string roomId);
|
|
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(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
|
|
// {
|
|
// "@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);
|
|
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);
|
|
void setLeaveRoomCallback(eventCallback cb);
|
|
void setInviteRoomCallback(eventCallback cb);
|
|
void setRoomInfoCallback(roomInfoCallback cb);
|
|
void setRoomLimitedCallback(roomLimitedCallback cb);
|
|
void syncLoop();
|
|
void uploadKeys(json_t* body);
|
|
|
|
// 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();
|
|
void getDevices();
|
|
void processToDevice(json_t* data);
|
|
};
|
|
|
|
}; // namespace Matrix
|
|
|
|
#endif // _matrixclient_h_
|