IRC parsing, tokenization, and state handling in C#

move example projects into a subfolder

+110 -100
+2 -2
IrcSharp.sln
··· 5 5 MinimumVisualStudioVersion = 10.0.40219.1 6 6 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IrcTokens", "IrcTokens\IrcTokens.csproj", "{9E812F45-B2CD-42D2-8378-EBEBF8697905}" 7 7 EndProject 8 - Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TokensSample", "TokensSample\TokensSample.csproj", "{A45DA39B-6B47-4713-8049-3B36E0235B67}" 8 + Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TokensSample", "Examples\Tokens\TokensSample.csproj", "{A45DA39B-6B47-4713-8049-3B36E0235B67}" 9 9 EndProject 10 10 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IrcStates", "IrcStates\IrcStates.csproj", "{233E3CB4-61F1-4368-9139-7E9F4A58ED2D}" 11 11 EndProject 12 - Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StatesSample", "StatesSample\StatesSample.csproj", "{BC9F6696-9D83-4F7A-9E15-CE4D3626C1AF}" 12 + Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StatesSample", "Examples\States\StatesSample.csproj", "{BC9F6696-9D83-4F7A-9E15-CE4D3626C1AF}" 13 13 EndProject 14 14 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{1A85EB22-D7B4-417F-AC3B-DAFD97DDEA08}" 15 15 ProjectSection(SolutionItems) = preProject
+96
IrcTokens/README.md
··· 1 + # irctokens 2 + 3 + this is a c\# port of jesopo's [irctokens]( 4 + https://github.com/jesopo/irctokens) 5 + 6 + ## usage 7 + 8 + ### tokenization 9 + 10 + using IrcTokens; 11 + 12 + ... 13 + 14 + var line = new Line("@id=123 :ben!~ben@host.tld PRIVMSG #channel :hello there!"); 15 + Console.WriteLine(line); 16 + Console.WriteLine(line.Format()); 17 + 18 + ### formatting 19 + 20 + var line = new Line {Command = "USER", Params = new List<string> {"user", "0", "*", "real name"}}; 21 + Console.WriteLine(line); 22 + Console.WriteLine(line.Format()); 23 + 24 + ### stateful 25 + 26 + see the full example in [Examples/Tokens/Client.cs](Examples/Tokens/Client.cs) 27 + 28 + public class Client 29 + { 30 + private readonly byte[] _bytes; 31 + private readonly StatefulDecoder _decoder; 32 + private readonly StatefulEncoder _encoder; 33 + private readonly Socket _socket; 34 + 35 + public Client() 36 + { 37 + _decoder = new StatefulDecoder(); 38 + _encoder = new StatefulEncoder(); 39 + _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); 40 + _bytes = new byte[1024]; 41 + } 42 + 43 + public void Start() 44 + { 45 + _socket.Connect("127.0.0.1", 6667); 46 + while (!_socket.Connected) Thread.Sleep(1000); 47 + 48 + Send(new Line {Command = "NICK", Params = new List<string> {"tokensbot"}}); 49 + Send(new Line {Command = "USER", Params = new List<string> {"tokensbot", "0", "*", "real name"}}); 50 + 51 + while (true) 52 + { 53 + var bytesReceived = _socket.Receive(_bytes); 54 + 55 + if (bytesReceived == 0) 56 + { 57 + Console.WriteLine("! disconnected"); 58 + _socket.Shutdown(SocketShutdown.Both); 59 + _socket.Close(); 60 + break; 61 + } 62 + 63 + var lines = _decoder.Push(_bytes, bytesReceived); 64 + 65 + foreach (var line in lines) 66 + { 67 + Console.WriteLine($"< {line.Format()}"); 68 + 69 + switch (line.Command) 70 + { 71 + case "PING": 72 + Send(new Line {Command = "PONG", Params = line.Params}); 73 + break; 74 + case "001": 75 + Send(new Line {Command = "JOIN", Params = new List<string> {"#test"}}); 76 + break; 77 + case "PRIVMSG": 78 + Send(new Line 79 + { 80 + Command = "PRIVMSG", Params = new List<string> {line.Params[0], "hello there"} 81 + }); 82 + break; 83 + } 84 + } 85 + } 86 + } 87 + 88 + private void Send(Line line) 89 + { 90 + Console.WriteLine($"> {line.Format()}"); 91 + _encoder.Push(line); 92 + while (_encoder.PendingBytes.Length > 0) 93 + _encoder.Pop(_socket.Send(_encoder.PendingBytes, SocketFlags.None)); 94 + } 95 + } 96 +
+10 -96
README.md
··· 1 - # irctokens 2 - 3 - [![Build Status](https://drone.tildegit.org/api/badges/ben/ircsharp/status.svg)](https://drone.tildegit.org/ben/ircsharp) 4 - 5 - this is a c\# port of jesopo's [irctokens]( 6 - https://github.com/jesopo/irctokens) 7 - 8 - ## usage 9 - 10 - ### tokenization 11 - 12 - using IrcTokens; 13 - 14 - ... 15 - 16 - var line = new Line("@id=123 :ben!~ben@host.tld PRIVMSG #channel :hello there!"); 17 - Console.WriteLine(line); 18 - Console.WriteLine(line.Format()); 19 - 20 - ### formatting 21 - 22 - var line = new Line {Command = "USER", Params = new List<string> {"user", "0", "*", "real name"}}; 23 - Console.WriteLine(line); 24 - Console.WriteLine(line.Format()); 25 - 26 - ### stateful 27 - 28 - see the full example in [TokensSample/Client.cs](TokensSample/Client.cs) 29 - 30 - public class Client 31 - { 32 - private readonly byte[] _bytes; 33 - private readonly StatefulDecoder _decoder; 34 - private readonly StatefulEncoder _encoder; 35 - private readonly Socket _socket; 36 - 37 - public Client() 38 - { 39 - _decoder = new StatefulDecoder(); 40 - _encoder = new StatefulEncoder(); 41 - _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); 42 - _bytes = new byte[1024]; 43 - } 44 - 45 - public void Start() 46 - { 47 - _socket.Connect("127.0.0.1", 6667); 48 - while (!_socket.Connected) Thread.Sleep(1000); 49 - 50 - Send(new Line {Command = "NICK", Params = new List<string> {"tokensbot"}}); 51 - Send(new Line {Command = "USER", Params = new List<string> {"tokensbot", "0", "*", "real name"}}); 52 - 53 - while (true) 54 - { 55 - var bytesReceived = _socket.Receive(_bytes); 1 + # IrcSharp 56 2 57 - if (bytesReceived == 0) 58 - { 59 - Console.WriteLine("! disconnected"); 60 - _socket.Shutdown(SocketShutdown.Both); 61 - _socket.Close(); 62 - break; 63 - } 3 + [![Build Status](https://drone.tildegit.org/api/badges/ben/irctokens/status.svg)](https://drone.tildegit.org/ben/irctokens) 64 4 65 - var lines = _decoder.Push(_bytes, bytesReceived); 5 + this is a collection of c\# projects to tokenize, parse, and maintain 6 + state for IRC clients. 66 7 67 - foreach (var line in lines) 68 - { 69 - Console.WriteLine($"< {line.Format()}"); 8 + unless otherwise noted, this is mostly a port of jesopo's python libraries. 70 9 71 - switch (line.Command) 72 - { 73 - case "PING": 74 - Send(new Line {Command = "PONG", Params = line.Params}); 75 - break; 76 - case "001": 77 - Send(new Line {Command = "JOIN", Params = new List<string> {"#test"}}); 78 - break; 79 - case "PRIVMSG": 80 - Send(new Line 81 - { 82 - Command = "PRIVMSG", Params = new List<string> {line.Params[0], "hello there"} 83 - }); 84 - break; 85 - } 86 - } 87 - } 88 - } 10 + - [irctokens](https://github.com/jesopo/irctokens) 11 + - [ircstates](https://github.com/jesopo/ircstates) 12 + - [ircrobots](https://github.com/jesopo/ircrobots) 89 13 90 - private void Send(Line line) 91 - { 92 - Console.WriteLine($"> {line.Format()}"); 93 - _encoder.Push(line); 94 - while (_encoder.PendingBytes.Length > 0) 95 - _encoder.Pop(_socket.Send(_encoder.PendingBytes, SocketFlags.None)); 96 - } 97 - } 98 - 99 - ## contact 100 - 101 - come say hi on [\#irctokens on irc.tilde.chat](https://web.tilde.chat/?join=irctokens) 14 + discussion and support on irc: [#irctokens]( 15 + https://web.tilde.chat/?join=irctokens) 102 16
StatesSample/Program.cs Examples/States/Program.cs
+1 -1
StatesSample/StatesSample.csproj Examples/States/StatesSample.csproj
··· 6 6 </PropertyGroup> 7 7 8 8 <ItemGroup> 9 - <ProjectReference Include="..\IrcStates\IrcStates.csproj" /> 9 + <ProjectReference Include="..\..\IrcStates\IrcStates.csproj" /> 10 10 </ItemGroup> 11 11 12 12 </Project>
TokensSample/Client.cs Examples/Tokens/Client.cs
TokensSample/Program.cs Examples/Tokens/Program.cs
+1 -1
TokensSample/TokensSample.csproj Examples/Tokens/TokensSample.csproj
··· 6 6 </PropertyGroup> 7 7 8 8 <ItemGroup> 9 - <ProjectReference Include="..\IrcTokens\IrcTokens.csproj" /> 9 + <ProjectReference Include="..\..\IrcTokens\IrcTokens.csproj" /> 10 10 </ItemGroup> 11 11 12 12 </Project>