user1080390
user1080390

Reputation: 471

Establishing a connection to IRC

Would it be possible to connect to IRC using the standard GCC libraries?

I understand that the IRC protocol is a very simple TCP protocol, and it should not be much harder than connecting to another console.

However, nickname, and channel would be additional requirements when connecting to IRC compared to a console connection which only requires and IP & Port.

Could anyone show me an example of how this is done?

Upvotes: 4

Views: 9999

Answers (1)

Igor Hatarist
Igor Hatarist

Reputation: 5442

Sure, it's easily possible! It's a pretty basic TCP protocol.

GCC is just a compiler (and a linker). There is no direct connection between GCC and IRC.
To connect to the server and communicate with it, you just need sys/socket.h; I won't go in details, there's a lot of examples and tutorials about server-client linux C/C++ programs.

Let's see and try to understand the IRC protocol using telnet (a command-line telnet client program).

First, we need to establish connection to the server (let's say it's irc.freenode.net with the default port, 6667):

$ telnet irc.freenode.net 6667

The server replies with:

:sturgeon.freenode.net NOTICE * :*** Looking up your hostname...
:sturgeon.freenode.net NOTICE * :*** Checking Ident
:sturgeon.freenode.net NOTICE * :*** No Ident response
:sturgeon.freenode.net NOTICE * :*** Couldn't look up your hostname

Now we need to send three things: PASS, USER and NICK.

PASS is a command that sends a connection password. On the public servers (which doesn't have any password) you may send PASS none.

USER is a command that tells the server your username, host name and real name. Like, when you whois someone, you see [email protected] and his "Real Name". That data the client sends just after establishing the connection.

NICK is a command to set your nickname. Of course the server should know your nickname, so you should sent it right after connection too!

So, we established a connection. Now you have to send those commands, just type them into telnet:

PASS none
NICK sorandom29      
USER blah blah blah blah

Woohoo, we've got an answer!

:lindbohm.freenode.net 001 sorandom29 :Welcome to the freenode Internet Relay Chat Network sorandom29

Another important thing - there are PING and PONG commands, so if you don't answer the server with a PONG for a long time, you get disconnected.

Let's see how it's done. We get a PING request:

PING :lindbohm.freenode.net

And then just answer it with the same parameters:

PONG :lindbohm.freenode.net

Then, you might wonder how could you join a channel.

JOIN #linux

Yeah, that's it.

:lindbohm.freenode.net 470 sorandom29 #linux ##linux :Forwarding to another channel
:sorandom29!~blah JOIN ##linux
:lindbohm.freenode.net 332 sorandom29 ##linux :Welcome to ##Linux! Freenode's general Linux support/discussion channel.

Another command is PRIVMSG, it sends an actual message to the channel or somebody else:

PRIVMSG #linux :hello guys! i'm using telnet to connect to irc and that's such a stupid idea, i have to respond to PINGs manually!

So everybody on #linux will see that:

<sorandom29> hello guys! i'm using telnet to connect to irc and that's such a stupid idea, i have to respond to PINGs manually!

Note that a : is a prefix before the message itself.

I guess that's enough to get into IRC. The whole lot of other stuff you can try yourself just by reading about IRC protocol itself, trying things with telnet or nc and then getting into coding a program using the basic sockets.

Upvotes: 18

Related Questions