forked from OthelloPlusPlus/ft_irc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.hpp
More file actions
204 lines (187 loc) · 6.15 KB
/
Server.hpp
File metadata and controls
204 lines (187 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* ************************************************************************** */
/* */
/* :::::::: */
/* Server.hpp :+: :+: */
/* +:+ */
/* By: ohengelm <ohengelm@student.42.fr> +#+ */
/* +#+ */
/* Created: 2023/03/11 13:37:13 by ohengelm #+# #+# */
/* Updated: 2023/03/11 13:39:19 by ohengelm ######## odam.nl */
/* */
/* ************************************************************************** */
#ifndef SERVER_HPP
# define SERVER_HPP
class Client;
class AClient;
class Channel;
# include <string>
// std::string
# include <poll.h>
// struct pollfd
# include <netinet/in.h>
// struct sockaddr_in
# include <vector>
// std::vector
# include <map>
// std::map
typedef struct file_s
{
std::string fileName;
std::string filePath;
std::string senderName;
std::string receiverName;
std::string line;
} file_t;
class Server
{
private:
std::string serverName;
struct pollfd pollInfo;
struct sockaddr_in socketAddress;
int port;
std::string publicIP; //https://www.whatsmyip.org/ 185-61-55-68.185-61-55-ip.fmo-solutions.nl
std::string localIP;
static bool state;
std::vector<Channel *> channels;
std::vector<AClient *> clients;
std::map<std::string, file_t> fileTr;
std::string passwordUser;
std::string motd;
//Server initialisation
void validatePort(void) const;
void readEnv(void);
void setLocalIP(void);
void bootUpServer(void);
void addBots(void);
//Server Shutdown
static void handlerSIGINT(int signal);
//Connection subfunctions
void acceptClient(void);
void sendWelcome(AClient &client) const;
protected:
public:
//(De)constructors
Server(int argc, char **argv);
~Server(void);
//Server Shutdown
void shutdownServer(AClient &client, const std::string password);
//Connection
void checkNewClient(void);
int validatePassword(const std::string password) const;
void checkClients(void);
void checkChannels(void);
//User interaction
void sendPrivMsg(AClient &client, const std::vector<std::string> &args) const;
void sendNotice(AClient &client, const std::vector<std::string> &args) const;
void sendPong(AClient &client, const std::string token) const;
void sendAllUsers(const std::string msg1, AClient &client, const std::string msg2) const;
//Channel Interaction
void sendChannelList(AClient &client) const;
void joinChannel(AClient &client, const std::vector<std::string> &args);
void sendInvite(AClient &client, const std::vector<std::string> &args) const;
void partChannel(AClient &client, const std::string channelName);
void kickUser(AClient &client, const std::vector<std::string> &args);
void sendNames(AClient &client, const std::vector<std::string> &args) const;
void sendWho(AClient &client, const std::string who) const;
void sendWhoIs(AClient &client, const std::string who) const;
void setChannelTopic(AClient &client, const std::vector<std::string> &args);
void setChannelMode(AClient &client, const std::vector<std::string> &args);
//Setters and Getters
void setName(void);
std::string getName(void) const;
std::string getIP(void) const;
int getPort(void) const;
int getFD(void) const;
void setMOTD(AClient &client, std::string msg);
std::string getMOTD(void) const;
AClient *getClient(std::string name) const;
size_t getClientsSize(void) const;
Channel *getChannel(std::string channel) const;
size_t getChannelsSize(void) const;
bool getState(void) const;
//File Transfer
void setTransferFile(std::string key, file_t &file);
file_t getTransferFile(std::string key);
void rmTransferFile(std::string key);
//Operator overload to caternate to character strings
# ifdef __APPLE__
friend std::string operator+(char c, const Server &src)
{
return (std::string(1, c) + src);
}
friend std::string operator+(const char *str, const Server &src)
{
return (std::string(str) + src);
}
friend std::string operator+(std::string str, const Server &src)
{
if (!src.serverName.empty())
return (str + src.serverName);
if (!src.localIP.empty())
return (str + src.localIP);
if (!src.publicIP.empty())
return (str + src.publicIP);
return (str);
}
std::string operator+(char c)
{
return (*this + std::string(1, c));
}
std::string operator+(const char *str)
{
return (*this + std::string(str));
}
std::string operator+(std::string str)
{
if (!this->serverName.empty())
return (this->serverName + str);
if (!this->localIP.empty())
return (this->localIP + str);
if (!this->publicIP.empty())
return (this->publicIP + str);
return (str);
}
# else
# include <type_traits>
template <typename T>
friend std::string operator+(const T add, const Server &src)
{
static_assert(std::is_same<T, char>::value ||
std::is_same<T, char*>::value ||
std::is_same<T, const char*>::value ||
std::is_same<T, std::string>::value, "Invalid type");
std::string ret;
ret = add;
if (!src.serverName.empty())
return (ret + src.serverName);
if (!src.localIP.empty())
return (ret + src.localIP);
if (!src.publicIP.empty())
return (ret + src.publicIP);
return (ret);
}
template <typename T>
std::string operator+(const T add)
{
static_assert(std::is_same<T, char>::value ||
std::is_same<T, char*>::value ||
std::is_same<T, const char*>::value ||
std::is_same<T, std::string>::value, "Invalid type");
std::string ret;
ret = add;
if (!this->serverName.empty())
return (this->serverName + ret);
if (!this->localIP.empty())
return (this->localIP + ret);
if (!this->publicIP.empty())
return (this->publicIP + ret);
return (ret);
}
# endif
};
# include "Client.hpp"
# include "AClient.hpp"
# include "Channel.hpp"
#else
class Server;
#endif