internal void AddMessageToServerConversation(IrcMessage message)
 {
     int x;
     var type = (Int32.TryParse(message.Type, out x)) ? IrcMessageType.ServerResponse : IrcMessageType.ServerRequest;
     _serverConversation.Messages.Add(new IrcConversationServerMessage()
     {
         TimeStamp = DateTime.Now,
         Message = message.ToString(),
         IsLoginError = message.IsLoginError(),
         Type = type,
         Username = _connectionInfo.Username,
         MessageType = message.Type,
     });
 }
 internal void AddMessageToChannelConversation(IrcMessage message, String channelName, IrcMessageType type)
 {
     foreach (var c in _conversations)
     {
         if (c is IrcChannelConversation)
         {
             if (((IrcChannelConversation)c).Name == channelName)
             {
                 c.Messages.Add(new IrcConversationMessage()
                 {
                     TimeStamp = DateTime.Now,
                     Message = message.ToString(),
                     Type = type,
                     Username = _connectionInfo.Username,
                     MessageType = message.Type,
                 });
                 return;
             }
         }
     }
 }
        internal void AddMessageToPersonConversation(IrcMessage message, String username, IrcMessageType type)
        {
            foreach (var c in _conversations)
            {
                if (c is IrcPersonConversation)
                {
                    if (((IrcPersonConversation)c).Name == username)
                    {
                        c.Messages.Add(new IrcConversationMessage()
                        {
                            TimeStamp = DateTime.Now,
                            Message = message.ToString(),
                            Type = type,
                            Username = _connectionInfo.Username,
                            MessageType = message.Type,
                        });
                        return;
                    }
                }
            }

            var newConversation = CreatePersonConversation(username);
            newConversation.Messages.Add(new IrcConversationMessage()
            {
                TimeStamp = DateTime.Now,
                Message = message.ToString(),
                Type = type,
                Username = _connectionInfo.Username,
                MessageType = message.Type,
            });
        }
 /// <summary>
 /// Send IRC message
 /// </summary>
 /// <param name="request">Request to send to server</param>
 public void Send(IrcMessage request)
 {
     if (_connection != null && _connection.State == IrcConnectionState.Opened)
     {
         _connection.SendAsync(request);
     }
     else
     {
         lock (_requestQue)
         {
             _requestQue.Enqueue(request);
         }
     }
 }
        public static bool TryParse(String message, out IrcMessage output)
        {
            if (!String.IsNullOrEmpty(message))
            {
                var factory = new IrcMessageFactory();

                String prefix = GetMessagePrefix(message);
                String msgWithoutPrefix = GetMessageWithoutPrefix(prefix, message);

                String type = GetMessageType(msgWithoutPrefix);
                String msgArguments = GetMessageArguments(msgWithoutPrefix, type);
                String[] arguments = ParseArguments(msgArguments);

                if (type.Equals(IrcDefinition.Request.Ping, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]))
                    {
                        output = factory.Ping(arguments[0]);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.ErrorNotRegistred, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]) && !String.IsNullOrEmpty(arguments[1]))
                    {
                        output = factory.NotRegistred(prefix, arguments[0], arguments[1]);
                        return true;
                    }
                    else if (!String.IsNullOrEmpty(arguments[0]))
                    {
                        output = factory.NotRegistred(prefix, arguments[0]);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.ErrorAlreadyRegistred, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[1]))
                    {
                        output = factory.AlreadyRegistred(prefix, arguments[1]);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.ErrorNeedMoreParams, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]) && !String.IsNullOrEmpty(arguments[1]))
                    {
                        output = factory.NeedMoreParams(prefix, arguments[0], arguments[1]);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.ErrorNoNicknameGiven, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]))
                    {
                        output = factory.NoNickNameGiven(prefix, arguments[0]);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.ErrorErrorneusNickname, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]) && !String.IsNullOrEmpty(arguments[1]))
                    {
                        output = factory.ErrorneusNickname(prefix, arguments[0], arguments[1]);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.ErrorNicknameInUse, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]) && !String.IsNullOrEmpty(arguments[1]))
                    {
                        output = factory.NicknameInUse(prefix, arguments[0], arguments[1]);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.ErrorNicknameCollision, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]) && !String.IsNullOrEmpty(arguments[1]))
                    {
                        output = factory.NicknameCollision(prefix, arguments[0], arguments[1]);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.ListStart, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]))
                    {
                        output = factory.ListStart(prefix, arguments[0]);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.ListEnd, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]))
                    {
                        output = factory.ListEnd(prefix, arguments[0]);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.List, StringComparison.Ordinal))
                {
                    if (arguments.Length > 1)
                    {
                        var nick = arguments[0];
                        var channel = (arguments.Length > 1 && !String.IsNullOrEmpty(arguments[1])) ? arguments[1] : String.Empty;
                        var visibility = (arguments.Length > 2 && !String.IsNullOrEmpty(arguments[2])) ? arguments[2] : String.Empty;
                        var topic = (arguments.Length > 3 && !String.IsNullOrEmpty(arguments[3])) ? arguments[3] : String.Empty;
                        output = factory.List(prefix, nick, channel, visibility, topic);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.EndOfNames, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]) && !String.IsNullOrEmpty(arguments[1]) && !String.IsNullOrEmpty(arguments[2]))
                    {
                        output = factory.EndOfNames(prefix, arguments[0], arguments[1], arguments[2]);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.NoTopic, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]) && !String.IsNullOrEmpty(arguments[1]))
                    {
                        output = factory.NoTopic(prefix, arguments[0], arguments[1]);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.Topic, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]) && !String.IsNullOrEmpty(arguments[1]) && !String.IsNullOrEmpty(arguments[2]))
                    {
                        output = factory.Topic(prefix, arguments[0], arguments[1], arguments[2]);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.NameReply, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]) && !String.IsNullOrEmpty(arguments[1]) && !String.IsNullOrEmpty(arguments[2]))
                    {
                        output = factory.NameReply(prefix, arguments[0], arguments[2], arguments[3].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.ChannelCreation, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]) && !String.IsNullOrEmpty(arguments[1]) && !String.IsNullOrEmpty(arguments[2]))
                    {
                        output = factory.ChannelCreation(prefix, arguments[0], arguments[1], arguments[2], (arguments.Length > 3) ? arguments[3] : String.Empty);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.ErrorNoSuchChannel, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]) && !String.IsNullOrEmpty(arguments[1]))
                    {
                        output = factory.NoSuchChannel(prefix, arguments[0], arguments[1], (arguments.Length > 2) ? arguments[2] : String.Empty);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.ErrorTooManyChannels, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]) && !String.IsNullOrEmpty(arguments[1]))
                    {
                        output = factory.TooManyChannels(prefix, arguments[0], arguments[1], (arguments.Length > 2) ? arguments[2] : String.Empty);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.ErrorBannedFromChannel, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]) && !String.IsNullOrEmpty(arguments[1]))
                    {
                        output = factory.BannedFromChannel(prefix, arguments[0], arguments[1], (arguments.Length > 2) ? arguments[2] : String.Empty);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.ErrorInviteOnlyChannel, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]) && !String.IsNullOrEmpty(arguments[1]))
                    {
                        output = factory.InviteOnlyChannel(prefix, arguments[0], arguments[1], (arguments.Length > 2) ? arguments[2] : String.Empty);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.ErrorChannelIsFull, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]) && !String.IsNullOrEmpty(arguments[1]))
                    {
                        output = factory.ChannelIsFull(prefix, arguments[0], arguments[1], (arguments.Length > 2) ? arguments[2] : String.Empty);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Response.ErrorBadChannelKey, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]) && !String.IsNullOrEmpty(arguments[1]))
                    {
                        output = factory.BadChannelKey(prefix, arguments[0], arguments[1], (arguments.Length > 2) ? arguments[2] : String.Empty);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Request.Topic, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]))
                    {
                        output = factory.Topic(prefix, arguments[0], (arguments.Length > 1) ? arguments[1] : String.Empty);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Request.Mode, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]) && !String.IsNullOrEmpty(arguments[1]))
                    {
                        if (arguments.Length == 2)
                        {
                            output = factory.Mode(prefix, arguments[0], arguments[1]);
                            return true;
                        }
                        else if (arguments.Length > 2)
                        {
                            output = factory.Mode(prefix, arguments[0], arguments[1], arguments[2]);
                            return true;
                        }
                    }
                }
                else if (type.Equals(IrcDefinition.Request.PrivateMessage, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]) && !String.IsNullOrEmpty(arguments[1]))
                    {
                        output = factory.PrivateMessage(prefix, arguments[0], arguments[1]);
                        return true;
                    }
                }
                else if (type.Equals(IrcDefinition.Request.Nick, StringComparison.Ordinal))
                {
                    if (!String.IsNullOrEmpty(arguments[0]))
                    {
                        output = factory.SetNick(arguments[0]);
                        return true;
                    }
                }
                else
                {
                    output = new IrcMessage(type, prefix, arguments);
                    return true;
                }
            }

            output = new IrcMessage();
            return false;
        }
 /// <summary>
 /// Retrive users from NAME reply
 /// </summary>
 /// <param name="nameReply">IRC NAME reply</param>
 public void RetrieveUsers(IrcMessage nameReply)
 {
     lock (_lock)
     {
         _people.Clear();
         if (nameReply.Type == IrcDefinition.Response.NameReply)
         {
             for (int i = 2; i < nameReply.Arguments.Count; i++)
             {
                 bool isOperator = false;
                 if (nameReply.Arguments[i][0] == '@')
                 {
                     isOperator = true;
                 }
                 _people.Add(new IrcPerson(_session ,nameReply.Arguments[i].Trim(':').Trim('@')) { IsOperator = isOperator});
             }
         }
     }
 }
 public IrcMessageEventArgs(IrcMessage message)
 {
     _message = message;
 }
 /// <summary>
 /// This method sends message to IRC server where IrcConnection is connected
 /// </summary>
 /// <param name="message">Message to send to IRC server</param>
 public void SendAsync(IrcMessage message)
 {
     if (message == null)
     {
         throw new ArgumentNullException("messages");
     }
     if (message.ToString().Length > 512)
     {
         throw new ArgumentException("message length");
     }
     var sendString = message.ToString() + IrcDefinition.NewLine;
     BeginWrite(IrcDefinition.Encoding.GetBytes(sendString));
 }