IRC parsing, tokenization, and state handling in C#

add params Line() constructor

+26 -44
+5 -9
Examples/Tokens/Client.cs
··· 1 1 using System; 2 - using System.Collections.Generic; 3 2 using System.Net.Sockets; 4 3 using System.Threading; 5 4 using IrcTokens; ··· 26 25 _socket.Connect("127.0.0.1", 6667); 27 26 while (!_socket.Connected) Thread.Sleep(1000); 28 27 29 - Send(new Line {Command = "NICK", Params = new List<string> {"tokensbot"}}); 30 - Send(new Line {Command = "USER", Params = new List<string> {"tokensbot", "0", "*", "real name"}}); 28 + Send(new Line("NICK", "tokensbot")); 29 + Send(new Line("USER", "tokensbot", "0", "*", "real name")); 31 30 32 31 while (true) 33 32 { ··· 50 49 switch (line.Command) 51 50 { 52 51 case "PING": 53 - Send(new Line {Command = "PONG", Params = line.Params}); 52 + Send(new Line("PONG", line.Params[0])); 54 53 break; 55 54 case "001": 56 - Send(new Line {Command = "JOIN", Params = new List<string> {"#test"}}); 55 + Send(new Line("JOIN", "#test")); 57 56 break; 58 57 case "PRIVMSG": 59 - Send(new Line 60 - { 61 - Command = "PRIVMSG", Params = new List<string> {line.Params[0], "hello there"} 62 - }); 58 + Send(new Line("PRIVMSG", line.Params[0], "hello there")); 63 59 break; 64 60 } 65 61 }
+1 -2
Examples/Tokens/Program.cs
··· 1 1 using System; 2 - using System.Collections.Generic; 3 2 using IrcTokens; 4 3 5 4 namespace TokensSample ··· 14 13 Console.WriteLine(line.Format()); 15 14 16 15 // formatting 17 - var line2 = new Line {Command = "USER", Params = new List<string> {"user", "0", "*", "real name"}}; 16 + var line2 = new Line("USER", "user", "0", "*", "real name"); 18 17 Console.WriteLine(line2); 19 18 Console.WriteLine(line2.Format()); 20 19
+6
IrcTokens/Line.cs
··· 21 21 { 22 22 } 23 23 24 + public Line(string command, params string[] parameters) 25 + { 26 + Command = command; 27 + Params = parameters.ToList(); 28 + } 29 + 24 30 /// <summary> 25 31 /// Build new <see cref="Line" /> object parsed from 26 32 /// <param name="line">a string</param>
+1 -1
IrcTokens/README.md
··· 23 23 24 24 ### stateful 25 25 26 - see the full example in [Examples/Tokens/Client.cs](Examples/Tokens/Client.cs) 26 + see the full example in [Examples/Tokens/Client.cs](../Examples/Tokens/Client.cs) 27 27 28 28 public class Client 29 29 {
+13 -32
IrcTokens/Tests/Format.cs
··· 10 10 [TestMethod] 11 11 public void TestTags() 12 12 { 13 - var line = new Line 13 + var line = new Line("PRIVMSG", "#channel", "hello") 14 14 { 15 - Command = "PRIVMSG", 16 - Params = new List<string> {"#channel", "hello"}, 17 - Tags = new Dictionary<string, string> {{"id", "\\" + " " + ";" + "\r\n"}} 15 + Tags = new Dictionary<string, string> {{"id", "\\" + " " + ";" + "\r\n"}} 18 16 }.Format(); 19 17 20 18 Assert.AreEqual("@id=\\\\\\s\\:\\r\\n PRIVMSG #channel hello", line); ··· 23 21 [TestMethod] 24 22 public void TestMissingTag() 25 23 { 26 - var line = new Line {Command = "PRIVMSG", Params = new List<string> {"#channel", "hello"}}.Format(); 24 + var line = new Line("PRIVMSG", "#channel", "hello").Format(); 27 25 28 26 Assert.AreEqual("PRIVMSG #channel hello", line); 29 27 } ··· 31 29 [TestMethod] 32 30 public void TestNullTag() 33 31 { 34 - var line = new Line 35 - { 36 - Command = "PRIVMSG", 37 - Params = new List<string> {"#channel", "hello"}, 38 - Tags = new Dictionary<string, string> {{"a", null}} 39 - }.Format(); 32 + var line = new Line("PRIVMSG", "#channel", "hello") {Tags = new Dictionary<string, string> {{"a", null}}} 33 + .Format(); 40 34 41 35 Assert.AreEqual("@a PRIVMSG #channel hello", line); 42 36 } ··· 44 38 [TestMethod] 45 39 public void TestEmptyTag() 46 40 { 47 - var line = new Line 48 - { 49 - Command = "PRIVMSG", 50 - Params = new List<string> {"#channel", "hello"}, 51 - Tags = new Dictionary<string, string> {{"a", ""}} 52 - }.Format(); 41 + var line = new Line("PRIVMSG", "#channel", "hello") {Tags = new Dictionary<string, string> {{"a", ""}}} 42 + .Format(); 53 43 54 44 Assert.AreEqual("@a PRIVMSG #channel hello", line); 55 45 } ··· 57 47 [TestMethod] 58 48 public void TestSource() 59 49 { 60 - var line = new Line 61 - { 62 - Command = "PRIVMSG", Params = new List<string> {"#channel", "hello"}, Source = "nick!user@host" 63 - }.Format(); 50 + var line = new Line("PRIVMSG", "#channel", "hello") {Source = "nick!user@host"}.Format(); 64 51 65 52 Assert.AreEqual(":nick!user@host PRIVMSG #channel hello", line); 66 53 } ··· 82 69 [TestMethod] 83 70 public void TestTrailingSpace() 84 71 { 85 - var line = new Line {Command = "PRIVMSG", Params = new List<string> {"#channel", "hello world"}}.Format(); 72 + var line = new Line("PRIVMSG", "#channel", "hello world").Format(); 86 73 87 74 Assert.AreEqual("PRIVMSG #channel :hello world", line); 88 75 } ··· 90 77 [TestMethod] 91 78 public void TestTrailingNoSpace() 92 79 { 93 - var line = new Line {Command = "PRIVMSG", Params = new List<string> {"#channel", "helloworld"}}.Format(); 80 + var line = new Line("PRIVMSG", "#channel", "helloworld").Format(); 94 81 95 82 Assert.AreEqual("PRIVMSG #channel helloworld", line); 96 83 } ··· 98 85 [TestMethod] 99 86 public void TestTrailingDoubleColon() 100 87 { 101 - var line = new Line {Command = "PRIVMSG", Params = new List<string> {"#channel", ":helloworld"}}.Format(); 88 + var line = new Line("PRIVMSG", "#channel", ":helloworld").Format(); 102 89 103 90 Assert.AreEqual("PRIVMSG #channel ::helloworld", line); 104 91 } ··· 106 93 [TestMethod] 107 94 public void TestInvalidNonLastSpace() 108 95 { 109 - Assert.ThrowsException<ArgumentException>(() => 110 - { 111 - new Line {Command = "USER", Params = new List<string> {"user", "0 *", "real name"}}.Format(); 112 - }); 96 + Assert.ThrowsException<ArgumentException>(() => { new Line("USER", "user", "0 *", "real name").Format(); }); 113 97 } 114 98 115 99 [TestMethod] 116 100 public void TestInvalidNonLastColon() 117 101 { 118 - Assert.ThrowsException<ArgumentException>(() => 119 - { 120 - new Line {Command = "PRIVMSG", Params = new List<string> {":#channel", "hello"}}.Format(); 121 - }); 102 + Assert.ThrowsException<ArgumentException>(() => { new Line("PRIVMSG", ":#channel", "hello").Format(); }); 122 103 } 123 104 } 124 105 }