ircsharp/IRCTokens/README.md

99 lines
3.0 KiB
Markdown
Raw Permalink Normal View History

2020-05-15 03:06:10 +00:00
# IRCTokens
2020-05-06 01:32:08 +00:00
this is a c\# port of jesopo's [irctokens](
https://github.com/jesopo/irctokens)
2020-05-18 00:39:22 +00:00
available on [nuget](https://www.nuget.org/packages/IRCTokens)
2020-05-06 01:32:08 +00:00
## usage
### tokenization
2020-05-15 03:06:10 +00:00
using IRCTokens;
2020-05-06 01:32:08 +00:00
...
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<string> {"user", "0", "*", "real name"}};
Console.WriteLine(line);
Console.WriteLine(line.Format());
### stateful
2020-05-15 02:17:22 +00:00
see the full example in [TokensSample/Client.cs](../Examples/Tokens/Client.cs)
2020-05-06 01:32:08 +00:00
2020-04-22 15:42:09 +00:00
public class Client
{
2020-04-28 04:44:58 +00:00
private readonly byte[] _bytes;
2020-04-22 15:42:09 +00:00
private readonly StatefulDecoder _decoder;
private readonly StatefulEncoder _encoder;
2020-04-28 04:44:58 +00:00
private readonly Socket _socket;
2020-04-22 15:42:09 +00:00
public Client()
{
_decoder = new StatefulDecoder();
_encoder = new StatefulEncoder();
2020-04-28 04:44:58 +00:00
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
_bytes = new byte[1024];
2020-04-22 15:42:09 +00:00
}
public void Start()
{
_socket.Connect("127.0.0.1", 6667);
2020-04-29 01:07:23 +00:00
while (!_socket.Connected) Thread.Sleep(1000);
2020-04-22 15:42:09 +00:00
2020-04-28 04:44:58 +00:00
Send(new Line {Command = "NICK", Params = new List<string> {"tokensbot"}});
2020-04-28 05:52:24 +00:00
Send(new Line {Command = "USER", Params = new List<string> {"tokensbot", "0", "*", "real name"}});
2020-04-22 15:42:09 +00:00
while (true)
{
var bytesReceived = _socket.Receive(_bytes);
if (bytesReceived == 0)
2020-04-22 15:42:09 +00:00
{
Console.WriteLine("! disconnected");
_socket.Shutdown(SocketShutdown.Both);
2020-04-29 01:07:23 +00:00
_socket.Close();
2020-04-22 15:42:09 +00:00
break;
}
2020-04-29 01:07:23 +00:00
var lines = _decoder.Push(_bytes, bytesReceived);
2020-04-22 15:42:09 +00:00
foreach (var line in lines)
{
Console.WriteLine($"< {line.Format()}");
switch (line.Command)
{
case "PING":
2020-04-28 04:44:58 +00:00
Send(new Line {Command = "PONG", Params = line.Params});
2020-04-22 15:42:09 +00:00
break;
case "001":
2020-04-28 05:52:24 +00:00
Send(new Line {Command = "JOIN", Params = new List<string> {"#test"}});
break;
case "PRIVMSG":
2020-04-29 01:07:23 +00:00
Send(new Line
{
Command = "PRIVMSG", Params = new List<string> {line.Params[0], "hello there"}
});
2020-04-22 15:42:09 +00:00
break;
}
}
}
}
private void Send(Line line)
{
Console.WriteLine($"> {line.Format()}");
_encoder.Push(line);
2020-04-28 05:52:24 +00:00
while (_encoder.PendingBytes.Length > 0)
2020-04-29 01:07:23 +00:00
_encoder.Pop(_socket.Send(_encoder.PendingBytes, SocketFlags.None));
2020-04-22 15:42:09 +00:00
}
}
2020-05-06 01:32:08 +00:00