IRC SERVER
The goal of this project is to make you write your own IRC server.
PrivMsg.hpp
Go to the documentation of this file.
1 #ifndef PRIVMSG_H
2 #define PRIVMSG_H
3 
4 // Our includes
5 #include "../Command.hpp"
6 
7 class PrivMsg : public Command
8 {
9  public:
11  {
12  _name = "privmsg";
13  _description = "Send message to a set of users/channels";
14  _usage = "privmsg <receptor|channel>{,<receptor|channel>} :<texto>";
15  _example[0] = "privmsg alfred hello";
16  _example[1] = "privmsg alfred,manolo hello";
17  _example[2] = "privmsg #uruguay :hola, buenas tardes";
18  _example[3] = "privmsg #uruguay,#peru :hola, buenas tardes";
19  }
20 
21  bool validate(void)
22  {
23  std::map<size_t, std::string> p = _message->getParams();
24  if (p.size() < 2)
25  {
27  _message->getCmd()));
28  return (false);
29  }
30  else if (p[0].at(0) == '#')
31  {
32  std::vector<std::string> _ch_params = split(p[0], ",");
33  for (size_t i = 0; i < _ch_params.size(); i++)
34  {
35  Channel *channel = _server->getChannel(_ch_params[i]);
36  if (channel == NULL)
37  {
39  ERR_NOSUCHCHANNEL(_sender->_servername, _sender->getNick(), _ch_params[i]));
40  return (false);
41  }
42  else if ((channel == NULL ||
43  (channel->joined(_sender) == false && !channel->hasMode(CHANNEL_MODE_CANT_SENT_MESSAGES_OUTSIDE)) ||
44  (channel->isModerated() && channel->isOpe(_sender) == false)))
45 
46  {
48  _sender->_servername, _sender->_nick, channel->getName()));
49  return (false);
50  }
51  }
52  return (true);
53  }
54  else
55  {
56  std::vector<std::string> _cl_params = split(p[0], ",");
57  for (size_t i = 0; i < _cl_params.size(); i++)
58  {
59  if (_server->getClient(_cl_params[i]) == NULL)
60  {
62  return (false);
63  }
64  else if (_sender->_nick == _cl_params[i])
65  {
67  std::string("You cannot send a message yourself!\n").c_str());
68  return (false);
69  }
70  }
71  }
72  return (true);
73  }
74 
75  std::vector<Message> parser(Message *message)
76  {
77  std::map<size_t, std::string> p = message->getParams();
78  if (p.size() < 2)
79  {
80  throw std::runtime_error(
82  }
83 
84  std::string msg = std::string(p[1] + "\n").c_str();
85  std::vector<std::string> _ch_params = split(p[0], ",");
86 
87  std::vector<Message> messages;
88  for (size_t i = 0; i < _ch_params.size(); i++)
89  {
90  std::string tmp(this->getName() + " " + _ch_params[i] + " :" + msg);
91  std::cout << tmp << std::endl;
92  messages.push_back(Message(tmp));
93  }
94  return (messages);
95  }
96 
97  void execute()
98  {
99  std::map<size_t, std::string> p = _message->getParams();
100  std::string msg = std::string(p[1] + "\n").c_str();
101  if (p[0].at(0) == '#')
102  {
103  std::vector<std::string> _ch_params = split(p[0], ",");
104  for (size_t i = 0; i < _ch_params.size(); i++)
105  {
106  std::string name = _ch_params[i].substr(1).c_str();
107  Channel * channel = _server->getChannel(name);
108  std::vector<Client *> clients = channel->getClients();
109  for (size_t j = 0; j < clients.size(); j++)
110  {
111  if (clients[j] != _sender)
112  {
113  clients[j]->message(std::string(":" + _sender->_nick + " PRIVMSG #" +
114  channel->getName() + " :" + msg + "\n")
115  .c_str());
116  }
117  }
118  }
119  }
120  else
121  {
122  std::vector<std::string> _cl_params = split(p[0], ",");
123  for (size_t i = 0; i < _cl_params.size(); i++)
124  {
125  Client *client = _server->getClient(_cl_params[i]);
126  client->message(
127  std::string(":" + _sender->_nick + " PRIVMSG " + client->getNick() + " :" + msg + "\n")
128  .c_str());
129  }
130  }
131  }
132 };
133 
134 #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
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
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
PrivMsg::PrivMsg
PrivMsg()
Definition: PrivMsg.hpp:10
ERR_CANNOTSENDTOCHAN
#define ERR_CANNOTSENDTOCHAN(servername, nick, channel)
Definition: Replies.hpp:109
ERR_NOSUCHNICK
#define ERR_NOSUCHNICK(servername, nick)
Definition: Replies.hpp:104
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_NEEDMOREPARAMS
#define ERR_NEEDMOREPARAMS(servername, nick, command)
Definition: Replies.hpp:131
CHANNEL_MODE_CANT_SENT_MESSAGES_OUTSIDE
@ CHANNEL_MODE_CANT_SENT_MESSAGES_OUTSIDE
Definition: Channel.hpp:13
Command::_example
std::map< size_t, std::string > _example
Definition: Command.hpp:16
PrivMsg::validate
bool validate(void)
Definition: PrivMsg.hpp:21
Command::getName
std::string getName() const
Definition: Command.hpp:25
PrivMsg::parser
std::vector< Message > parser(Message *message)
Definition: PrivMsg.hpp:75
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
Message
Definition: Message.hpp:12
PrivMsg::execute
void execute()
Definition: PrivMsg.hpp:97
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
Channel::hasMode
bool hasMode(ChannelMode mode)
Definition: Channel.hpp:337
Command::_sender
Client * _sender
Definition: Command.hpp:19
PrivMsg
Definition: PrivMsg.hpp:7
Client::_nick
std::string _nick
Definition: Client.hpp:26
Channel::isModerated
bool isModerated(void)
Definition: Channel.hpp:393
Client::_servername
std::string _servername
Definition: Client.hpp:30
Command::_server
Server * _server
Definition: Command.hpp:20