forked from OthelloPlusPlus/ft_irc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChannel.hpp
More file actions
160 lines (143 loc) · 4.53 KB
/
Channel.hpp
File metadata and controls
160 lines (143 loc) · 4.53 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* Channel.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 CHANNEL_HPP
# define CHANNEL_HPP
class AClient;
class Server;
#include <string>
// std::string
#include <vector>
// std::vector
#include <set>
// std::set
struct ChannelUser
{
AClient *client;
bool admin;
unsigned int timestamp;
};
class Channel
{
private:
std::string name;
std::string topic;
bool modeInvite;
size_t userLimit;
std::string key;
bool modeTopic;
Server *server;
std::vector<ChannelUser> users;
std::set<std::string> invitedUsers;
//User Management
bool addClientValidate(const AClient &newClient, const std::string password);
void sendTopic(AClient &client) const;
//Channel Management
bool userIsAdmin(const AClient &client) const;
void setModeI(AClient &client, std::string flag);
void setModeT(AClient &client, std::string flag);
void setModeK(AClient &client, std::string flag, std::string newPass);
void setModeO(AClient &client, std::string flag, std::string targetName);
void setModeL(AClient &client, std::string flag, std::string count);
//Getter
ChannelUser *getChannelUser(std::string clientName);
protected:
public:
//(De)constructors
Channel(std::string name, Server *server);
~Channel(void);
//User Management
void addClient(AClient &newClient, bool admin, const std::string password);
void addInvite(AClient &addClient);
void kickUser(AClient &client, const std::vector<std::string> &names);
void removeUser(const AClient &client);
void promoteOldestUser(void);
bool userIsInChannel(const AClient &client) const;
//Channel Management
void setMode(AClient &client, std::string flag, std::string arg);
void setTopic(AClient &client, const std::string newTopic);
//Channel information
void sendNames(AClient &client) const;
void sendWho(AClient &client) const;
void sendMode(AClient &client) const;
//Messaging
void sendToChannel(const std::string msg) const;
void sendToChannel(AClient &exclude, const std::string msg) const;
//Getters
std::string getName(void) const;
std::string getTopic(void) const;
size_t getSize(void) const;
size_t getAdminSize(void) const;
//Operator overload to caternate to character strings
# ifdef __APPLE__
friend std::string operator+(char c, const Channel &src)
{
return (std::string(1, c) + src);
}
friend std::string operator+(const char *str, const Channel &src)
{
return (std::string(str) + src);
}
friend std::string operator+(std::string str, const Channel &src)
{
if (!src.name.empty())
return (str + src.name);
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->name.empty())
return (this->name + str);
return (str);
}
# else
# include <type_traits>
template <typename T>
friend std::string operator+(const T add, const Channel &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.name.empty())
return (ret + src.name);
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->name.empty())
return (this->name + ret);
return (ret);
}
# endif
};
# include "AClient.hpp"
# include "Server.hpp"
#else
class Channel;
#endif