-
Notifications
You must be signed in to change notification settings - Fork 1
/
ircutils.h
338 lines (295 loc) · 7.18 KB
/
ircutils.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#ifndef IRCUTILS_H_INCLUDED
#define IRCUTILS_H_INCLUDED
#include "ircdefs.h"
#include "etc.h"
bool split_nick_usermode(const std::string &str, std::string *mode, std::string *nick);
class irc_ident
{
friend class irc_privmsg;
friend class irc_user;
public:
irc_ident()
{
this->set_ident("");
}
irc_ident(const std::string &ident)
{
this->set_ident(ident);
}
irc_ident(const irc_ident &obj)
{
this->ident = obj.ident;
this->nick = obj.nick;
this->user = obj.user;
this->host = obj.host;
}
~irc_ident(){}
bool operator==(const irc_ident &obj)
{
if(obj.ident == this->ident)
return true;
else
return false;
}
int set_ident(const std::string &ident)
{
static pcrecpp::RE re_split_ident("^([^!]+)!([^@]+)@(.*)\\z");
static pcrecpp::RE re_remove_modechar_from_nick("^[~&@%+]*");
this->ident = ident;
if(ident.empty())
{
this->nick.clear();
this->user.clear();
this->host.clear();
this->ident = "!@";
return 0;
}
else if(re_split_ident.FullMatch(ident, &this->nick, &this->user, &this->host))
{
re_remove_modechar_from_nick.GlobalReplace("", &this->nick);
return 0;
}
else
{
this->nick = ident;
this->user = "";
this->host = ident;
return -1;
}
}
void set_nick(const std::string &nick)
{
static pcrecpp::RE re_remove_modechar_from_nick("^[~&@%+]*");
this->nick = nick;
re_remove_modechar_from_nick.GlobalReplace("", &this->nick);
this->reconstruct_ident();
}
void set_user(const std::string &user)
{
this->user = user;
this->reconstruct_ident();
}
void set_host(const std::string &host)
{
this->host = host;
this->reconstruct_ident();
}
const std::string &getident() const{return this->ident;}
const std::string &getnick() const{return this->nick;}
const std::string &getuser() const{return this->user;}
const std::string &gethost() const{return this->host;}
private:
std::string ident, nick, user, host;
void reconstruct_ident()
{
this->ident = this->nick + "!" + this->user + "@" + this->host;
}
};
class irc_msginfo
{
public:
irc_msginfo()
:chantype(IRC_CHANTYPE_CHAN), msgtype(IRC_MSGTYPE_NORMAL),
priority(IRC_PRIORITY_BNORMAL), line_converted(false)
{
}
irc_msginfo(const std::string &chan,
irc_msgtype msgtype = IRC_MSGTYPE_NORMAL,
irc_chantype chantype = IRC_CHANTYPE_CHAN,
bool line_converted = false)
:chantype(chantype), msgtype(msgtype), chan(chan),
priority(IRC_PRIORITY_BNORMAL), line_converted(line_converted)
{
}
irc_msginfo(const char *chan,
irc_msgtype msgtype = IRC_MSGTYPE_NORMAL,
irc_chantype chantype = IRC_CHANTYPE_CHAN,
bool line_converted = false)
:chantype(chantype), msgtype(msgtype), chan(chan),
priority(IRC_PRIORITY_BNORMAL), line_converted(line_converted)
{
}
irc_msginfo(const irc_msginfo &msginfo, irc_msgtype msgtype)
:chantype(msginfo.chantype), msgtype(msgtype), chan(msginfo.chan),
priority(msginfo.priority), line_converted(msginfo.line_converted)
{
}
irc_msginfo(const irc_msginfo &msginfo)
:chantype(msginfo.chantype), msgtype(msginfo.msgtype), chan(msginfo.chan),
priority(msginfo.priority), line_converted(msginfo.line_converted)
{
}
~irc_msginfo()
{
}
// 비교시 priority는 무시됨
const std::string &operator=(const std::string &chan){return this->chan = chan;}
bool operator==(const irc_msginfo &obj) const;
bool operator!=(const irc_msginfo &obj) const
{
return !(obj == *this);
}
void setchantype(irc_chantype chantype){this->chantype = chantype;}
void setmsgtype(irc_msgtype msgtype){this->msgtype = msgtype;}
void setchan(const std::string &chan){this->chan = chan;}
void setpriority(int priority){this->priority = priority;}
void set_line_converted(bool cv){this->line_converted = cv;}
irc_chantype getchantype() const{return this->chantype;}
irc_msgtype getmsgtype() const{return this->msgtype;}
const std::string &getchan() const{return this->chan;}
int getpriority() const{return this->priority;}
int is_line_converted() const{return this->line_converted;}
private:
irc_chantype chantype;
irc_msgtype msgtype;
std::string chan;
int priority;
bool line_converted;
};
class irc_privmsg :public irc_ident, public irc_msginfo
{
public:
irc_privmsg(const irc_ident &ident, const irc_msginfo &msginfo, const std::string &msg);
irc_privmsg(const irc_privmsg &obj)
: irc_ident(obj), irc_msginfo(obj), msg(obj.msg)
{
}
irc_privmsg()
:irc_ident()
{
}
~irc_privmsg()
{
}
const irc_msginfo &getmsginfo() const{return *this;}
void setmsg(const std::string &msg){this->msg = msg;}
const std::string &getmsg() const{return this->msg;}
private:
std::string msg;
};
// 정확히 쓰면 irc_chan_user
class irc_user: public irc_ident
{
public:
irc_user()
{
}
irc_user(const std::string &nick)
{
this->set_nick(nick);
}
int set_nick(const std::string &nick)
{
std::string nick2;
split_nick_usermode(nick, &this->chanmode,
&static_cast<irc_ident *>(this)->nick);
return 0;
}
~irc_user()
{
}
const std::string &getchanusermode() const
{
return this->chanmode;
}
int setchanusermode(const std::string &newmode)
{
this->chanmode = newmode;
return 0;
}
int add_to_chanusermode(const std::string &newmode)
{
for(std::string::const_iterator it = newmode.begin();
it != newmode.end(); it++)
{
if(this->chanmode.find(*it) == std::string::npos)
this->chanmode += *it;
}
return 0;
}
int del_from_chanusermode(const std::string &newmode)
{
for(std::string::const_iterator it = newmode.begin();
it != newmode.end(); it++)
{
size_t loc = this->chanmode.find(*it);
if(loc != std::string::npos)
this->chanmode = this->chanmode.erase(loc, 1);
}
return 0;
}
bool is_user_has_owner_priv() const
{
if(this->chanmode.find('~') != std::string::npos)
return true;
return false;
}
bool is_user_protected() const
{
if(this->chanmode.find('&') != std::string::npos)
return true;
return false;
}
bool is_user_has_op_priv() const
{
if(this->chanmode.find('~') != std::string::npos)
return true;
if(this->chanmode.find('@') != std::string::npos)
return true;
return false;
}
bool is_user_has_hop_priv() const
{
if(this->chanmode.find('~') != std::string::npos)
return true;
if(this->chanmode.find('@') != std::string::npos)
return true;
if(this->chanmode.find('%') != std::string::npos)
return true;
return false;
}
bool is_user_has_voice() const
{
if(this->chanmode.find('+') != std::string::npos)
return true;
return false;
}
bool add_owner_priv()
{
if(this->chanmode.find('~') != std::string::npos)
return false;
this->chanmode += '~';
return true;
}
bool add_protected_priv()
{
if(this->chanmode.find('&') != std::string::npos)
return false;
this->chanmode += '&';
return true;
}
bool add_op_priv()
{
if(this->chanmode.find('@') != std::string::npos)
return false;
this->chanmode += '@';
return true;
}
bool add_hop_priv()
{
if(this->chanmode.find('%') != std::string::npos)
return false;
this->chanmode += '%';
return true;
}
bool add_voice_priv()
{
if(this->chanmode.find('+') != std::string::npos)
return false;
this->chanmode += '+';
return true;
}
private:
std::string chanmode;
};
#endif // IRCUTILS_H_INCLUDED