IRC SERVER
The goal of this project is to make you write your own IRC server.
Join.hpp
Go to the documentation of this file.
1 #ifndef JOIN_H
2 #define JOIN_H
3 
4 // Our includes
5 #include "../Command.hpp"
6 
7 class Join : public Command
8 {
9  public:
10  Join()
11  {
12  _name = "join";
13  _description = "une al usuario al canal si existe o genera un nuevo "
14  "canal";
15  _usage = "join <canal>{,<canal>} [<clave>{,<clave>}]";
16  _example[0] = "join #canal1 clave1";
17  _example[1] = "join #canal1,#canal2 clave1,clave2";
18 
19  // ERR_NOSUCHCHANNEL (403)
20  }
21 
22  bool validate(void)
23  {
24  std::map<size_t, std::string> p = _message->getParams();
25  if (p.size() < 1 || p.size() > 2)
26  {
28  _message->getCmd()));
29  return (false);
30  }
31  else
32  {
33  if (p[0][0] != '#')
34  {
36  return (false);
37  }
39  {
42  return (false);
43  }
44 
45  Channel *channel = _server->getChannel(p[0]);
46  if (channel)
47  {
48  if (channel->joined(_sender))
49  {
52  _sender->getUsername(), channel->getName())); // ERR_USERONCHANNEL 443
53  return (false);
54  }
55  else if ((channel->_normal_clients.size() +
56  channel->_ope_clients.size()) > channel->getUserLimit())
57  {
59  _sender->_servername, _sender->getNick(), channel->getName())); // ERR_CHANNELISFULL 471
60  return (false);
61  }
62  else if ((channel->getPassword() != "" && (p.size() == 0 || channel->getPassword() != p[1])))
63  {
65  _sender->_servername, _sender->getNick(), channel->getName())); // ERR_BADCHANNELKEY 475
66  return (false);
67  }
68  else if (channel->isBanned(_sender))
69  {
71  _sender->_servername, _sender->getNick(), channel->getName())); // ERR_BANNEDFROMCHAN 474
72  return (false);
73  }
74  else if (channel->isInviteOnly() && !channel->isInvited(_sender))
75  {
77  _sender->_servername, _sender->getNick(), channel->getName())); // ERR_INVITEONLYCHAN 473
78  return (false);
79  }
80  }
81  }
82  return (true);
83  }
84 
85  std::vector<Message> parser(Message *message)
86  {
87  std::map<size_t, std::string> p = message->getParams();
88  if (p.size() < 1)
89  {
90  throw std::runtime_error(ERR_NEEDMOREPARAMS(
91  _sender->_servername, _sender->getNick(), message->getCmd()));
92  }
93 
94  std::vector<std::string> _ch_params = split(p[0], ",");
95  std::vector<std::string> _pw_params = split(p[1], ",");
96  std::vector<Message> messages;
97  for (size_t i = 0; i < _ch_params.size(); i++)
98  {
99  std::string _ch_param = _ch_params[i];
100  if (_pw_params.size() > 0 && i < _pw_params.size())
101  {
102  std::string tmp(std::string(this->getName() + " " + _ch_param + " " + _pw_params[i]));
103  messages.push_back(Message(tmp));
104  }
105  else
106  {
107  std::string tmp(std::string(this->getName() + " " + _ch_param));
108  messages.push_back(Message(tmp));
109  }
110  }
111 
112  return (messages);
113  }
114 
115  void execute()
116  {
117  std::map<size_t, std::string> p = _message->getParams();
118 
119  Channel *channel = _server->getChannel(p[0]);
120 
121  if (channel)
122  {
123  channel->_normal_clients.push_back(_sender);
124  channel->broadcastMessage(RPL_CUSTOM_JOIN(_sender->_nick, channel->getName()));
125  std::string topic = channel->getTopic();
127  channel->getName(),
128  (topic.size() > 0 ? topic : "No topic is set"))); // RPL_TOPIC 332
129 
130  std::vector<Client *> clients = channel->getClients();
131  std::string users_str = "";
132  for (size_t i = 0; i < clients.size(); i++)
133  {
134  users_str +=
135  channel->getClientRoleString(clients[i]) + clients[i]->getNick() + (i == clients.size() - 1 ? "" : " ");
136  }
138  channel->getModeString(), channel->getName(), users_str));
139  _sender->message(
141  }
142  else
143  {
144  Channel * channel;
145  std::string password = p.size() > 0 ? p[1] : std::string("");
146  channel = _server->createChannel(p[0], password);
147  channel->setCreator(_sender);
149  _sender->message(
150  RPL_TOPIC(_sender->_servername, _sender->_nick, channel->getName(), "No topic is set"));
152  "@" + _sender->getNick()));
153  _sender->message(
155  }
156  }
157 };
158 #endif
Channel
Definition: Channel.hpp:26
Channel::getName
std::string & getName(void)
Definition: Channel.hpp:82
ERR_BADCHANNELKEY
#define ERR_BADCHANNELKEY(servername, nick, channel)
Definition: Replies.hpp:149
Join::validate
bool validate(void)
Definition: Join.hpp:22
ERR_BANNEDFROMCHAN
#define ERR_BANNEDFROMCHAN(servername, nick, channel)
Definition: Replies.hpp:147
Channel::isInvited
bool isInvited(Client *client)
Definition: Channel.hpp:225
Command::_description
std::string _description
Definition: Command.hpp:12
Join::execute
void execute()
Definition: Join.hpp:115
Channel::getTopic
std::string & getTopic(void)
Definition: Channel.hpp:87
Client::getNick
const std::string getNick(void) const
Definition: Client.hpp:142
Command::_name
std::string _name
Definition: Command.hpp:11
Join::Join
Join()
Definition: Join.hpp:10
Channel::getClientRoleString
std::string getClientRoleString(Client *client)
Definition: Channel.hpp:356
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_INVITEONLYCHAN
#define ERR_INVITEONLYCHAN(servername, nick, channel)
Definition: Replies.hpp:145
Server::createChannel
Channel * createChannel(std::string &name, std::string &password)
Definition: Server.hpp:221
ERR_BADCHANMASK
#define ERR_BADCHANMASK(servername, nick)
Definition: Replies.hpp:151
Channel::setCreator
void setCreator(Client *creator)
Definition: Channel.hpp:58
RPL_ENDOFNAMES
#define RPL_ENDOFNAMES(servername, nick, channel)
Definition: Replies.hpp:85
Channel::_normal_clients
std::vector< Client * > _normal_clients
Definition: Channel.hpp:43
Channel::isInviteOnly
bool isInviteOnly(void)
Definition: Channel.hpp:231
Client::getUsername
const std::string getUsername(void) const
Definition: Client.hpp:147
MAX_CHANNELS
#define MAX_CHANNELS
Definition: config.hpp:6
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
ERR_TOOMANYCHANNELS
#define ERR_TOOMANYCHANNELS(servername, nick, channel)
Definition: Replies.hpp:111
Channel::getPassword
std::string & getPassword(void)
Definition: Channel.hpp:77
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
Channel::isBanned
bool isBanned(Client *client)
Definition: Channel.hpp:213
RPL_TOPIC
#define RPL_TOPIC(servername, nick, channel, topic)
Definition: Replies.hpp:71
RPL_CUSTOM_JOIN
#define RPL_CUSTOM_JOIN(user, channel)
Definition: Replies.hpp:187
Command::getName
std::string getName() const
Definition: Command.hpp:25
Join::parser
std::vector< Message > parser(Message *message)
Definition: Join.hpp:85
Join
Definition: Join.hpp:7
Channel::getModeString
std::string getModeString(void)
Definition: Channel.hpp:311
Channel::broadcastMessage
void broadcastMessage(std::string message)
Definition: Channel.hpp:384
Channel::getClients
std::vector< Client * > getClients(void) const
Definition: Channel.hpp:172
Channel::_ope_clients
std::vector< Client * > _ope_clients
Definition: Channel.hpp:45
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
Channel::getUserLimit
size_t getUserLimit(void)
Definition: Channel.hpp:164
Message
Definition: Message.hpp:12
Server::getChannel
Channel * getChannel(std::string &name)
Definition: Server.hpp:202
RPL_NAMREPLY
#define RPL_NAMREPLY(servername, nick, symbol, channel, users)
Definition: Replies.hpp:83
Command::_usage
std::string _usage
Definition: Command.hpp:13
ERR_CHANNELISFULL
#define ERR_CHANNELISFULL(servername, nick, channel)
Definition: Replies.hpp:141
Client::getUserId
const std::string getUserId(void) const
Definition: Client.hpp:162
Server::getRelatedChannels
std::map< std::string, Channel * > getRelatedChannels(Client *client)
Definition: Server.hpp:114
Command::_sender
Client * _sender
Definition: Command.hpp:19
ERR_USERONCHANNEL
#define ERR_USERONCHANNEL(servername, nick, username, channel)
Definition: Replies.hpp:128
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