Created
August 7, 2014 07:09
-
-
Save Sebbyastian/935458cdbfcc6a17cfb0 to your computer and use it in GitHub Desktop.
IRC_Socket.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using System.Net.Sockets; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
class MessageHandlerDictionary | |
: Dictionary<Regex, IRC_Socket.MessageHandler> | |
{ | |
public void Add(String regex_string, IRC_Socket.MessageHandler handler) | |
{ | |
base.Add(new Regex(regex_string, RegexOptions.Compiled), handler); | |
} | |
} | |
class IRC_Socket | |
: Socket | |
{ | |
protected byte[] buffer = new byte[512]; | |
protected int offset = 0, size = 0; | |
protected MessageHandlerDictionary handler = new MessageHandlerDictionary(); | |
public delegate void MessageHandler(Match m); | |
public MessageHandlerDictionary Handler | |
{ | |
get { return handler; } | |
} | |
public IRC_Socket(AddressFamily addressFamily) | |
: base(addressFamily, SocketType.Stream, ProtocolType.Tcp) | |
{ | |
} | |
public void Receive() | |
{ | |
size += this.Receive(buffer, size, buffer.Length - size, SocketFlags.None); | |
for (;;) | |
{ | |
offset = Array.FindIndex(buffer, offset, size - offset, c => "\r\n\0".Contains((char)c)); | |
if (offset < 0) | |
{ | |
offset = size; | |
break; | |
} | |
Handle(Encoding.ASCII.GetString(buffer.Take(offset).ToArray())); | |
offset++; | |
size -= offset; | |
byte[] remainder = buffer.Skip(offset).Take(size).ToArray(); | |
Array.Copy(remainder, buffer, size); | |
offset = 0; | |
} | |
} | |
public void Send(string format, params Object[] args) | |
{ | |
this.Send(Encoding.ASCII.GetBytes(string.Format(format, args) + "\r\n")); | |
} | |
public new void Handle(string message) | |
{ | |
Debug.WriteLine(message); | |
foreach (var h in handler) | |
{ | |
Match m = h.Key.Match(message); | |
if (m.Success) | |
{ | |
h.Value(m); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Text.RegularExpressions; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string Server = "irc.freenode.net"; | |
int Port = 6667; | |
string Nickname = "Insert your nickname here"; | |
string Username = "Insert your username here"; | |
string Password = "Insert your password here"; | |
string Channels = "##newboston,##some_other_channels,..."; | |
IRC_Socket s = new IRC_Socket(System.Net.Sockets.AddressFamily.InterNetwork); | |
s.Connect(Server, Port); | |
s.Send("NICK {0}", Nickname); | |
s.Send("USER {0} \"{0}\" \"{0}\" :{0}", Username); | |
s.Handler.Add("^PING(?<param> .*)?$", m => s.Send("PONG {0}", m.Groups["param"].Value)); | |
s.Handler.Add("^PING(?<param> .*)?$", m => s.Send("JOIN {0}", Channels)); | |
s.Handler.Add("^[^ ]+ 001 ", m => { s.Send("ns identify {0} {1}", Username, Password); }); | |
s.Handler.Add("^\\:(?<fulladdress>(?<nick>[^!]+)!(?<user>[^@]+)@(?<host>[^ ]+)) PRIVMSG ##newboston \\:list$", m => s.Send("PRIVMSG ##newboston :Not yet supported, {0}", m.Groups["nick"].Value)); | |
s.Handler.Add("^\\:(?<fulladdress>(?<nick>[^!]+)!(?<user>[^@]+)@(?<host>[^ ]+)) PRIVMSG ##newboston \\:vote (?<identifier>.*)$", m => s.Send("PRIVMSG ##newboston :Not yet supported, {0}", m.Groups["nick"].Value)); | |
s.Handler.Add("^\\:(?<nick>[^ !]+)(![^ ]+)? PRIVMSG ##newboston \\:kick me$", m => s.Send("kick ##newboston {0} :lol you idiot", m.Groups["nick"].Value)); | |
while (s.Connected) | |
{ | |
s.Receive(); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment