From 21f1e95fb8e935134a969bc3d729964d8d2aadfa Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Thu, 14 May 2020 23:06:10 -0400 Subject: rename Irc to IRC --- IRCTokens/README.md | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 IRCTokens/README.md (limited to 'IRCTokens/README.md') diff --git a/IRCTokens/README.md b/IRCTokens/README.md new file mode 100644 index 0000000..d3769aa --- /dev/null +++ b/IRCTokens/README.md @@ -0,0 +1,96 @@ +# IRCTokens + +this is a c\# port of jesopo's [irctokens]( +https://github.com/jesopo/irctokens) + +## 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 {"user", "0", "*", "real name"}}; + Console.WriteLine(line); + Console.WriteLine(line.Format()); + +### stateful + +see the full example in [TokensSample/Client.cs](../Examples/Tokens/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 {"tokensbot"}}); + Send(new Line {Command = "USER", Params = new List {"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 {"#test"}}); + break; + case "PRIVMSG": + Send(new Line + { + Command = "PRIVMSG", Params = new List {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)); + } + } + -- cgit 1.4.1