A Console IRC Bot.
The first one is how to get on IRC with C#?. So, here it is, the first article of (hopefully) many.
Let's start by telling what IRC is. This is best done by reading the Internet Relay Chat Protocol RFC. You can find anything you want about the IRC protocol in there.
Getting on IRC is as simple as:
- Establishing a connection.
- Logging in.
- Maintaining a connection and reacting to commands.
We'll make a seperate class for the IRC functions so we could re-use it later when we want to add a UI.
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace System.Net {
public class IRC {
} /* IRC */
} /* System.Net */
Needed info.
Let's think about what info we need for a connection.
First of all, the server and port ofcourse. Then the nickname to be used, and also the userinfo. The userinfo contains the actual username (used in ident), and the Real Name (which you can see when doing a WHOIS).
So, add the IrcServer, IrcPort, IrcNick, IrcUser and IrcRealName properties.
For this example I'm going to create a single channel IRC bot. So we can also add an IrcChannel property. If you would take this further you would split that off. The first thing I'm thinking of is, create a channel object and manage each channel you're on with one of those.
I also added a bool IsInvisible property, as this is the only mode you can set as a normal user (See 3.1.5 User mode message).
The Connection.
Now that we have all our info to make our first connection, let's implement it.
I'll be using this application as an example on how to create events as well.
To start our bot, we will just have one event. The eventReceiving. This will occur every time a command gets received from the IRC server. For now we'll just write it to the console.
What we need is the following. After our namespace we add:
public delegate void CommandReceived(string IrcCommand);
public event CommandReceived eventReceiving;
This is how our application will look as a start:
using System;
using System.Net;
namespace cIRC {
class cIRC {
static void
IRC cIRC = new IRC("CumpsD", "#mypreciousss");
cIRC.eventReceiving += new CommandReceived(IrcCommandReceived);
cIRC.Connect("efnet.xs4all.nl", 6667);
} /* Main */
static void IrcCommandReceived(string IrcCommand) {
Console.WriteLine(IrcCommand);
} /* IrcCommandReceived */
} /* cIRC */
} /* cIRC */
As you can see, we have bound the eventReceiving to a local method, which will handle the data.
I'll supply the source at the end of the article so you can check out the constructor and other details yourself.
The logic behind our bot is that after we launch the .Connect on it, it keeps running, and fires off events when it detects a command. For this article I'll display everything to the console in a nice format.
First, we connect with the server and register ourself.
// Connect with the IRC server.
this.IrcConnection = new TcpClient(this.IrcServer, this.IrcPort);
this.IrcStream = this.IrcConnection.GetStream();
this.IrcReader = new StreamReader(this.IrcStream);
this.IrcWriter = new StreamWriter(this.IrcStream);
// Authenticate our user
string isInvisible = this.IsInvisble ? "8" : "0";
this.IrcWriter.WriteLine(String.Format("USER {0} {1} * :{2}", this.IrcUser, isInvisible, this.IrcRealName));
this.IrcWriter.Flush();
this.IrcWriter.WriteLine(String.Format("NICK {0}", this.IrcNick));
this.IrcWriter.Flush();
this.IrcWriter.WriteLine(String.Format("JOIN {0}", this.IrcChannel));
this.IrcWriter.Flush();
After we are connected there is a listening loop which looks like:
// Listen for commands
while (true) {
string ircCommand;
while ((ircCommand = this.IrcReader.ReadLine()) != null) {
if (eventReceiving != null) { this.eventReceiving(ircCommand); }
string[] commandParts = new string[ircCommand.Split(' ').Length];
commandParts = ircCommand.Split(' ');
if (commandParts[0].Substring(0, 1) == ":") {
commandParts[0] = commandParts[0].Remove(0, 1);
}
if (commandParts[0] == this.IrcServer) {
// Server message
switch (commandParts[1]) {
case "332": this.IrcTopic(commandParts); break;
case "333": this.IrcTopicOwner(commandParts); break;
case "353": this.IrcNamesList(commandParts); break;
case "366": /*this.IrcEndNamesList(commandParts);*/ break;
case "372": /*this.IrcMOTD(commandParts);*/ break;
case "376": /*this.IrcEndMOTD(commandParts);*/ break;
default: this.IrcServerMessage(commandParts); break;
}
} else if (commandParts[0] == "
// Server PING, send PONG back
this.IrcPing(commandParts);
} else {
//
string commandAction = commandParts[1];
switch (commandAction) {
case "JOIN": this.IrcJoin(commandParts); break;
case "PART": this.IrcPart(commandParts); break;
case "MODE": this.IrcMode(commandParts); break;
case "NICK": this.IrcNick(commandParts); break;
case "KICK": this.IrcKick(commandParts); break;
case "QUIT": this.IrcQuit(commandParts); break;
}
}
}
this.IrcWriter.Close();
this.IrcReader.Close();
this.IrcConnection.Close();
}
What is happing here is:
First we fetch a command coming from the server.
Then we split it up into parts, delimited by a space and then we decide what action to take depending on wether it's a server command or a normal user mode command.
The server and user codes can be found in the RFC if you want to add more.
Responding to commands.
My plan was just to display data, but let me show you how you can respond to commands. I know this could be solved cleaner, but I'm writing this as a proof of concept and to explain what would be required and how it works.
We want to welcome everyone joining a channel. But we want it by notice. When someone joins the IrcJoin method gets called:
private void IrcJoin(string[] IrcCommand) {
string IrcChannel = IrcCommand[2];
string IrcUser = IrcCommand[0].Split('!')[0];
if (eventJoin != null) { this.eventJoin(IrcChannel.Remove(0, 1), IrcUser); }
} /* IrcJoin */
Which in turns fires the join event and gets processed in our console app:
private void IrcJoin(string IrcChan, string IrcUser) {
Console.WriteLine(String.Format("{0} joins {1}", IrcUser, IrcChan));
IrcObject.IrcWriter.WriteLine(String.Format("NOTICE {0} :Hello {0}, welcome to {1}!", IrcUser, IrcChan));
IrcObject.IrcWriter.Flush ();
} /* IrcJoin */
I modified our console app a bit so it would create an object of itself in the Main with our IrcObject as a private variable. That way I can access the IrcWriter I created in there. Ofcourse it is a bad idea to make that writer public. A better practice would be to create methods like NoticeUser, KickUser, etc... to control it's behaviour. But that exceeds the purpose of this article.
This concludes how you use C# to get on IRC.
Here are some ideas where you could extend this application:
- Listen for certain triggers in the channel, then do some action. (Example: '!google searchterm', have your app do a query and reply the results)
- Make this bot an opp and listen in PRIVMSG for a user to authenticate and op him. (Authenticate him against Active Directory, that way you'll learn how to work with AD as well)
- Detect kicks against trusted users and take action to prevent takeovers, or auto rejoin when the bot gets kicked.
- ...
I uploaded the source so you could learn from them. Enjoy!