Exemplo n.º 1
0
        public void HandleReceive()
        {
            while (_outgoingLines.TryDequeue(out var line))
            {
                this._connection.WriteLine(line);
                LineSent?.Invoke(this, new LineEventArgs(Line.Parse(line)));
            }

            var selectableSockets = new List <Socket>()
            {
                _connection.Socket
            };

            Socket.Select(selectableSockets, null, null, 1000);

            if (selectableSockets.Count == 0)
            {
                return;
            }

            var linesReceived = _connection.HandleReceive();

            foreach (var line in linesReceived)
            {
                if (line.Command == "PING") // Automatically respond to ping
                {
                    this._connection.WriteLine($"PONG :{line.Params[0]}");
                }

                // Gross and basic parsing, for demonstration purposes.
                Message.Type type = line.Command switch
                {
                    "372" => Message.Type.Motd,
                    "376" => Message.Type.EndOfMotd,
                    "PRIVMSG" => "#&".IndexOf(line.Params[0][0]) != -1 ? Message.Type.ChannelMessage : Message.Type.PrivateMessage,
                    "PING" => Message.Type.Ping,
                    "JOIN" => Message.Type.Join,
                    "PART" => Message.Type.Part,
                    "NOTICE" => Message.Type.Notice,
                    _ => Message.Type.Raw
                };

                MessageReceived?.Invoke(this, new MessageEventArgs(new Message(line, type)));
                LineReceived?.Invoke(this, new LineEventArgs(line));
            }
        }