void m_client_UserSubscribed(TwitchClient sender, string user)
        {
            ListItem item = ListItem.CreateFromNewSub(user);

            m_eventQueue.Enqueue(new NewSubscriberEvent(item, user));
        }
 /// <summary>
 /// Called when twitch lets us know that a user is a subscriber to the channel.  Since chat messages
 /// and InformSubscriber events are asynchronous, and may appear in any order, we need to go update
 /// all messages we have received to add the subscriber icon.
 /// </summary>
 void m_client_InformSubscriber(TwitchClient sender, string user)
 {
     m_eventQueue.Enqueue(new NotifySubscriberEvent(user));
 }
 /// <summary>
 /// This is called when IrcDotNet hits an exception.  We will reset the connection with a new
 /// IrcDotNet instance.  In practice, this should never happen.
 /// </summary>
 void m_client_ErrorOccurred(TwitchClient sender, IrcErrorEventArgs error)
 {
     m_error = true;
 }
        void m_client_StatusUpdate(TwitchClient sender, string message)
        {
            ListItem item = ListItem.CreateFromStatus(message);

            m_eventQueue.Enqueue(new NewListItemEvent(item));
        }
 /// <summary>
 /// Called when twitch lets us know that a user is a subscriber to the channel.  Since chat messages
 /// and InformSubscriber events are asynchronous, and may appear in any order, we need to go update
 /// all messages we have received to add the subscriber icon.
 /// </summary>
 void m_client_InformSubscriber(TwitchClient sender, string user)
 {
     m_eventQueue.Enqueue(new NotifySubscriberEvent(user));
 }
        /// <summary>
        /// We receive messages asynchronously.  Here, we process the message to see if we should grab it.
        /// If so, we add the message to the queue and process it during the dispatcher timer tick.
        /// </summary>
        void m_client_MessageReceived(TwitchClient sender, IrcMessageEventArgs msg)
        {
            // Check if the user is on the ignore list.
            string user = msg.Source.Name;

            foreach (string ignore in m_options.UserIgnoreList)
            {
                if (user.Equals(ignore, StringComparison.InvariantCultureIgnoreCase))
                {
                    return;
                }
            }

            // check if the text is on the ignore list
            var text      = msg.Text;
            var lowerText = text.ToLower();

            // check if the text contains a highlight word
            foreach (string highlight in m_options.HighlightList)
            {
                if (lowerText.Contains(highlight.ToLower()))
                {
                    if (ShouldIgnore(lowerText))
                    {
                        return;
                    }

                    if (m_messageMap != null && CheckDuplicate(user, lowerText))
                    {
                        return;
                    }

                    ListItem item = ListItem.CreateFromHighlight(m_twitch.ChannelData, user, text);
                    if (m_messageMap != null)
                    {
                        m_messageMap[lowerText] = item;
                    }

                    m_eventQueue.Enqueue(new NewListItemEvent(item));
                    return;
                }
            }

            // check if the text contains a grab word
            foreach (string grab in m_options.GrabList)
            {
                if (lowerText.Contains(grab.ToLower()))
                {
                    if (ShouldIgnore(lowerText))
                    {
                        return;
                    }

                    if (m_messageMap != null && CheckDuplicate(user, lowerText))
                    {
                        return;
                    }

                    ListItem item = ListItem.CreateFromQuestion(m_twitch.ChannelData, user, text);
                    if (m_messageMap != null)
                    {
                        m_messageMap[lowerText] = item;
                    }

                    m_eventQueue.Enqueue(new NewListItemEvent(item));
                    return;
                }
            }
        }
 void m_client_StatusUpdate(TwitchClient sender, string message)
 {
     ListItem item = ListItem.CreateFromStatus(message);
     m_eventQueue.Enqueue(new NewListItemEvent(item));
 }
 void m_client_UserSubscribed(TwitchClient sender, string user)
 {
     ListItem item = ListItem.CreateFromNewSub(user);
     m_eventQueue.Enqueue(new NewSubscriberEvent(item, user));
 }
 /// <summary>
 /// This is called when IrcDotNet hits an exception.  We will reset the connection with a new
 /// IrcDotNet instance.  In practice, this should never happen.
 /// </summary>
 void m_client_ErrorOccurred(TwitchClient sender, IrcErrorEventArgs error)
 {
     m_error = true;
 }
        /// <summary>
        /// We receive messages asynchronously.  Here, we process the message to see if we should grab it.
        /// If so, we add the message to the queue and process it during the dispatcher timer tick.
        /// </summary>
        void m_client_MessageReceived(TwitchClient sender, IrcMessageEventArgs msg)
        {
            // Check if the user is on the ignore list.
            string user = msg.Source.Name;
            foreach (string ignore in m_options.UserIgnoreList)
                if (user.Equals(ignore, StringComparison.InvariantCultureIgnoreCase))
                    return;

            // check if the text is on the ignore list
            var text = msg.Text;
            var lowerText = text.ToLower();

            // check if the text contains a highlight word
            foreach (string highlight in m_options.HighlightList)
            {
                if (lowerText.Contains(highlight.ToLower()))
                {
                    if (ShouldIgnore(lowerText))
                        return;

                    if (m_messageMap != null && CheckDuplicate(user, lowerText))
                        return;

                    ListItem item = ListItem.CreateFromHighlight(m_twitch.ChannelData, user, text);
                    if (m_messageMap != null)
                        m_messageMap[lowerText] = item;

                    m_eventQueue.Enqueue(new NewListItemEvent(item));
                    return;
                }
            }

            // check if the text contains a grab word
            foreach (string grab in m_options.GrabList)
            {
                if (lowerText.Contains(grab.ToLower()))
                {
                    if (ShouldIgnore(lowerText))
                        return;

                    if (m_messageMap != null && CheckDuplicate(user, lowerText))
                        return;

                    ListItem item = ListItem.CreateFromQuestion(m_twitch.ChannelData, user, text);
                    if (m_messageMap != null)
                        m_messageMap[lowerText] = item;

                    m_eventQueue.Enqueue(new NewListItemEvent(item));
                    return;
                }
            }
        }
        /// <summary>
        /// Subscribe to IRC events and then asynchronously connect to chat.
        /// </summary>
        private void InitIrc()
        {
            m_twitch = new TwitchClient();
            m_twitch.UserSubscribed += m_client_UserSubscribed;
            m_twitch.StatusUpdate += m_client_StatusUpdate;
            m_twitch.ErrorOccurred += m_client_ErrorOccurred;
            m_twitch.MessageReceived += m_client_MessageReceived;
            m_twitch.InformSubscriber += m_client_InformSubscriber;

            Task t = new Task(delegate() { m_twitch.Connect(m_options.Stream, m_options.TwitchUsername, m_options.OauthPassword); });
            t.Start();
        }
        /// <summary>
        /// Disconnects and then Reconnects to chat (useful if an error occurred).
        /// </summary>
        private void ResetConnection()
        {
            lock (this)
            {
                m_error = false;
                if (m_twitch != null)
                {
                    m_twitch.UserSubscribed -= m_client_UserSubscribed;
                    m_twitch.StatusUpdate -= m_client_StatusUpdate;
                    m_twitch.ErrorOccurred -= m_client_ErrorOccurred;
                    m_twitch.MessageReceived -= m_client_MessageReceived;

                    m_twitch.Dispose();
                    m_twitch = null;
                }

                InitIrc();
            }
        }
        void m_client_UserSubscribed(TwitchClient sender, string user)
        {
            ListItem item = ListItem.CreateFromNewSub(user);
            m_eventQueue.Enqueue(new NewSubscriberEvent(item, user));

            if (!string.IsNullOrEmpty(m_options.SubscriberFile))
            {
                List<string> subs = new List<string>();
                subs.Add(user);
                if (File.Exists(m_options.SubscriberFile))
                    subs.AddRange(File.ReadAllText(m_options.SubscriberFile).Split(',').Select(s=>s.Trim()));

                if (subs.Count > 10)
                    subs.RemoveRange(10, subs.Count - 10);

                File.WriteAllText(m_options.SubscriberFile, string.Join(", ", subs));
            }
        }