#pragma once #include #include #include #include #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 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 &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 writeQueue; std::deque readQueue; threads::Mutex flushThreadLock; threads::Thread* flushThread = nullptr; bool final_disconnected = false; std::string lineBuffer; std::chrono::time_point connectedTimestamp; uint16_t eventMask[QueryEventGroup::QEVENTGROUP_MAX]; std::recursive_mutex lock_packet_handle; std::recursive_mutex lock_query_tick; std::shared_ptr query_account; protected: command_result handleCommand(Command &command) override; public: //Silent events bool notifyClientNeededPermissions() override; bool notifyChannelSubscribed(const std::deque> &deque) override; bool notifyChannelUnsubscribed(const std::deque> &deque) override; bool notifyServerUpdated(std::shared_ptr ptr) override; bool notifyClientPoke(std::shared_ptr invoker, std::string msg) override; bool notifyClientUpdated(const std::shared_ptr &ptr, const std::deque &deque, bool lock_channel_tree) override; bool notifyPluginCmd(std::string name, std::string msg,std::shared_ptr) override; bool notifyClientChatComposing(const std::shared_ptr &ptr) override; bool notifyClientChatClosed(const std::shared_ptr &ptr) override; bool notifyTextMessage(ChatMessageMode mode, const std::shared_ptr &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 &invoker, const std::shared_ptr &client, const std::shared_ptr &group) override; bool notifyServerGroupClientRemove(std::shared_ptr invoker, std::shared_ptr client, std::shared_ptr group) override; bool notifyClientChannelGroupChanged(const std::shared_ptr &invoker, const std::shared_ptr &client, const std::shared_ptr &ptr, const std::shared_ptr &shared_ptr, const std::shared_ptr &group, bool lock_channel_tree) override; bool notifyChannelMoved(const std::shared_ptr &channel, ChannelId order, const std::shared_ptr &invoker) override; bool notifyChannelCreate(const std::shared_ptr &channel, ChannelId orderId, const std::shared_ptr &invoker) override; bool notifyChannelDescriptionChanged(std::shared_ptr channel) override; bool notifyChannelPasswordChanged(std::shared_ptr ) override; bool notifyChannelEdited(const std::shared_ptr &ptr, const std::vector &vector, const std::shared_ptr &sharedPtr, bool b) override; bool notifyChannelDeleted(const std::deque &deque, const std::shared_ptr &ptr) override; bool notifyMusicQueueAdd(const std::shared_ptr &bot, const std::shared_ptr &entry, int index, const std::shared_ptr &invoker) override; bool notifyMusicQueueRemove(const std::shared_ptr &bot, const std::deque> &entry, const std::shared_ptr &invoker) override; bool notifyMusicQueueOrderChange(const std::shared_ptr &bot, const std::shared_ptr &entry, int order, const std::shared_ptr &invoker) override; bool notifyMusicPlayerSongChange(const std::shared_ptr &bot, const std::shared_ptr &newEntry) override; bool notifyClientEnterView(const std::shared_ptr &client, const std::shared_ptr &invoker, const std::string &string, const std::shared_ptr &to, ViewReasonId reasonId, const std::shared_ptr &from, bool) override; bool notifyClientEnterView(const std::deque> &deque, const ViewReasonSystemT &t) override; bool notifyClientMoved(const std::shared_ptr &client, const std::shared_ptr &target_channel, ViewReasonId reason, std::string msg, std::shared_ptr invoker, bool lock_channel_tree) override; bool notifyClientLeftView(const std::shared_ptr &client, const std::shared_ptr &target_channel, ViewReasonId reasonId, const std::string &reasonMessage, std::shared_ptr invoker, bool lock_channel_tree) override; bool notifyClientLeftView(const std::deque> &deque, const std::string &string, bool b, const ViewReasonServerLeftT &t) override; bool notifyClientLeftViewKicked(const std::shared_ptr &client, const std::shared_ptr &target_channel, const std::string &message, std::shared_ptr invoker, bool lock_channel_tree) override; bool notifyClientLeftViewBanned(const std::shared_ptr &client, const std::string &message, std::shared_ptr 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&); }; }