-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathServer.hpp
122 lines (100 loc) · 4 KB
/
Server.hpp
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
#pragma once
#include <iostream>
#include <string>
#include <sys/socket.h> //socket, bind
#include <sys/epoll.h> //epoll
#include <netinet/in.h> //sockaddr
#include <fcntl.h> //fcntl
#include <unistd.h> //open, close
#include <arpa/inet.h> //htons -- convert to network byte order
#include <algorithm> //find
#include <vector>
#include <map>
#include <cstring>
#include <sstream>
#include <exception>
#include "User.hpp"
#include "Colors.hpp"
#include "Channel.hpp"
#include "Utils.hpp"
#include "Log.hpp"
#include "Error.hpp"
# define user_id(nickname, username) (":" + nickname + "!" + username + "@localhost")
# define RPL_WELCOME(user_id, nickname) (":ft_irc 001 " + nickname + " :Welcome to the Internet Relay Network " + user_id + "\r\n")
# define RPL_YOURHOST(client, servername, version) (":ft_irc 002 " + client + " :Your host is " + servername + " (localhost), running version " + version + "\r\n")
# define RPL_CREATED(client, datetime) (":ft_irc 003 " + client + " :This server was created " + datetime + "\r\n")
# define RPL_MYINFO(client, servername, version, user_modes, chan_modes, chan_param_modes) (":ft_irc 004 " + client + " :" + servername + " " + version + " " + user_modes + " " + chan_modes + " " + chan_param_modes + "\r\n")
# define RPL_ISUPPORT(client, tokens) (":ft_irc 005 " + client + " :" + tokens " :are supported by this server\r\n")
class Server
{
private:
int _port;
int _server_socket;
int _epoll_socket;
std::string _password;
std::vector<User *> _users;
User *_server_user;
std::string _msg;
std::string _start_time;
std::map<std::string, Channel *> _channels;
typedef int (Server::*CommandFunc)(User *, std::stringstream &);
std::map<std::string, CommandFunc> _command_map;
//default constructor
Server();
void init_command_map();
public:
//constructors
Server(const Server &src);
Server(int port, std::string password);
//overloaded ops
Server &operator = (const Server &src);
//destructor
~Server();
//setters
//getters
int get_epoll_socket();
int get_server_socket();
std::string get_password();
std::string get_start_time();
//server functions
int client_message(User *user);
int new_connection();
int get_command(User *user, std::string msg);
std::string find_next_cmd(std::stringstream ¶ms, std::stringstream &parsed_params);
// void reply(User *user, std::string prefix, std::string command,
// std::string target, std::string message);
void register_client(User *user);
void send_server_response(User *user, std::string send_buf);
//users
User *get_user_from_nick(std::string nick);
void add_user(User *user);
void remove_user(User *user);
// channels
Channel *get_channel(const std::string &name);
Channel *create_channel(const std::string &name);
void remove_channel(const std::string &name);
//server commands
// Command function implementations
int CAP(User *user, std::stringstream &command);
int OPER(User *user, std::stringstream &command);
int PASS(User *user, std::stringstream &command);
int QUIT(User *user, std::stringstream &command);
int KILL(User *user, std::stringstream &command);
int PING(User *user, std::stringstream &command);
int LIST(User *user, std::stringstream &command);
int MODE(User *user, std::stringstream &command);
int INVITE(User *user, std::stringstream &command);
int KICK(User *user, std::stringstream &command);
int PART(User *user, std::stringstream &command);
int PRIVMSG(User *user, std::stringstream &command);
int WHO(User *user, std::stringstream &command);
int NICK(User *user, std::stringstream &command);
int TOPIC(User *user, std::stringstream &command);
int USER(User *user, std::stringstream &command);
int JOIN(User *user, std::stringstream &command);
int REMOVE_CHANNEL(User *user, std::stringstream &command); // needs a better name, probably :D
//error messages
};
//changed this from a member function
void reply(User *user, std::string prefix, std::string command,
std::string target, std::string message);