-
Notifications
You must be signed in to change notification settings - Fork 1
/
join.cpp
218 lines (198 loc) · 6.24 KB
/
join.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
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
#include "cmd.hpp"
#include "channel.hpp"
#include "client.hpp"
#include <cstdlib>
void cmd::joinExistChannel(Client *me, Channel *ch, string pass)
{
string msg;
string msg2;
string msg3;
string channel_users;
vector<Client *> users;
if (ch->isClientInvite(me))
/* invite 받은 client인 경우 */
{
string topicMsg = "";
ch->addUser(me);
me->joinChannel(ch);
ch->delInviteUser(me);
channel_users = ch->getUsersName();
users = ch->getUsers();
if (ch->getTopic().size() != 0)
{
Client *topicer = searchClient(ch->getTopicMaker());
if (!topicer)
return ;
topicMsg = ":irc.local 332 " + me->getNickname() + " " \
+ ch->getChannelName() + " :" + ch->getTopic() + " \r\n" \
+ ":irc.local 333 " + me->getNickname() + " " \
+ ch->getChannelName() + " " + topicer->getNickname() + "!" \
+ topicer->getUserName() + "@127.0.0.1 :" + ch->getTopicTime() + "\r\n";
}
msg = ":" + me->getNickname() + "!" + me->getUserName() \
+ "@127.0.0.1 JOIN :" + ch->getChannelName() + "\r\n";
msg2 = ":irc.local 353 " + me->getNickname() + " = " + ch->getChannelName() \
+ " :" + channel_users + "\r\n";
msg3 = ":irc.local 366 " + me->getNickname() + " " + ch->getChannelName() \
+ " :End of /NAMES list.\r\n";
send(_clntSock, (msg + topicMsg + msg2 + msg3).c_str(), (msg + topicMsg + msg2 + msg3).size(), 0);
for (vector<Client *>::iterator it = users.begin(); it != users.end(); it++)
if ((*it) != me)
send((*it)->getSock(), msg.c_str(), msg.size(), 0);
}
else
{
if (ch->getChPassFlag() && ch->getChPassFlag() && pass != ch->getPassWord())
/* password가 일치하지않는 경우 */
{
msg = ":irc.local 475 " + me->getNickname() + " " + ch->getChannelName() \
+ " :Cannot join channel (incorrect channel key)\r\n";
send(_clntSock, msg.c_str(), msg.size(), 0);
}
/* password가 일치하는 경우 */
else
{
if (ch->getInviteOnlyFlag())
/* channel이 invite only 인 경우 */
{
msg = ":irc.local 473 " + me->getNickname() + " " + ch->getChannelName() \
+ " :Cannot join channel (invite only)\r\n";
send(_clntSock, msg.c_str(), msg.size(), 0);
}
else
{
if (ch->getChLimitFlag() && static_cast<int>(ch->getUsers().size()) >= ch->getUserLimit())
/* 유저제한을 넘는 경우 */
{
msg = ":irc.local 471 " + me->getNickname() + " " + ch->getChannelName() \
+ " :Cannot join channel (channel is full)\r\n";
send(_clntSock, msg.c_str(), msg.size(), 0);
}
else
/* 정상 적으로 채널에 들어가는 경우 */
{
string topicMsg = "";
ch->addUser(me);
me->joinChannel(ch);
channel_users = ch->getUsersName();
users = ch->getUsers();
if (ch->getTopic().size() != 0)
{
Client *topicer = searchClient(ch->getTopicMaker());
if (topicer)
return ;
topicMsg = ":irc.local 332 " + me->getNickname() + " " \
+ ch->getChannelName() + " :" + ch->getTopic() + " \r\n" \
+ ":irc.local 333 " + me->getNickname() + " " \
+ ch->getChannelName() + " " + topicer->getNickname() + "!" \
+ topicer->getUserName() + "@127.0.0.1 :" + ch->getTopicTime() + "\r\n";
}
msg = ":" + me->getNickname() + "!" + me->getUserName() \
+ "@127.0.0.1 JOIN :" + ch->getChannelName() + "\r\n";
msg2 = ":irc.local 353 " + me->getNickname() + " = " + ch->getChannelName() \
+ " :" + channel_users + "\r\n";
msg3 = ":irc.local 366 " + me->getNickname() + " " + ch->getChannelName() \
+ " :End of /NAMES list.\r\n";
send(_clntSock, (msg + topicMsg + msg2 + msg3).c_str(), (msg + topicMsg + msg2 + msg3).size(), 0);
for (vector<Client *>::iterator it = users.begin(); it != users.end(); it++)
if ((*it) != me)
send((*it)->getSock(), msg.c_str(), msg.size(), 0);
}
}
}
}
}
void cmd::joinNewChannel(Client *me, string channel)
{
Channel *ch;
string msg;
ch = addChannel(channel);
if (!ch)
exit(1);
else
{
ch->addOpUser(me);
ch->addUser(me);
me->joinChannel(ch);
msg = ":" + me->getNickname() + "!" + me->getUserName() \
+ "@127.0.0.1 JOIN :" + ch->getChannelName() + "\r\n"
+ ":irc.local 353 " + me->getNickname() + " = " + ch->getChannelName() \
+ " :" + "@" + me->getNickname() + "\r\n" \
+ ":irc.local 366 " + me->getNickname() + " " + ch->getChannelName() \
+ " :End of /NAMES list.\r\n"; send(_clntSock, msg.c_str(), msg.size(), 0);
}
}
void cmd::joinWithPass(vector<string> &chList, vector<string> &passList)
{
vector<string>::iterator channel = chList.begin();
vector<string>::iterator pass = passList.begin();
Client *me = searchClient(_clntSock);
if (!me)
return ;
Channel *ch;
for (;channel != chList.end(); channel++)
{
ch = searchChannel((*channel));
if (ch)
/* 존재하는 채널에 들어가는 경우 */
joinExistChannel(me, ch, (*pass));
else
/* 새로운 채널을 생성할때 */
joinNewChannel(me, (*channel));
pass++;
}
}
void cmd::joinNoPass(vector<string> &chList)
{
vector<string>::iterator channel = chList.begin();
Client *me = searchClient(_clntSock);
Channel *ch;
if (!me)
return ;
for (;channel != chList.end(); channel++)
{
ch = searchChannel((*channel));
if (ch)
/* 존재하는 채널에 들어가는 경우 */
joinExistChannel(me, ch, "");
else
/* 새로운 채널을 생성할때 */
joinNewChannel(me, (*channel));
}
}
void cmd::join(string arg)
{
stringstream ss(arg);
string channels;
string passwords;
vector<string> chList;
vector<string> passList;
size_t start = 0;
size_t pos = 0;
if (arg.empty())
return ;
/* 채널 이름들 ',' 기준으로 스플릿*/
ss >> channels;
while ((pos = channels.find(",", start)) != string::npos)
{
chList.push_back(channels.substr(start, pos - start));
start = pos + 1;
}
chList.push_back(channels.substr(start, channels.size() - start));
/* password들 ',' 기준으로 스플릿*/
if (!ss.eof())
{
ss >> passwords;
start = 0; pos = 0;
while ((pos = passwords.find(",", start)) != string::npos)
{
passList.push_back(passwords.substr(start, pos - start));
start = pos + 1;
}
passList.push_back(passwords.substr(start, passwords.size() - start));
}
if (passList.size())
joinWithPass(chList, passList);
else
joinNoPass(chList);
}