-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandleMessage.cpp
176 lines (159 loc) · 4.76 KB
/
HandleMessage.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "includes/HandleMessage.hpp"
#include "includes/Utils.hpp"
#include "includes/Pass.hpp"
#include "includes/Nick.hpp"
#include "includes/User.hpp"
#include "includes/Join.hpp"
#include "includes/Topic.hpp"
#include "includes/Part.hpp"
#include "includes/Privmsg.hpp"
#include "includes/Quit.hpp"
#include "includes/Cap.hpp"
#include "includes/Who.hpp"
#include "includes/Kick.hpp"
#include "includes/Mode.hpp"
#include "includes/Notice.hpp"
#include "includes/Ping.hpp"
#include "includes/Names.hpp"
HandleMessage::HandleMessage(Client *client)
{
if (client->getAuthStatus() == NOTAUTHENTICATED)
processNotAuthenticated();
else if (client->getAuthStatus() == AUTHENTICATE)
processAuthenticate();
else
processRegistered();
}
std::map<std::string, ICommand *> HandleMessage::getCommandMap()
{
return (_commandMap);
}
void HandleMessage::processNotAuthenticated()
{
_commandMap.insert(std::make_pair("USER", new User()));
_commandMap.insert(std::make_pair("NICK", new Nick()));
_commandMap.insert(std::make_pair("CAP", new Cap()));
_commandMap.insert(std::make_pair("PING", new Ping()));
_commandMap.insert(std::make_pair("QUIT", new Quit()));
}
void HandleMessage::processAuthenticate()
{
_commandMap.insert(std::make_pair("TOPIC", new Topic()));
_commandMap.insert(std::make_pair("KICK", new Kick()));
_commandMap.insert(std::make_pair("PASS", new Pass()));
_commandMap.insert(std::make_pair("NICK", new Nick()));
_commandMap.insert(std::make_pair("USER", new User()));
_commandMap.insert(std::make_pair("JOIN", new Join()));
_commandMap.insert(std::make_pair("PART", new Part()));
_commandMap.insert(std::make_pair("PRIVMSG", new Privmsg()));
_commandMap.insert(std::make_pair("QUIT", new Quit()));
_commandMap.insert(std::make_pair("WHO", new Who()));
_commandMap.insert(std::make_pair("MODE", new Mode()));
_commandMap.insert(std::make_pair("NOTICE", new Notice()));
_commandMap.insert(std::make_pair("PING", new Ping()));
_commandMap.insert(std::make_pair("names", new Names()));
}
void HandleMessage::processRegistered()
{
_commandMap.insert(std::make_pair("PASS", new Pass()));
_commandMap.insert(std::make_pair("NICK", new Nick()));
_commandMap.insert(std::make_pair("USER", new User()));
_commandMap.insert(std::make_pair("CAP", new Cap()));
_commandMap.insert(std::make_pair("PING", new Ping()));
_commandMap.insert(std::make_pair("QUIT", new Quit()));
}
int HandleMessage::handleMsg(Server &server, Client *client, std::string msg)
{
if (msg.size() > 512)
{
Numeric::printNumeric(client, server, ERR_INPUTTOOLONG());
msg[511] = '\r';
msg[512] = '\n';
msg.clear();
return (0);
}
if (msg == "")
return 0;
size_t pos = msg.find("\r\n");
if (pos == std::string::npos)
pos = msg.find("\n");
std::string executablePart = msg.substr(0, pos);
if (executablePart == "")
return 0;
std::vector<std::string> params = Utils::split(executablePart, ' ');
client->setCommand(params[0]);
params.erase(params.begin());
client->setParams(params);
return (1);
}
ICommand *HandleMessage::getCommand(std::string command)
{
std::map<std::string, ICommand *>::iterator it;
it = this->_commandMap.find(command);
if (it == this->_commandMap.end())
return NULL;
return it->second;
}
int HandleMessage::checkAuthCommand(Server &server, Client *client)
{
std::vector<std::string> _allCommands;
_allCommands.push_back("PASS");
_allCommands.push_back("NICK");
_allCommands.push_back("USER");
_allCommands.push_back("JOIN");
_allCommands.push_back("PART");
_allCommands.push_back("TOPIC");
_allCommands.push_back("PRIVMSG");
if (client->getAuthStatus() == NOTAUTHENTICATED)
{
for (size_t i = 0; i < _allCommands.size(); i++)
{
if (client->getCommand() == _allCommands[i])
{
server.messageToClient(client, client, "Error: You can only send PASS");
_allCommands.clear();
return 1;
}
}
}
else if (client->getAuthStatus() == AUTHENTICATE)
{
for (size_t i = 0; i < _allCommands.size(); i++)
{
if (!(client->getCommand() == "NICK" || client->getCommand() == "USER") && client->getCommand() == _allCommands[i])
{
server.messageToClient(client, client, "Error: You can only send NICK or USER");
_allCommands.clear();
return 1;
}
}
if (client->getCommand() == "NICK" || client->getCommand() == "USER")
{
_allCommands.clear();
return 1;
}
}
_allCommands.clear();
return 0;
}
void HandleMessage::removeExecutedPart(std::string &msg)
{
int deleteCount = 2;
size_t pos = msg.find("\r\n");
if (pos == std::string::npos)
{
pos = msg.find("\n");
deleteCount = 1;
}
if (pos != std::string::npos)
msg.erase(0, pos + deleteCount);
}
HandleMessage::~HandleMessage()
{
std::map<std::string, ICommand *>::iterator it;
for (it = _commandMap.begin(); it != _commandMap.end(); it++)
{
delete it->second;
}
_commandMap.clear();
}