Compare commits
44 Commits
readme
...
9093ac3511
| Author | SHA1 | Date | |
|---|---|---|---|
| 9093ac3511 | |||
|
|
d408d08249 | ||
| 78aa891d38 | |||
|
|
3578bd5b5c | ||
| 644dbe92a8 | |||
|
|
226a271a41 | ||
| 292fe11f15 | |||
|
|
7f40babccc | ||
|
|
2f995fe856 | ||
|
|
c61e0826c6 | ||
|
|
d6652af02d | ||
|
|
bdc24594a4 | ||
| b201de3796 | |||
| 1c6c9be6ec | |||
| d1577cdb08 | |||
| 86e3034a0a | |||
| 3614237b8b | |||
| ffd650da20 | |||
| 63ebb5b9ba | |||
| b688d04fde | |||
| cd4eb82af2 | |||
|
|
752eb89e72 | ||
|
|
4beabead01 | ||
|
|
0c29c50e23 | ||
| 3a43f9d2bd | |||
| 5051c6cfcd | |||
| f70a46dde0 | |||
| def1787cb3 | |||
| 38823adb9d | |||
|
|
fd7a24de3d | ||
| 635335831c | |||
|
|
cbc6c7fc41 | ||
| f2bbec366b | |||
|
|
b47c7dc9b8 | ||
| d2e02440b3 | |||
|
|
e08ef37928 | ||
| bbeaae5923 | |||
|
|
c31c69c0fa | ||
|
|
206ff02eb4 | ||
| ddfe1202fd | |||
|
|
77e3166f15 | ||
|
|
c7bc6f90ca | ||
| 279a71aa31 | |||
|
|
b520ef7c34 |
55
Channel/Channel.cpp
Normal file
55
Channel/Channel.cpp
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* Channel.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/08 16:41:40 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2026/05/08 16:41:40 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "Channel.hpp"
|
||||||
|
|
||||||
|
//////////////////
|
||||||
|
// Constructors //
|
||||||
|
//////////////////
|
||||||
|
|
||||||
|
Channel::Channel() { /* std::cout << "Channel default constructor called" << std::endl; */}
|
||||||
|
|
||||||
|
Channel::~Channel() { /* std::cout << "Channel destructor called" << std::endl; */}
|
||||||
|
|
||||||
|
Channel::Channel(const Channel &other)
|
||||||
|
{
|
||||||
|
*this = other;
|
||||||
|
// std::cout << "Channel copy constructor called" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
Channel& Channel::operator=(const Channel &other)
|
||||||
|
{
|
||||||
|
if (this != &other)
|
||||||
|
{
|
||||||
|
name_ = other.name_;
|
||||||
|
members_ = other.members_;
|
||||||
|
operators_ = other.operators_;
|
||||||
|
}
|
||||||
|
// std::cout << "Channel copy assignment operator called" << std::endl;
|
||||||
|
return (*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Constructor with name
|
||||||
|
|
||||||
|
Channel::Channel(std::string name) : name_(name) { /* std::cout << "Channel with name constructor called" << std::endl; */ }
|
||||||
|
|
||||||
|
|
||||||
|
// Members
|
||||||
|
void Channel::addMember(int fd) { this->members_.insert(fd); }
|
||||||
|
void Channel::removeMember(int fd) { this->members_.erase(fd); }
|
||||||
|
bool Channel::hasMember(int fd) const { return (this->members_.count(fd) > 0); }
|
||||||
|
|
||||||
|
// Operators
|
||||||
|
void Channel::addOperator(int fd) { this->operators_.insert(fd); }
|
||||||
|
void Channel::removeOperator(int fd) { this->operators_.erase(fd); }
|
||||||
|
bool Channel::hasOperator(int fd) const { return (this->operators_.count(fd) > 0); }
|
||||||
47
Channel/Channel.hpp
Normal file
47
Channel/Channel.hpp
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* Channel.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/08 16:41:40 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2026/05/08 16:41:40 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef CHANNEL_HPP
|
||||||
|
|
||||||
|
# define CHANNEL_HPP
|
||||||
|
|
||||||
|
# include <iostream>
|
||||||
|
# include <set>
|
||||||
|
|
||||||
|
class Channel
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::string name_;
|
||||||
|
std::set<int> members_;
|
||||||
|
std::set<int> operators_;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Channel();
|
||||||
|
Channel(std::string name);
|
||||||
|
Channel(const Channel &other);
|
||||||
|
Channel& operator=(const Channel &other);
|
||||||
|
~Channel();
|
||||||
|
|
||||||
|
// Users
|
||||||
|
void addMember(int fd);
|
||||||
|
void removeMember(int fd);
|
||||||
|
bool hasMember(int fd) const;
|
||||||
|
|
||||||
|
// Operators
|
||||||
|
void addOperator(int fd);
|
||||||
|
void removeOperator(int fd);
|
||||||
|
bool hasOperator(int fd) const;
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
44
Makefile
Normal file
44
Makefile
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
NAME = ircserv
|
||||||
|
|
||||||
|
SRC = main.cpp Server/Server.cpp User/User.cpp
|
||||||
|
|
||||||
|
HEADERS = Server/Server.hpp User/User.hpp
|
||||||
|
|
||||||
|
OBJ = $(SRC:.cpp=.o)
|
||||||
|
|
||||||
|
CC = c++
|
||||||
|
CFLAGS = -Wall -Wextra -Werror -std=c++98
|
||||||
|
|
||||||
|
GREEN = \033[0;32m
|
||||||
|
RED = \033[0;31m
|
||||||
|
RESET = \033[0m
|
||||||
|
|
||||||
|
TOTAL := $(words $(SRC))
|
||||||
|
COUNT = 0
|
||||||
|
|
||||||
|
all: $(NAME)
|
||||||
|
|
||||||
|
$(NAME): $(OBJ) $(HEADERS)
|
||||||
|
@rm -f .build_start
|
||||||
|
@$(CC) $(CFLAGS) $(OBJ) -o $(NAME)
|
||||||
|
@printf "\n$(GREEN)✔ Listo (100%%)\n$(RESET)"
|
||||||
|
|
||||||
|
%.o: %.cpp
|
||||||
|
@if [ ! -f .build_start ]; then printf "$(GREEN)Compiling objects...\n$(RESET)"; touch .build_start; fi
|
||||||
|
@$(eval COUNT = $(shell echo $$(($(COUNT)+1))))
|
||||||
|
@PERCENT=$$(($(COUNT)*100/$(TOTAL))); \
|
||||||
|
BAR=$$(printf "%0.s#" $$(seq 1 $$((PERCENT/5)))); \
|
||||||
|
SPACE=$$(printf "%0.s " $$(seq 1 $$((20-PERCENT/5)))); \
|
||||||
|
printf "\r [$$BAR$$SPACE] %3d%% (%d/%d) $< " $$PERCENT $(COUNT) $(TOTAL)
|
||||||
|
@$(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@printf "$(RED)Removing objects...\n$(RESET)"
|
||||||
|
@rm -f $(OBJ)
|
||||||
|
|
||||||
|
fclean: clean
|
||||||
|
@rm -f $(NAME)
|
||||||
|
|
||||||
|
re: fclean all
|
||||||
|
|
||||||
|
.PHONY: all clean fclean re
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
**This project has been created as part of the 42 curriculum by iherman- and aortigos**
|
*This project has been created as part of the 42 curriculum by iherman- and aortigos*
|
||||||
|
|
||||||
# Description
|
# Description
|
||||||
|
|
||||||
@@ -8,3 +8,5 @@ The goal of this project is to do an IRC Server
|
|||||||
|
|
||||||
# Resources
|
# Resources
|
||||||
|
|
||||||
|
|
||||||
|
Thanks for reading :)
|
||||||
|
|||||||
289
Server/Server.cpp
Normal file
289
Server/Server.cpp
Normal file
@@ -0,0 +1,289 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* Server.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/06 17:19:12 by iherman- #+# #+# */
|
||||||
|
/* Updated: 2026/05/12 20:34:58 by iherman- ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "Server.hpp"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
|
#include <poll.h>
|
||||||
|
|
||||||
|
#include <cerrno>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
const int Server::kConnectionQueueLimit = 10;
|
||||||
|
|
||||||
|
// TEMPORARY TESTING FUNCTION
|
||||||
|
void echo(User& client, std::istringstream& input)
|
||||||
|
{
|
||||||
|
std::string message = "Server received: " + input.str();
|
||||||
|
send(client.getFd(), message.c_str(), message.size(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
Server::Server() :
|
||||||
|
port_(PORT_DEFAULT),
|
||||||
|
password_("password")
|
||||||
|
{
|
||||||
|
serverSocket_ = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
if (serverSocket_ < 0)
|
||||||
|
throw std::runtime_error("Failed to create socket");
|
||||||
|
|
||||||
|
struct sockaddr_in addr;
|
||||||
|
std::memset(&addr, 0, sizeof(addr));
|
||||||
|
|
||||||
|
addr.sin_family = AF_INET;
|
||||||
|
addr.sin_port = htons(port_);
|
||||||
|
addr.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
|
||||||
|
if (bind(serverSocket_, (struct sockaddr*)&addr, sizeof(addr)))
|
||||||
|
throw std::runtime_error("Failed to bind");
|
||||||
|
|
||||||
|
if (listen(serverSocket_, kConnectionQueueLimit))
|
||||||
|
throw std::runtime_error("Failed to listen");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
std::cout << "Server port: " << port_
|
||||||
|
<< "\nServer Password: " << password_
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
Server::Server(const Server& other)
|
||||||
|
{
|
||||||
|
port_ = other.port_;
|
||||||
|
serverSocket_ = other.serverSocket_;
|
||||||
|
password_ = other.password_;
|
||||||
|
sockets_ = other.sockets_;
|
||||||
|
}
|
||||||
|
|
||||||
|
Server::Server(int port, const std::string& password) :
|
||||||
|
port_(port),
|
||||||
|
password_(password)
|
||||||
|
{
|
||||||
|
if (port_ < 1 || port_ > 65535)
|
||||||
|
throw std::runtime_error("Invalid port");
|
||||||
|
|
||||||
|
if (password_.empty())
|
||||||
|
throw std::runtime_error("Empty password");
|
||||||
|
|
||||||
|
serverSocket_ = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
if (serverSocket_ < 0)
|
||||||
|
throw std::runtime_error("Failed to create socket");
|
||||||
|
|
||||||
|
int opt = 1;
|
||||||
|
if (setsockopt(serverSocket_, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)
|
||||||
|
{
|
||||||
|
close(serverSocket_);
|
||||||
|
throw std::runtime_error("setsockopt failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sockaddr_in addr;
|
||||||
|
std::memset(&addr, 0, sizeof(addr));
|
||||||
|
|
||||||
|
addr.sin_family = AF_INET;
|
||||||
|
addr.sin_port = htons(port_);
|
||||||
|
addr.sin_addr.s_addr = INADDR_ANY;
|
||||||
|
|
||||||
|
if (bind(serverSocket_, (struct sockaddr*)&addr, sizeof(addr)))
|
||||||
|
{
|
||||||
|
close(serverSocket_);
|
||||||
|
throw std::runtime_error("Failed to bind");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (listen(serverSocket_, kConnectionQueueLimit))
|
||||||
|
{
|
||||||
|
close(serverSocket_); // maybe make an fd class that cleans up automatically using the destructor
|
||||||
|
throw std::runtime_error("Failed to listen");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add all new commands to commands_ here
|
||||||
|
commands_["echo"] = &echo;
|
||||||
|
|
||||||
|
std::cout << "Server port: " << port_
|
||||||
|
<< "\nServer Password: " << password_
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
Server::~Server()
|
||||||
|
{
|
||||||
|
for (std::map<int, User>::iterator it = clients_.begin(); it != clients_.end(); ++it)
|
||||||
|
close(it->first);
|
||||||
|
|
||||||
|
close(serverSocket_);
|
||||||
|
}
|
||||||
|
|
||||||
|
Server &Server::operator=(const Server& other)
|
||||||
|
{
|
||||||
|
if (this != &other)
|
||||||
|
{
|
||||||
|
port_ = other.port_;
|
||||||
|
serverSocket_ = other.serverSocket_;
|
||||||
|
password_ = other.password_;
|
||||||
|
sockets_ = other.sockets_;
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Server::parseCommand(User& client)
|
||||||
|
{
|
||||||
|
std::istringstream args(client.getBuffer());
|
||||||
|
std::string command;
|
||||||
|
|
||||||
|
args >> command;
|
||||||
|
|
||||||
|
std::map<std::string, void (*)(User&, std::istringstream&)>::iterator it = commands_.find(command);
|
||||||
|
if (it == commands_.end())
|
||||||
|
{
|
||||||
|
std::string message = "Error: command not found!\n";
|
||||||
|
send(client.getFd(), message.c_str(), message.size(), 0);
|
||||||
|
client.clearBuffer();
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
it->second(client, args);
|
||||||
|
client.clearBuffer();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Server::handleClient(User& client)
|
||||||
|
{
|
||||||
|
const std::size_t kBufferSize = 1024;
|
||||||
|
char buffer[kBufferSize] = {0};
|
||||||
|
|
||||||
|
int recv_amount = recv(client.getFd(), buffer, kBufferSize, 0);
|
||||||
|
|
||||||
|
if (recv_amount == 0)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (recv_amount < 0)
|
||||||
|
{
|
||||||
|
if (errno == EAGAIN || errno == EWOULDBLOCK)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
client.appendBuffer(std::string(buffer, recv_amount));
|
||||||
|
|
||||||
|
if (client.getBuffer().find('\n') != std::string::npos)
|
||||||
|
parseCommand(client);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct pollfd newPollfd(int fd)
|
||||||
|
{
|
||||||
|
pollfd pfd;
|
||||||
|
|
||||||
|
pfd.fd = fd;
|
||||||
|
pfd.events = POLLIN;
|
||||||
|
pfd.revents = 0;
|
||||||
|
return pfd;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Server::addClient()
|
||||||
|
{
|
||||||
|
int newClientSocket = accept(serverSocket_, NULL, NULL);
|
||||||
|
|
||||||
|
if (newClientSocket == -1)
|
||||||
|
{
|
||||||
|
// Not sure if program should terminate
|
||||||
|
std::cerr << "Problem occured accepting client" << std::endl;
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
|
||||||
|
fcntl(newClientSocket, F_SETFL, O_NONBLOCK);
|
||||||
|
sockets_.push_back(newPollfd(newClientSocket));
|
||||||
|
clients_[newClientSocket] = User(newClientSocket);
|
||||||
|
std::cout << "Client with fd: " << newClientSocket << " connected" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<struct pollfd>::iterator Server::removeClient(std::vector<struct pollfd>::iterator& client)
|
||||||
|
{
|
||||||
|
close(client->fd);
|
||||||
|
|
||||||
|
std::cout << "Client with fd: " << client->fd << " disconnected " << std::endl;
|
||||||
|
|
||||||
|
clients_.erase(client->fd);
|
||||||
|
return (sockets_.erase(client));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Server::run()
|
||||||
|
{
|
||||||
|
// add serverSocket_ to sockets_
|
||||||
|
sockets_.push_back(newPollfd(serverSocket_));
|
||||||
|
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
int ret = poll(sockets_.data(), sockets_.size(), -1);
|
||||||
|
|
||||||
|
if (ret <= 0)
|
||||||
|
{
|
||||||
|
// not sure if program should terminate
|
||||||
|
std::cerr << "Poll failed" << std::endl;
|
||||||
|
continue ;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (std::vector<struct pollfd>::iterator it = sockets_.begin(); it != sockets_.end(); )
|
||||||
|
{
|
||||||
|
if (it->revents & (POLLERR | POLLHUP | POLLNVAL))
|
||||||
|
{
|
||||||
|
it = removeClient(it);
|
||||||
|
continue ;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(it->revents & POLLIN))
|
||||||
|
{
|
||||||
|
it++;
|
||||||
|
continue ;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "Checking client with fd: " << it->fd << std::endl;
|
||||||
|
|
||||||
|
// check if listening port is open, if so add client
|
||||||
|
if (it->fd == serverSocket_)
|
||||||
|
{
|
||||||
|
addClient();
|
||||||
|
it++;
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
|
||||||
|
// call handleClient for each other socket
|
||||||
|
std::map<int, User>::iterator clientIt = clients_.find(it->fd);
|
||||||
|
if (clientIt == clients_.end())
|
||||||
|
{
|
||||||
|
std::cerr << "Unknown fd in poll list: " << it->fd << std::endl;
|
||||||
|
close(it->fd);
|
||||||
|
it = sockets_.erase(it);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool disconnected = handleClient(clientIt->second);
|
||||||
|
|
||||||
|
if (disconnected)
|
||||||
|
it = removeClient(it);
|
||||||
|
else
|
||||||
|
it++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
62
Server/Server.hpp
Normal file
62
Server/Server.hpp
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* Server.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/06 17:18:11 by iherman- #+# #+# */
|
||||||
|
/* Updated: 2026/05/12 19:57:32 by iherman- ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef SERVER_HPP
|
||||||
|
# define SERVER_HPP
|
||||||
|
|
||||||
|
// C++ lib functions
|
||||||
|
# include <iostream>
|
||||||
|
|
||||||
|
# include <string>
|
||||||
|
# include <cstring>
|
||||||
|
|
||||||
|
# include <vector>
|
||||||
|
# include <map>
|
||||||
|
|
||||||
|
# include "../User/User.hpp"
|
||||||
|
|
||||||
|
# define PORT_DEFAULT 9898
|
||||||
|
|
||||||
|
class Server
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
static const int kConnectionQueueLimit;
|
||||||
|
|
||||||
|
int port_;
|
||||||
|
int serverSocket_;
|
||||||
|
|
||||||
|
std::string password_;
|
||||||
|
|
||||||
|
std::vector<struct pollfd> sockets_;
|
||||||
|
std::map<int, User> clients_;
|
||||||
|
|
||||||
|
std::map<std::string, void (*)(User&, std::istringstream&)> commands_;
|
||||||
|
|
||||||
|
bool handleClient(User& client);
|
||||||
|
|
||||||
|
void addClient();
|
||||||
|
std::vector<struct pollfd>::iterator removeClient(std::vector<struct pollfd>::iterator& client);
|
||||||
|
|
||||||
|
void parseCommand(User& client);
|
||||||
|
|
||||||
|
Server(const Server& other);
|
||||||
|
Server &operator=(const Server& other);
|
||||||
|
|
||||||
|
public:
|
||||||
|
Server();
|
||||||
|
Server(int port, const std::string& password);
|
||||||
|
~Server();
|
||||||
|
|
||||||
|
void run();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // SERVER_HPP
|
||||||
77
User/User.cpp
Normal file
77
User/User.cpp
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* User.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/07 10:22:52 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2026/05/07 10:22:52 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "User.hpp"
|
||||||
|
|
||||||
|
//////////////////
|
||||||
|
// Constructors //
|
||||||
|
//////////////////
|
||||||
|
|
||||||
|
User::User() : fd(-1), nick(""), username(""), realname(""), buffer(""), authenticated(false), registered(false)
|
||||||
|
{
|
||||||
|
// std::cout << "User default constructor called" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
User::User(int fd) : fd(fd), nick(""), username(""), realname(""), buffer(""), authenticated(false), registered(false)
|
||||||
|
{
|
||||||
|
// std::cout << "User with fd constructor called" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
User::User(const User &other)
|
||||||
|
{
|
||||||
|
*this = other;
|
||||||
|
// std::cout << "User copy constructor called" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
User& User::operator=(const User &other)
|
||||||
|
{
|
||||||
|
if (this != &other)
|
||||||
|
{
|
||||||
|
fd = other.fd;
|
||||||
|
nick = other.nick;
|
||||||
|
username = other.username;
|
||||||
|
realname = other.realname;
|
||||||
|
buffer = other.buffer;
|
||||||
|
authenticated = other.authenticated;
|
||||||
|
registered = other.registered;
|
||||||
|
}
|
||||||
|
// std::cout << "User copy assignment operator called" << std::endl;
|
||||||
|
return (*this);
|
||||||
|
}
|
||||||
|
|
||||||
|
User::~User()
|
||||||
|
{
|
||||||
|
// std::cout << "User destructor called" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Getters
|
||||||
|
|
||||||
|
int User::getFd() const { return (this->fd); }
|
||||||
|
std::string User::getNick() const { return (this->nick); }
|
||||||
|
std::string User::getUsername() const { return (this->username); }
|
||||||
|
std::string User::getRealname() const { return (this->realname); }
|
||||||
|
std::string &User::getBuffer() { return (this->buffer); }
|
||||||
|
bool User::isAuthenticated() const { return (this->authenticated); }
|
||||||
|
bool User::isRegistered() const { return (this->registered); }
|
||||||
|
|
||||||
|
|
||||||
|
// Setters
|
||||||
|
|
||||||
|
void User::appendBuffer(std::string buff) { this->buffer.append(buff); }
|
||||||
|
void User::clearBuffer() { this->buffer.clear(); }
|
||||||
|
|
||||||
|
void User::setNick(std::string nick) { this->nick = nick; }
|
||||||
|
void User::setUsername(std::string username) { this->username = username; }
|
||||||
|
void User::setRealname(std::string realname) { this->realname = realname; }
|
||||||
|
|
||||||
|
void User::setAuthenticated(bool value) { this->authenticated = value; }
|
||||||
|
void User::setRegistered(bool value) { this->registered = value; }
|
||||||
61
User/User.hpp
Normal file
61
User/User.hpp
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* User.hpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/07 10:22:52 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2026/05/07 10:22:52 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef USER_HPP
|
||||||
|
|
||||||
|
# define USER_HPP
|
||||||
|
|
||||||
|
# include <iostream>
|
||||||
|
|
||||||
|
class User
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
int fd;
|
||||||
|
std::string nick;
|
||||||
|
std::string username;
|
||||||
|
std::string realname;
|
||||||
|
std::string buffer;
|
||||||
|
bool authenticated;
|
||||||
|
bool registered;
|
||||||
|
|
||||||
|
public:
|
||||||
|
User();
|
||||||
|
User(int fd);
|
||||||
|
User(const User &other);
|
||||||
|
User& operator=(const User &other);
|
||||||
|
~User();
|
||||||
|
|
||||||
|
|
||||||
|
// Getters
|
||||||
|
|
||||||
|
int getFd() const;
|
||||||
|
std::string getNick() const;
|
||||||
|
std::string getUsername() const;
|
||||||
|
std::string getRealname() const;
|
||||||
|
std::string &getBuffer();
|
||||||
|
bool isAuthenticated() const;
|
||||||
|
bool isRegistered() const;
|
||||||
|
|
||||||
|
|
||||||
|
// Setters
|
||||||
|
|
||||||
|
void setNick(std::string nick);
|
||||||
|
void setUsername(std::string username);
|
||||||
|
void setRealname(std::string realname);
|
||||||
|
void appendBuffer(std::string buff);
|
||||||
|
void clearBuffer();
|
||||||
|
|
||||||
|
void setAuthenticated(bool value);
|
||||||
|
void setRegistered(bool value);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
34
cmds/pass.cpp
Normal file
34
cmds/pass.cpp
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* pass.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/10 22:06:22 by aortigos #+# #+# */
|
||||||
|
/* Updated: 2026/05/10 22:45:31 by aortigos ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../Server/Server.hpp"
|
||||||
|
|
||||||
|
void Server::pass_cmd(User &client, std::istringstream &ss)
|
||||||
|
{
|
||||||
|
std::string args;
|
||||||
|
|
||||||
|
ss >> args;
|
||||||
|
if (client.isAuthenticated())
|
||||||
|
{
|
||||||
|
// Still need to find what server should reply to different errors
|
||||||
|
std::string res = "You are already authenticated";
|
||||||
|
send(client.getFd(), res.c_str(), res.size(), 0);
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
if (this->password_ == args)
|
||||||
|
client->authenticated = true;
|
||||||
|
else {
|
||||||
|
// Still need to find what server should reply to different errors
|
||||||
|
std::string res = "Invalid password";
|
||||||
|
send(client.getFd(), res.c_str(), res.size(), 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
50
main.cpp
Normal file
50
main.cpp
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* main.cpp :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: iherman- <iherman-@student.42malaga.com +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2026/05/06 17:57:53 by iherman- #+# #+# */
|
||||||
|
/* Updated: 2026/05/10 19:02:29 by iherman- ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "Server/Server.hpp"
|
||||||
|
#include <sstream>
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
int get_port(const char* arg)
|
||||||
|
{
|
||||||
|
std::stringstream input(arg);
|
||||||
|
int port;
|
||||||
|
|
||||||
|
if (!(input >> port))
|
||||||
|
throw std::runtime_error("Invalid port");
|
||||||
|
|
||||||
|
if (input.peek() != EOF)
|
||||||
|
throw std::runtime_error("Malformed port");
|
||||||
|
|
||||||
|
return port;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
if (argc != 3)
|
||||||
|
{
|
||||||
|
std::cerr << "Usage: ./ircserv <port> <password>" << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
int port = get_port(argv[1]);
|
||||||
|
Server server(port, argv[2]);
|
||||||
|
server.run();
|
||||||
|
}
|
||||||
|
catch (std::exception& e)
|
||||||
|
{
|
||||||
|
std::cerr << e.what() << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
55
tasks.md
Normal file
55
tasks.md
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
# Tasks for IRC
|
||||||
|
|
||||||
|
### Useful info
|
||||||
|
|
||||||
|
You can try connect IRC server with
|
||||||
|
|
||||||
|
```bash
|
||||||
|
nc localhost <port>
|
||||||
|
```
|
||||||
|
---
|
||||||
|
|
||||||
|
### User
|
||||||
|
|
||||||
|
Represents a connected IRC client.
|
||||||
|
|
||||||
|
| Attribute | Type | Description |
|
||||||
|
|-----------------|----------|------------------------------------------------|
|
||||||
|
| `fd` | `int` | TCP s-ocket file descriptor |
|
||||||
|
| `nick` | `string` | IRC nickname |
|
||||||
|
| `username` | `string` | Username |
|
||||||
|
| `realname` | `string` | Real name |
|
||||||
|
| `buffer` | `string` | Incoming data buffer, accumulates until `\r\n` |
|
||||||
|
| `authenticated` | `bool` | `true` after correct `PASS` |
|
||||||
|
| `registered` | `bool` | `true` after `NICK` + `USER` received |
|
||||||
|
|
||||||
|
**Registration flow:** `PASS` → authenticated → `NICK` + `USER` → registered → client ready
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
### First stage
|
||||||
|
|
||||||
|
- [✓] Server starts
|
||||||
|
- [✓] It accepts two params, ./ircserv <port> <password>
|
||||||
|
- [✓] The server accepts one user, user can send messages and we can see from server
|
||||||
|
- [✓] Only 1 poll()
|
||||||
|
|
||||||
|
### Second stage
|
||||||
|
|
||||||
|
- [✓] Server can handle multiple clients simultaneously
|
||||||
|
- [✓] Manage SO_REUSEADDR (restarting server fails to bind same port)
|
||||||
|
|
||||||
|
### Third stage
|
||||||
|
|
||||||
|
- [✓] Dispatcher (select function for each command)
|
||||||
|
- [ ] Implement generic parser (extract command and pass args to command function)
|
||||||
|
- [ ] Client has nickname and username
|
||||||
|
- [ ] PASS command for authenticate
|
||||||
|
|
||||||
|
### Fourth stage
|
||||||
|
|
||||||
|
- [ ] Client can create/connect to channels
|
||||||
|
- [ ] JOIN command (user can create channel)
|
||||||
|
- [ ] PRIVMSG command (send message to a channel)
|
||||||
|
|
||||||
|
*It will continue...*
|
||||||
Reference in New Issue
Block a user