Example #1
0
        void ProccessMessageData(object sender, ReceivedDataArgs ea)
        {
            //Collecting all data from mMessageBuffer and ReceivedDataArgs in the single buffer - tempTotalData.
            //Collected data will be parsed for distinct commands.
            //Collecting data...
            int MessagesBufferLength = (int)mMessagesBuffer.Length;

            byte[] tempTotalData = new byte[MessagesBufferLength + ea.Data.Length];
            mMessagesBuffer.ToArray().CopyTo(tempTotalData, 0);
            Array.Copy(ea.Data, 0, tempTotalData, MessagesBufferLength, ea.Data.Length);

            mMessagesBuffer.SetLength(0);
            //Collecting finishes here.
            //Parsing data...

            int msgStart = 0;

            for (int i = 0; i < tempTotalData.Length - 1; i++)
            {
                if (tempTotalData[i] == 13 && tempTotalData[i + 1] == 10)         // byte 10 = LF and byte 13 = CR
                {
                    byte[] message = new byte[i - msgStart];
                    Array.Copy(tempTotalData, msgStart, message, 0, i - msgStart);         // Copy data[msgStart:i] to message
                    //Console.WriteLine("Command Received: {0}",Encoding.UTF8.GetString(message));
                    string inMessage = Encoding.UTF8.GetString(message);
                    if (inMessage.Length > 0)
                    {
                        //OnNotice("test");
                        mMessageQ.Enqueue(inMessage);
                        Console.WriteLine("COMMAND NAME: {0}", IrcCommand.Parse(inMessage).Name);
                        Console.WriteLine("PARAMETER: ");
                        foreach (var p in IrcCommand.Parse(inMessage).Parameters)
                        {
                            Console.WriteLine(p.ToString());
                        }
                        IrcCommand incCommand = IrcCommand.Parse(inMessage);
                        IrcCommand outCommand = mIrcCommandAnalyzer.GetResponse(IrcCommand.Parse(inMessage));
                        if (outCommand != null)
                        {
                            SendMessage(outCommand.ToString() + "\r\n");
                        }
                        if (incCommand.Name == "PRIVMSG")
                        {
                            int    indexOfExclamationSign = incCommand.Prefix.IndexOf('!');
                            string name = incCommand.Prefix.Substring(0, indexOfExclamationSign);

                            if (name != "jtv")
                            {
                                privMessages.Add(String.Format("{0}:{1}\n", name, incCommand.Parameters[incCommand.Parameters.Length - 1].Value));
                                PrivMessages = PrivMessages;
                                mQE.Process(incCommand);
                            }
                            else
                            {
                                if (incCommand.Parameters != null && incCommand.Parameters.Length > 0)
                                {
                                    if (incCommand.Parameters[incCommand.Parameters.Length - 1].Value == "Your message was not sent because you are sending messages too quickly.")
                                    {
                                        OnNotice("Your message was not sent because you are sending messages too quickly. Possible solution: grant mod priveleges to bot");
                                    }
                                    else if (incCommand.Parameters[incCommand.Parameters.Length - 1].Value == "Your message was not sent because it is identical to the previous one you sent, less than 30 seconds ago.")
                                    {
                                        OnNotice("Your message was not sent because it is identical to the previous one you sent, less than 30 seconds ago.");
                                    }
                                }
                            }


                            //Text addition to the chat window
                        }
                        if (incCommand.Name == "NOTICE")
                        {
                            if (incCommand.Parameters[incCommand.Parameters.Length - 1].Value == "Login unsuccessful")
                            {
                                Disconnect();
                                AuthorizedName = "";
                                Auth.AuthKey   = "";
                                OnNotice("Seems like you should re-authorize and re-connect");
                                //throw new TwitchChatBotException("Seems like you should re-authorize and re-connect");
                                //throw new InvalidOperationException("Seems like you should re-authorize and re-connect");
                            }
                            else
                            {
                                Disconnect();
                                AuthorizedName = "";
                                Auth.AuthKey   = "";
                                OnNotice(incCommand.Parameters[incCommand.Parameters.Length - 1].Value);
                            }

                            Console.WriteLine(incCommand.Parameters[incCommand.Parameters.Length - 1].Value);
                        }
                    }
                    msgStart = i = i + 2;
                }
            }

            // What is left from msgStart til the end of data is only a partial message.
            // We want to save that for when the rest of the message arrives.
            mMessagesBuffer.Write(tempTotalData, msgStart, tempTotalData.Length - msgStart);
        }