Esempio n. 1
0
        /// <summary>
        /// Sends a premade IRC statement to the server.
        /// </summary>
        /// <param name="statement">The statement to send.</param>
        /// <returns><c>true</c> if the statement was sent successfully.</returns>
        public bool IrcCommand(IrcStatement statement)
        {
            Throw.If.Null(statement, "statement");

            var buffer = statement.ToByteArray();

            lock (SyncRoot)
            {
#if DEBUG
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.Write("< " + new IrcString(buffer));
                Console.ResetColor();
#endif

                var context = _context;
                if (context == null)
                {
                    return(false);
                }

                try
                {
                    context.Stream.Write(buffer, 0, buffer.Length);
                }
                catch (IOException)
                {
                    return(false);
                }
                catch (ObjectDisposedException)
                {
                    return(false);
                }

                return(true);
            }
        }
Esempio n. 2
0
        protected virtual void OnIrcStatementReceived(IrcStatement statement)
        {
            IrcString ctcpCommand; IrcString[] ctcpParameters; IrcString ctcpRawParameter;

            var source  = statement.Source;
            var @params = statement.Parameters;

#if DEBUG
            Console.ForegroundColor = ConsoleColor.DarkBlue;
            Console.Write("> " + (string)new IrcString(statement.ToByteArray()));
            Console.ResetColor();
#endif
            switch ((string)statement.Command)
            {
            case "NICK":
                if (@params.Count >= 1)
                {
                    OnGotNameChange(new NameChangeEventArgs(source, @params[0]));
                }
                break;

            case "INVITE":
                if (@params.Count >= 2)
                {
                    OnGotInvitation(new InvitationEventArgs(source, @params[0], @params[1]));
                }
                break;

            case "KICK":
                if (@params.Count >= 2)
                {
                    OnGotUserKicked(new KickEventArgs(source,
                                                      @params[1], @params[0],
                                                      @params.Count >= 3 ? @params[2] : null));
                }
                break;

            case "PRIVMSG":
                if (@params.Count >= 2)
                {
                    if (TryCtcpDecode(@params[1], out ctcpCommand, out ctcpParameters, out ctcpRawParameter))
                    {
                        OnCtcpCommandReceived(source, @params[0], ctcpCommand, ctcpParameters, ctcpRawParameter);
                    }
                    else
                    {
                        OnGotMessage(new ChatMessageEventArgs(source, @params[0], @params[1]));
                    }
                }
                break;

            case "NOTICE":
                if (@params.Count >= 2)
                {
                    if (TryCtcpDecode(@params[1], out ctcpCommand, out ctcpParameters, out ctcpRawParameter))
                    {
                        OnCtcpReplyReceived(source, @params[0], ctcpCommand, ctcpParameters, ctcpRawParameter);
                    }
                    else
                    {
                        OnGotNotice(new ChatMessageEventArgs(source, @params[0], @params[1]));
                    }
                }
                break;

            case "JOIN":
                if (@params.Count >= 1)
                {
                    OnGotJoinChannel(new JoinLeaveEventArgs(source, @params[0].Split((byte)',')));
                }
                break;

            case "PART":
                if (@params.Count >= 1)
                {
                    OnGotLeaveChannel(new JoinLeaveEventArgs(source, @params[0].Split((byte)',')));
                }
                break;

            case "QUIT":
                OnGotUserQuit(new QuitEventArgs(source, @params.Count >= 1 ? @params[0] : null));
                break;

            case "MODE":
                if (@params.Count >= 2)
                {
                    OnGotMode(new ModeEventArgs(source, @params[0], @params[1], @params.Skip(2).ToArray()));
                }
                break;

            case "PING":
                if (@params.Count >= 1)
                {
                    IrcCommand("PONG", @params[0]);
                }
                break;

            case "001":
                if (@params.Count >= 1)
                {
                    OnGotWelcomeMessage(new SimpleMessageEventArgs(@params[0]));
                }
                break;

            case "375":
                OnGotMotdBegin();
                break;

            case "372":
                if (@params.Count >= 1)
                {
                    OnGotMotdText(new SimpleMessageEventArgs(@params[@params.Count - 1]));
                }
                break;

            case "376":
                OnGotMotdEnd();
                break;

            case "353":
                if (@params.Count >= 2)
                {
                    OnGotNameListReply(new NameListReplyEventArgs(@params[@params.Count - 2],
                                                                  @params[@params.Count - 1].Split((byte)' ')));
                }
                break;

            case "366":
                if (@params.Count >= 1)
                {
                    OnGotNameListEnd(new NameListEndEventArgs(@params[0]));
                }
                break;

            case "321":
                OnGotChannelListBegin();
                break;

            case "322":
                if (@params.Count >= 4)
                {
                    int userCount;
                    if (int.TryParse(@params[2], out userCount))
                    {
                        OnGotChannelListEntry(new ChannelListEntryEventArgs(@params[1], userCount, @params[@params.Count - 1]));
                    }
                }
                break;

            case "323":
                OnGotChannelListEnd();
                break;

            case "331":
                if (@params.Count >= 3)
                {
                    OnGotChannelTopicChange(new ChannelTopicChangeEventArgs(@params[1], IrcString.Empty));
                }
                break;

            case "332":
                if (@params.Count >= 3)
                {
                    OnGotChannelTopicChange(new ChannelTopicChangeEventArgs(@params[1], @params[2]));
                }
                break;

            case "TOPIC":
                if (@params.Count >= 2)
                {
                    OnGotChannelTopicChange(new ChannelTopicChangeEventArgs(@params[0], @params[1]));
                }
                break;

            default:
                if ((int)statement.ReplyCode >= 400 && (int)statement.ReplyCode < 600)
                {
                    OnGotIrcError(new Events.IrcErrorEventArgs(statement.ReplyCode, statement));
                }
                else
                {
                    OnGotUnknownIrcStatement(new Events.IrcUnknownStatementEventArgs(statement));
                }
                break;
            }
        }
        /// <summary>
        /// Creates a new instance of <see cref="IrcUnknownStatementEventArgs"/>.
        /// </summary>
        /// <param name="error">The type of statement that has occured.</param>
        /// <param name="data">The raw IRC statement data. This can be used to garner more information about the statement.</param>
        public IrcUnknownStatementEventArgs(IrcStatement data)
        {
            Throw.If.Null(data, "data");

            Data = data;
        }
Esempio n. 4
0
        /// <summary>
        /// Creates a new instance of <see cref="IrcErrorEventArgs"/>.
        /// </summary>
        /// <param name="error">The type of error that has occured.</param>
        /// <param name="data">The raw IRC statement data. This can be used to garner more information about the error.</param>
        public IrcErrorEventArgs(IrcReplyCode error, IrcStatement data)
        {
            Throw.If.Null(error, "error").Null(data, "data");

            Error = error; Data = data;
        }
Esempio n. 5
0
 internal void SendCommand(IrcStatement command)
 {
     this.Wait();
     this.Client.IrcCommand(command);
     this.CommandSent();
 }