IRC SERVER
The goal of this project is to make you write your own IRC server.
Channel.hpp
Go to the documentation of this file.
1 #ifndef CHANNEL_H
2 #define CHANNEL_H
3 
4 #include "./Client.hpp"
5 
7 {
8  CHANNEL_MODE_PRIVATE = 0, //+p OK
9  CHANNEL_MODE_SECRET = 1, //+s OK
15  CHANNEL_MODE_KEY = 7, //+k
18 };
19 
21 {
24 };
25 
26 class Channel
27 {
28  private:
29  std::string _name;
30  std::string _password;
31  Client * _creator;
32  time_t _created_at;
33  std::vector<Message *> _messages;
34  long long int _user_limit;
35  std::string _topic;
36  ChannelMode _mode;
37  // New mode implementation...
38  std::vector<ChannelMode> _modes;
39  std::vector<std::string> _ban_masks;
40  std::vector<Client *> _invite_list;
41 
42  public:
43  std::vector<Client *> _normal_clients;
44  std::vector<Client *> _voice_clients;
45  std::vector<Client *> _ope_clients;
46  std::vector<std::pair<Client *, ClientMode> > _clients;
47 
48  public:
49  Channel(std::string &name, std::string &password)
50  : _name(name), _password(password), _creator(NULL), _user_limit(MAX_CLIENTS_PER_CHANNEL)
51  {
52  _created_at = time(0);
53  if (password.size() > 0)
55  };
56 
57  // Setters
58  void setCreator(Client *creator)
59  {
60  if (_creator)
61  throw std::runtime_error("Creator is already assigned");
62  _creator = creator;
63  _ope_clients.push_back(creator);
64  };
65 
66  void setTopic(std::string &topic)
67  {
68  _topic = topic;
69  };
70 
71  void setPassword(std::string passwd)
72  {
73  _password = passwd;
74  };
75 
76  // Getter
77  std::string &getPassword(void)
78  {
79  return (_password);
80  }
81 
82  std::string &getName(void)
83  {
84  return (_name);
85  }
86 
87  std::string &getTopic(void)
88  {
89  return (_topic);
90  }
91 
93  {
94  return (_creator);
95  }
96 
97  time_t getCreatedAt(void)
98  {
99  return (_created_at);
100  }
101 
103  {
104  return (_mode);
105  }
106 
107  std::vector<ChannelMode> getModes(void)
108  {
109  return (_modes);
110  }
111 
113  {
114  char identifier = 0;
115  switch (_mode)
116  {
118  identifier = 'b';
119  break;
121  identifier = 'n';
122  break;
124  identifier = 'i';
125  break;
126  case CHANNEL_MODE_KEY:
127  identifier = 'k';
128  break;
130  identifier = 'm';
131  break;
133  identifier = 'o';
134  break;
136  identifier = 'p';
137  break;
138  case CHANNEL_MODE_SECRET:
139  identifier = 's';
140  break;
142  identifier = 't';
143  break;
145  identifier = 'l';
146  break;
147  }
148  return (identifier);
149  }
150 
151  std::string getStringModes(void)
152  {
153  std::string stringModes = "";
154  for (std::vector<ChannelMode>::const_iterator it = _modes.begin();
155  it != _modes.end(); ++it)
156  {
157  char identifier = getIdentifier(*it);
158  if (identifier)
159  stringModes += getIdentifier(*it);
160  }
161  return (stringModes);
162  }
163 
164  size_t getUserLimit(void)
165  {
166  return (_user_limit);
167  }
168  void setUserLimit(size_t limit)
169  {
170  _user_limit = limit;
171  }
172  std::vector<Client *> getClients(void) const
173  {
174  std::vector<Client *> clients;
175  clients.insert(clients.end(), _normal_clients.begin(), _normal_clients.end());
176  clients.insert(clients.end(), _ope_clients.begin(), _ope_clients.end());
177  return (clients);
178  }
179 
180  // --------------
181  // Client stuff
182  // --------------
183  bool joined(Client *client)
184  {
185  std::vector<Client *> clients = this->getClients();
186 
187  return (std::find(clients.begin(), clients.end(), client) != clients.end());
188  }
189 
190  bool isOwner(Client *client)
191  {
192  return (_creator == client);
193  }
194 
195  bool isOpe(Client *client)
196  {
197  return (std::find(_ope_clients.begin(), _ope_clients.end(), client) !=
198  _ope_clients.end());
199  }
200 
201  bool isNormal(Client *client)
202  {
203  return (std::find(_normal_clients.begin(), _normal_clients.end(), client) !=
204  _normal_clients.end());
205  }
206 
207  bool isVoice(Client *client)
208  {
209  return (std::find(_voice_clients.begin(), _voice_clients.end(), client) !=
210  _voice_clients.end());
211  }
212 
213  bool isBanned(Client *client)
214  {
215  for (std::vector<std::string>::iterator it = _ban_masks.begin();
216  it != _ban_masks.end(); ++it)
217  {
218  if (*it == client->getNick() || *it == client->getUsername() ||
219  *it == client->getHost())
220  return (true);
221  }
222  return (false);
223  }
224 
225  bool isInvited(Client *client)
226  {
227  return (std::find(_invite_list.begin(), _invite_list.end(), client) !=
228  _invite_list.end());
229  }
230 
231  bool isInviteOnly(void)
232  {
233  return (std::find(_modes.begin(), _modes.end(), CHANNEL_MODE_INVITE_ONLY) !=
234  _modes.end());
235  }
236 
237  bool isSecret(void)
238  {
239  return (std::find(_modes.begin(), _modes.end(), CHANNEL_MODE_SECRET) !=
240  _modes.end());
241  }
242 
243  void addOpe(Client *client)
244  {
245  std::vector<Client *>::iterator it =
246  std::find(_normal_clients.begin(), _normal_clients.end(), client);
247  if (it != _normal_clients.end())
248  _normal_clients.erase(it);
249  if (std::find(_ope_clients.begin(), _ope_clients.end(), client) ==
250  _ope_clients.end())
251  _ope_clients.push_back(client);
252  }
253 
254  void removeOpe(Client *client)
255  {
256  std::vector<Client *>::iterator it =
257  std::find(_ope_clients.begin(), _ope_clients.end(), client);
258  if (it != _ope_clients.end())
259  _ope_clients.erase(it);
260  if (std::find(_normal_clients.begin(), _normal_clients.end(), client) ==
261  _normal_clients.end())
262  _normal_clients.push_back(client);
263  }
264 
265  void kick(Client *client)
266  {
267  std::vector<Client *>::iterator it =
268  std::find(_normal_clients.begin(), _normal_clients.end(), client);
269  if (it != _normal_clients.end())
270  _normal_clients.erase(it);
271  it = std::find(_ope_clients.begin(), _ope_clients.end(), client);
272  if (it != _ope_clients.end())
273  _ope_clients.erase(it);
274  it = std::find(_voice_clients.begin(), _voice_clients.end(), client);
275  if (it != _voice_clients.end())
276  _voice_clients.erase(it);
277  }
278 
279  void invite(Client *client)
280  {
281  if (std::find(_invite_list.begin(), _invite_list.end(), client) ==
282  _invite_list.end())
283  this->_invite_list.push_back(client);
284  }
285 
286  void removeInvite(Client *client)
287  {
288  std::vector<Client *>::iterator it =
289  std::find(_invite_list.begin(), _invite_list.end(), client);
290  if (it != _invite_list.end())
291  _invite_list.erase(it);
292  }
293 
294  void addBanMask(std::string mask)
295  {
296  if (std::find(_ban_masks.begin(), _ban_masks.end(), mask) == _ban_masks.end())
297  this->_ban_masks.push_back(mask);
298  }
299 
300  void removeBanMask(std::string mask)
301  {
302  std::vector<std::string>::iterator it =
303  std::find(_ban_masks.begin(), _ban_masks.end(), mask);
304  if (it != _ban_masks.end())
305  _ban_masks.erase(it);
306  }
307 
308  // --------------
309  // Utils
310  // --------------
311  std::string getModeString(void)
312  {
313  std::string mode;
314  for (size_t i = 0; i < _modes.size(); i++)
315  {
316  if (_modes[i] == CHANNEL_MODE_PRIVATE)
317  mode += "*";
318  else if (_modes[i] == CHANNEL_MODE_SECRET)
319  mode += "@";
320  else if (_modes[i] == CHANNEL_MODE_INVITE_ONLY)
321  mode += "!";
322  else if (_modes[i] == CHANNEL_MODE_MODERATED)
323  mode += "%";
325  mode += "+";
326  else if (_modes[i] == CHANNEL_MODE_CANT_SENT_MESSAGES_OUTSIDE)
327  mode += "~";
328  else if (_modes[i] == CHANNEL_MODE_BAN_MASK)
329  mode += "&";
330  else if (_modes[i] == CHANNEL_MODE_KEY)
331  mode += "!";
332  }
333  std::cout << "MODE: " << mode << std::endl;
334  return (mode);
335  }
336 
337  bool hasMode(ChannelMode mode)
338  {
339  return std::find(this->_modes.begin(), this->_modes.end(), mode) !=
340  this->_modes.end();
341  }
342 
343  void addMode(ChannelMode mode)
344  {
345  if (!this->hasMode(mode))
346  this->_modes.push_back(mode);
347  }
348 
350  {
351  std::vector<ChannelMode>::iterator it = std::find(_modes.begin(), _modes.end(), mode);
352  if (it != _modes.end())
353  _modes.erase(it);
354  }
355 
356  std::string getClientRoleString(Client *client) // TODO: Can a user have multiple roles?
357  {
358  if (this->isOpe(client))
359  return ("@");
360  if (this->isVoice(client))
361  return ("+");
362  return ("");
363  }
364 
366  {
367  std::vector<Client *> related_channels_clients = getClients();
368  for (size_t j = 0; j < related_channels_clients.size(); j++)
369  {
370  if (related_channels_clients[j] == client)
371  {
372  if (isOpe(related_channels_clients[j]))
373  _ope_clients.erase(
374  std::find(_ope_clients.begin(), _ope_clients.end(), client));
375  else
376  {
377  _normal_clients.erase(
378  std::find(_normal_clients.begin(), _normal_clients.end(), client));
379  }
380  }
381  }
382  }
383 
384  void broadcastMessage(std::string message)
385  {
386  std::vector<Client *> clients = this->getClients();
387  for (size_t i = 0; i < clients.size(); i++)
388  {
389  clients[i]->message(message);
390  }
391  }
392 
393  bool isModerated(void)
394  {
395  return (std::find(_modes.begin(), _modes.end(), CHANNEL_MODE_MODERATED) !=
396  _modes.end());
397  }
398 
399  bool hasKey(void)
400  {
401  return (std::find(_modes.begin(), _modes.end(), CHANNEL_MODE_KEY) !=
402  _modes.end());
403  }
404 };
405 
406 #endif
Channel::removeInvite
void removeInvite(Client *client)
Definition: Channel.hpp:286
Channel::setPassword
void setPassword(std::string passwd)
Definition: Channel.hpp:71
Channel
Definition: Channel.hpp:26
Channel::getIdentifier
char getIdentifier(ChannelMode _mode)
Definition: Channel.hpp:112
Channel::getName
std::string & getName(void)
Definition: Channel.hpp:82
Channel::getModes
std::vector< ChannelMode > getModes(void)
Definition: Channel.hpp:107
Channel::isSecret
bool isSecret(void)
Definition: Channel.hpp:237
ClientMode
ClientMode
Definition: Channel.hpp:20
Channel::isOpe
bool isOpe(Client *client)
Definition: Channel.hpp:195
Channel::isInvited
bool isInvited(Client *client)
Definition: Channel.hpp:225
Channel::Channel
Channel(std::string &name, std::string &password)
Definition: Channel.hpp:49
CHANNEL_MODE_OPERATOR
@ CHANNEL_MODE_OPERATOR
Definition: Channel.hpp:17
ChannelMode
ChannelMode
Definition: Channel.hpp:6
Channel::kick
void kick(Client *client)
Definition: Channel.hpp:265
Channel::getTopic
std::string & getTopic(void)
Definition: Channel.hpp:87
Client::getNick
const std::string getNick(void) const
Definition: Client.hpp:142
CHANNEL_MODE_TOPIC_SETTABLE_BY_CHANNEL_OPERATOR_ONLY
@ CHANNEL_MODE_TOPIC_SETTABLE_BY_CHANNEL_OPERATOR_ONLY
Definition: Channel.hpp:12
Channel::getClientRoleString
std::string getClientRoleString(Client *client)
Definition: Channel.hpp:356
Client::getHost
const std::string getHost(void) const
Definition: Client.hpp:157
CHANNEL_MODE_SECRET
@ CHANNEL_MODE_SECRET
Definition: Channel.hpp:9
Channel::isVoice
bool isVoice(Client *client)
Definition: Channel.hpp:207
Channel::getCreatedAt
time_t getCreatedAt(void)
Definition: Channel.hpp:97
CHANNEL_MODE_KEY
@ CHANNEL_MODE_KEY
Definition: Channel.hpp:15
Channel::getStringModes
std::string getStringModes(void)
Definition: Channel.hpp:151
Channel::removeMode
void removeMode(ChannelMode mode)
Definition: Channel.hpp:349
Channel::invite
void invite(Client *client)
Definition: Channel.hpp:279
CLIENT_MODE_OPERATOR
@ CLIENT_MODE_OPERATOR
Definition: Channel.hpp:23
Channel::addBanMask
void addBanMask(std::string mask)
Definition: Channel.hpp:294
Channel::_voice_clients
std::vector< Client * > _voice_clients
Definition: Channel.hpp:44
Channel::_clients
std::vector< std::pair< Client *, ClientMode > > _clients
Definition: Channel.hpp:46
Channel::setCreator
void setCreator(Client *creator)
Definition: Channel.hpp:58
CHANNEL_MODE_MODERATED
@ CHANNEL_MODE_MODERATED
Definition: Channel.hpp:11
Channel::_normal_clients
std::vector< Client * > _normal_clients
Definition: Channel.hpp:43
CHANNEL_MODE_PRIVATE
@ CHANNEL_MODE_PRIVATE
Definition: Channel.hpp:8
Channel::isInviteOnly
bool isInviteOnly(void)
Definition: Channel.hpp:231
Channel::getCreator
Client * getCreator(void)
Definition: Channel.hpp:92
Client::getUsername
const std::string getUsername(void) const
Definition: Client.hpp:147
Channel::getPassword
std::string & getPassword(void)
Definition: Channel.hpp:77
CHANNEL_MODE_CANT_SENT_MESSAGES_OUTSIDE
@ CHANNEL_MODE_CANT_SENT_MESSAGES_OUTSIDE
Definition: Channel.hpp:13
Channel::isBanned
bool isBanned(Client *client)
Definition: Channel.hpp:213
Channel::isOwner
bool isOwner(Client *client)
Definition: Channel.hpp:190
CHANNEL_MODE_BAN_MASK
@ CHANNEL_MODE_BAN_MASK
Definition: Channel.hpp:14
MAX_CLIENTS_PER_CHANNEL
#define MAX_CLIENTS_PER_CHANNEL
Definition: config.hpp:10
Channel::removeClientFromChannel
void removeClientFromChannel(Client *client)
Definition: Channel.hpp:365
Channel::hasKey
bool hasKey(void)
Definition: Channel.hpp:399
Channel::removeBanMask
void removeBanMask(std::string mask)
Definition: Channel.hpp:300
Channel::removeOpe
void removeOpe(Client *client)
Definition: Channel.hpp:254
Channel::setUserLimit
void setUserLimit(size_t limit)
Definition: Channel.hpp:168
Channel::getModeString
std::string getModeString(void)
Definition: Channel.hpp:311
Channel::broadcastMessage
void broadcastMessage(std::string message)
Definition: Channel.hpp:384
Channel::getMode
ChannelMode getMode(void)
Definition: Channel.hpp:102
Channel::getClients
std::vector< Client * > getClients(void) const
Definition: Channel.hpp:172
Channel::_ope_clients
std::vector< Client * > _ope_clients
Definition: Channel.hpp:45
Channel::joined
bool joined(Client *client)
Definition: Channel.hpp:183
Channel::getUserLimit
size_t getUserLimit(void)
Definition: Channel.hpp:164
CHANNEL_MODE_USER_LIMIT
@ CHANNEL_MODE_USER_LIMIT
Definition: Channel.hpp:16
Channel::setTopic
void setTopic(std::string &topic)
Definition: Channel.hpp:66
Channel::isNormal
bool isNormal(Client *client)
Definition: Channel.hpp:201
Client
Definition: Client.hpp:22
CLIENT_MODE_VOICE
@ CLIENT_MODE_VOICE
Definition: Channel.hpp:22
Channel::hasMode
bool hasMode(ChannelMode mode)
Definition: Channel.hpp:337
Channel::isModerated
bool isModerated(void)
Definition: Channel.hpp:393
CHANNEL_MODE_INVITE_ONLY
@ CHANNEL_MODE_INVITE_ONLY
Definition: Channel.hpp:10
Client.hpp
Channel::addOpe
void addOpe(Client *client)
Definition: Channel.hpp:243
Channel::addMode
void addMode(ChannelMode mode)
Definition: Channel.hpp:343