2020-04-14 11:49:07 +02:00

184 lines
9.8 KiB
C++

#pragma once
#include <src/client/ConnectedClient.h>
#include <poll.h>
#include <protocol/buffers.h>
#include <pipes/ssl.h>
#include "misc/queue.h"
namespace ts::server {
class QueryServer;
class QueryAccount;
class QueryClient : public ConnectedClient {
friend class QueryServer;
enum ConnectionType {
PLAIN,
SSL_ENCRIPTED,
UNKNOWN
};
public:
QueryClient(QueryServer*, int sockfd);
~QueryClient() override;
void writeMessage(const std::string&);
void sendCommand(const ts::Command &command, bool low = false) override;
void sendCommand(const ts::command_builder &command, bool low) override;
bool disconnect(const std::string &reason) override;
bool close_connection(const std::chrono::system_clock::time_point& timeout = std::chrono::system_clock::time_point()) override;
void disconnectFinal();
bool eventActive(QueryEventGroup, QueryEventSpecifier);
void toggleEvent(QueryEventGroup, QueryEventSpecifier, bool);
void resetEventMask();
bool ignoresFlood() override;
void disconnect_from_virtual_server();
inline std::shared_ptr<QueryAccount> getQueryAccount() { return this->query_account; }
protected:
void preInitialize();
void postInitialize();
void tick(const std::chrono::system_clock::time_point &time) override;
void queryTick();
protected:
void initializeSSL();
bool handleMessage(const pipes::buffer_view&);
bool tickIOMessageProgress();
void handleMessageRead(int, short, void*);
void handleMessageWrite(int, short, void*);
void writeRawMessage(const std::string&);
void applySelfLock(const std::shared_ptr<QueryClient> &cl);
private:
QueryServer* handle;
ConnectionType connectionType = ConnectionType::UNKNOWN;
bool whitelisted = false;
int clientFd = -1;
::event* readEvent = nullptr;
::event* writeEvent = nullptr;
threads::Mutex closeLock;
pipes::SSL ssl_handler;
std::mutex buffer_lock;
std::deque<std::string> writeQueue;
std::deque<std::string> readQueue;
threads::Mutex flushThreadLock;
threads::Thread* flushThread = nullptr;
bool final_disconnected = false;
std::string lineBuffer;
std::chrono::time_point<std::chrono::system_clock> connectedTimestamp;
uint16_t eventMask[QueryEventGroup::QEVENTGROUP_MAX];
std::recursive_mutex lock_packet_handle;
std::recursive_mutex lock_query_tick;
std::shared_ptr<QueryAccount> query_account;
protected:
command_result handleCommand(Command &command) override;
public:
//Silent events
bool notifyClientNeededPermissions() override;
bool notifyChannelSubscribed(const std::deque<std::shared_ptr<BasicChannel>> &deque) override;
bool notifyChannelUnsubscribed(const std::deque<std::shared_ptr<BasicChannel>> &deque) override;
bool notifyServerUpdated(std::shared_ptr<ConnectedClient> ptr) override;
bool notifyClientPoke(std::shared_ptr<ConnectedClient> invoker, std::string msg) override;
bool notifyClientUpdated(const std::shared_ptr<ConnectedClient> &ptr, const std::deque<const property::PropertyDescription*> &deque, bool lock_channel_tree) override;
bool notifyPluginCmd(std::string name, std::string msg,std::shared_ptr<ConnectedClient>) override;
bool notifyClientChatComposing(const std::shared_ptr<ConnectedClient> &ptr) override;
bool notifyClientChatClosed(const std::shared_ptr<ConnectedClient> &ptr) override;
bool notifyTextMessage(ChatMessageMode mode, const std::shared_ptr<ConnectedClient> &sender, uint64_t targetId, ChannelId channel_id, const std::chrono::system_clock::time_point&, const std::string &textMessage) override;
bool notifyServerGroupClientAdd(const std::shared_ptr<ConnectedClient> &invoker, const std::shared_ptr<ConnectedClient> &client, const std::shared_ptr<Group> &group) override;
bool notifyServerGroupClientRemove(std::shared_ptr<ConnectedClient> invoker, std::shared_ptr<ConnectedClient> client, std::shared_ptr<Group> group) override;
bool notifyClientChannelGroupChanged(const std::shared_ptr<ConnectedClient> &invoker, const std::shared_ptr<ConnectedClient> &client, const std::shared_ptr<BasicChannel> &ptr, const std::shared_ptr<BasicChannel> &shared_ptr,
const std::shared_ptr<Group> &group, bool lock_channel_tree) override;
bool notifyChannelMoved(const std::shared_ptr<BasicChannel> &channel, ChannelId order, const std::shared_ptr<ConnectedClient> &invoker) override;
bool notifyChannelCreate(const std::shared_ptr<BasicChannel> &channel, ChannelId orderId,
const std::shared_ptr<ConnectedClient> &invoker) override;
bool notifyChannelDescriptionChanged(std::shared_ptr<BasicChannel> channel) override;
bool notifyChannelPasswordChanged(std::shared_ptr<BasicChannel> ) override;
bool notifyChannelEdited(const std::shared_ptr<BasicChannel> &ptr, const std::vector<property::ChannelProperties> &vector, const std::shared_ptr<ConnectedClient> &sharedPtr, bool b) override;
bool notifyChannelDeleted(const std::deque<ChannelId> &deque, const std::shared_ptr<ConnectedClient> &ptr) override;
bool notifyMusicQueueAdd(const std::shared_ptr<MusicClient> &bot, const std::shared_ptr<ts::music::SongInfo> &entry, int index, const std::shared_ptr<ConnectedClient> &invoker) override;
bool notifyMusicQueueRemove(const std::shared_ptr<MusicClient> &bot, const std::deque<std::shared_ptr<music::SongInfo>> &entry, const std::shared_ptr<ConnectedClient> &invoker) override;
bool notifyMusicQueueOrderChange(const std::shared_ptr<MusicClient> &bot, const std::shared_ptr<ts::music::SongInfo> &entry, int order, const std::shared_ptr<ConnectedClient> &invoker) override;
bool notifyMusicPlayerSongChange(const std::shared_ptr<MusicClient> &bot, const std::shared_ptr<music::SongInfo> &newEntry) override;
bool notifyClientEnterView(const std::shared_ptr<ConnectedClient> &client, const std::shared_ptr<ConnectedClient> &invoker, const std::string &string, const std::shared_ptr<BasicChannel> &to, ViewReasonId reasonId,
const std::shared_ptr<BasicChannel> &from, bool) override;
bool notifyClientEnterView(const std::deque<std::shared_ptr<ConnectedClient>> &deque, const ViewReasonSystemT &t) override;
bool notifyClientMoved(const std::shared_ptr<ConnectedClient> &client, const std::shared_ptr<BasicChannel> &target_channel, ViewReasonId reason, std::string msg, std::shared_ptr<ConnectedClient> invoker, bool lock_channel_tree) override;
bool notifyClientLeftView(const std::shared_ptr<ConnectedClient> &client, const std::shared_ptr<BasicChannel> &target_channel, ViewReasonId reasonId, const std::string &reasonMessage, std::shared_ptr<ConnectedClient> invoker,
bool lock_channel_tree) override;
bool notifyClientLeftView(const std::deque<std::shared_ptr<ConnectedClient>> &deque, const std::string &string, bool b, const ViewReasonServerLeftT &t) override;
bool notifyClientLeftViewKicked(const std::shared_ptr<ConnectedClient> &client, const std::shared_ptr<BasicChannel> &target_channel, const std::string &message, std::shared_ptr<ConnectedClient> invoker, bool lock_channel_tree) override;
bool notifyClientLeftViewBanned(const std::shared_ptr<ConnectedClient> &client, const std::string &message, std::shared_ptr<ConnectedClient> invoker, size_t length, bool lock_channel_tree) override;
private:
command_result handleCommandExit(Command&);
command_result handleCommandLogin(Command&);
command_result handleCommandLogout(Command&);
command_result handleCommandServerSelect(Command &);
command_result handleCommandServerInfo(Command&);
command_result handleCommandChannelList(Command&);
command_result handleCommandJoin(Command&);
command_result handleCommandLeft(Command&);
command_result handleCommandServerList(Command&);
command_result handleCommandServerCreate(Command&);
command_result handleCommandServerDelete(Command&);
command_result handleCommandServerStart(Command&);
command_result handleCommandServerStop(Command&);
command_result handleCommandInstanceInfo(Command&);
command_result handleCommandInstanceEdit(Command&);
command_result handleCommandBindingList(Command&);
command_result handleCommandHostInfo(Command&);
command_result handleCommandGlobalMessage(Command&);
command_result handleCommandServerIdGetByPort(Command&);
command_result handleCommandServerSnapshotDeploy(Command&);
command_result handleCommandServerSnapshotDeployNew(const command_parser&);
command_result handleCommandServerSnapshotCreate(Command&);
command_result handleCommandServerProcessStop(Command&);
command_result handleCommandServerNotifyRegister(Command&);
command_result handleCommandServerNotifyList(Command&);
command_result handleCommandServerNotifyUnregister(Command&);
command_result handleCommandSetCompressionMode(Command&);
};
}