ARTICLE

C# IRC Bot

Posted by Pasihavia Havia Articles | Networking November 19, 2001
This is a very simple program that establishes a connection to irc server, joins a channel and greets every nick that joins.
Reader Level:
Download Files:
 

This is a very simple program that establishes a connection to irc server, joins a channel and greets every nick that joins. This can be very helpful for all of you irc addicts that want to make your own bot written in C# language.

IrcBot.cs:
using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
/*
* This program establishes a connection to irc server, joins a channel and greets every nickname that
* joins the channel.
*
* Coded by Pasi Havia 17.11.2001 http://koti.mbnet.fi/~curupted
*/

class IrcBot
{
// Irc server to connect
public static string SERVER = "irc.df.lth.se";
// Irc server's port (6667 is default port)
private static int PORT = 6667;
// User information defined in RFC 2812 (Internet Relay Chat: Client Protocol) is sent to irc server
private static string USER = "USER CSharpBot 8 * :I'm a C# irc bot";
// Bot's nickname
private static string NICK = "BotNick";
// Channel to join
private static string CHANNEL = "#my_channel";
// StreamWriter is declared here so that PingSender can access it
public static StreamWriter writer;
static void Main (string[] args)
{
NetworkStream stream;
TcpClient irc;
string inputLine;
StreamReader reader;
string nickname;
try
{
irc =
new TcpClient (SERVER, PORT);
stream = irc.GetStream ();
reader =
new StreamReader (stream);
writer =
new StreamWriter (stream);
// Start PingSender thread
PingSender ping = new PingSender ();
ping.Start ();
writer.WriteLine (USER);
writer.Flush ();
writer.WriteLine ("NICK " + NICK);
writer.Flush ();
writer.WriteLine ("JOIN " + CHANNEL);
writer.Flush ();
while (true)
{
while ( (inputLine = reader.ReadLine () ) != null )
{
if (inputLine.EndsWith ("JOIN :" + CHANNEL) )
{
// Parse nickname of person who joined the channel
nickname = inputLine.Substring(1, inputLine.IndexOf ("!") - 1);
// Welcome the nickname to channel by sending a notice
writer.WriteLine ("NOTICE " + nickname + " :Hi " + nickname +
" and welcome to " + CHANNEL + " channel!");
writer.Flush ();
// Sleep to prevent excess flood
Thread.Sleep (2000);
}
}
// Close all streams
writer.Close ();
reader.Close ();
irc.Close ();
}
}
catch (Exception e)
{
// Show the exception, sleep for a while and try to establish a new connection to irc server
Console.WriteLine (e.ToString () );
Thread.Sleep (5000);
string[] argv = { };
Main (argv);
}
}
}
PingSender.cs:
using System;
using System.Threading;
/*
* Class that sends PING to irc server every 15 seconds
*/
class PingSender
{
static string PING = "PING :";
private Thread pingSender;
// Empty constructor makes instance of Thread
public PingSender ()
{
pingSender =
new Thread (new ThreadStart (this.Run) );
}
// Starts the thread
public void Start ()
{
pingSender.Start ();
}
// Send PING to irc server every 15 seconds
public void Run ()
{
while (true)
{
IrcBot.writer.WriteLine (PING + IrcBot.SERVER);
IrcBot.writer.Flush ();
Thread.Sleep (15000);
}
}
}

Login to add your contents and source code to this article
post comment
     

Here, I fixed this up. I think the problem with that code is that its so old. Anyways, this is kinda hackish, and I'm pretty new to C#, but it connects and stays connected, and has the basic framework for adding more commands, etc to it. Let me know if it doesn't work on your server, and what the server is. I've only tested this on one server running Unreal3.2.8.1.



main.cs

using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
/* 
* This program establishes a connection to irc server and joins a channel. Thats it.
*
* Coded by Pasi Havia 17.11.2001 http://koti.mbnet.fi/~curupted
*
* Updated / fixed by Blake 09.10.2010
*/
class IrcBot
{
    // Irc server to connect 
    public static string SERVER = "irc.changeme.com";
    // Irc server's port (6667 is default port)
    private static int PORT = 6667;
    // User information defined in RFC 2812 (Internet Relay Chat: Client Protocol) is sent to irc server 
    private static string USER = "USER IrcBot 0 * :IrcBot";
    // Bot's nickname
    private static string NICK = "IrcBot";
    // Channel to join
    private static string CHANNEL = "#opers";

    public static StreamWriter writer;
    static void Main(string[] args)
    {
        NetworkStream stream;
        TcpClient irc;
        string inputLine;
        StreamReader reader;
        try
        {
            irc = new TcpClient(SERVER, PORT);
            stream = irc.GetStream();
            reader = new StreamReader(stream);
            writer = new StreamWriter(stream);
            writer.WriteLine("NICK " + NICK);
            writer.Flush();
            writer.WriteLine(USER);
            writer.Flush();
            while (true)
            {
                while ((inputLine = reader.ReadLine()) != null)
                {
                    Console.WriteLine("<-" + inputLine);

                    // Split the lines sent from the server by spaces. This seems the easiest way to parse them.
                    string [] splitInput = inputLine.Split(new Char[] { ' ' });

                    if (splitInput[0] == "PING")
                    {
                        string PongReply = splitInput[1];
                        //Console.WriteLine("->PONG " + PongReply);
                        writer.WriteLine("PONG " + PongReply);
                        writer.Flush();
                        continue;
                    }

                    switch (splitInput[1])
                    {
                        // This is the 'raw' number, put out by the server. Its the first one
                        // so I figured it'd be the best time to send the join command.
                        // I don't know if this is standard practice or not.
                        case "001":
                            string JoinString = "JOIN " + CHANNEL;
                            writer.WriteLine(JoinString);
                            writer.Flush();
                            break;
                        default:
                            break;
                    }
                }
                // Close all streams
                writer.Close();
                reader.Close();
                irc.Close();
            }
        }
        catch (Exception e)
        {
            // Show the exception, sleep for a while and try to establish a new connection to irc server
            Console.WriteLine(e.ToString());
            Thread.Sleep(5000);
            string[] argv = { };
            Main(argv);
        }
    }
}

Posted by Blake Bartlett Oct 09, 2010

Tried this Using MS Visual Studio 2008 Pro on a Vista Home Premium 64 Bit Box. It connects, I can find it using /whois but it will not go to the specified channel.

any help would be appreciated.. 

Posted by Scott Dec 07, 2009

Wow. Could you post the things i would need for the: using System;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;


-Colt

Posted by colt eaves Aug 29, 2009

Hi, i'm coding in vb2005. After i got connected to server. How do i specific which channel i sent message? I am using delegate too

Posted by supra56 Oct 29, 2007

could not connect.

error: can not read from a closed text reader.

please help!!!

 

EMAIL: vincecarter15x15@aol.com

Alternative Email: Punk123@myway.com

AIM: skatecrashrepeat

 

PLEASE HELP!!!

Posted by stephen maloney Apr 19, 2007
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.