using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; 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 static string chan = "#halsa"; } 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 " + Configuration.chan); } catch (Exception e) { Console.WriteLine(e.ToString()); } } static public void run() { 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 (Regex.IsMatch(inputLine, @"^:(.*?)\!(.*?) PRIVMSG (#.*?) :Hullo")) EH.makeHulloEvent(inputLine, args); else if (Regex.IsMatch(inputLine, @":(.*?)\!(.*?) PRIVMSG (.*?) :(.*?)\s?")) { if (Regex.IsMatch(inputLine, @":(.*?)\!(.*?) PRIVMSG (.*?) :\u0001")) EH.makePingEvent(inputLine, args); else if (args.Length > 3) EH.makeMsgEvent(inputLine, args); } if (inputLine.IndexOf("PING :" + Configuration.server) > -1) EH.makeServerPingEvent(inputLine, args); } static public void send(string msg) { writer.WriteLine(msg); writer.Flush(); } static public void say(string msg) { writer.WriteLine("PRIVMSG :" + msg); writer.Flush(); } } public class EventHandling { bool run = false; //MSG public delegate void MsgEvent(object sender, MsgEventArgs args); public event MsgEvent OnMsg; //Ping public delegate void PingEvent(object sender, PingEventArgs args); public event PingEvent OnPing; //Hullo public delegate void HulloEvent(object sender, HulloEventArgs args); public event HulloEvent OnHullo; //ServerPing public delegate void ServerPingEvent(object sender, ServerPingEventArgs args); public event ServerPingEvent OnServerPing; //Event public void mainEventThing() { if (!run) { this.OnHullo += new HulloEvent(this.handleHullo); this.OnPing += new PingEvent(this.handlePing); this.OnMsg += new MsgEvent(this.handleMsg); run = true; } } 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]; HulloEventArgs hubj = new HulloEventArgs(nick, ident, channel); if (OnHullo != null) OnHullo(this, hubj); } public void makeMsgEvent(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]; string msg = line.Split(':')[2]; MsgEventArgs msgea = new MsgEventArgs(nick, ident, channel, msg); if (OnMsg != null) OnMsg(this, msgea); } public void makePingEvent(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]; string msg = line.Split(':')[2]; PingEventArgs hubj = new PingEventArgs(nick, ident, channel, msg); if (OnPing != null) OnPing(this, hubj); } public void makeServerPingEvent(string line, string[] args) { string server = args[1]; ServerPingEventArgs hubj = new ServerPingEventArgs(server); if (OnServerPing != null) OnServerPing(this, hubj); } //Handling public void handleHullo(object sender, HulloEventArgs args) { Program.send("PRIVMSG " + Configuration.chan + " :Hullo " + args.nick + " on " + args.channel); } public void handlePing(object sender, PingEventArgs args) { Program.send("NOTICE " + args.nick + " :" + args.msg); } public void handleServerPing(object sender, ServerPingEventArgs args) { Program.send("PONG :" + args.server); } public void handleMsg(object sender, MsgEventArgs args) { // Program.send("PRIVMSG " + Configuration.chan + " :" + args.msg + " from " + args.nick); } } 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; } } public class MsgEventArgs : EventArgs { public readonly string nick; public readonly string ident; public readonly string channel; public readonly string msg; public MsgEventArgs(string who, string id, string chan, string msg) { nick = who; ident = id; channel = chan; this.msg = msg; } } public class PingEventArgs : EventArgs { public readonly string nick; public readonly string ident; public readonly string channel; public readonly string msg; public PingEventArgs(string who, string id, string chan, string msg) { nick = who; ident = id; channel = chan; this.msg = msg; } } public class ServerPingEventArgs : EventArgs { public readonly string server; public ServerPingEventArgs(string server) { this.server = server; } } }