IRC SERVER
The goal of this project is to make you write your own IRC server.
Nick.hpp
Go to the documentation of this file.
1 #ifndef NICK_H
2 #define NICK_H
3 
4 // Our includes
5 #include "../Command.hpp"
6 
7 class Nick : public Command
8 {
9  public:
10  Nick()
11  {
12  _name = "nick";
13  _description = "";
14  _usage = "nick";
15  _example[0] = "nick <nick>";
16  _example[1] = "nick <nuevo_nick>";
17  _needs_auth = false;
18  }
19 
20  bool validate(void)
21  {
22  std::map<size_t, std::string> p = _message->getParams();
23 
24  if (p.size() == 0)
25  {
27  return (false);
28  }
29  else if (p.size() > 1)
30  {
32  return (false);
33  }
34 
35  std::string name = _message->getParams()[0];
36  std::map<size_t, Client *>::iterator it = _server->_clients.begin();
37  for (; it != _server->_clients.end(); it++)
38  {
39  if (it->second->_nick == name)
40  {
42  return (false);
43  }
44  }
45  /* A name can only contain the following characters:
46 
47  A through to Z. (Lowercase and uppercase.)
48  0 through to 9.
49  `|^_-{}[] and \
50 
51  And a name cannot start with a number or hyphen.
52  */
53  if (std::count_if(name.begin(), name.end(), ::isalnum) != static_cast<long>(name.length())) // MAAAAL
54  {
56  return (false);
57  }
58  return (true);
59  }
60 
61  void execute() // doesnt work fine
62  {
63  std::map<size_t, Client *> clients = _server->_clients;
64  std::string name = _message->getParams()[0];
65 
66  std::vector<Client *> related_clients = _server->getRelatedClients(_sender);
67  for (size_t i = 0; i < related_clients.size(); i++)
68  {
69  related_clients[i]->message(std::string(":" + _sender->_nick + "!" +
70  _sender->_username + "@" +
71  _sender->_servername + " NICK :" + name + "\n")
72  .c_str());
73  }
74  _sender->message(std::string(":" + _sender->_nick + "!" +
75  _sender->_username + "@" +
76  _sender->_servername + " NICK :" + name + "\n")
77  .c_str());
78  _sender->setNick(name);
79  }
80 };
81 #endif
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
Client::message
void message(char const *message)
Definition: client.cpp:15
Server::getRelatedClients
std::vector< Client * > getRelatedClients(Client *client)
Definition: Server.hpp:93
Server::_clients
std::map< size_t, Client * > _clients
Definition: Server.hpp:48
Nick
Definition: Nick.hpp:7
ERR_NONICKNAMEGIVEN
#define ERR_NONICKNAMEGIVEN(servername)
Definition: Replies.hpp:118
Nick::execute
void execute()
Definition: Nick.hpp:61
Command::_needs_auth
bool _needs_auth
Definition: Command.hpp:15
ERR_ERRONEUSNICKNAME
#define ERR_ERRONEUSNICKNAME(servername, nick)
Definition: Replies.hpp:120
Command::_message
Message * _message
Definition: Command.hpp:21
ERR_NICKNAMEINUSE
#define ERR_NICKNAMEINUSE(servername, nick)
Definition: Replies.hpp:122
Command::_example
std::map< size_t, std::string > _example
Definition: Command.hpp:16
Client::setNick
void setNick(std::string const &nick)
Definition: Client.hpp:188
Nick::validate
bool validate(void)
Definition: Nick.hpp:20
Message::getParams
std::map< size_t, std::string > getParams(void) const
Definition: message.cpp:79
Command::_usage
std::string _usage
Definition: Command.hpp:13
Command::_sender
Client * _sender
Definition: Command.hpp:19
Nick::Nick
Nick()
Definition: Nick.hpp:10
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