IRC SERVER
The goal of this project is to make you write your own IRC server.
Mode.hpp
Go to the documentation of this file.
1 #ifndef MODE_H
2 #define MODE_H
3 
4 // Our includes
5 #include "../Command.hpp"
6 
7 enum ModeSign
8 {
9  PLUS = 0,
10  MINUS = 1
11 };
12 
14 {
15  public:
20  std::map<size_t, std::string> _params;
23 
24  public:
26 
27  void execute()
28  {
29  if (_sign == PLUS)
30  add();
31  else
32  remove();
33 
36  (_sign == PLUS ? "+" : "-") + _channel->getIdentifier(_mode) + " " + _params[2]));
37  }
38 
39  void setMode(ChannelMode mode)
40  {
41  _mode = mode;
42  }
43 
44  void setSign(ModeSign sign)
45  {
46  _sign = sign;
47  }
48 
49  void setChannel(Channel *channel)
50  {
51  _channel = channel;
52  }
53 
54  void setParams(std::map<size_t, std::string> params)
55  {
56  _params = params;
57  }
58 
59  void setSender(Client *sender)
60  {
61  _sender = sender;
62  }
63 
64  void setServer(Server *server)
65  {
66  _server = server;
67  }
68 
69  virtual bool validate(void)
70  {
71  return (true);
72  }
73  virtual void add() = 0;
74  virtual void remove() = 0;
75 
76  virtual ~ChannelModeType()
77  {
78  }
79 };
80 
81 #include "./mode/BanMask.hpp"
83 #include "./mode/InviteOnly.hpp"
84 #include "./mode/Key.hpp"
85 #include "./mode/Moderated.hpp"
86 #include "./mode/Operator.hpp"
87 #include "./mode/Secret.hpp"
89 #include "./mode/UserLimit.hpp"
90 
91 class Mode : public Command
92 {
93  private:
94  enum Type
95  {
96  USER_MODE = 0,
97  CHANNEL_MODE = 1
98  };
99 
100  private:
101  Type getModelType(void)
102  {
103  std::map<size_t, std::string> target = _message->getParams();
104 
105  return target[0].at(0) == '#' ? CHANNEL_MODE : USER_MODE;
106  }
107 
108  std::vector<ChannelModeType *> parseChannelMode()
109  {
110  std::map<size_t, std::string> params = _message->getParams();
111  Channel * channel = _server->getChannel(params[0]);
112  std::string modeParams = params[1];
113  ModeSign sign = modeParams[0] == '+' ? PLUS : MINUS;
114  std::vector<ChannelModeType *> modes;
115 
116  for (size_t i = 1; i < modeParams.size(); i++)
117  {
118  ChannelModeType *mode = NULL;
119  switch (modeParams[i])
120  {
121  case 'o':
122  mode = new Operator();
123  break;
124  case 'p':
125  mode = new CantSentMessagesOutside();
126  break;
127  case 's':
128  mode = new Secret();
129  break;
130  case 'i':
131  mode = new InviteOnly();
132  break;
133  case 'm':
134  mode = new Moderated();
135  break;
136  case 't':
138  break;
139  case 'n':
140  mode = new CantSentMessagesOutside();
141  break;
142  case 'b':
143  mode = new BanMask();
144  break;
145  case 'k':
146  mode = new Key();
147  break;
148  case 'l':
149  mode = new UserLimit();
150  break;
151  default:
152  break;
153  }
154  if (mode != NULL)
155  {
156  mode->setSign(sign);
157  mode->setParams(params);
158  mode->setChannel(&(*channel));
159  mode->setSender(&(*_sender));
160  mode->setServer(&(*_server));
161  modes.push_back(mode);
162  }
163  }
164  return (modes);
165  }
166 
167  public:
169  {
170  _name = "mode";
171  _description = "add or remove a mode to a channel or user";
172  _usage = "mode <canal> {[+|-]|o|p|s|i|t|n|b|v} [<límite>] "
173  "[<usuario>] [<máscara de ban>]";
174  _example[0] = "mode #canal +o nick";
175  _example[1] = "mode #canal +l 10";
176  _example[1] = "mode #canal +ts";
177  _example[2] = "mode roberto +it";
178  //_is_ope = true;
179  }
180 
181  bool validate()
182  {
183  std::map<size_t, std::string> params = _message->getParams();
184 
185  if (params.size() < 1)
186  {
188  return (false);
189  }
190  Channel *_channel = _server->getChannel(params[0]);
191  if (_channel == NULL)
192  {
194  return (false);
195  }
196  else if (params.size() > 2 && _channel->isOpe(_sender) == false)
197  {
199  _channel->getName()));
200  return (false);
201  }
202 
203  std::vector<ChannelModeType *> modes = parseChannelMode();
204  for (size_t i = 0; i < modes.size(); i++)
205  {
206  if (modes[i]->validate() == false)
207  {
208  delete modes[i];
209  return (false);
210  }
211  delete modes[i];
212  }
213  modes.clear();
214  return (true);
215  }
216 
217  void execute()
218  {
219  std::map<size_t, std::string> params = _message->getParams();
220 
221  if (params.size() == 1)
222  {
223  Channel *channel = _server->getChannel(params[0]);
225  channel->getName(), channel->getStringModes()));
226  return;
227  }
228  else
229  {
230  Type type = getModelType();
231  if (type == CHANNEL_MODE)
232  {
233  std::vector<ChannelModeType *> modes = parseChannelMode();
234  std::vector<ChannelModeType *>::iterator it;
235 
236  for (size_t i = 0; i < modes.size(); i++)
237  {
238  modes[i]->execute();
239  delete modes[i];
240  }
241 
242  modes.clear();
243  }
244  else if (type == USER_MODE)
245  {
246  }
247  }
248  }
249 };
250 #endif
Channel
Definition: Channel.hpp:26
Key.hpp
Channel::getIdentifier
char getIdentifier(ChannelMode _mode)
Definition: Channel.hpp:112
Channel::getName
std::string & getName(void)
Definition: Channel.hpp:82
MINUS
@ MINUS
Definition: Mode.hpp:10
ChannelModeType::setChannel
void setChannel(Channel *channel)
Definition: Mode.hpp:49
ChannelModeType::_sender
Client * _sender
Definition: Mode.hpp:21
Channel::isOpe
bool isOpe(Client *client)
Definition: Channel.hpp:195
ERR_NOSUCHCHANNEL
#define ERR_NOSUCHCHANNEL(servername, nick, channel)
Definition: Replies.hpp:107
PLUS
@ PLUS
Definition: Mode.hpp:9
Command::_description
std::string _description
Definition: Command.hpp:12
ChannelMode
ChannelMode
Definition: Channel.hpp:6
ChannelModeType::ChannelModeType
ChannelModeType()
Definition: Mode.hpp:25
ChannelModeType::setSender
void setSender(Client *sender)
Definition: Mode.hpp:59
TopicSettableByChannelOperatorOnly.hpp
ChannelModeType::add
virtual void add()=0
Command::_name
std::string _name
Definition: Command.hpp:11
ChannelModeType::setServer
void setServer(Server *server)
Definition: Mode.hpp:64
ChannelModeType::~ChannelModeType
virtual ~ChannelModeType()
Definition: Mode.hpp:76
Command
Definition: Command.hpp:7
ChannelModeType::_mode
ChannelMode _mode
Definition: Mode.hpp:16
ChannelModeType::validate
virtual bool validate(void)
Definition: Mode.hpp:69
Client::message
void message(char const *message)
Definition: client.cpp:15
BanMask
Definition: BanMask.hpp:7
Channel::getStringModes
std::string getStringModes(void)
Definition: Channel.hpp:151
RPL_CHANNELMODEIS
#define RPL_CHANNELMODEIS(servername, nick, channel, modes)
Definition: Replies.hpp:65
Mode::execute
void execute()
Definition: Mode.hpp:217
Secret
Definition: Secret.hpp:7
ChannelModeType::execute
void execute()
Definition: Mode.hpp:27
ChannelModeType::_params
std::map< size_t, std::string > _params
Definition: Mode.hpp:20
ChannelModeType
Definition: Mode.hpp:13
Mode::Mode
Mode()
Definition: Mode.hpp:168
InviteOnly
Definition: InviteOnly.hpp:7
ChannelModeType::remove
virtual void remove()=0
Command::_message
Message * _message
Definition: Command.hpp:21
UserLimit
Definition: UserLimit.hpp:7
CantSentMessagesOutside.hpp
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
Moderated.hpp
ChannelModeType::setSign
void setSign(ModeSign sign)
Definition: Mode.hpp:44
Operator
Definition: Operator.hpp:7
Moderated
Definition: Moderated.hpp:7
ModeSign
ModeSign
Definition: Mode.hpp:7
Mode
Definition: Mode.hpp:91
ChannelModeType::_identifier
char _identifier
Definition: Mode.hpp:17
ChannelModeType::setParams
void setParams(std::map< size_t, std::string > params)
Definition: Mode.hpp:54
Channel::broadcastMessage
void broadcastMessage(std::string message)
Definition: Channel.hpp:384
TopicSettableByChannelOperatorOnly
Definition: TopicSettableByChannelOperatorOnly.hpp:7
Operator.hpp
InviteOnly.hpp
Secret.hpp
ChannelModeType::_channel
Channel * _channel
Definition: Mode.hpp:19
UserLimit.hpp
Message::getParams
std::map< size_t, std::string > getParams(void) const
Definition: message.cpp:79
CantSentMessagesOutside
Definition: CantSentMessagesOutside.hpp:7
Server::getChannel
Channel * getChannel(std::string &name)
Definition: Server.hpp:202
Client
Definition: Client.hpp:22
BanMask.hpp
Command::_usage
std::string _usage
Definition: Command.hpp:13
RPL_CUSTOM_MODE
#define RPL_CUSTOM_MODE(user, channel, mode)
Definition: Replies.hpp:190
Client::getUserId
const std::string getUserId(void) const
Definition: Client.hpp:162
Command::_sender
Client * _sender
Definition: Command.hpp:19
Key
Definition: Key.hpp:7
ChannelModeType::setMode
void setMode(ChannelMode mode)
Definition: Mode.hpp:39
Server
Definition: Server.hpp:32
Client::_nick
std::string _nick
Definition: Client.hpp:26
ChannelModeType::_server
Server * _server
Definition: Mode.hpp:22
Client::_servername
std::string _servername
Definition: Client.hpp:30
Mode::validate
bool validate()
Definition: Mode.hpp:181
Command::_server
Server * _server
Definition: Command.hpp:20
ChannelModeType::_sign
ModeSign _sign
Definition: Mode.hpp:18