IRC SERVER
The goal of this project is to make you write your own IRC server.
Notice.hpp
Go to the documentation of this file.
1 #ifndef NOTICE_H
2 #define NOTICE_H
3 
4 // Our includes
5 #include "../Command.hpp"
6 
7 class Notice : public Command
8 {
9  public:
11  {
12  _name = "notice";
13  _description = "Send message to a set of users/channels";
14  _usage = "notice <receptor|channel>{,<receptor|channel>} :<texto>";
15  _example[0] = "notice alfred hello";
16  _example[1] = "notice alfred,manolo hello";
17  _example[2] = "notice #uruguay :hola, buenas tardes";
18  _example[3] = "notice #uruguay,#peru :hola, buenas tardes";
19  }
20 
21  /*
22  The NOTICE message is used similarly to PRIVMSG. The difference
23  between NOTICE and PRIVMSG is that automatic replies must never be
24  sent in response to a NOTICE message.
25  */
26 
27  bool validate(void)
28  {
29  std::map<size_t, std::string> p = _message->getParams();
30  if (p.size() < 2)
31  {
32  return (false);
33  }
34  else if (p[0].at(0) == '#')
35  {
36  std::vector<std::string> _ch_params = split(p[0], ",");
37  for (size_t i = 1; i < _ch_params.size(); i++)
38  {
39  Channel *channel = _server->getChannel(_ch_params[i]);
40  if (channel == NULL || channel->joined(_sender) == false ||
41  (channel->isModerated() && channel->isOpe(_sender) == false))
42  {
43  return (false);
44  }
45  }
46  return (true);
47  }
48  else
49  {
50  std::vector<std::string> _cl_params = split(p[0], ",");
51  for (size_t i = 0; i < _cl_params.size(); i++)
52  {
53  if (_server->getClient(_cl_params[i]) == NULL ||
54  _cl_params[i] == _sender->_nick)
55  {
56  return (false);
57  }
58  }
59  }
60  return (true);
61  }
62 
63  void execute()
64  {
65  std::map<size_t, std::string> p = _message->getParams();
66  std::string msg = std::string(p[1] + "\n").c_str();
67  if (p[0].at(0) == '#')
68  {
69  std::vector<std::string> _ch_params = split(p[0], ",");
70  for (size_t i = 0; i < _ch_params.size(); i++)
71  {
72  std::string name = _ch_params[i].substr(1).c_str();
73  Channel * channel = _server->getChannel(name);
74  std::vector<Client *> clients = channel->getClients();
75  for (size_t j = 0; j < clients.size(); j++)
76  {
77  if (clients[j] != _sender)
78  {
79  clients[j]->message(std::string(":" + _sender->_nick + " NOTICE #" +
80  channel->getName() + " :" + msg + "\n")
81  .c_str());
82  }
83  }
84  }
85  }
86  else
87  {
88  std::vector<std::string> _cl_params = split(p[0], ",");
89  for (size_t i = 0; i < _cl_params.size(); i++)
90  {
91  Client *client = _server->getClient(_cl_params[i]);
92  client->message(
93  std::string(":" + _sender->_nick + " NOTICE " + client->getNick() + " :" + msg + "\n")
94  .c_str());
95  }
96  }
97  }
98 };
99 
100 #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
Command::_description
std::string _description
Definition: Command.hpp:12
Notice::Notice
Notice()
Definition: Notice.hpp:10
Notice::validate
bool validate(void)
Definition: Notice.hpp:27
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
Client::message
void message(char const *message)
Definition: client.cpp:15
Notice::execute
void execute()
Definition: Notice.hpp:63
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
Notice
Definition: Notice.hpp:7
Command::_example
std::map< size_t, std::string > _example
Definition: Command.hpp:16
Server::getClient
Client * getClient(std::string const &name)
Definition: Server.hpp:140
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
Client
Definition: Client.hpp:22
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
Channel::isModerated
bool isModerated(void)
Definition: Channel.hpp:393
Command::_server
Server * _server
Definition: Command.hpp:20