Bettercallous / MiniChat

Minimalist IRC server in C++98

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MiniChat 💬

A simple IRC server

GitHub code size in bytes Code language count GitHub top language


📜 Table of contents

• Introduction

• What is IRC ?

• Why use IRC ?

• How to use ?

• Server Commands

• What is a Socket ?

• How's a connection set between a client and a server ?

• Resources


💡 Introduction :

This test project is dedicated to exploring the complex workings of sockets and their interactions within the framework of an IRC chat/server. The primary goal is to replicate the functionality of an IRC server, offering a comprehensive understanding of its operation.

What is IRC ?

Internet Relay Chat (IRC) is an Internet application that was developed by Jakko Oikarinen in Finland• Chat is the most convenient immediate way to communicate with others via Internet• There are a number of topics called “channels” through which you can chat with many people all over the world• After joining channel, you can see what other people on this channel type on their keyboards. In that situation, everyone on this channel can see whatever you type on your keyboard. You can also hold individual conversations with someone. Channels get live on different servers around the world. Some servers have only a few channels, while others have many of them.

IRC follows client-server model. It means that both client and server software are required in order to use it. Many IT (Information Technology) clients are available for different kinds of computers, so whether you have a PC, Macintosh, or UNIX work-section, you will be able to use IRC. ~ source

Why use IRC ?

IRC stands out for several reasons. Firstly, it's a protocol, not just a software, meaning its basic structure isn't tied to any specific program. This makes it reliable—your IRC channel won't disappear overnight if a particular platform shuts down or becomes restricted. Additionally, IRC is low-key and straightforward. You don't need to share personal details, and it's not cluttered with unnecessary features. While it may lack flashy avatars or intricate profiles, it excels at its core purpose:communication. For those who prefer simplicity and efficiency, the absence of unnecessary bells and whistles is actually a plus.


How to use ?

  1. Clone the repository and run make
$> git clone git@github.com:Bettercallous/MiniChat.git
$> cd MiniChat
$> make
  1. Run the server
$> ./minichat <port> <password>
  • port: The port number on which the IRC server will be listening to for incoming IRC connections.
  • password: The connection password. It will be needed by any IRC client that tries to connect to the server.

The server is compatible with irssi client, it may not work with other IRC clients.

  • Install irssi, then start it with simply typing irssi in the terminal.
  • Connect to the server with the command /connect <address> <port> in the client.
  • Now you must register in order to access the server. To do so, you'll be asked to provide the connection password, to set a nickname and a username as follows :
  • /quote PASS <serverpassword>
  • /quote NICK <nickname>
  • /quote USER <username> <hostname> <servername> <realname>

Don't stress about USER arguments they don't really matter.

In Irssi, the /quote command is used to send raw IRC commands directly to the IRC server. This allows you to execute IRC commands that are not directly supported by Irssi's built-in commands or to send commands that require special formatting.

And that's it! Now you can join channels and send private messages in the server.

Server Commands :

A command must be prefixed with slash / . Example: /JOIN random_channel

A command can be either in uppercase or lowercase.

Command Description
JOIN Join a channel. If the channel specified does not exist, a new one will be created with the given name.
INVITE Invite a user to a channel.
KICK Eject a user from the channel.
TOPIC Change or view the topic of the given channel.
MSG Send private messages between users.
QUIT Terminate a client’s connection to the server.
MODE Set or remove options (or modes) to a given target.
Mode Description
i Set/remove Invite-only channel.
o Give/take channel operator privilege.
t Set/remove the restrictions of the TOPIC command to channel operators.
k Set/remove the channel key (password).
l Set/remove the user limit to channel.

What is a Socket ?

  • A socket is an end-point of communication between 2 devices (or processes).
  • For 2 computers to send and receive a message over a network, they need to have a socket at both ends.
  • A socket is identified by an IP address concatenated with a port number. example ( 146.86.5.20:1625 ).
  • Sockets are commonly used for client and server interaction.

Here is an analogy:

Say you want to communicate/chat with your friend, who lives not at your address. For that to happen, you have to establish a "communication channel". Say, you want to do this communication using telephones. You know that there is a network of telephone lines in the city that is extended to every house.

Now, there is a telephone socket in your house, and one in your friends house. For the communication to take place, you and your friend have to connect to the network by plugging your phone to the socket, at the both end of the communication. Sockets in programming is the same, conceptually, as the telephone sockets.

In programming, you have two processes (running programs) that want to communicate with each other. For that, they have to establish a communication link between themselves. Again, there is a network available, they just need to connect to this network using some sort of sockets. Unix sockets are one such socket that provides this connectivity/pluggability to the network. So, in each of the two programs, you will have some piece of code that does the job of connecting to the network through sockets.

The rest are details.

How's a connection set between a client and a server ?

1. Setting up the Server:

The server creates a socket using the socket() function, specifying the address family (IPv4 or IPv6), socket type (typically SOCK_STREAM for TCP or SOCK_DGRAM for UDP), and protocol (usually 0 for default). Next, the server binds the socket to a specific IP address and port using the bind() function. The server then listens for incoming connections using the listen() function, specifying the maximum number of queued connections. Once listening, the server enters a loop where it waits for incoming connection requests.

2. Setting up the Client:

The client creates a socket similar to the server using the socket() function. It then establishes a connection to the server by calling the connect() function, providing the server's IP address and port number.

3. Handshake (TCP):

For TCP connections, after the client initiates the connection, a handshake occurs between the client and server to establish the connection. This involves a series of messages exchanged between the client and server to ensure both parties are ready to communicate. Once the handshake is complete, the connection is established, and both client and server can send and receive data.

4. Data Transmission:

Once the connection is established, both client and server can send and receive data using the established socket connection. Data is typically sent in chunks or packets. The send() function is used to send data from the client to the server, and the recv() function is used to receive data on the server side. Conversely, the send() function is used on the server side to send data to the client, and the recv() function is used on the client side to receive data.

5. Closing the Connection:

When the communication is complete, either the client or the server can initiate the closure of the connection. To close the connection, the client calls the close() function on its socket, or the server calls close() on the socket associated with the client's connection. This initiates a graceful closure, where any remaining data in transit is sent and acknowledged before the connection is fully closed.



📖 Resources :


About

Minimalist IRC server in C++98


Languages

Language:C++ 98.9%Language:Makefile 1.1%