tangled
alpha
login
or
join now
benharri.org
/
ircsharp
1
fork
atom
IRC parsing, tokenization, and state handling in C#
1
fork
atom
overview
issues
pulls
pipelines
add example to README.md
benharri.org
5 years ago
f77737bb
c1c77aaf
+86
1 changed file
expand all
collapse all
unified
split
README.md
+86
README.md
···
3
3
this is a c\# port of jesopo's [irctokens](
4
4
https://github.com/jesopo/irctokens)
5
5
6
6
+
## usage
7
7
+
8
8
+
### tokenization
9
9
+
10
10
+
using IrcTokens;
11
11
+
12
12
+
...
13
13
+
14
14
+
var line = new Line("@id=123 :ben!~ben@host.tld PRIVMSG #channel :hello there!");
15
15
+
Console.WriteLine(line);
16
16
+
Console.WriteLine(line.Format());
17
17
+
18
18
+
### formatting
19
19
+
20
20
+
var line = new Line {Command = "USER", Params = new List<string> {"user", "0", "*", "real name"}};
21
21
+
Console.WriteLine(line);
22
22
+
Console.WriteLine(line.Format());
23
23
+
24
24
+
### stateful
25
25
+
26
26
+
see the full example in [Sample/Client.cs](Sample/Client.cs)
27
27
+
28
28
+
public class Client
29
29
+
{
30
30
+
private readonly Socket _socket;
31
31
+
private readonly StatefulDecoder _decoder;
32
32
+
private readonly StatefulEncoder _encoder;
33
33
+
private readonly byte[] _bytes;
34
34
+
35
35
+
public Client()
36
36
+
{
37
37
+
_decoder = new StatefulDecoder();
38
38
+
_encoder = new StatefulEncoder();
39
39
+
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
40
40
+
_bytes = new byte[1024];
41
41
+
}
42
42
+
43
43
+
public void Start()
44
44
+
{
45
45
+
_socket.Connect("127.0.0.1", 6667);
46
46
+
47
47
+
Send(new Line {Command = "USER", Params = new List<string> {"username", "0", "*", "real name"}});
48
48
+
Send(new Line {Command = "NICK", Params = new List<string> {"statefulbot"}});
49
49
+
50
50
+
while (true)
51
51
+
{
52
52
+
var bytesReceived = _socket.Receive(_bytes);
53
53
+
var lines = _decoder.Push(_bytes);
54
54
+
55
55
+
if (lines.Count == 0)
56
56
+
{
57
57
+
Console.WriteLine("! disconnected");
58
58
+
_socket.Shutdown(SocketShutdown.Both);
59
59
+
break;
60
60
+
}
61
61
+
62
62
+
foreach (var line in lines)
63
63
+
{
64
64
+
Console.WriteLine($"< {line.Format()}");
65
65
+
66
66
+
switch (line.Command)
67
67
+
{
68
68
+
case "PING":
69
69
+
Send(new Line {Command = "PONG", Params = line.Params});
70
70
+
break;
71
71
+
case "001":
72
72
+
Send(new Line {Command = "JOIN", Params = new List<string> {"#channel"}});
73
73
+
break;
74
74
+
}
75
75
+
}
76
76
+
}
77
77
+
}
78
78
+
79
79
+
private void Send(Line line)
80
80
+
{
81
81
+
Console.WriteLine($"> {line.Format()}");
82
82
+
_encoder.Push(line);
83
83
+
while (_encoder.PendingBytes.Length > 0)
84
84
+
_encoder.Pop(_socket.Send(_encoder.PendingBytes));
85
85
+
}
86
86
+
}
87
87
+
88
88
+
## contact
89
89
+
90
90
+
come say hi on [\#irctokens on irc.tilde.chat](https://web.tilde.chat/?join=irctokens)
91
91
+