Quantcast
Viewing latest article 1
Browse Latest Browse All 11

dotRant: Making an IRC library for .NET in C# – Part 1: The IRC protocol

Hi, and welcome to my first blog-series ever. This blog series is about creating a IRC library in C#, that can (hopefully xD) be used by any .NET language. If you do not know what IRC is, it stands for “Internet Relay Chat” and is an old chatting-protocol. Not that many people use IRC anymore, and many people don’t see a reason why they should use it either. For me the point about IRC is that it takes a bit of a different approach on chatting than most modern chatting-application, and it’s made to chat with groups, not single persons. The point with IRC is that you enter a “room”, or a “group”, and then send messages to them, as opposed to applications like WLM which are made to send person to person messages. I know that most modern IM’s also has the capability to do group-chats, but you need to be invited to those. You can’t just join a group based on its name and talk to persons you have never talked to before, and that’s what I use IRC for.

Then the question comes to mind, why on earth would I want to write a library for IRCing in .NET? The answer is simple, to make a bot. Or several bots actually. And in some feature, I’d really like to create an IRC client in WPF, simply for the fun of doing it, but currently I’m focusing on the bot part. Another thing to know about this series is that I don’t know when it’s going to be finished, or how complete my library will be when I reach that point, because, to simply make a capable IRC bot, does not require it to understand all of the data coming from the IRC server. It can simply disregard what it doesn’t understand, and that works out just fine. The same can be said for all IRC clients. Most clients I’ve used, if they receive something they don’t understand, they simply display it to the user as-is, without any changes, and display it as a message received from the server itself. But anyways, we’ll see more about that when we get a little further.

The IRC protocol

The IRC protocol is a simple protocol which makes creating an IRC C# library a task that’s not very complicated looking at the network part. The protocol consists of sending messages, or commands as I’ll refer them as, back and forth from the client and the server. These messages should be within a certain size (I don’t know if there is actually a set limit for the command-size, but I’ve experienced servers rejecting commands that are too long), and consists of a prefix (only received messages as far as I know), a command-name, and a (possibly empty) sequence of parameters. The parameters are separated by one or more spaces, and the command is separated by “CRLF” (windows, and telnet newline). The prefix is generally who sent the command (which might vary depending on the command), and the command-name is ether a string of letters, or a number. The prefix and the command-name is also separated by a space. Empty commands are allowed (you can send one by sending “CRLFCRLF”), and they are simply ignored by the receiver. Parameters are separated by space, and can thus normally not contain spaces, however, as this would be pretty inconvenient (you would only be able to send messages with one word) the last parameter can contain spaces by prefixing it with a colon (‘:’). An example of this would be the command below, it shows the command to send a message to the channel “#test” containing the text “This is simply a test.”.

PRIVMSG #test :This is simply a test.

To connect to an IRC network you first need to identify yourself. This isn’t like login with username and password to enter, it’s simply telling the server who you are, and what you would like to be called. This is done with the “USER”, “PASS” and the “NICK” commands. “NICK” can also be used later to change your nick to something else. An example of what a connecting client would send to the server when it connects would be something like this:

PASS *
NICK <nick>
USER guest 8 * :<full name>

What happens here is that the user tells the server that it would like to login with the nick <nick>, and that its full-name is <full name>. The 8 in the “USER”-command signifies that the user would like to be invisible, and the * in the USER-command is an unused parameter placed there for legacy-reasons.

Some of the important commands to know in IRC are the following:

  • PASS: Used when connecting to a server. In theory (and maybe in practice too) servers might have a password set, to disable users from connecting that don’t know the password, but normally you just pass “*” as the password
  • NICK: Used to set your nick. May result in the server sending an error complaining about the fact that the nick you picked is already in use by someone else.
  • USER: Used when connecting, mostly specifies full name.
  • PRIVMSG: Used to send messages to users or channels (groups).
  • JOIN: Used to join a channel.
  • PART: Used to leave a channel.
  • PONG: Used to answer the server’s ping-requests

All of these are commands that should be sent to the server from the client. One of the things that’s important to notice is the “PONG” command. From time to time the server will send you a “PING” command (if you are not active). This is to ensure that the client has not disconnected. If you do not reply to the “PING”, the server will assume that you have lost the connection, and disconnect from you. This means no more IRC until you reconnect, which is not a good thing as you probably will miss messages. “PING” is sent from the server with a single parameter, and you just have to send “PONG” back with the same parameter. Simple as that.

IRC in telnet

To try out the IRC protocol, the simplest thing to do is to test it out using telnet. If you are on Windows, you might need to turn on the telnet-client (“turn windows features on/off” from control panel). To connect to an IRC server you need to know it’s host-name or ip, and the port it’s listening on (usually 6667 for normal and 9999 for ssl). To connect, simply open a command-prompt (cmd in windows, terminal on mac, and if you are on linux and don’t know how to get a command prompt then shame on you) and type “telnet “. At first, after it’s connected, you will probably not see anything (except for an empty console). To start, you need to send the “PASS”, “NICK” and “USER” commands. After you have done this, wait a few seconds, and data should be popping down in your console. Simple as that. If you have any problems at all with getting this to work, please leave me a comment in the comment-section, and I’ll get back to you so we can figure out what is wrong).

What’s next?

Now, this was a long post about writing a library in C#, and without a single line of code. However, I hope I’ve given you a brief explanation of how IRC works, and that is required to know before we start building our library. In the next post we will start coding, and laying the foundation for the dotRant IRC library.

Until then. Alxandr.


Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing latest article 1
Browse Latest Browse All 11

Trending Articles