Skip to content
1-veerPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

192 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This project has been created as part of the 42 curriculum by mel-mouh, hoel-mos, abougati

ft_irc

An IRC (Internet Relay Chat) server built from scratch in C++98, following the IRC RFC specification. The server handles multiple simultaneous client connections using non-blocking I/O and poll(), and is compatible with standard IRC clients such as HexChat, irssi, and netcat.


Description

ft_irc is a project from the 42 curriculum that challenges students to implement a fully functional IRC server. The goal is to understand network programming fundamentals, socket management, the IRC protocol, and concurrent client handling, all within the constraints of the C++98 standard.

The server supports:

  • Client authentication via a connection password.
  • Nickname and username registration following IRC protocol conventions.
  • Channel management — creating, joining, parting, and deleting channels dynamically.
  • Channel operator privileges — operators can kick users, set channel modes, invite users, and manage topics.
  • Private messaging between users via PRIVMSG.
  • Channel modes: invite-only (+i), topic restriction (+t), channel key/password (+k), user limit (+l), and operator status (+o).
  • A built-in bot (chrif) accessible via the custom CHRIF command (e.g., CHRIF !help) that provides media recommendations, server help, and the current time.

Features

Supported IRC Commands

Command Description
PASS Authenticate with the server password
NICK Set or change your nickname
USER Register your username and real name
JOIN Join one or more channels (supports keys)
PART Leave a channel
PRIVMSG Send a message to a user or channel
NOTICE Send a notice to a user or channel
KICK Remove a user from a channel (operator only)
TOPIC View or set the channel topic
MODE View or change channel modes
QUIT Disconnect from the server
INFO Display server or channel information
STATUS View a user's channel membership and roles

Bot Commands

The built-in bot chrif responds to the following commands in channels or via private message:

Command Description
!help List available bot commands
!help server List available IRC server commands
!time Display the current server time
!song <genre> Get a random song recommendation by genre
!movie <genre> Get a random movie recommendation by genre
!show <genre> Get a random TV show recommendation by genre
!book <genre> Get a random book recommendation by genre

Available genres vary by media type (e.g., pop, rap, romance, action, scifi, drama, comedy, fantasy, classics).


I/O Multiplexing Architecture

The server's networking layer is built around poll()-based I/O multiplexing, enabling a single-threaded process to handle an arbitrary number of concurrent clients without spawning threads or forking processes. This component was developed by abougati.

How It Works

┌──────────────────────────────────────────────────────────┐
│                    poll() Event Loop                     │
│                                                          │
│  ┌─────────────┐    POLLIN     ┌──────────────────────┐  │
│  │   Master     │─────────────▶│  accept() new client │  │
│  │   Socket     │              │  set O_NONBLOCK      │  │
│  │  (listening) │              │  add to _fds[]       │  │
│  └─────────────┘              └──────────────────────┘  │
│                                                          │
│  ┌─────────────┐    POLLIN     ┌──────────────────────┐  │
│  │   Client     │─────────────▶│  recv() → readBuffer │  │
│  │   Socket     │              │  extract \r\n lines   │  │
│  │              │              │  processFullCommand() │  │
│  │              │   POLLOUT    ┌──────────────────────┐  │
│  │              │─────────────▶│  send() writeBuffer  │  │
│  │              │              │  drain & toggle off   │  │
│  └─────────────┘              └──────────────────────┘  │
│                                                          │
│  POLLERR/POLLHUP/POLLNVAL ──▶ removeClient() + cleanup  │
└──────────────────────────────────────────────────────────┘

Key Design Decisions

  1. Non-blocking sockets — Every accepted client socket is set to O_NONBLOCK via fcntl(). This ensures that recv() and send() never block the event loop, even when a client is slow or unresponsive.

  2. Per-client read buffers (_readBuffers) — Incoming bytes are accumulated per client until a complete \r\n-terminated IRC message is found. This handles partial reads gracefully (TCP does not guarantee message boundaries). An oversized buffer (>2048 bytes with no CRLF) is cleared as a safety measure.

  3. Per-client write buffers (_writeBuffers) — Outgoing messages are queued per client. The POLLOUT event is toggled on only when there is data to send (enqueueMessage()), and toggled off once the buffer is fully drained. This avoids busy-spinning on write-readiness.

  4. Single-pass event scanning — After poll() returns, a single loop iterates through all file descriptors. For each fd:

    • POLLERR / POLLHUP / POLLNVAL → disconnect and cleanup
    • POLLIN on master socket → accept() a new connection
    • POLLIN on client socket → drain all available data via recv() loop, then extract and process complete commands
    • POLLOUT on client socket → send() as many bytes as possible, erase the sent portion
  5. Graceful shutdown — SIGINT, SIGTERM, and SIGQUIT are caught by a static signal handler that sets an atomic flag (_Signal). The main loop checks this flag each iteration, allowing it to exit cleanly and call CloseAllSockets(). SIGPIPE is explicitly ignored to prevent the server from terminating when writing to a disconnected client.


Instructions

Prerequisites

  • A C++ compiler supporting the C++98 standard (e.g., c++, g++, or clang++).
  • make build system.
  • A Unix-like operating system (Linux / macOS).

Compilation

Clone the repository and build the project:

git clone <repository-url> ft_irc
cd ft_irc
make

The Makefile provides the following targets:

Target Description
make Build the ircserv binary
make clean Remove object files
make fclean Remove object files and the binary
make re Full rebuild (fclean + all)

Running the Server

./ircserv <port> <password>
  • <port>: The port number the server will listen on (e.g., 6667).
  • <password>: The connection password clients must provide to authenticate.

Example:

./ircserv 6667 mypassword

Connecting with an IRC Client

Using HexChat / irssi

  1. Add a new server with address 127.0.0.1 and port 6667.
  2. Set the server password to the one used at launch.
  3. Connect and start chatting.

Using netcat (for testing)

nc 127.0.0.1 6667
PASS mypassword
NICK mynick
USER myuser 0 * :My Real Name
JOIN #general
PRIVMSG #general :Hello, world!

Stopping the Server

Press Ctrl+C to gracefully shut down the server. The signal handler ensures all sockets are properly closed.


Resources

References

AI Usage

AI tools were used during the development of this project for the following purposes:

  • Debugging assistance: Identifying edge cases in message parsing and protocol compliance.
  • Documentation: Generating, enhancing and structuring overall documentation.
  • Code review: Reviewing socket handling logic and memory management patterns for potential issues.

All core logic, architecture, and implementation were written by the team members. AI was not used to generate the server's functional code.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages