Library tutorials & articles
A Console IRC Bot
- Introduction
- Establishing the Connection
- 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.
Related articles
Related discussion
-
Help me how to dynamic create row column of TableLayoutpanel at run time ??????
by anatha1 (0 replies)
-
Very Urgent regarding deleting the images from a folder
by rameshbandi (2 replies)
-
How to Export Datagridview contents to Excel
by BarbaMariolino (8 replies)
-
Help accessing sound card
by daz4904 (0 replies)
-
How to Write a GPS Application
by stoyac (19 replies)
Related podcasts
-
Object-Oriented Programming in Ruby
In this episode, I talk with Scott Bellware about object-oriented programming in Ruby, and Ruby's object model. This is taken from a private conversation, and the audio quality suffers at times. Much thanks to Scott for allowing this to be released.This episode of the Alt.NET Podcast is bro...
i have the same error as this guy, i know this was LONG ago but i cannot figure out how to get past this !
can someone please contact me
Email: vincecarter15x15@aol.com
AIM : skatecrashrepeat
Alternative Email: Punk123@myway.com
PLEASE I NEED THIS DESPERATELY
That is not a C# error.. It's actually an IRC error, which means you connected to the server. You need to do a few commands so that you can login/etc.
Example:
USER Veeresh Veeresh :Veeresh Veeresh\r\n
NICK Veeresh\r\n
JOIN #quakenet\r\n
PRIVMSG #quakenet :Hey, what's up\r\n
i tried to run this application, i got following error, plz help me out.
NOTICE AUTH :* Looking up your hostname NOTICE AUTH : Checking Ident NOTICE AUTH :** Couldn't look up your hostname :blueyonder2.uk.quakenet.org 451 * :Register first. :blueyonder2.uk.quakenet.org 451 * :Register first. :blueyonder2.uk.quakenet.org 451 * :Register first. :blueyonder2.uk.quakenet.org 451 * :Register first. :blueyonder2.uk.quakenet.org 451 * :Register first. :blueyonder2.uk.quakenet.org 451 * :Register first. ERROR :Closing Link: by blueyonder2.uk.quakenet.org (Registration Timeout) Cannot read from a closed TextReader.
please put it in vb.net form :]
or any vb
Welcome to Developer Fusion, David!
Feel free to ask me anything on my blog if you want more info.
This thread is for discussions of A Console IRC Bot.