summaryrefslogtreecommitdiff
path: root/src/core/chatManager.cpp
blob: e8e4d5f4bc8a622f3a5f50cbe7ceb45141664471 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "chatManager.hpp"
#include "../debug/log.hpp"
#include "../io/IOManager.hpp"
#include "../io/inputManager.hpp"
#include "sessionManager.hpp"

CChatManager::CChatManager() {
	log(LOG, "ChatManager: initialized");
}

CChatManager::~CChatManager() {
	log(SYS, "ChatManager: bye");
}

void CChatManager::newMessage(const SMessage &msg) {
	log(LOG, "{}: {}", msg.username, msg.msg);
	const auto session = msg.sender ? msg.sender->lock() : g_pSessionManager->getByName(msg.username);
	if (session && session->isMuted()) {
		session->write("You are muted");
		return;
	}
	g_pSessionManager->broadcast(msg);
}

std::string CChatManager::fmtBroadcastMessage(const SMessage &msg) {
	return NFormatter::fmt(NONE, "{}: {}", msg.username, msg.msg);
}

std::vector<CChatManager::SMessage> CChatManager::getChat() const {
	std::vector<SMessage> chat;

	for (const auto &[msg, _, _] : g_pIOManager->getIO())
		if (msg)
			chat.push_back(*msg);
	return chat;
}