This project has been created as part of the 42 curriculum by mel-mouh, hoel-mos, abougati
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.
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 customCHRIFcommand (e.g.,CHRIF !help) that provides media recommendations, server help, and the current time.
| 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 |
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).
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.
┌──────────────────────────────────────────────────────────┐
│ 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 │
└──────────────────────────────────────────────────────────┘
-
Non-blocking sockets — Every accepted client socket is set to
O_NONBLOCKviafcntl(). This ensures thatrecv()andsend()never block the event loop, even when a client is slow or unresponsive. -
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. -
Per-client write buffers (
_writeBuffers) — Outgoing messages are queued per client. ThePOLLOUTevent 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. -
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
-
Graceful shutdown —
SIGINT,SIGTERM, andSIGQUITare 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 callCloseAllSockets().SIGPIPEis explicitly ignored to prevent the server from terminating when writing to a disconnected client.
- A C++ compiler supporting the C++98 standard (e.g.,
c++,g++, orclang++). makebuild system.- A Unix-like operating system (Linux / macOS).
Clone the repository and build the project:
git clone <repository-url> ft_irc
cd ft_irc
makeThe 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) |
./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- Add a new server with address
127.0.0.1and port6667. - Set the server password to the one used at launch.
- Connect and start chatting.
nc 127.0.0.1 6667
PASS mypassword
NICK mynick
USER myuser 0 * :My Real Name
JOIN #general
PRIVMSG #general :Hello, world!Press Ctrl+C to gracefully shut down the server. The signal handler ensures all sockets are properly closed.
- RFC 2812 — Internet Relay Chat: Client Protocol — The official IRC client protocol specification.
- RFC 2811 — Internet Relay Chat: Channel Management — Channel modes and management details.
- Beej's Guide to Network Programming — An essential guide to socket programming in C/C++.
- Modern IRC Client Protocol — A modern, developer-friendly reformulation of the IRC protocol.
- poll(2) man page — Reference for the
poll()system call used for I/O multiplexing. - chirc — An IRC Server Implementation Guide — A university-level guide to implementing an IRC server.
- numeric replies references
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.