IRC SERVER
The goal of this project is to make you write your own IRC server.
Kick.hpp
Go to the documentation of this file.
1 #ifndef KICK_H
2 #define KICK_H
3 
4 // Our includes
5 #include "../Command.hpp"
6 
7 class Kick : public Command
8 {
9  public:
10  Kick()
11  {
12  _name = "kick";
13  _description = "expulsar del servidor";
14  _usage = "kick";
15  _example[0] = "kick <canal> <usuario> :[<comentario>]";
16  _example[1] = "kick #hola user1";
17  _example[2] = "kick #hola user1,user2";
18  _example[3] = "kick #hola user1,user2 :coment";
19  }
20 
21  bool validate(void)
22  {
23  std::map<size_t, std::string> p = _message->getParams();
24 
25  if (p.size() > 3 || p.size() < 2)
26  {
28  _message->getCmd())); // ERR_NEEDMOREPARAMS (461)
29  return (false);
30  }
31  else
32  {
33  std::vector<std::string> _user_params = split(p[1], ",");
34 
35  if (p[0][0] != '#')
36  {
37  _sender->message(ERR_BADCHANMASK(_sender->_servername, _sender->getNick())); // ERR_BADCHANMASK (476)
38  return (false);
39  }
40 
41  Channel *channel = _server->getChannel(p[0]);
42  if (channel)
43  {
44  if (!channel->joined(_sender))
45  {
48  return (false);
49  }
50  if (!channel->isOpe(_sender))
51  {
54  return (false);
55  }
56  for (size_t i = 0; i < _user_params.size(); i++)
57  {
58  Client *user = _server->getClient(_user_params[i]);
59  if (!user)
60  {
63  return (false);
64  }
65  if (!channel->joined(user))
66  {
68  _sender->_servername, _sender->getNick(), channel->getName())); // ERR_USERNOTINCHANNEL (441)
69  return (false);
70  }
71  }
72  }
73  else
74  {
76  return (false);
77  }
78  }
79  return (true);
80  }
81 
82  void execute()
83  {
84  std::map<size_t, std::string> p = _message->getParams();
85  std::vector<std::string> _user_params = split(p[1], ",");
86 
87  Channel *channel = _server->getChannel(p[0]);
88  for (size_t i = 0; i < _user_params.size(); i++)
89  {
90  Client *user = _server->getClient(_user_params[i]);
91  channel->broadcastMessage(
92  RPL_CUSTOM_KICK(_sender->getUserId(), channel->getName(), user->getNick(), p[2]));
93  channel->kick(user);
94  }
95  };
96 };
97 #endif
Channel
Definition: Channel.hpp:26
Channel::getName
std::string & getName(void)
Definition: Channel.hpp:82
Channel::isOpe
bool isOpe(Client *client)
Definition: Channel.hpp:195
ERR_NOSUCHCHANNEL
#define ERR_NOSUCHCHANNEL(servername, nick, channel)
Definition: Replies.hpp:107
Kick::execute
void execute()
Definition: Kick.hpp:82
Command::_description
std::string _description
Definition: Command.hpp:12
Channel::kick
void kick(Client *client)
Definition: Channel.hpp:265
Client::getNick
const std::string getNick(void) const
Definition: Client.hpp:142
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
RPL_CUSTOM_KICK
#define RPL_CUSTOM_KICK(user, channel, nick, reason)
Definition: Replies.hpp:188
ERR_BADCHANMASK
#define ERR_BADCHANMASK(servername, nick)
Definition: Replies.hpp:151
Kick::validate
bool validate(void)
Definition: Kick.hpp:21
ERR_NOSUCHNICK
#define ERR_NOSUCHNICK(servername, nick)
Definition: Replies.hpp:104
Command::_message
Message * _message
Definition: Command.hpp:21
Kick
Definition: Kick.hpp:7
split
std::vector< std::string > split(const std::string &str, const std::string &delimiters)
Definition: functions.cpp:37
ERR_CHANOPRIVSNEEDED
#define ERR_CHANOPRIVSNEEDED(servername, nick, channel)
Definition: Replies.hpp:154
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
ERR_USERNOTINCHANNEL
#define ERR_USERNOTINCHANNEL(servername, nick, channel)
Definition: Replies.hpp:124
Channel::broadcastMessage
void broadcastMessage(std::string message)
Definition: Channel.hpp:384
Server::getClient
Client * getClient(std::string const &name)
Definition: Server.hpp:140
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
Kick::Kick
Kick()
Definition: Kick.hpp:10
Server::getChannel
Channel * getChannel(std::string &name)
Definition: Server.hpp:202
Client
Definition: Client.hpp:22
Command::_usage
std::string _usage
Definition: Command.hpp:13
Client::getUserId
const std::string getUserId(void) const
Definition: Client.hpp:162
Command::_sender
Client * _sender
Definition: Command.hpp:19
Client::_servername
std::string _servername
Definition: Client.hpp:30
Command::_server
Server * _server
Definition: Command.hpp:20