private void OnMessageReceived(Object sender, IrcMessageEventArgs e) { var message = e.Message; var type = e.Message.Type; switch (type) { case IrcDefinition.Request.Ping: Pong(message.Arguments[0]); return; case IrcDefinition.Response.ErrorNotRegistred: ChangeState(IrcSessionState.Opening, new IrcException(String.Format(Resources.NotRegistred, message.Arguments[0]))); break; case IrcDefinition.Response.ErrorNeedMoreParams: NotifyChangeState(IrcSessionState.Error, new IrcException(String.Format(Resources.NeedMoreParams, message.Arguments[0]))); break; case IrcDefinition.Response.ErrorAlreadyRegistred: NotifyChangeState(IrcSessionState.Error, new IrcException(String.Format(Resources.AlreadyRegistred, message.Arguments[0]))); break; case IrcDefinition.Response.ErrorNoNicknameGiven: ChangeState(IrcSessionState.Opening, new IrcException(String.Format(Resources.NoNickname, message.Arguments[0]))); break; case IrcDefinition.Response.ErrorErrorneusNickname: ChangeState(IrcSessionState.Opening, new IrcException(String.Format(String.Format(Resources.ErrorneusNickname, message.Arguments[0], message.Arguments[1])))); break; case IrcDefinition.Response.ErrorNicknameInUse: ChangeState(IrcSessionState.Opening, new IrcException(String.Format(Resources.NicknameInUse, message.Arguments[0], message.Arguments[1]))); break; case IrcDefinition.Response.ErrorNicknameCollision: ChangeState(IrcSessionState.Opening, new IrcException(String.Format(Resources.NicknameCollision, message.Arguments[0], message.Arguments[1]))); break; case IrcDefinition.Request.Nick: case IrcDefinition.Response.ListStart: case IrcDefinition.Response.ListEnd: case IrcDefinition.Response.EndOfNames: return; case IrcDefinition.Response.List: _channels.Add(new IrcChannel(this, message.Arguments[1], (message.Arguments.Count > 3) ? message.Arguments[3] : String.Empty)); return; case IrcDefinition.Response.NoTopic: { AddMessageToChannelConversation(message, message.Arguments[1], IrcMessageType.ChannelResponse); return; } case IrcDefinition.Response.Topic: { if (Channels.Where(c => c.Name == message.Arguments[1]).Count() > 0) { Channels.Where(c => c.Name == message.Arguments[1]).First().Topic = message.Arguments[2]; } AddMessageToChannelConversation(message, message.Arguments[1], IrcMessageType.ChannelResponse); return; } case IrcDefinition.Response.NameReply: { if (Channels.Where(c => c.Name == message.Arguments[1]).Count() > 0) { Channels.Where(c => c.Name == message.Arguments[1]).First().RetrieveUsers(message); } return; } case IrcDefinition.Request.Join: { var name = IrcPerson.GetUsernameFromFullname(message.Prefix); if (Channels.Where(c => c.Name == message.Arguments[0]).Count() > 0) { Channels.Where(c => c.Name == message.Arguments[0]).First().AddUser(new IrcPerson(this, name)); } AddMessageToChannelConversation(message, message.Arguments[0], IrcMessageType.ChannelRequest); return; } case IrcDefinition.Request.Part: { var name = message.Prefix; if (message.Prefix.IndexOf('!') > 0) { name = message.Prefix.Substring(0, message.Prefix.IndexOf('!')); } if (Channels.Where(c => c.Name == message.Arguments[0]).Count() > 0) { Channels.Where(c => c.Name == message.Arguments[0]).First().RemoveUser(new IrcPerson(this, name)); } AddMessageToChannelConversation(message, message.Arguments[0], IrcMessageType.ChannelRequest); return; } case IrcDefinition.Response.ChannelCreation: { AddMessageToChannelConversation(message, message.Arguments[1], IrcMessageType.ChannelResponse); return; } case IrcDefinition.Response.ErrorNoSuchChannel: { var channels = _channels.Where(c => c.Name == message.Arguments[1]); if (channels.Count() > 0) { _channels.Remove(channels.First()); } AddMessageToChannelConversation(message, message.Arguments[1], IrcMessageType.ChannelResponse); return; } case IrcDefinition.Response.ErrorTooManyChannels: { var channels = _channels.Where(c => c.Name == message.Arguments[1]); if (channels.Count() > 0) { _channels.Remove(channels.First()); } AddMessageToChannelConversation(message, message.Arguments[1], IrcMessageType.ChannelResponse); return; } case IrcDefinition.Response.ErrorBannedFromChannel: case IrcDefinition.Response.ErrorInviteOnlyChannel: case IrcDefinition.Response.ErrorChannelIsFull: case IrcDefinition.Response.ErrorBadChannelKey: { AddMessageToChannelConversation(message, message.Arguments[1], IrcMessageType.ChannelResponse); return; } case IrcDefinition.Request.Topic: { if (Channels.Where(c => c.Name == message.Arguments[0]).Count() > 0) { Channels.Where(c => c.Name == message.Arguments[0]).First().Topic = message.Arguments[1]; } AddMessageToChannelConversation(message, message.Arguments[0], IrcMessageType.ChannelRequest); return; } case IrcDefinition.Request.Mode: { if (IrcChannel.IsChannelName(message.Arguments[0])) { AddMessageToChannelConversation(message, message.Arguments[0], IrcMessageType.ChannelRequest); return; } break; } case IrcDefinition.Request.PrivateMessage: { if (IrcChannel.IsChannelName(message.Arguments[0])) { AddMessageToChannelConversation(message, message.Arguments[0], IrcMessageType.Message); return; } else { AddMessageToPersonConversation(message, IrcPerson.GetUsernameFromFullname(message.Prefix), IrcMessageType.Message); return; } } default: { var msg = message.Arguments.Count > 0 ? message.Arguments.Last() : message.ToString(); NotifyChangeState(IrcSessionState.Notification, msg); break; } } AddMessageToServerConversation(message); }
private void OnMessageSent(Object sender, IrcMessageEventArgs e) { lock (_requestQue) { if (_requestQue.Count > 0) { // End of registering and sending LIST command if (_requestQue.Count == 1) { ChangeState(IrcSessionState.Opened); } _connection.SendAsync(_requestQue.Dequeue()); } } if (State == IrcSessionState.Closing) { ChangeState(IrcSessionState.Closed); _connection.Dispose(); } }
protected virtual void OnMessageReceived(IrcMessageEventArgs e) { _events.InvokeEvent(EventMessageReceived, this, e); }