IRC SERVER
The goal of this project is to make you write your own IRC server.
Part.hpp
Go to the documentation of this file.
1 #ifndef PART_H
2 #define PART_H
3 
4 // Our includes
5 #include "../Command.hpp"
6 
7 class Part : public Command
8 {
9  public:
10  Part()
11  {
12  _name = "part";
13  _description = "salir de un canal";
14  _usage = "part";
15  _example[0] = "part <canal>{,<canal>}";
16  _example[1] = "part :#uruguay";
17  }
18 
19  bool validate(void)
20  {
21  std::map<size_t, std::string> p = _message->getParams();
22  if (p.size() < 1)
23  {
25  _message->getCmd()));
26  return (false);
27  }
28  else
29  {
30  std::vector<std::string> _ch_params = split(p[0], ",");
31 
32  for (size_t i = 0; i < _ch_params.size(); i++)
33  {
34  if (_ch_params[i][0] != '#')
35  {
37  ERR_BADCHANMASK(_sender->_servername, _sender->_nick)); // ERR_BADCHANMASK (476)
38  return (false);
39  }
40  }
41  for (size_t i = 0; i < _ch_params.size(); i++)
42  {
43  Channel *channel = _server->getChannel(_ch_params[i]);
44  if (channel)
45  {
46  if (!channel->joined(_sender))
47  {
49  ERR_NOTONCHANNEL(_sender->_servername, _sender->_nick, _ch_params[i]));
50  return (false);
51  }
52  }
53  else
54  {
57  return (false);
58  }
59  }
60  }
61  return (true);
62  }
63 
64  void execute()
65  {
66  std::map<size_t, std::string> p = _message->getParams();
67 
68  std::vector<std::string> _ch_params = split(p[0], ",");
69 
70  for (size_t i = 0; i < _ch_params.size(); i++)
71  {
72  Channel *channel = _server->getChannel(_ch_params[i]);
73 
74  std::vector<Client *> related_channels_clients = channel->getClients();
75  for (size_t j = 0; j < related_channels_clients.size(); j++)
76  {
77  related_channels_clients[j]->message(
78  ":" + _sender->_nick + "!" + _sender->_username + "@" +
79  _sender->_host + " PART #" + channel->getName() + " :" + p[1] + "\n");
80  }
82  }
83  }
84 };
85 #endif
Part::execute
void execute()
Definition: Part.hpp:64
Channel
Definition: Channel.hpp:26
Channel::getName
std::string & getName(void)
Definition: Channel.hpp:82
Part::validate
bool validate(void)
Definition: Part.hpp:19
ERR_NOSUCHCHANNEL
#define ERR_NOSUCHCHANNEL(servername, nick, channel)
Definition: Replies.hpp:107
Command::_description
std::string _description
Definition: Command.hpp:12
Client::_username
std::string _username
Definition: Client.hpp:27
Command::_name
std::string _name
Definition: Command.hpp:11
Command
Definition: Command.hpp:7
Message::getCmd
std::string getCmd(void) const
Definition: message.cpp:74
Client::message
void message(char const *message)
Definition: client.cpp:15
ERR_BADCHANMASK
#define ERR_BADCHANMASK(servername, nick)
Definition: Replies.hpp:151
Client::_host
std::string _host
Definition: Client.hpp:29
Command::_message
Message * _message
Definition: Command.hpp:21
split
std::vector< std::string > split(const std::string &str, const std::string &delimiters)
Definition: functions.cpp:37
Part
Definition: Part.hpp:7
ERR_NEEDMOREPARAMS
#define ERR_NEEDMOREPARAMS(servername, nick, command)
Definition: Replies.hpp:131
Command::_example
std::map< size_t, std::string > _example
Definition: Command.hpp:16
ERR_NOTONCHANNEL
#define ERR_NOTONCHANNEL(servername, nick, channel)
Definition: Replies.hpp:126
Channel::removeClientFromChannel
void removeClientFromChannel(Client *client)
Definition: Channel.hpp:365
Part::Part
Part()
Definition: Part.hpp:10
Channel::getClients
std::vector< Client * > getClients(void) const
Definition: Channel.hpp:172
Channel::joined
bool joined(Client *client)
Definition: Channel.hpp:183
Message::getParams
std::map< size_t, std::string > getParams(void) const
Definition: message.cpp:79
Server::getChannel
Channel * getChannel(std::string &name)
Definition: Server.hpp:202
Command::_usage
std::string _usage
Definition: Command.hpp:13
Command::_sender
Client * _sender
Definition: Command.hpp:19
Client::_nick
std::string _nick
Definition: Client.hpp:26
Client::_servername
std::string _servername
Definition: Client.hpp:30
Command::_server
Server * _server
Definition: Command.hpp:20