Monday, March 31, 2014

The IRC Connection Handshake

This isn't really documented anywhere, so I suppose I'll take a moment to write it down.

IRC is a fairly simple text-based protocol that manages interactions between servers and clients. Messages are made of either a command or server message code followed by arguments and optionally some trailing data.

So, a message from a server to a client might look like this:
:irc.some.net 999 arg moreArgs :Trailing data counted as one arg

A message from a server indicating that a client did something interesting might look like this:
:person!usrId@host.isp.net PART #chan :Goodbye everyone!

The most difficult part of creating an IRC program is getting the handshake right. To start, the first command you need to send is user. This identifies your session name and will fill the "real name" field on a who-is query.
USER sessionId irc.some.net irc.some.net :Real name

It's not really specified what to put in the two middle fields, but I find it works to just put the IP or hostname of the server you connected to. Once that's done, send a NICK command, which will set the name you use to talk to people.
NICK UncreativePersonName

If all goes well, you should receive a ping message from the server with some random letters and numbers in the trail. Send a pong back with those same characters, and you're in!
Server: PING :10A45D9F
Client: PONG :10A45D9F

There will be ping messages later in the connection that have a server name in the trail, but just put whatever trail is given to you back into the pong.

Once the server checks the pong, you'll receive an 001 (welcome) message, probably followed by all kinds of stuff, MOTD and other info. Keep replying to pongs, and you can have a stable IRC robot!

No comments:

Post a Comment