about summary refs log tree commit diff
path: root/IrcTokens/README.md
blob: 216dcf8ca41ffe0a62c70287fdd154b60922be53 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# irctokens

this is a c\# port of jesopo's [irctokens](
https://github.com/jesopo/irctokens)

## 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

see the full example in [Examples/Tokens/Client.cs](../Examples/Tokens/Client.cs)

    public class Client
    {
        private readonly byte[] _bytes;
        private readonly StatefulDecoder _decoder;
        private readonly StatefulEncoder _encoder;
        private readonly Socket _socket;

        public Client()
        {
            _decoder = new StatefulDecoder();
            _encoder = new StatefulEncoder();
            _socket  = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            _bytes   = new byte[1024];
        }

        public void Start()
        {
            _socket.Connect("127.0.0.1", 6667);
            while (!_socket.Connected) Thread.Sleep(1000);

            Send(new Line {Command = "NICK", Params = new List<string> {"tokensbot"}});
            Send(new Line {Command = "USER", Params = new List<string> {"tokensbot", "0", "*", "real name"}});

            while (true)
            {
                var bytesReceived = _socket.Receive(_bytes);

                if (bytesReceived == 0)
                {
                    Console.WriteLine("! disconnected");
                    _socket.Shutdown(SocketShutdown.Both);
                    _socket.Close();
                    break;
                }

                var lines = _decoder.Push(_bytes, bytesReceived);

                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":
                            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"}
                            });
                            break;
                    }
                }
            }
        }

        private void Send(Line line)
        {
            Console.WriteLine($"> {line.Format()}");
            _encoder.Push(line);
            while (_encoder.PendingBytes.Length > 0)
                _encoder.Pop(_socket.Send(_encoder.PendingBytes, SocketFlags.None));
        }
    }