IRC SERVER
The goal of this project is to make you write your own IRC server.
Topic.hpp
Go to the documentation of this file.
1 #ifndef TOPIC_H
2 #define TOPIC_H
3 
4 // Our includes
5 #include "../Command.hpp"
6 
7 class Topic : public Command
8 {
9  public:
11  {
12  _name = "topic";
13  _description = "Allows the client to query or set the channel topic on "
14  "channel";
15  _usage = "topic";
16  _example[0] = "TOPIC #channel1 :another topic";
17  _example[1] = "TOPIC #channel1";
18  }
19 
20  bool validate(void)
21  {
22  std::map<size_t, std::string> p = _message->getParams();
23  if (p.size() < 1)
24  {
26  _message->getCmd())); // ERR_NEEDMOREPARAMS (461)
27  return (false);
28  }
29 
30  Channel *channel = _server->getChannel(p[0]);
31  if (channel)
32  {
33  if (!channel->joined(_sender))
34  {
36  return (false);
37  }
38  else if (!channel->isOpe(_sender) && channel->hasMode(CHANNEL_MODE_TOPIC_SETTABLE_BY_CHANNEL_OPERATOR_ONLY) && p.size() > 1)
39  {
42  return (false);
43  }
44  }
45  else
46  {
48  return (false);
49  }
50  return (true);
51  }
52 
53  void execute()
54  {
55  std::map<size_t, std::string> p = _message->getParams();
56  Channel * channel = _server->getChannel(p[0]);
57 
58  if (p.size() == 1)
59  {
60  std::string topic = channel->getTopic();
61  if (topic.empty())
64  else
66  channel->getName(), channel->getTopic()));
67  }
68  else
69  {
70  channel->setTopic(p[1]);
71 
72  std::vector<Client *> related_channels_clients = channel->getClients();
73  for (size_t j = 0; j < related_channels_clients.size(); j++)
74  {
75  related_channels_clients[j]->message(
76  RPL_TOPIC(_sender->_servername, related_channels_clients[j]->_nick,
77  channel->getName(), channel->getTopic()));
78  }
79  }
80  }
81 };
82 #endif
Channel
Definition: Channel.hpp:26
Channel::getName
std::string & getName(void)
Definition: Channel.hpp:82
RPL_NOTOPIC
#define RPL_NOTOPIC(servername, nick, channel)
Definition: Replies.hpp:69
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
Channel::getTopic
std::string & getTopic(void)
Definition: Channel.hpp:87
Command::_name
std::string _name
Definition: Command.hpp:11
CHANNEL_MODE_TOPIC_SETTABLE_BY_CHANNEL_OPERATOR_ONLY
@ CHANNEL_MODE_TOPIC_SETTABLE_BY_CHANNEL_OPERATOR_ONLY
Definition: Channel.hpp:12
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
Topic::validate
bool validate(void)
Definition: Topic.hpp:20
Command::_message
Message * _message
Definition: Command.hpp:21
Topic::execute
void execute()
Definition: Topic.hpp:53
ERR_CHANOPRIVSNEEDED
#define ERR_CHANOPRIVSNEEDED(servername, nick, channel)
Definition: Replies.hpp:154
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
Topic::Topic
Topic()
Definition: Topic.hpp:10
ERR_NOTONCHANNEL
#define ERR_NOTONCHANNEL(servername, nick, channel)
Definition: Replies.hpp:126
RPL_TOPIC
#define RPL_TOPIC(servername, nick, channel, topic)
Definition: Replies.hpp:71
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
Topic
Definition: Topic.hpp:7
Channel::setTopic
void setTopic(std::string &topic)
Definition: Channel.hpp:66
Server::getChannel
Channel * getChannel(std::string &name)
Definition: Server.hpp:202
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::_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