-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpFunctions.cpp
More file actions
100 lines (91 loc) · 2.77 KB
/
helpFunctions.cpp
File metadata and controls
100 lines (91 loc) · 2.77 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "Channel.hpp"
#include "Server.hpp"
#include "Irc.hpp"
std::vector<Client>::iterator Server::getClient(int clientFd)
{
std::vector<Client>::iterator clientIt = _clients.begin();
while (clientIt != _clients.end())
{
if (clientFd == clientIt->getFd())
return clientIt;
++clientIt;
}
throw std::runtime_error("Client not found");
}
void Server::makeUserList(std::string channel_name)//isto esta ok
{
std::map<std::string, Channel>::iterator channelIt = _channels.begin();
while (channelIt != _channels.end())
{
if (getLower(channelIt->first) == getLower(channel_name))
break;
++channelIt;
}
if (channelIt != _channels.end())
{
Channel channel = channelIt->second;
std::map<int, std::vector<std::string> > clients = channel.getClients();
std::string nameList = ":localhost 353 " + getClient(_clientFd)->getNickname() + " @ " + channel.getName() + " :";
for (std::map<int, std::vector<std::string> >::iterator It = clients.begin(); It != clients.end(); It++)
{
if (channel.isOperator(It->first))
nameList += "@";
if (It->first == 424242)
nameList+= "Comrade ";
else
nameList+= getClient(It->first)->getNickname() + " ";
}
nameList += "\r\n";
broadcastMessageToChannel(nameList, channel.getName());
std::string endOfNames = ":localhost 366 " + getClient(_clientFd)->getNickname() + " " + channel.getName() + " :End of /NAMES list.\r\n";
send(_clientFd, endOfNames.c_str(), endOfNames.size(), 0);
}
else
throw std::runtime_error("No server was found!");
}
void Server::broadcastMessageToClients(const std::string& message)
{
for (std::vector<Client>::iterator it = _clients.begin(); it != _clients.end(); ++it)
{
int clientFd = it->getFd();
if (clientFd != 424242)
send(clientFd, message.c_str(), message.size(), 0);
}
}
void Server::broadcastMessageToChannel(const std::string& message, std::string channel)
{
std::map<std::string, Channel>::iterator channelIt = _channels.begin();
while (channelIt != _channels.end())
{
if (getLower(channelIt->first) == getLower(channel))
break;
++channelIt;
}
if (channelIt != _channels.end())
{
Channel channel = channelIt->second;
std::map<int, std::vector<std::string> > clients = channel.getClients();
for (std::map<int, std::vector<std::string> >::iterator It = clients.begin(); It != clients.end(); It++)
{
int clientFd = It->first;
if (clientFd != 424242)
send(clientFd, message.c_str(), message.size(), 0);
}
}
else
throw std::runtime_error("No server found in the BroadCast Message");
}
std::string getFullMsg(std::string &msg, std::istringstream &lineStream, int size)
{
if (msg[0] == ':' && msg.size() > 1)
{
msg.erase(0,1);
std::string nextWord;
while (lineStream >> nextWord)
{
msg += " ";
msg += nextWord;
}
}
return msg.substr(0, size);
}