ircsharp/README.md

103 lines
3.2 KiB
Markdown
Raw Normal View History

2020-04-20 00:52:41 +00:00
# irctokens
2020-04-28 05:54:16 +00:00
[![Build Status](https://drone.tildegit.org/api/badges/ben/ircsharp/status.svg)](https://drone.tildegit.org/ben/ircsharp)
2020-04-22 15:50:50 +00:00
2020-04-20 00:52:41 +00:00
this is a c\# port of jesopo's [irctokens](
https://github.com/jesopo/irctokens)
2020-04-22 15:48:54 +00:00
## usage
### tokenization
using IrcTokens;
...
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-04-29 01:16:38 +00:00
see the full example in [TokensSample/Client.cs](TokensSample/Client.cs)
2020-04-22 15:48:54 +00:00
public class Client
{
2020-04-29 01:07:23 +00:00
private readonly byte[] _bytes;
2020-04-22 15:48:54 +00:00
private readonly StatefulDecoder _decoder;
private readonly StatefulEncoder _encoder;
2020-04-29 01:07:23 +00:00
private readonly Socket _socket;
2020-04-22 15:48:54 +00:00
public Client()
{
_decoder = new StatefulDecoder();
_encoder = new StatefulEncoder();
2020-04-29 01:07:23 +00:00
_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
_bytes = new byte[1024];
2020-04-22 15:48:54 +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:48:54 +00:00
2020-04-29 01:07:23 +00:00
Send(new Line {Command = "NICK", Params = new List<string> {"tokensbot"}});
Send(new Line {Command = "USER", Params = new List<string> {"tokensbot", "0", "*", "real name"}});
2020-04-22 15:48:54 +00:00
while (true)
{
var bytesReceived = _socket.Receive(_bytes);
2020-04-29 01:07:23 +00:00
if (bytesReceived == 0)
2020-04-22 15:48:54 +00:00
{
Console.WriteLine("! disconnected");
_socket.Shutdown(SocketShutdown.Both);
2020-04-29 01:07:23 +00:00
_socket.Close();
2020-04-22 15:48:54 +00:00
break;
}
2020-04-29 01:07:23 +00:00
var lines = _decoder.Push(_bytes, bytesReceived);
2020-04-22 15:48:54 +00:00
foreach (var line in lines)
{
Console.WriteLine($"< {line.Format()}");
switch (line.Command)
{
case "PING":
Send(new Line {Command = "PONG", Params = line.Params});
break;
case "001":
2020-04-29 01:07:23 +00:00
Send(new Line {Command = "JOIN", Params = new List<string> {"#test"}});
break;
case "PRIVMSG":
Send(new Line
{
Command = "PRIVMSG", Params = new List<string> {line.Params[0], "hello there"}
});
2020-04-22 15:48:54 +00:00
break;
}
}
}
}
private void Send(Line line)
{
Console.WriteLine($"> {line.Format()}");
_encoder.Push(line);
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:48:54 +00:00
}
}
## contact
come say hi on [\#irctokens on irc.tilde.chat](https://web.tilde.chat/?join=irctokens)