summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-03-23 12:56:38 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-04-03 18:31:41 +0800
commit50de5f9b80f1859b6eaa3b68c10a3f1cfb994737 (patch)
treebd125b0af19af04776e3740f707a0012555d460f
parentb02e484013ede3ed8d43ad19c177af2afb0bbfc8 (diff)
feat: deaf
necessary for being deaf during registeration
-rw-r--r--src/session.cpp29
-rw-r--r--src/session.hpp7
-rw-r--r--src/sessionManager.cpp4
3 files changed, 33 insertions, 7 deletions
diff --git a/src/session.cpp b/src/session.cpp
index 526e041..5bd41d9 100644
--- a/src/session.cpp
+++ b/src/session.cpp
@@ -119,8 +119,11 @@ std::unique_ptr<CSession::SRecvData> CSession::read() {
return recvData;
}
-std::unique_ptr<CSession::SRecvData> CSession::read(const std::string &msg) {
+std::unique_ptr<CSession::SRecvData> CSession::read(const std::string &msg, bool bypassDeaf) {
+ const auto deaf = isDeaf();
+ if (bypassDeaf && deaf) setDeaf(false);
write(NONEWLINE, "{}", msg, false);
+ if (bypassDeaf && deaf) setDeaf(true);
m_isReading = true;
return read();
}
@@ -140,26 +143,38 @@ void CSession::run() {
}
bool CSession::isMuted() const {
- return m_isMuted;
+ return m_bMuted;
}
void CSession::setMuted(bool mute) {
- m_isMuted = mute;
+ m_bMuted = mute;
+}
+
+bool CSession::isDeaf() const {
+ return m_bDeaf;
+}
+
+void CSession::setDeaf(bool deaf) {
+ m_bDeaf = deaf;
}
bool CSession::registerSession() {
- auto recvData = read("Name: ");
+ auto recvData = read("Name: ", true);
if (!recvData->good)
return false;
recvData->sanitize();
if (recvData->isEmpty()) {
+ setDeaf(false);
write("Name cannot be empty");
+ setDeaf(true);
return registerSession();
}
if (g_pSessionManager->nameExists(recvData->data)) {
+ setDeaf(false);
write("Name already exists");
+ setDeaf(true);
return registerSession();
}
@@ -167,6 +182,7 @@ bool CSession::registerSession() {
log(LOG, "Client registered as: {}", m_name);
+ setDeaf(false);
return true;
}
@@ -181,6 +197,8 @@ bool CSession::isValid() {
template <typename... Args>
bool CSession::write(std::format_string<Args...> fmt, Args &&...args) {
+ if (isDeaf())
+ return false;
std::string msg = NFormatter::fmt(NONE, fmt, std::forward<Args>(args)...);
if (m_isReading)
msg.insert(0, "\n");
@@ -197,6 +215,9 @@ bool CSession::write(std::format_string<Args...> fmt, Args &&...args) {
template <typename... Args>
bool CSession::write(eFormatType type, std::format_string<Args...> fmt, Args &&...args) {
+ if (isDeaf())
+ return false;
+
std::string msg = NFormatter::fmt(type, fmt, std::forward<Args>(args)...);
if (m_isReading)
msg.insert(0, "\n");
diff --git a/src/session.hpp b/src/session.hpp
index 070a8d4..8aee414 100644
--- a/src/session.hpp
+++ b/src/session.hpp
@@ -27,7 +27,7 @@ class CSession {
void setSelf(std::pair<std::jthread, std::shared_ptr<CSession>> *self);
std::unique_ptr<SRecvData> read();
- std::unique_ptr<SRecvData> read(const std::string &msg);
+ std::unique_ptr<SRecvData> read(const std::string &msg, bool bypassDeaf = false);
bool write(const std::string &msg);
template <typename... Args>
bool write(std::format_string<Args...> fmt, Args &&...args);
@@ -37,6 +37,8 @@ class CSession {
bool isMuted() const;
void setMuted(bool mute);
+ bool isDeaf() const;
+ void setDeaf(bool deaf);
void run();
@@ -60,7 +62,8 @@ class CSession {
bool m_isAdmin = false;
bool m_isReading = false;
- bool m_isMuted = false;
+ bool m_bMuted = false;
+ bool m_bDeaf = true; // init as true as we don't want anything during registerSession
void recvLoop();
bool registerSession();
diff --git a/src/sessionManager.cpp b/src/sessionManager.cpp
index 3d60db2..d1cf953 100644
--- a/src/sessionManager.cpp
+++ b/src/sessionManager.cpp
@@ -91,8 +91,10 @@ void CSessionManager::kick(std::pair<std::jthread, std::shared_ptr<CSession>> *s
auto it = std::ranges::find_if(m_vSessions, [session](const auto &s) { return s.second.get() == session->second.get(); });
if (it != m_vSessions.end()) {
- if (!reason.empty())
+ if (!reason.empty()) {
+ it->second->setDeaf(false);
it->second->write(reason);
+ }
const auto native_handle = it->first.native_handle();
if (it->first.joinable())
it->first.detach(); // .detach the thread since it's removing itself