This repository was archived by the owner on Aug 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecvParser.cpp
More file actions
195 lines (182 loc) · 4.63 KB
/
RecvParser.cpp
File metadata and controls
195 lines (182 loc) · 4.63 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* RecvParser.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vkuusela <vkuusela@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/22 11:31:54 by vkuusela #+# #+# */
/* Updated: 2025/08/22 12:58:20 by vkuusela ### ########.fr */
/* */
/* ************************************************************************** */
#include "RecvParser.hpp"
std::ostream &operator<<(std::ostream &os, const Message &msg)
{
if (msg.prefix)
os << "Prefix: " << *(msg.prefix) << " ";
os << "Command: " << msg.command << ", Params: ";
for (auto param : msg.params)
os << param;
return (os);
}
/**
* Feed the recv() buffer into std::string buffer
* @param read_buf The buffer to read
* @param len Length of the buffer
*/
void RecvParser::feed(const char *read_buf, size_t len)
{
_buffer.append(read_buf, len);
_normalizeNewLines();
_parseBuffer();
}
/**
* Make all newlines conform to IRC protocol to make evals take less time
*/
void RecvParser::_normalizeNewLines(void)
{
std::string normalized;
normalized.reserve(_buffer.size());
for (size_t i = 0; i < _buffer.size(); ++i)
{
if (_buffer[i] == '\r')
{
if (i + 1 < _buffer.size() && _buffer[i + 1] == '\n')
{
normalized += "\r\n";
++i;
}
else
normalized += "\r\n";
}
else if (_buffer[i] == '\n')
normalized += "\r\n";
else
normalized += _buffer[i];
}
_buffer = std::move(normalized);
}
/**
* Parse the internal buffer for commands.
* Newlines are \r\n as per IRC protocol.
*/
void RecvParser::_parseBuffer(void)
{
size_t pos;
while ((pos = _buffer.find("\r\n")) != std::string::npos)
{
std::string line = _buffer.substr(0, pos);
_buffer.erase(0, pos + 2);
try
{
Message command = _parseMessage(line);
_handle(command);
}
catch (std::exception &e)
{
std::cerr << "Parsing error: " << e.what();
}
}
}
Message RecvParser::_parseMessage(const std::string &msg)
{
if (msg.size() > 512)
throw (std::runtime_error("Line is over 512 bytes"));
Message ret;
std::string line = msg;
auto end = line.find_last_not_of('\r');
if (end != std::string::npos)
line = line.substr(0, end);
std::istringstream line_stream(line);
std::string token;
if (line[0] == ':')
{
line_stream >> token;
ret.prefix = token.substr(1);
}
if (!(line_stream >> ret.command))
throw (std::runtime_error("Line is missing a command"));
while (line_stream >> token)
{
if (token[0] == ':')
{
std::string trailing_param = token.substr(1);
std::string rest;
std::getline(line_stream, rest);
if (!rest.empty())
trailing_param += " " + rest;
ret.params.push_back(trailing_param);
break ;
}
else
ret.params.push_back(token);
}
return (ret);
}
/**
* Handle the parsed line
* @param msg The line to handle
*/
void RecvParser::_handle(const Message &msg)
{
auto hash = [](const std::string &cmd)
{
if (cmd.compare(0, 4, "NICK") == 0)
return (NICK);
if (cmd.compare(0, 4, "USER") == 0)
return (USER);
if (cmd.compare(0, 4, "JOIN") == 0)
return (JOIN);
if (cmd.compare(0, 4, "PART") == 0)
return (PART);
if (cmd.compare(0, 7, "PRIVMSG") == 0)
return (PRIVMSG);
if (cmd.compare(0, 4, "KICK") == 0)
return (KICK);
if (cmd.compare(0, 6, "INVITE") == 0)
return (INVITE);
if (cmd.compare(0, 5, "TOPIC") == 0)
return (TOPIC);
if (cmd.compare(0, 4, "QUIT") == 0)
return (QUIT);
if (cmd.compare(0, 4, "MODE") == 0)
return (MODE);
return (ERROR);
};
std::cout << "Received: ";
switch (hash(msg.command))
{
case NICK:
std::cout << msg << std::endl;
break ;
case USER:
std::cout << msg << std::endl;
break ;
case JOIN:
std::cout << msg << std::endl;
break ;
case PART:
std::cout << msg << std::endl;
break ;
case PRIVMSG:
std::cout << msg << std::endl;
break ;
case KICK:
std::cout << msg << std::endl;
break ;
case INVITE:
std::cout << msg << std::endl;
break ;
case TOPIC:
std::cout << msg << std::endl;
break ;
case QUIT:
std::cout << msg << std::endl;
break ;
case MODE:
std::cout << msg << std::endl;
break ;
default:
std::cout << "bad/unsupported command" << std::endl;
}
}