-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPART_command.cpp
More file actions
58 lines (55 loc) · 2 KB
/
PART_command.cpp
File metadata and controls
58 lines (55 loc) · 2 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
#include "Server.hpp"
#include "msg_parse.hpp"
void Server::send_msg_to_channel_users(Channel &chan, std::string &message)
{
for (std::list<User *>::iterator it = chan.get_users().begin() ; it != chan.get_users().end(); it++)
{
write_socket((*it)->get_fd(), message);
}
}
void Server::PART_handler(User &user, msg_parse &command)
{
std::string channel_name;
size_t index = 0;
size_t prev_index = 0;
std::list<Channel>::iterator chan;
if ((command.get_cmd_params().size() > 0 && command.get_cmd_params().size() <= 2) || (command.get_cmd_params().size() == 1 && command.get_additional_param().size()))
{
std::string _channels = command.get_cmd_params()[0];
while (((index = _channels.find(',', index)) != std::string::npos) || prev_index < _channels.length())
{
index == std::string::npos ? index = _channels.length() : 0;
channel_name = _channels.substr(prev_index, index - prev_index);
if (find_channel(channel_name[0], channel_name.substr(1, channel_name.length() - 1)) != __channels.end())
{
chan = find_channel(channel_name[0], channel_name.substr(1, channel_name.length() - 1));
if (find_user_in_channel(user, *chan) != *((*chan).get_users().end()))
{
std::string full_msg = user.full_id() + " PART " + (*chan).get_name() + "\n";
if (command.get_cmd_params().size() == 2)
full_msg = user.full_id() + " PART " + ":" + command.get_cmd()[1] + "\n";
else if (command.get_additional_param().size())
full_msg = user.full_id() + " PART "+ ":" + command.get_additional_param() + "\n";
if (!(*chan).get_modes().get_q())
send_msg_to_channel_users(*chan, full_msg);
(*chan).remove_user(&user);
if ((*chan).get_users().size() == 0)
delete_channel((*chan).get_name());
user.remove_channel(&(*chan));
}
else
{
write_reply(user, ERR_NOTONCHANNEL, command);
}
}
else
{
write_reply(user, ERR_NOSUCHCHANNEL, command);
}
index++;
prev_index = index;
}
}
else
write_reply(user, ERR_NEEDMOREPARAMS, command);
}