-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConfig.cpp
More file actions
207 lines (189 loc) · 7 KB
/
Config.cpp
File metadata and controls
207 lines (189 loc) · 7 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
196
197
198
199
200
201
202
203
204
205
206
207
#include "Config.hpp"
#include "LogManager.hpp"
#include "consts.hpp"
#include "reply_codes.hpp"
#include <fstream>
const Config ircConfig(SERVER_CONF_FILE);
#ifdef TEST
const Config ircConfigTest(SERVER_CONF_FILE_FOR_TEST);
#endif
/************************************************************
* 🥚 CONSTRUCTORS & DESTRUCTOR *
************************************************************/
Config::Config(const std::string& fileName) :
_name(SERVER_NAME),
_psswd(DEFAULT_PASSWORD),
_port(DEFAULT_PORT),
_maxJoinedChannels(MAX_JOINED_CHANNELS),
_chanNameMaxLen(CHAN_NAME_MAX_LEN),
_nicknameMaxLen(NICKNAME_MAX_LEN),
_targetLimit(TARGET_LIMIT),
_codes(),
_trailings(),
_motd()
{
// NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
std::string actualFileName = fileName;
#ifdef TEST
if (fileName == SERVER_CONF_FILE)
actualFileName = SERVER_CONF_FILE_FOR_TEST;
#endif
if (!_parse_config_file(actualFileName)) {
LOG_SERVER.warning("CONF FILE not loaded!");
exit(1);
}
if (_motd.empty()) {
LOG_SERVER.warning("MOTD is empty!");
}
if (_codes.empty()) {
LOG_SERVER.warning("RFC CODES are empty!");
}
if (_trailings.empty()) {
LOG_SERVER.warning("RFC TRAILING MESSAGES are empty!");
}
}
Config::~Config() {}
/*************************************************************
* 🛠️ FUNCTIONS *
*************************************************************/
static std::string trim(const std::string& s)
{
size_t start = s.find_first_not_of(" \t\r\n");
size_t end = s.find_last_not_of(" \t\r\n");
if (start == std::string::npos)
return "";
return s.substr(start, end - start + 1);
}
void Config::_parse_codes(const std::string& line)
{
size_t posAssign = line.find('=');
size_t posComment = line.find('#');
size_t posColon = line.find(':');
// Skip comments and lines without assignment
if (posAssign == std::string::npos || posComment != std::string::npos)
return;
std::string keyStr = line.substr(0, posAssign);
int code = std::atoi(keyStr.c_str());
if (code < 0 && code > MAX_CODE_NUMBER)
return;
if (posColon != std::string::npos) {
// Format: 001=RPL_WELCOME: Welcome message
std::string value = line.substr(posAssign + 1, posColon - posAssign - 1);
std::string trailing = line.substr(posColon + 1);
_codes[code] = value;
_trailings[code] = trailing;
} else {
// Format: 001=RPL_WELCOME
std::string value = line.substr(posAssign + 1);
_codes[code] = value;
_trailings[code] = "";
}
}
void Config::_parse_conf(const std::string& line)
{
size_t posAssign = line.find('=');
size_t posComment = line.find('#');
if (posAssign != std::string::npos && posComment == std::string::npos) {
std::string key = trim(line.substr(0, posAssign));
std::string value = trim(line.substr(posAssign + 1));
_set_key_value(key, value);
}
}
void Config::_parse_motd(const std::string& line) { _motd.push_back(line); }
bool Config::_parse_config_file(const std::string& fileName)
{
std::string currentSection;
std::ifstream file(fileName.c_str());
if (!file.is_open())
return false;
std::string line;
while (std::getline(file, line)) {
if (line[0] == '[' && line[line.size() - 1] == ']') {
currentSection = line.substr(1, line.size() - 2);
continue;
}
if (currentSection == "irc.conf")
_parse_conf(line);
else if (currentSection == "irc.codes")
_parse_codes(line);
else if (currentSection == "irc.motd")
_parse_motd(line);
}
file.close();
return true;
}
void Config::_set_key_value(const std::string& key, std::string& value)
{
const size_t nbParam = 7;
std::string keyList[nbParam]
= {"server_name", "password", "port", "maxJoinedChannels", "chanNameMaxLen", "nicknameMaxLen", "targetLimit"};
void (Config::* functions[nbParam])(std::string&) = {&Config::_set_name,
&Config::_set_password,
&Config::_set_port,
&Config::_set_max_joined_channels,
&Config::_set_chan_name_max_len,
&Config::_set_nickname_max_len,
&Config::_set_target_limit};
for (size_t i = 0; i < nbParam; i++) {
if (key == keyList[i]) {
(this->*functions[i])(value);
return;
}
}
}
void Config::_set_name(std::string& value) { _name = value; }
void Config::_set_password(std::string& value) { _psswd = value; }
void Config::_set_port(std::string& value)
{
int port = static_cast<int>(atoi(value.c_str()));
if (port >= 1 && port <= MAX_PORT)
_port = port;
}
void Config::_set_max_joined_channels(std::string& value)
{
int maxJoinedChannels = static_cast<int>(atoi(value.c_str()));
if (maxJoinedChannels >= 1 && maxJoinedChannels < MAX_JOINED_CHANNELS)
_maxJoinedChannels = maxJoinedChannels;
}
void Config::_set_chan_name_max_len(std::string& value)
{
int chanNameMaxLen = static_cast<int>(atoi(value.c_str()));
if (chanNameMaxLen >= 1 && chanNameMaxLen < CHAN_NAME_MAX_LEN)
_chanNameMaxLen = chanNameMaxLen;
}
void Config::_set_nickname_max_len(std::string& value)
{
int nicknameMaxLen = static_cast<int>(atoi(value.c_str()));
if (nicknameMaxLen >= 1 && nicknameMaxLen < NICKNAME_MAX_LEN)
_nicknameMaxLen = nicknameMaxLen;
}
void Config::_set_target_limit(std::string& value)
{
int targetLimit = static_cast<int>(atoi(value.c_str()));
if (targetLimit >= 1 && targetLimit < TARGET_LIMIT)
_targetLimit = targetLimit;
}
const std::string& Config::str(ReplyCode code) const
{
std::map<int, std::string>::const_iterator it = _codes.find(static_cast<int>(code));
if (it != _codes.end()) {
return it->second;
}
static std::string fallback = "UNKNOWN_CODE";
return fallback;
}
const std::string& Config::trailing(ReplyCode code) const
{
std::map<int, std::string>::const_iterator it = _trailings.find(static_cast<int>(code));
if (it != _trailings.end()) {
return it->second;
}
static std::string fallback = "";
return fallback;
}
const std::string& Config::get_name() const { return _name; }
const std::string& Config::get_password() const { return _psswd; }
int Config::get_max_joined_channels() const { return _maxJoinedChannels; }
size_t Config::get_chan_name_max_len() const { return _chanNameMaxLen; }
size_t Config::get_nickname_max_len() const { return _nicknameMaxLen; }
std::vector<std::string> Config::get_motd() const { return _motd; }