A Console IRC Bot

Page 2 of 3
  1. Introduction
  2. Establishing the Connection
  3. Responding to Commands

Establishing the Connection

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 ).

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);

And right after our class we add:

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 Main(string[] args) {
                  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();

I don't have any error handling when you pick an already chosen nick. You can implement that in the listener loop and abort the connection, let the user choose another nick, and retry. 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] == "PING") {
                  // Server PING, send PONG back
                  this.IrcPing(commandParts);
            } else {
                  // Normal message
                  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 whether 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.

 

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems.” - Jamie Zawinski