Ejemplo n.º 1
0
 public void Process(IrcCommand inCommand)
 {
     if (inCommand.Name == "PRIVMSG")
     {
         mIncomingMessagesQueue.Enqueue(inCommand);
     }
 }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
0
        async Task ReadIncomingMessages(CancellationToken ct)
        {
            while (!ct.IsCancellationRequested)
            {
                Task <IrcCommand> tic = Task.Run((Func <Task <IrcCommand> >)GetMessageFromQ, ct);
                IrcCommand        ic  = await tic;

                if (ic != null)
                {
                    Console.WriteLine("InCommand: " + ic.ToString());
                }

                //check privmsg for being a command
                if (ic != null && ic.Prefix != null)
                {
                    string message = ic.Parameters[ic.Parameters.Length - 1].Value;
                    if (message.Length > 0 && message[0] == '!')
                    {
                        int    indexOfExclamationSign = ic.Prefix.IndexOf('!');
                        string name = ic.Prefix.Substring(0, indexOfExclamationSign);
                        //then its a bot-command
                        ProcessIncomingBotCommand(message, name);
                    }
                }


                //here we have a privmsg and have to check for a valid answer
                if (quizIsRunning && mCurrentObject != null && !(mCurrentObject.IsAnswered()) && ic != null && ic.Prefix != null)
                {
                    Console.WriteLine("{0} is guessed it is \"{1}\" ({2})!", ic.Prefix, ic.Parameters[ic.Parameters.Length - 1].Value, mCurrentObject.Answer);

                    bool IsRightAnswer = false;

                    if (ForgiveSmallMisspelling)
                    {
                        int    distance   = LevenshteinDistance.Compute(mCurrentObject.Answer.ToLower(), ic.Parameters[ic.Parameters.Length - 1].Value.ToLower());
                        double percentage = ((double)distance / (double)mCurrentObject.Answer.Length) * 100;
                        if (percentage < 25)
                        {
                            IsRightAnswer = true;
                        }
                    }
                    else
                    {
                        if (ic.Parameters[ic.Parameters.Length - 1].Value.ToLower() == mCurrentObject.Answer.ToLower())
                        {
                            IsRightAnswer = true;
                        }
                    }


                    if (IsRightAnswer)
                    {
                        int    indexOfExclamationSign = ic.Prefix.IndexOf('!');
                        string name = ic.Prefix.Substring(0, indexOfExclamationSign);

                        if (mScore.ContainsKey(name))
                        {
                            mScore[name]++;
                        }
                        else
                        {
                            mScore[name] = 1;
                        }

                        ScoreObject scoreObj = name.AsScoreObject();
                        if (mScoreList.Contains(scoreObj))
                        {
                            mScoreList[mScoreList.IndexOf(scoreObj)].Score++;
                        }
                        else
                        {
                            scoreObj.Score++;
                            mScoreList.Add(scoreObj);
                        }
                        Score = Score; //Notifying


                        //Console.WriteLine("{0} is right, it is \"{1}\" !", name, mCurrentQAPair.Item2);
                        string message = String.Format("{0} is right, it is \"{1}\", {0}'s score is {2}!", name, mCurrentObject.Answer, mScore[name]);
                        if (mCurrentObject.IsImageQuestion())
                        {
                            mCurrentObject.UncoverFullImage();
                            ShowStaff(mCurrentObject.GetImageQuestion());
                        }
                        //SendMessage(new IrcCommand(null, "PRIVMSG", new IrcCommandParameter("#sovietmade", false), new IrcCommandParameter(message, true)).ToString() + "\r\n");
                        SendMessage(message);
                        string commandL = ProcessLoyalityCommand(name);
                        if (commandL != null)
                        {
                            SendMessage(commandL);
                        }
                        mCurrentObject.SetAnswered(true);
                        OnTimeToAskAQuestion(null, null);
                    }
                    Console.WriteLine("after GetMessageFromQ:{0} ", ic.Name);
                }
            }
        }
Ejemplo n.º 4
0
 IrcCommand Privmsg(IrcCommand inCommand)
 {
     return(null);
 }
Ejemplo n.º 5
0
 public override IrcCommand GetResponse(IrcCommand inRequestCommand)
 {
     return(mDispatchTable.ContainsKey(inRequestCommand.Name) ? mDispatchTable[inRequestCommand.Name].Invoke(inRequestCommand) : null);
 }
Ejemplo n.º 6
0
 IrcCommand Ping(IrcCommand inCommand)
 {
     Console.WriteLine("Ping-Pong");
     //return new IrcCommand(null, "PONG", new IrcCommandParameter("tmi.twitch.tv"));
     return(new IrcCommand(null, "PONG", inCommand.Parameters));
 }
Ejemplo n.º 7
0
 public abstract IrcCommand GetResponse(IrcCommand inRequestCommand);
Ejemplo n.º 8
0
		IrcCommand Privmsg(IrcCommand inCommand)
		{
			return null;
		}
Ejemplo n.º 9
0
 IrcCommand Ping(IrcCommand inCommand) 
 {
     Console.WriteLine("Ping-Pong");
     //return new IrcCommand(null, "PONG", new IrcCommandParameter("tmi.twitch.tv"));
     return new IrcCommand(null, "PONG", inCommand.Parameters);
 }
Ejemplo n.º 10
0
 public override IrcCommand GetResponse(IrcCommand inRequestCommand)
 {
     return mDispatchTable.ContainsKey(inRequestCommand.Name) ? mDispatchTable[inRequestCommand.Name].Invoke(inRequestCommand) : null;
     
 }
Ejemplo n.º 11
0
 public abstract IrcCommand GetResponse(IrcCommand inRequestCommand);