-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessageHandling.cpp
83 lines (78 loc) · 2.58 KB
/
MessageHandling.cpp
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
#include "Server.hpp"
void Server::SendMessage(int fd, std::string message)
{
send(fd, message.c_str(), message.size(), 0);
}
void Server::cmdsManegement(int fd, std::vector<std::string> &cmd, const char *buffer)
{
std::string message;
if (cmd[0] == "PASS" || cmd[0] == "pass" || cmd[0] == "USER" || cmd[0] == "user" || cmd[0] == "NICK" || cmd[0] == "nick")
ERR_ALREADYREGISTERED(fd);
else if (cmd[0] == "join" || cmd[0] == "JOIN")
Join(cmd, &clients[fd]);
else if (cmd[0] == "mode" || cmd[0] == "MODE")
Mode(cmd, &clients[fd]);
else if (cmd[0] == "kick" || cmd[0] == "KICK")
Kick(cmd, &clients[fd]);
else if (cmd[0] == "topic" || cmd[0] == "TOPIC")
Topic(cmd, &clients[fd]);
else if (cmd[0] == "invite" || cmd[0] == "INVITE")
Invite(cmd, &clients[fd]);
else if (cmd[0] == "privmsg" || cmd[0] == "PRIVMSG")
Privmsg(cmd, buffer, &clients[fd]);
else if (cmd[0] == "/GORILLABOT" || cmd[0] == "/gorillabot")
PrintFacts(fd);
}
void Server::cmd_PONG(int fd) {
std::string message("PONG :HALAL_TINDER\r\n");
send(fd, message.c_str(), message.size(), 0);
}
void Server::ReceiveData(int fd)
{
char buffer[1000];
static std::string incomplete;
int bytes = recv(fd, buffer, sizeof(buffer) - 1, 0);
buffer[bytes] = '\0';
if (bytes <= 0)
{
if (bytes == -1)
throw std::string("read() failled reading data received from client");
else
RemoveClient(fd);
}
else
{
std::string message = incomplete + buffer;
if (message[message.size() - 1] != '\n')
{
size_t pos = message.rfind('\n');
if (pos != std::string::npos)
{
incomplete = message.substr(pos + 1);
message = message.substr(0, pos);
}
else
{
incomplete = message;
return;
}
}
else
{
std::cout << message << std::endl;
incomplete.clear();
std::vector<std::string> cmd;
std::istringstream iss(message.c_str());
std::string input;
while (iss >> input)
cmd.push_back(input);
if (std::string(message.c_str()).find("PING") != std::string::npos)
cmd_PONG(fd);
if (clients[fd].get_registered() < 3)
RegisterClient(cmd, fd);
else
cmdsManegement(fd, cmd, message.c_str());
// std::cout << buffer << std::endl;
}
}
}