This project has been created as part of the 42 curriculum by aldiaz-u, danielji, and mparra-s
This project is an IRC server written in C++98 using Linux sockets and non-blocking I/O. Any client should be able to connect and communicate with it following standard IRC protocols.
Adapted from The Linux socket API explained by Chris Kanich.
Both server and clients perform similar system calls before any connection attempt. When the server is listening, a client first requests a connection with connect() and the server accepts it by calling accept(). Once server and client are connected through a socket, they can both send and receive data from each other using recv and send.
When a client disconnects it sends an EOF message, the server reads a message of 0 bytes length and then closes the connection.
SERVER CLIENT
====== ======
getaddrinfo getaddrinfo
↓ ↓
socket socket
↓ |
bind |
↓ |
listen |
↓ ↓
accept <-------- connect
↓ ↓
recv <---------- send
↓ ↓
send ----------> recv
↓ ↓
recv <-- EOF --- close
↓
close
The recv, send and accept system calls are blocking. That means the server would block while recv waits for more data to come.
This IRC server relies on poll to achieve non-blocking I/O operations. poll watches file descriptors and tells us when any file descriptor is ready to perform I/O operations. We then only call recv when poll guarantees the data is already available, so recv returns immediately instead of blocking the server.
# Compile:
$ make
# Compile bot:
$ make bonus
# Run:
$ ./ircserv <port> <password>
# Run bot:
$ ./ircserv_bonus
# For example:
$ ./ircserv 6667 1234
# Press Ctrl+C to stop and exit the program:
$ ^CYou can use Ncat or Netcat to connect to the IRC server. Run nc with -C option to use CRLF for end of line sequence. Specify the same host and port used in the server. For example:
nc -C localhost 6667To complete the registration process send the following commands in this order. Replace <pass> by the server's password and <nick> by any valid nickname of your choice.
CAP LS 302
PASS <pass>
NICK <nick>
USER <nick> 0 * :realname
CAP END
Upon registration you may now join to channels, send private messages, etc.
JOIN #42madrid
PRIVMSG #42madrid :Hello you all!
Type Ctrl+C to quit.
HexChat is an open source IRC client. It provides a friendly interface to interact with.
When using HexChat you must prefix every command with /, (i.e. /join #42madrid).
- Open HexChat, fill in Nick name and User name.
- Press Add to add a new network and then press Edit....
- Change
newserver/6697to the server's IP and port. (For example,localhost/6667or127.0.0.1/6667). - Uncheck
Use SSL for all the servers on this network. - Set Login method to
DefaultorServer password (/PASS password). - Press Close to apply the changes and finally click on Connect.
Clients can send the following commands to the IRC server:
| Command | Description | Example | Reference |
|---|---|---|---|
CAP |
Used during user registration for capability negotiation between a server and a client. | CAP * LS 302 |
CAP |
PASS |
Set a connection password. | PASS 1234 |
PASS |
NICK |
Give the client a nickname or change the previous one. | NICK homer89 |
NICK |
USER |
Used at the beginning of a connection to specify the username and realname of a new user. | USER homer 0 * :Homer Simpson |
USER |
PING |
Sent by either clients or servers to check if the other side of the connection is still connected. | PING LAG1781564777701 |
PING |
QUIT |
Terminate a client’s connection to the server. | QUIT :Gone to evaluate someone's Libft |
QUIT |
JOIN |
Join or create a channel. (notes) | JOIN #born2code |
JOIN |
PART |
Exit from the given channels. | PART #born2code,#42madrid :Absorbed by the blackhole |
PART |
TOPIC |
Change or view the topic of the given channel. (notes) | TOPIC #catmemes :Share your favorite cat memes |
TOPIC |
NAMES |
List the nicknames joined to a channel. Channel operators are prefixed with @. |
NAMES #42madrid |
NAMES |
LIST |
Get a list of channels along with some information about each channel. | LIST |
LIST |
INVITE |
Invite a user to a channel. | INVITE aldiaz-u #born2code |
INVITE |
KICK |
Remove a user from a channel. | KICK #born2code danielji :Very noisy user |
KICK |
PRIVMSG |
Send private messages between users or to members of channels. | PRIVMSG rachel-g :How you doin? |
PRIVMSG |
NOTICE |
Send notices between users or to members of channels. | NOTICE #born2code :Can someone evaluate an Inception? |
NOTICE |
WHO |
Query a list of users from a channel. | WHO #42madrid |
WHO |
MODE |
Set or unset options from a channel. (notes) | MODE #mychan +k 12345 |
MODE |
Use JOIN to join an existing channel or create a new one. If the specified channel doesn't exist it will be created and you will become a channel operator.
You may specify a list of comma-separated channels.
Provide a password argument if the channel requires it or if you want to create a password-protected channel.
JOIN #born2code
JOIN #born2code 1234
JOIN #born2code,#42madrid 1234
If the protected topic mode is set on a channel, clients must have appropriate channel permissions to modify the topic of that channel.
TOPIC #catmemes
TOPIC #catmemes :Share your favorite cat memes
Set (+) or unset (-) options from a channel. You may combine multiple options and their arguments in one single command. The server should reply with a list of the applied changes, if any.
+i: Set channel to invite-only mode+t: Only channel operators can modify topic+k: Set a password to join channel+l: Set a user limit to a channel+o: Give operator privileges to a user
MODE #mychan
MODE #mychan +k 1234
MODE #mychan +l 100
MODE #mychan +o joey33
MODE #mychan +ikl-t 1234 100
Run the bot in a different terminal from the server. The bot will join every
public channel available and also create its own #Omni-bot channel.
The bot acts as an AI assistant that connects to a public API to answer any
question users ask. Use the !ask command followed by your question and the
bot will reply in the same channel, with a response of up to 400 characters.
- Jack Allnutt, Daniel Oaks, Val Lorentz: Modern IRC Client Protocol
- IRC Protocol Documentation
- The UChicago χ-Projects: chirc
- Chris Kanich: The Linux socket API explained
- Brian “Beej Jorgensen” Hall: Beej's Guide to Network Programming
AI tools were helpful to clarify basic concepts about socket connections and non-blocking operations, as well as to solve bugs we were having trouble with.
Additionaly we asked AI tools how to force system errors to test the server's error handling.
