Example #1
0
        internal static Task HandlePrivmsgMessage(IrcClient client, IrcMessage message)
        {
            // Check if it's a cheer message and handle event, otherwise pass it to IrcClient.HandleTargetedMessage.
            if (message.Tags.ContainsKey("bits"))
            {
                IrcTargetedMessage targetedMessage = new IrcTargetedMessage(client, message);
                TwitchUser         user            = (TwitchUser)targetedMessage.Source;
                TwitchChannel      channel         = (TwitchChannel)targetedMessage.Target;
                int bits = Convert.ToInt32(message.Tags["bits"]);

                var e = new TwitchCheerEventArgs(user, channel, targetedMessage.Message, bits);
                ((TwitchIrcClient)client).OnUserCheered(e);
            }
            else
            {
                IrcMessageHandlers.HandleTargetedMessage(client, message);
            }

            return(Task.CompletedTask);
        }
Example #2
0
        internal static Task HandleUsernoticeMessage(IrcClient client, IrcMessage message)
        {
            string        channelName = message.Parameters[0].Substring(1);
            TwitchChannel channel     = (TwitchChannel)client.Channels[channelName];

            if (message.Tags.ContainsKey("login"))
            {
                TwitchUser user = new TwitchUser(client, message.Tags["login"], message.Tags);

                if (message.Tags.ContainsKey("msg-id"))
                {
                    string noticeType = message.Tags["msg-id"];

                    if (noticeType == "sub" | noticeType == "resub")
                    {
                        int totalMonthsSubscribed = Convert.ToInt32(message.Tags["msg-param-cumulative-months"]);
                        TwitchSubscriptionPlan subscriptionPlan = TwitchUtils.ConvertSubscriptionPlan(message.Tags["msg-param-sub-plan"]);
                        int    consecutiveMonthsSubscribed      = 0;
                        string subMessage = null;

                        if (message.Parameters.Length == 2)
                        {
                            subMessage = message.Parameters[1];
                        }

                        if (message.Tags.ContainsKey("msg-param-should-share-streak") && message.Tags["msg-param-should-share-streak"] == "1")
                        {
                            consecutiveMonthsSubscribed = Convert.ToInt32(message.Tags["msg-param-streak-months"]);
                        }

                        var e = new TwitchSubscriptionEventArgs(user, channel, subscriptionPlan, subMessage, totalMonthsSubscribed, consecutiveMonthsSubscribed);
                        ((TwitchIrcClient)client).OnUserSubscribed(e);
                    }
                    else if (noticeType == "subgift" | noticeType == "anonsubgift")
                    {
                        int totalMonthsSubscribed = Convert.ToInt32(message.Tags["msg-param-months"]);
                        TwitchSubscriptionPlan subscriptionPlan = TwitchUtils.ConvertSubscriptionPlan(message.Tags["msg-param-sub-plan"]);
                        int        recipientUserID      = Convert.ToInt32(message.Tags["msg-param-recipient-id"]);;
                        string     recipientUserName    = message.Tags["msg-param-recipient-user-name"];
                        string     recipientDisplayName = message.Tags["msg-param-recipient-display-name"];
                        TwitchUser recipient            = new TwitchUser(client, recipientUserName, recipientDisplayName, recipientUserID);
                        string     subMessage           = null;

                        if (message.Parameters.Length == 2)
                        {
                            subMessage = message.Parameters[1];
                        }

                        if (noticeType == "anonsubgift")
                        {
                            var e = new TwitchSubscriptionEventArgs(null, channel, subscriptionPlan, subMessage, totalMonthsSubscribed, true, recipient, true);
                            ((TwitchIrcClient)client).OnUserSubscribed(e);
                        }
                        else
                        {
                            var e = new TwitchSubscriptionEventArgs(user, channel, subscriptionPlan, subMessage, totalMonthsSubscribed, true, recipient, false);
                            ((TwitchIrcClient)client).OnUserSubscribed(e);
                        }
                    }
                }
                else
                {
                    throw new TwitchProtocolException("USERNOTICE message does not have a msg-id tag.");
                }
            }
            else
            {
                throw new TwitchProtocolException("USERNOTICE message does not have a login tag.");
            }

            return(Task.CompletedTask);
        }
Example #3
0
        /// <summary>
        ///     Initializes a new instance of <see cref="IrcTargetedMessage"/>.
        /// </summary>
        /// <param name="message"></param>
        public IrcTargetedMessage(IrcClient client, IrcMessage message)
        {
            if (message.Prefix == null)
            {
                throw new ArgumentException("Message contains no prefix.");
            }

            if (message.Parameters.Length < 2)
            {
                throw new ArgumentException("The message needs to have 2 parameters to specify the target and the message.");
            }

            if (message.Command == "PRIVMSG")
            {
                Type = MessageType.Privmsg;
            }
            else if (message.Command == "NOTICE")
            {
                Type = MessageType.Notice;
            }
            else
            {
                Type = MessageType.Unknown;
            }

            IrcHostMask hostMask = IrcHostMask.Parse(message.Prefix);

            if (hostMask != null)
            {
                if (client is TwitchIrcClient)
                {
                    Source = new TwitchUser(client, hostMask, message.Tags);
                }
                else
                {
                    Source = new IrcUser(client, hostMask);
                }
            }
            else
            {
                Source = new IrcServer(message.Prefix);
            }

            if (message.Parameters[0].StartsWith("#"))
            {
                string channelName = message.Parameters[0].Substring(1);

                if (client.Channels.ContainsKey(channelName))
                {
                    Target = client.Channels[channelName];
                }
                else
                {
                    Debug.WriteLine("[IrcTargetedMessage] Could not find the targeted channel in the client's channels list.");
                }

                IsChannelMessage = true;
            }
            else
            {
                Target = client.User;
            }

            Tags    = message.Tags;
            Message = message.Parameters[1];
            Client  = client;
        }