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