-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathServer.hpp
More file actions
85 lines (73 loc) · 3.25 KB
/
Copy pathServer.hpp
File metadata and controls
85 lines (73 loc) · 3.25 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
#pragma once
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <poll.h>
#include <fcntl.h>
#include <algorithm>
#include <cstring>
#include <cerrno>
#include <cstdlib>
#include <sstream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include "Client.hpp"
#include "Channel.hpp"
#define MAX_CLIENTS 100
#define BUFFER_SIZE 512
#define RESET_COLOR "\033[0m"
#define RED_COLOR "\033[31m"
#define GREEN_COLOR "\033[32m"
#define PORT_LIMIT 65535
class Channel;
class Server {
private:
int server_fd;
std::string password;
std::map<int, Client*> clients;
std::vector<struct pollfd> poll_fds;
std::map<std::string, Channel*> channels;
typedef void (Server::*CommandFunc)(Client*, const std::vector<std::string>&);
typedef void (Server::*ChannelCommandFunc)(Channel*, Client*, const std::vector<std::string>&);
typedef void (Server::*commonFunc)(Client*, const std::vector<std::string>&);
std::map<std::string, CommandFunc> command_map;
std::map<std::string, ChannelCommandFunc> channel_command_map;
std::map<std::string, commonFunc> common_command_map;
// server functions
void initServer(const std::string& port_str);
void acceptNewClient();
void removeClient(Client *client, const std::vector<std::string>& params);
void handleClientMessage(int fd);
void setNonBlocking(int fd);
void parseCommand(Client* client, const std::string& message);
void registerCommands();
// channel commands
void leaveChannel(Channel *channel, Client* client, const std::vector<std::string>& params);
void deleteChannel(Channel *channel, Client* client, const std::vector<std::string>& params);
void kickMemberFromChannel(Channel *channel, Client* client, const std::vector<std::string>& params);
void listChannelMembers(Channel *channel, Client *client, const std::vector<std::string>& params);
void addOpToChannel(Channel *channel, Client* client, const std::vector<std::string>& params);
// server commands
void handlePASS(Client*, const std::vector<std::string>& params);
void handleNICK(Client* client, const std::vector<std::string>& params);
void handleUSER(Client* client, const std::vector<std::string>& params);
void handlePING(Client* client, const std::vector<std::string>& params);
void handlePRIVMSG(Client* client, const std::vector<std::string>& params);
void createChannel(Client *client, const std::vector<std::string>& params);
void joinChannel(Client *client, const std::vector<std::string>& params);
void listChannels(Client *client, const std::vector<std::string>& params);
void handleHelp(Client *client, const std::vector<std::string>& params);
// utils
void sendWelcomeMessage(Client *client);
const std::string Prefix(Client *client) const;
public:
static bool isNumber(const std::string& input);
Server(const std::string& port_str, const std::string& password);
~Server();
void run();
void sendMessage(int fd, const std::string& message);
void removeChannel(Channel* channel);
};