IRC SERVER
The goal of this project is to make you write your own IRC server.
Names.hpp
Go to the documentation of this file.
1 #ifndef NAMES_H
2 #define NAMES_H
3 
4 // Our includes
5 #include "../Command.hpp"
6 
7 class Names : public Command
8 {
9  public:
11  {
12  _name = "Names";
13  _description = "Mostrar los usuarios pertenecianentes a los canales";
14  _usage = "names [<canales>]";
15  _example[0] = "names";
16  _example[0] = "names #hola";
17  _example[0] = "names #hola,#chau";
18  }
19 
20  void send_channel(std::string &name)
21  {
22  Channel *channel = _server->getChannel(name);
23  if (channel == NULL ||
24  (channel->hasMode(CHANNEL_MODE_SECRET) && !channel->joined(_sender)))
25  return;
26  std::vector<Client *> clients = channel->getClients();
27  std::string users_str = "";
28  for (size_t i = 0; i < clients.size(); i++)
29  {
30  if (!channel->joined(_sender) && clients[i]->isInvisible())
31  continue;
32  users_str += channel->getClientRoleString(clients[i]);
33  if (i == clients.size() - 1)
34  users_str += clients[i]->getNick();
35  else
36  users_str += clients[i]->getNick() + " ";
37  }
39  RPL_NAMREPLY(_sender->_servername, _sender->getNick(), channel->getModeString(), name, users_str));
40  }
41 
42  void execute()
43  {
44  std::map<size_t, std::string> p = _message->getParams();
45  if (p.size() == 1)
46  {
47  std::vector<std::string> _ch_params = split(p[0], ",");
48  for (size_t i = 0; i < _ch_params.size(); i++)
49  {
50  send_channel(_ch_params[i]);
52  }
53  }
54  else
55  {
56  std::map<std::string, Channel *>::iterator it = _server->_channels.begin();
57  for (; it != _server->_channels.end(); it++)
58  {
59  send_channel(it->second->getName());
61  }
62  }
63  }
64 };
65 #endif
Channel
Definition: Channel.hpp:26
Names::send_channel
void send_channel(std::string &name)
Definition: Names.hpp:20
Names::execute
void execute()
Definition: Names.hpp:42
Command::_description
std::string _description
Definition: Command.hpp:12
Client::getNick
const std::string getNick(void) const
Definition: Client.hpp:142
Command::_name
std::string _name
Definition: Command.hpp:11
Channel::getClientRoleString
std::string getClientRoleString(Client *client)
Definition: Channel.hpp:356
Command
Definition: Command.hpp:7
CHANNEL_MODE_SECRET
@ CHANNEL_MODE_SECRET
Definition: Channel.hpp:9
Client::message
void message(char const *message)
Definition: client.cpp:15
Server::_channels
std::map< std::string, Channel * > _channels
Definition: Server.hpp:54
RPL_ENDOFNAMES
#define RPL_ENDOFNAMES(servername, nick, channel)
Definition: Replies.hpp:85
Names
Definition: Names.hpp:7
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
Command::_example
std::map< size_t, std::string > _example
Definition: Command.hpp:16
Channel::getModeString
std::string getModeString(void)
Definition: Channel.hpp:311
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
Names::Names
Names()
Definition: Names.hpp:10
RPL_NAMREPLY
#define RPL_NAMREPLY(servername, nick, symbol, channel, users)
Definition: Replies.hpp:83
Command::_usage
std::string _usage
Definition: Command.hpp:13
Channel::hasMode
bool hasMode(ChannelMode mode)
Definition: Channel.hpp:337
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