ircsharp/IRCTokens
Ben Harris 35bbd30c25
continuous-integration/drone/push Build is passing Details
Move tests to a separate project
2020-11-10 18:35:21 -05:00
..
EnumerableExtensions.cs add nuget packaging metadata 2020-05-15 16:12:20 -04:00
Hostmask.cs rename Irc to IRC 2020-05-14 23:17:47 -04:00
IRCTokens.csproj Move tests to a separate project 2020-11-10 18:35:21 -05:00
Line.cs rename Irc to IRC 2020-05-14 23:17:47 -04:00
README.md add some links to READMEs 2020-05-17 20:39:22 -04:00
StatefulDecoder.cs Add xmldocs, and handle null bytes 2020-05-16 22:08:40 -04:00
StatefulEncoder.cs rename Irc to IRC 2020-05-14 23:17:47 -04:00

README.md

IRCTokens

this is a c# port of jesopo's irctokens

available on nuget

usage

tokenization

using IRCTokens;

...

var line = new Line("@id=123 :ben!~ben@host.tld PRIVMSG #channel :hello there!");
Console.WriteLine(line);
Console.WriteLine(line.Format());

formatting

var line = new Line {Command = "USER", Params = new List<string> {"user", "0", "*", "real name"}};
Console.WriteLine(line);
Console.WriteLine(line.Format());

stateful

see the full example in TokensSample/Client.cs

public class Client
{
    private readonly byte[] _bytes;
    private readonly StatefulDecoder _decoder;
    private readonly StatefulEncoder _encoder;
    private readonly Socket _socket;

    public Client()
    {
        _decoder = new StatefulDecoder();
        _encoder = new StatefulEncoder();
        _socket  = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
        _bytes   = new byte[1024];
    }

    public void Start()
    {
        _socket.Connect("127.0.0.1", 6667);
        while (!_socket.Connected) Thread.Sleep(1000);

        Send(new Line {Command = "NICK", Params = new List<string> {"tokensbot"}});
        Send(new Line {Command = "USER", Params = new List<string> {"tokensbot", "0", "*", "real name"}});

        while (true)
        {
            var bytesReceived = _socket.Receive(_bytes);

            if (bytesReceived == 0)
            {
                Console.WriteLine("! disconnected");
                _socket.Shutdown(SocketShutdown.Both);
                _socket.Close();
                break;
            }

            var lines = _decoder.Push(_bytes, bytesReceived);

            foreach (var line in lines)
            {
                Console.WriteLine($"< {line.Format()}");

                switch (line.Command)
                {
                    case "PING":
                        Send(new Line {Command = "PONG", Params = line.Params});
                        break;
                    case "001":
                        Send(new Line {Command = "JOIN", Params = new List<string> {"#test"}});
                        break;
                    case "PRIVMSG":
                        Send(new Line
                        {
                            Command = "PRIVMSG", Params = new List<string> {line.Params[0], "hello there"}
                        });
                        break;
                }
            }
        }
    }

    private void Send(Line line)
    {
        Console.WriteLine($"> {line.Format()}");
        _encoder.Push(line);
        while (_encoder.PendingBytes.Length > 0)
            _encoder.Pop(_socket.Send(_encoder.PendingBytes, SocketFlags.None));
    }
}