IRC SERVER
The goal of this project is to make you write your own IRC server.
Client.hpp
Go to the documentation of this file.
1 #ifndef CLIENT_H
2 #define CLIENT_H
3 
4 // Main libraries
5 #include <algorithm>
6 #include <fstream>
7 #include <netdb.h>
8 #include <unistd.h>
9 
10 // Our includes
11 #include "./Message.hpp"
12 #include "./Replies.hpp"
13 #include "./config.hpp"
14 
16 {
20 };
21 
22 class Client
23 {
24  public:
25  int _fd;
26  std::string _nick;
27  std::string _username;
28  std::string _realname;
29  std::string _host;
30  std::string _servername;
31 
32  std::string _serverversion;
33  std::vector<Message> _messagesSent;
34  std::vector<Message> _messagesReceived;
37  bool _is_ope;
38  int const _maxChannels;
39  int _attempts;
42  std::vector<UserMode> _modes;
43 
44  public:
45  Client(int &fd, std::string host, std::string servername, std::string version)
46  : _fd(fd), _host(host), _servername(servername), _serverversion(version), _is_authenticated(false), _is_passLogged(false), _is_ope(false), _maxChannels(MAX_CHANNELS), _attempts(0), _status(ALIVE){
47  // message(RPL_WELCOME(_servername, _nick));
48  };
50  {
51  close(_fd);
52  _fd = -1;
53  std::cout << C_RED << "Client " << _nick << " disconnected" C_X << std::endl;
54  };
55 
56  // Helpers
57  void message(char const *message);
58  void message(std::string const message);
59  void read(void);
60  void disconnect(void)
61  {
62  delete this;
63  }
64  void ban(void)
65  {
67  disconnect();
68  }
69  std::vector<std::string> readConfigFile(char const *filename)
70  {
71  std::vector<std::string> motd;
72  std::ifstream data;
73  data.open(filename);
74  if (data.is_open())
75  {
76  std::string line;
77  while (std::getline(data, line))
78  {
79  motd.push_back(line);
80  }
81  data.close();
82  }
83  return motd;
84  }
85 
86  std::string getSupportString(void)
87  {
88  std::string support = "";
89  std::vector<std::string> motd = readConfigFile("./config/support");
90 
91  for (std::vector<std::string>::iterator it = motd.begin(); it != motd.end(); ++it)
92  {
93  support += *it + " ";
94  }
95  return support.substr(0, support.size() - 1);
96  }
97 
98  void authenticate(void)
99  {
100  _is_authenticated = true;
107  std::vector<std::string> motd = readConfigFile("./config/motd");
108  for (std::vector<std::string>::iterator it = motd.begin(); it != motd.end(); ++it)
109  {
111  }
113  };
114  void loginFail(void)
115  {
116  _attempts++;
117  if (_attempts >= MAX_ATTEMPTS)
118  ban();
119  };
120 
121  // Getters
122  std::vector<Message> messagesSent() const
123  {
124  return _messagesSent;
125  };
126 
127  std::vector<Message> messagesReceived() const
128  {
129  return _messagesReceived;
130  };
131 
132  bool isAuthenticated(void) const
133  {
134  return _is_authenticated;
135  };
136 
137  bool canPerformPassCommand(void) const
138  {
139  return (_nick != "" && _username != "" && _realname != "");
140  };
141 
142  std::string const getNick(void) const
143  {
144  return _nick;
145  };
146 
147  std::string const getUsername(void) const
148  {
149  return _username;
150  };
151 
152  std::string const getRealname(void) const
153  {
154  return _realname;
155  };
156 
157  std::string const getHost(void) const
158  {
159  return _host;
160  };
161 
162  std::string const getUserId(void) const
163  {
164  return _nick + "!" + _username + "@" + _host;
165  };
166 
167  std::string const getServername(void) const
168  {
169  return _servername;
170  };
171 
172  bool isInvisible(void)
173  {
174  return this->hasMode(USER_MODE_INVISIBLE);
175  };
176 
177  bool isOperator(void)
178  {
179  return this->hasMode(USER_MODE_OPERATOR);
180  };
181 
182  bool isRegistered(void)
183  {
184  return this->hasMode(USER_MODE_REGISTERED);
185  };
186 
187  // Setters
188  void setNick(std::string const &nick)
189  {
190  _nick = nick;
191  };
192  void setUsername(std::string const &username)
193  {
194  _username = username;
195  };
196  void setRealName(std::string const &realname)
197  {
198  _realname = realname;
199  };
200 
201  // Utils
202  bool hasMode(UserMode mode)
203  {
204  return std::find(this->_modes.begin(), this->_modes.end(), mode) !=
205  this->_modes.end();
206  }
207 
208  void addMode(UserMode mode)
209  {
210  if (!this->hasMode(mode))
211  this->_modes.push_back(mode);
212  }
213 };
214 
215 #endif
RPL_MOTDSTART
#define RPL_MOTDSTART(servername, nick)
Definition: Replies.hpp:92
Client::_status
ClientStatus _status
Definition: Client.hpp:40
Client::Client
Client(int &fd, std::string host, std::string servername, std::string version)
Definition: Client.hpp:45
Client::read
void read(void)
Definition: client.cpp:28
Client::hasMode
bool hasMode(UserMode mode)
Definition: Client.hpp:202
Client::_serverversion
std::string _serverversion
Definition: Client.hpp:32
Message.hpp
Client::readConfigFile
std::vector< std::string > readConfigFile(char const *filename)
Definition: Client.hpp:69
Client::loginFail
void loginFail(void)
Definition: Client.hpp:114
Client::isOperator
bool isOperator(void)
Definition: Client.hpp:177
config.hpp
Client::disconnect
void disconnect(void)
Definition: Client.hpp:60
Client::_messagesReceived
std::vector< Message > _messagesReceived
Definition: Client.hpp:34
Client::ban
void ban(void)
Definition: Client.hpp:64
Client::_messagesSent
std::vector< Message > _messagesSent
Definition: Client.hpp:33
C_RED
#define C_RED
Definition: Color.hpp:6
Client::_username
std::string _username
Definition: Client.hpp:27
Client::getNick
const std::string getNick(void) const
Definition: Client.hpp:142
ERR_YOUREBANNEDCREEP
#define ERR_YOUREBANNEDCREEP(servername, nick)
Definition: Replies.hpp:137
RPL_ENDOFMOTD
#define RPL_ENDOFMOTD(servername, nick)
Definition: Replies.hpp:96
Client::getHost
const std::string getHost(void) const
Definition: Client.hpp:157
Client::isRegistered
bool isRegistered(void)
Definition: Client.hpp:182
Client::~Client
~Client()
Definition: Client.hpp:49
Client::message
void message(char const *message)
Definition: client.cpp:15
Client::_modes
std::vector< UserMode > _modes
Definition: Client.hpp:42
Client::getServername
const std::string getServername(void) const
Definition: Client.hpp:167
Client::isAuthenticated
bool isAuthenticated(void) const
Definition: Client.hpp:132
Client::_host
std::string _host
Definition: Client.hpp:29
Client::getSupportString
std::string getSupportString(void)
Definition: Client.hpp:86
Client::setUsername
void setUsername(std::string const &username)
Definition: Client.hpp:192
RPL_WELCOME
#define RPL_WELCOME(servername, nick)
Definition: Replies.hpp:16
Client::messagesSent
std::vector< Message > messagesSent() const
Definition: Client.hpp:122
Client::isInvisible
bool isInvisible(void)
Definition: Client.hpp:172
Client::_message
Message * _message
Definition: Client.hpp:41
USER_MODE_REGISTERED
@ USER_MODE_REGISTERED
Definition: Client.hpp:19
Client::getUsername
const std::string getUsername(void) const
Definition: Client.hpp:147
MAX_CHANNELS
#define MAX_CHANNELS
Definition: config.hpp:6
Client::messagesReceived
std::vector< Message > messagesReceived() const
Definition: Client.hpp:127
USER_MODE_INVISIBLE
@ USER_MODE_INVISIBLE
Definition: Client.hpp:17
Client::_attempts
int _attempts
Definition: Client.hpp:39
Client::setNick
void setNick(std::string const &nick)
Definition: Client.hpp:188
Client::_is_ope
bool _is_ope
Definition: Client.hpp:37
Client::_is_passLogged
bool _is_passLogged
Definition: Client.hpp:36
RPL_CREATED
#define RPL_CREATED(servername, nick)
Definition: Replies.hpp:20
RPL_YOURHOST
#define RPL_YOURHOST(servername, nick, version)
Definition: Replies.hpp:18
Client::_maxChannels
const int _maxChannels
Definition: Client.hpp:38
Client::_realname
std::string _realname
Definition: Client.hpp:28
RPL_MYINFO
#define RPL_MYINFO(servername, nick, version, usermodes, chanmodes)
Definition: Replies.hpp:22
ClientStatus
ClientStatus
Definition: config.hpp:12
UserMode
UserMode
Definition: Client.hpp:15
Message
Definition: Message.hpp:12
MAX_ATTEMPTS
#define MAX_ATTEMPTS
Definition: config.hpp:8
Client
Definition: Client.hpp:22
Client::addMode
void addMode(UserMode mode)
Definition: Client.hpp:208
RPL_ISUPPORT
#define RPL_ISUPPORT(servername, nick, tokens)
Definition: Replies.hpp:24
Client::getUserId
const std::string getUserId(void) const
Definition: Client.hpp:162
Client::canPerformPassCommand
bool canPerformPassCommand(void) const
Definition: Client.hpp:137
Client::setRealName
void setRealName(std::string const &realname)
Definition: Client.hpp:196
Client::getRealname
const std::string getRealname(void) const
Definition: Client.hpp:152
ALIVE
@ ALIVE
Definition: config.hpp:14
USER_MODE_OPERATOR
@ USER_MODE_OPERATOR
Definition: Client.hpp:18
Client::_nick
std::string _nick
Definition: Client.hpp:26
Client::_fd
int _fd
Definition: Client.hpp:25
C_X
#define C_X
Definition: Color.hpp:4
Client::_servername
std::string _servername
Definition: Client.hpp:30
Replies.hpp
RPL_MOTD
#define RPL_MOTD(servername, nick, motd)
Definition: Replies.hpp:94
Client::authenticate
void authenticate(void)
Definition: Client.hpp:98
Client::_is_authenticated
bool _is_authenticated
Definition: Client.hpp:35