summaryrefslogtreecommitdiff
path: root/src/chatManager.cpp
blob: e5a63980f636d11ae9d33ea3690f2bd1a9fd8434 (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 "IOManager.hpp"
#include "inputManager.hpp"
#include "log.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->get() : 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;
}