-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.h
321 lines (257 loc) · 7.1 KB
/
server.h
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
* COPYRIGHT 2014 (C) Jason Volk
* COPYRIGHT 2014 (C) Svetlana Tkachenko
*
* DISTRIBUTED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) (see: LICENSE)
*/
struct Server
{
std::string name;
std::string vers;
ISupport isupport;
std::string user_modes, user_pmodes;
std::string chan_modes, chan_pmodes;
std::string serv_modes, serv_pmodes;
std::set<std::string> caps;
bool has_cap(const std::string &cap) const { return caps.count(cap); }
// 3.2 CHANLIMIT
size_t get_chanlimit(const char &prefix) const;
// 3.10 MAXLIST
std::string get_maxlist_assoc(const char &prefix) const;
size_t get_maxlist(const char &prefix) const;
// 3.14 PREFIX
bool has_prefix(const char &prefix) const;
bool has_prefix_mode(const char &mode) const;
char prefix_to_mode(const char &prefix) const;
char mode_to_prefix(const char &mode) const;
// Delta/mode rules and validations
Delta::Type mode_type(const char &mode) const;
Delta::Type mode_type(const Delta &delta) const;
bool mode_has_arg(const char &mode, const bool &sign) const;
bool mode_has_invmask(const char &m) const;
bool need_mask(const Delta &d) const;
bool user_mode(const char &m) const;
bool user_mode(const Delta &d) const;
bool chan_mode(const char &m) const;
bool chan_mode(const Delta &d) const;
const char *valid_arg(const Delta &delta) const; // static string on invalid, else null
const char *valid(const Delta &delta, const std::nothrow_t) const; // static string on invalid, else null
void valid(const Delta &delta) const; // throws reason for invalid
friend std::ostream &operator<<(std::ostream &s, const Server &srv);
};
inline
void Server::valid(const Delta &d)
const
{
const auto reason(valid(d,std::nothrow));
if(reason)
throw Exception(reason);
}
inline
const char *Server::valid(const Delta &d,
const std::nothrow_t)
const
{
if(!user_mode(d) && !chan_mode(d))
return "Mode is not valid on this server.";
return valid_arg(d);
}
inline
const char *Server::valid_arg(const Delta &d)
const
{
using std::get;
if(get<d.MASK>(d).empty() && need_mask(d))
return "Mode requries an argument.";
if(!get<d.MASK>(d).empty() && !need_mask(d))
return "Mode requires no argument.";
if(need_mask(d) && !mode_has_invmask(get<d.MODE>(d)) && get<d.MASK>(d) == Mask::INVALID)
return "Mode requires a valid mask argument.";
switch(get<d.MODE>(d))
{
case 'l': return isnumeric(get<d.MASK>(d))? nullptr : "argument must be numeric.";
case 'j':
{
const auto arg(split(get<d.MASK>(d),":"));
return arg.first.empty() || !isnumeric(arg.first)? "join throttle user count invalid.":
arg.second.empty() || !isnumeric(arg.second)? "join throttle seconds count invalid.":
nullptr;
}
default: return nullptr;
}
}
inline
bool Server::need_mask(const Delta &d)
const
{
return mode_has_arg(std::get<d.MODE>(d),std::get<d.SIGN>(d));
}
inline
bool Server::chan_mode(const Delta &d)
const
{
return chan_mode(std::get<d.MODE>(d));
}
inline
bool Server::chan_mode(const char &m)
const
{
return chan_modes.find(m) != std::string::npos ||
chan_pmodes.find(m) != std::string::npos;
}
inline
bool Server::user_mode(const Delta &d)
const
{
return user_mode(std::get<d.MODE>(d));
}
inline
bool Server::user_mode(const char &m)
const
{
return user_modes.find(m) != std::string::npos ||
user_pmodes.find(m) != std::string::npos;
}
inline
bool Server::mode_has_invmask(const char &m)
const
{
switch(m)
{
case 'o':
case 'v':
case 'l':
case 'j':
case 'k':
case 'f': return true;
default: return false;
}
}
inline
bool Server::mode_has_arg(const char &mode,
const bool &sign)
const
{
switch(mode_type(mode))
{
case Delta::Type::A: return true;
case Delta::Type::B: return true;
case Delta::Type::C: return sign;
default:
case Delta::Type::D: return false;
}
}
inline
Delta::Type Server::mode_type(const Delta &d)
const
{
return mode_type(std::get<d.MODE>(d));
}
inline
Delta::Type Server::mode_type(const char &mode)
const
{
size_t i(0);
const auto cm(tokens(isupport["CHANMODES"],","));
for(; i < 4; ++i)
if(cm.size() > i && cm.at(i).find(mode) != std::string::npos)
break;
else if(i == 1 && has_prefix_mode(mode))
break;
return Delta::Type(i);
}
inline
char Server::mode_to_prefix(const char &prefix)
const
{
const auto &pxs(isupport["PREFIX"]);
const auto modes(between(pxs,"(",")"));
const auto prefx(split(pxs,")").second);
const auto pos(modes.find(prefix));
return pos != std::string::npos? prefx.at(pos) : '\0';
}
inline
char Server::prefix_to_mode(const char &mode)
const
{
const auto &pxs(isupport["PREFIX"]);
const auto modes(between(pxs,"(",")"));
const auto prefx(split(pxs,")").second);
const auto pos(prefx.find(mode));
return pos != std::string::npos? modes.at(pos) : '\0';
}
inline
bool Server::has_prefix(const char &prefix)
const
{
return prefix_to_mode(prefix) != '\0';
}
inline
bool Server::has_prefix_mode(const char &mode)
const
{
const auto &pxs(isupport["PREFIX"]);
const auto modes(between(pxs,"(",")"));
return modes.find(mode) != std::string::npos;
}
inline
size_t Server::get_maxlist(const char &prefix)
const
{
for(const auto &tok : tokens(isupport["MAXLIST"],","))
{
const auto kv(split(tok,":"));
if(kv.first.find(prefix) != std::string::npos)
return kv.second.empty()? std::numeric_limits<size_t>::max():
lex_cast<size_t>(kv.second);
}
return std::numeric_limits<size_t>::max();
}
inline
std::string Server::get_maxlist_assoc(const char &prefix)
const
{
for(const auto &tok : tokens(isupport["MAXLIST"],","))
{
const auto kv(split(tok,":"));
if(kv.first.find(prefix) != std::string::npos)
return kv.first;
}
return {};
}
inline
size_t Server::get_chanlimit(const char &prefix)
const
{
for(const auto &tok : tokens(isupport["CHANLIMIT"],","))
{
const auto kv(split(tok,":"));
if(kv.first.find(prefix) != std::string::npos)
return kv.second.empty()? std::numeric_limits<size_t>::max():
lex_cast<size_t>(kv.second);
}
return std::numeric_limits<size_t>::max();
}
inline
std::ostream &operator<<(std::ostream &s,
const Server &srv)
{
s << "[Server Modes]: " << std::endl;
s << "name: " << srv.name << std::endl;
s << "vers: " << srv.vers << std::endl;
s << "user modes: [" << srv.user_modes << "]" << std::endl;
s << " w/ parm: [" << srv.user_pmodes << "]" << std::endl;
s << "chan modes: [" << srv.chan_modes << "]" << std::endl;
s << " w/ parm: [" << srv.chan_pmodes << "]" << std::endl;
s << "serv modes: [" << srv.serv_modes << "]" << std::endl;
s << " w/ parm: [" << srv.serv_pmodes << "]" << std::endl;
s << std::endl;
s << "[Server Configuration]:" << std::endl;
for(const auto &p : srv.isupport)
s << std::setw(16) << p.first << " = " << p.second << std::endl;
s << std::endl;
s << "[Server Extended Capabilities]:" << std::endl;
for(const auto &cap : srv.caps)
s << cap << std::endl;
return s;
}