Ejemplo n.º 1
0
        internal static Task HandleModeMessage(IrcClient client, IrcMessage message)
        {
            if (message.Parameters.Length < 2)
            {
                throw new IrcProtocolException("Received MODE message with an invalid amount of parameters.");
            }

            string target = message.Parameters[0];
            string flags  = message.Parameters[1];

            if (target.StartsWith("#"))
            {
                string channelName = target.Substring(1);

                // Handle all mode messages related to channels.
                if (client.Channels.ContainsKey(channelName))
                {
                    IrcChannel channel = client.Channels[channelName];
                    bool       grant   = flags[0] == '+';
                    char[]     modes   = flags.Substring(1).ToCharArray();

                    foreach (char mode in modes)
                    {
                        // Operator status
                        if (mode == 'o')
                        {
                            if (message.Parameters.Length == 3)
                            {
                                string  nickName = message.Parameters[2];
                                IrcUser user     = new IrcUser(client, nickName);

                                // Check if the local user has been granted operator status and update the channel's IsOperator property if necessary.
                                if (user.IsClientUser())
                                {
                                    channel.IsOperator = true;
                                }

                                client.OnUserOperatorStatusUpdated(new IrcOperatorEventArgs(channel, user, grant));
                            }
                            else
                            {
                                throw new IrcProtocolException("Received granting operator MODE message with an invalid amount of parameters.");
                            }
                        }
                    }
                }
                else
                {
                    throw new IrcProtocolException(string.Format("Received mode message from a channel the client has not joined. (Channel: {0}", channelName));
                }
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Connects asynchronously to the server and registers the connection.
        /// </summary>
        public async Task ConnectAsync()
        {
            if (State == IrcState.Connected)
            {
                throw new InvalidOperationException("The client is already connected to a server.");
            }
            else if (State == IrcState.Connecting)
            {
                throw new InvalidOperationException("The client is already connecting to a server.");
            }


            State         = IrcState.Connecting;
            Channels      = new Dictionary <string, IrcChannel>();
            _messageQueue = new ConcurrentQueue <string>();
            _tcpClient    = new TcpClient();

            try
            {
                await _tcpClient.ConnectAsync(Host, Port);

                NetworkStream stream = _tcpClient.GetStream();
                _writer = new StreamWriter(stream)
                {
                    AutoFlush = true
                };
                _reader = new StreamReader(stream);

                User = new IrcUser(this, _registrationInfo.NickName);

                Task __ = ReceiveDataAsync();
                await RegisterConnectionAsync(_registrationInfo);

                if (Capabilities != null)
                {
                    await RequestCapabilitiesAsync(Capabilities);
                }

                Debug.WriteLine(string.Format("IrcClient connected to {0}:{1}.", Host, Port));
                State = IrcState.Connected;

                OnConnected();
            }
            catch (SocketException ex)
            {
                HandleSocketError(ex);
            }
            catch (Exception ex)
            {
                OnError(new IrcErrorEventArgs(ex));
            }
        }
Ejemplo n.º 3
0
        internal static Task HandleJoinMessage(IrcClient client, IrcMessage message)
        {
            if (message.Parameters.Length > 0)
            {
                string channelName = message.Parameters[0].Substring(1);
                var    hostMask    = IrcHostMask.Parse(message.Prefix);

                if (hostMask != null)
                {
                    IrcChannel channel;
                    var        user = new IrcUser(client, hostMask);

                    if (!client.Channels.ContainsKey(channelName))
                    {
                        // The client joined the channel.

                        if (client is TwitchIrcClient)
                        {
                            channel = new TwitchChannel(client, channelName);
                            client.Channels.Add(channelName, channel);

                            // Return to not fire the ChannelJoined event and let the ROOMSTATE message handler handle it.
                            return(Task.CompletedTask);
                        }

                        channel = new IrcChannel(client, channelName);
                        client.Channels.Add(channelName, channel);
                        client.User.Update(hostMask);
                    }
                    else
                    {
                        channel = client.Channels[channelName];
                    }

                    var e = new IrcUserEventArgs(channel, user);
                    client.OnChannelJoined(e);
                }
                else
                {
                    throw new IrcProtocolException("Invalid source.");
                }
            }
            else
            {
                throw new IrcProtocolException("1 parameter is required to determine the channel.");
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 4
0
        public bool Equals(IrcUser other)
        {
            if (other == null)
            {
                return(false);
            }

            // Only compare nicknames if one of the instances doesn't have ident or host defined.
            if (NickName != null && (Ident == null || Host == null) || other.NickName != null && (other.Ident == null || other.Host == null))
            {
                return(String.Equals(NickName, other.NickName, StringComparison.OrdinalIgnoreCase));
            }

            return(String.Equals(other.ToString(), ToString(), StringComparison.OrdinalIgnoreCase));
        }
Ejemplo n.º 5
0
        internal static Task HandleNickMessage(IrcClient client, IrcMessage message)
        {
            if (message.Parameters.Length == 1)
            {
                string newNickname = message.Parameters[0];
                var    hostMask    = IrcHostMask.Parse(message.Prefix);
                var    user        = new IrcUser(client, hostMask);

                if (user == client.User)
                {
                    client.User.NickName = newNickname;
                }
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 6
0
        internal static Task HandlePartMessage(IrcClient client, IrcMessage message)
        {
            if (message.Parameters.Length > 0)
            {
                string channelName = message.Parameters[0].Substring(1);

                if (client.Channels.ContainsKey(channelName))
                {
                    var hostMask = IrcHostMask.Parse(message.Prefix);

                    if (hostMask != null)
                    {
                        var user    = new IrcUser(client, IrcHostMask.Parse(message.Prefix));
                        var channel = client.Channels[channelName];

                        //The local user left the channel.
                        if (Equals(client.User, user))
                        {
                            client.Channels.Remove(channelName);
                            client.OnChannelLeft(new IrcChannelEventArgs(client, channel));
                        }
                        else
                        {
                            var e = new IrcUserEventArgs(channel, user);
                            channel.OnUserLeft(e);
                        }
                    }
                    else
                    {
                        throw new IrcProtocolException("Invalid source.");
                    }
                }
                else
                {
                    throw new IrcProtocolException("PART message received from a channel the client is not present in.");
                }
            }
            else
            {
                throw new IrcProtocolException("1 parameter is required to determine the channel.");
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 7
0
 public IrcOperatorEventArgs(IrcChannel channel, IrcUser user, bool statusGranted) : base(channel, user)
 {
     StatusGranted = statusGranted;
 }
Ejemplo n.º 8
0
        /// <summary>
        ///     Initializes a new instance of <see cref="IrcTargetedMessage"/>.
        /// </summary>
        /// <param name="message"></param>
        public IrcTargetedMessage(IrcClient client, IrcMessage message)
        {
            if (message.Prefix == null)
            {
                throw new ArgumentException("Message contains no prefix.");
            }

            if (message.Parameters.Length < 2)
            {
                throw new ArgumentException("The message needs to have 2 parameters to specify the target and the message.");
            }

            if (message.Command == "PRIVMSG")
            {
                Type = MessageType.Privmsg;
            }
            else if (message.Command == "NOTICE")
            {
                Type = MessageType.Notice;
            }
            else
            {
                Type = MessageType.Unknown;
            }

            IrcHostMask hostMask = IrcHostMask.Parse(message.Prefix);

            if (hostMask != null)
            {
                if (client is TwitchIrcClient)
                {
                    Source = new TwitchUser(client, hostMask, message.Tags);
                }
                else
                {
                    Source = new IrcUser(client, hostMask);
                }
            }
            else
            {
                Source = new IrcServer(message.Prefix);
            }

            if (message.Parameters[0].StartsWith("#"))
            {
                string channelName = message.Parameters[0].Substring(1);

                if (client.Channels.ContainsKey(channelName))
                {
                    Target = client.Channels[channelName];
                }
                else
                {
                    Debug.WriteLine("[IrcTargetedMessage] Could not find the targeted channel in the client's channels list.");
                }

                IsChannelMessage = true;
            }
            else
            {
                Target = client.User;
            }

            Tags    = message.Tags;
            Message = message.Parameters[1];
            Client  = client;
        }