rendered paste bodyusing System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace Csharpbot_tng
{
public class Configuration
{
public static int port = 6667;
public static string server = "irc.homelien.no";
public static string nick = "CsharpTNG";
}
public class Program
{
public static string inputLine;
public static StreamWriter writer;
public static StreamReader reader;
public static NetworkStream nStream;
static TcpClient ircSock = new TcpClient();
static void Main(string[] args)
{
connections();
run();
}
static public void connections()
{
try
{
ircSock.Connect(Configuration.server, Configuration.port);
nStream = ircSock.GetStream();
reader = new StreamReader(nStream, Encoding.GetEncoding("iso-8859-15"));
writer = new StreamWriter(nStream, Encoding.GetEncoding("iso-8859-15"));
Console.WriteLine("Connecting to {0}, port: {1}", Configuration.server, Configuration.port);
send("USER Espenfjo 8 * :A simple IRC-bot using the .NET framework");
send("NICK CsharpTNG");
send("JOIN #el-csharp");
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
static public void run()
{
//Subscribe to events
while (true)
{
while (reader != null && (inputLine = reader.ReadLine()) != null)
{
inputParser(inputLine);
}
}
}
public static void inputParser(string inputLine)
{
EventHandling EH = new EventHandling();
EH.mainEventThing();
string[] args = inputLine.Split(' ');
Console.WriteLine(inputLine);
if (args[0].Substring(1) == Configuration.server && args[1] == "375")
EH.makeMOTDEvent(inputLine, args);
else if (inputLine.IndexOf("Hullo") > -1)
EH.makeHulloEvent(inputLine, args);
}
static public void send(string msg)
{
writer.WriteLine(msg);
writer.Flush();
}
}
public class EventHandling
{
//Hullo
public delegate void HulloEvent(object sender, HulloEventArgs args);
public event HulloEvent OnHullo;
bool run = false;
//MOTDEvent
public delegate void MOTDEvent(object sender, MOTDEventArgs args);
public event MOTDEvent OnMOTD;
public void mainEventThing()
{
if (!run)
{
this.OnHullo += new HulloEvent(this.handleHullo);
run = true;
}
}
public void makeMOTDEvent(string line, string[] args)
{
bool endReached = false;
string MOTD = "";
//read in all lines until 376 (end of motd) is reached
do
{
string motdline = Program.reader.ReadLine();
args = motdline.Split(' ');
if (args[1] == "376") endReached = true;
else if (args[1] == "372")
{
//Line of motd received
MOTD += motdline.Substring(motdline.IndexOf("372 " + Configuration.nick) + Configuration.nick.Length + 6) + "\n";
}
}
while (!endReached);
//only call the event if we have subscribers
if (OnMOTD != null)
{
//Create object and fire event
MOTDEventArgs mea = new MOTDEventArgs(MOTD);
OnMOTD(this, mea);
}
}
public void makeHulloEvent(string line, string[] args)
{
//Channel join - get data, create args object and fire
string[] who = args[0].Split('!');
string nick = who[0].Substring(1);
string ident = who[1];
string channel = args[2].Substring(1);
HulloEventArgs hubj = new HulloEventArgs(nick, ident, channel);
if (OnHullo != null)
OnHullo(this, hubj);
}
public void handleHullo(object sender, HulloEventArgs args)
{
Program.send("PRIVMSG #el-csharp :Hello " + args.nick);
}
}
public class MOTDEventArgs : EventArgs
{
public readonly string MOTD;
public MOTDEventArgs(string motd)
{
MOTD = motd;
}
}
public class HulloEventArgs : EventArgs
{
public readonly string nick;
public readonly string ident;
public readonly string channel;
public HulloEventArgs(string who, string id, string chan)
{
nick = who;
ident = id;
channel = chan;
}
}
}