Пример #1
0
        public void Deny(WinterBot sender, TwitchUser user, string cmd, string value)
        {
            value = value.Trim();

            if (!TwitchUsers.IsValidUserName(value))
            {
                sender.SendResponse(Importance.Med, "{0}: Usage: !deny [user]", user.Name);
                return;
            }

            var target = sender.Users.GetUser(value);

            if (target.IsModerator)
            {
                return;
            }

            if (m_permit.Contains(target))
            {
                m_permit.Remove(target);
            }

            m_denyList.Add(target);
            sender.SendResponse(Importance.High, "{0}: {1} is no longer allowed to post links.", user.Name, target.Name);
        }
Пример #2
0
 protected override IEnumerable <TwitchUser> Deserialize(IEnumerable <string> lines)
 {
     foreach (var line in lines.Select(l => l.Trim()))
     {
         if (!string.IsNullOrWhiteSpace(line) && TwitchUsers.IsValidUserName(line))
         {
             yield return(Bot.Users.GetUser(line));
         }
     }
 }
Пример #3
0
        public WinterBot(Options options, string channel, string user, string oauth)
        {
            m_options = options;
            m_channel = channel.ToLower();
            m_data    = new TwitchUsers(this);

            Passive = m_options.Passive;

            MessageReceived += TryProcessCommand;
            LastMessageSent  = DateTime.Now;

            LoadExtensions();
        }
Пример #4
0
        private void SetRegular(WinterBot sender, string cmd, string value, bool regular)
        {
            value = value.Trim();
            if (!TwitchUsers.IsValidUserName(value))
            {
                sender.WriteDiagnostic(DiagnosticFacility.UserError, "{0}: Invalid username '{1}.", cmd, value);
                return;
            }

            TwitchUser target = sender.Users.GetUser(value);

            target.IsRegular = regular;
            sender.SendResponse(Importance.Med, "{0} {1} the regular list.", target.Name, regular ? "added to " : "removed from");
        }
Пример #5
0
        public void Permit(WinterBot sender, TwitchUser user, string cmd, string value)
        {
            Debug.Assert(m_winterBot == sender);

            value = value.Trim();
            if (!TwitchUsers.IsValidUserName(value))
            {
                m_winterBot.WriteDiagnostic(DiagnosticFacility.UserError, "{0}: Invalid username '{1}.", cmd, value);
                return;
            }

            var target = sender.Users.GetUser(value);

            if (target.IsModerator)
            {
                return;
            }

            bool removed = m_denyList.TryRemove(target);

            if (removed)
            {
                if (m_urlOptions.ShouldEnforce(target))
                {
                    m_winterBot.SendResponse(Importance.Med, "{0}: {1} was removed from the deny list.", user.Name, target.Name);
                }
                else
                {
                    m_winterBot.SendResponse(Importance.Med, "{0}: {1} can now post links again.", user.Name, target.Name);
                }
            }
            else
            {
                if (m_urlOptions.ShouldEnforce(target))
                {
                    m_permit.Add(target);
                    m_winterBot.SendResponse(Importance.Med, "{0} -> {1} has been granted permission to post a single link.", user.Name, target.Name);
                }
                else
                {
                    m_winterBot.SendResponse(Importance.Low, "{0}: {1} can posts links.", user.Name, target.Name);
                }
            }
        }
Пример #6
0
        void UserFollowedHandler(string channel, IEnumerable <string> users)
        {
            if (!channel.Equals(m_channel, StringComparison.CurrentCultureIgnoreCase))
            {
                return;
            }

            var evt = UserFollowed;

            if (evt != null)
            {
                foreach (var user in users)
                {
                    if (TwitchUsers.IsValidUserName(user))
                    {
                        evt(this, Users.GetUser(user));
                    }
                }
            }
        }
Пример #7
0
        public TwitchUser GetUser()
        {
            MovePastWhitespace();

            string name = GetOneWord(' ');

            if (name == null)
            {
                Error = "Expected twitch user.";
                return(null);
            }
            else if (!TwitchUsers.IsValidUserName(name))
            {
                Error = string.Format("{0} is not a valid twitch user.", name);
                return(null);
            }

            MovePastWhitespace();

            return(m_users.GetUser(name));
        }
Пример #8
0
        public WinterBot(Options options, string channel, string user, string oauth)
        {
            m_options = options;
            m_channel = channel.ToLower();
            m_data = new TwitchUsers(this);

            Passive = m_options.Passive;

            MessageReceived += TryProcessCommand;
            LastMessageSent = DateTime.Now;

            LoadExtensions();
        }
Пример #9
0
        /// <summary>
        /// Connect to the given stream, returns true if we successfully connected.  Note
        /// that this function executes synchronously, and will block until fully connected
        /// to the IRC server.
        /// </summary>
        /// <param name="stream">The stream to connect to.</param>
        /// <param name="user">The twitch username this connection will use.</param>
        /// <param name="auth">The twitch API token used to log in.  This must begin with 'oauth:'.</param>
        public ConnectResult Connect(string stream, string user, string auth, int timeout = 10000)
        {
            if (m_shutdown)
            {
                throw new InvalidOperationException("Attempted to connect while disconnecting.");
            }

            user     = user.ToLower();
            m_stream = stream.ToLower();

            if (m_data == null)
            {
                m_data = new TwitchUsers(m_stream);
            }

            // Create client and hook up events.
            m_client = new IrcClient();

            m_client.Connected            += client_Connected;
            m_client.UnsuccessfulLogin    += m_client_UnsuccessfulLogin;
            m_client.ConnectFailed        += client_ConnectFailed;
            m_client.Error                += client_Error;
            m_client.Registered           += client_Registered;
            m_client.ErrorMessageReceived += client_ErrorMessageReceived;
            m_client.PongReceived         += m_client_PongReceived;
            m_client.PingReceived         += m_client_PingReceived;

            m_flood = new FloodPreventer(this);
            m_flood.RejectedMessage += m_flood_RejectedMessage;
            m_client.FloodPreventer  = m_flood;

            int      currTimeout = timeout;
            DateTime started     = DateTime.Now;

            m_connectedEvent.Reset();
            m_registeredEvent.Reset();
            m_joinedEvent.Reset();

            // Connect to server.
            m_client.Connect("irc.twitch.tv", 6667, false, new IrcUserRegistrationInfo()
            {
                NickName = user,
                UserName = user,
                RealName = user,
                Password = auth
            });

            // Wait for the server to connect.  The connect function on client operates asynchronously, so we
            // wait on s_connectedEvent which is set when client_Connected is called.
            if (!m_connectedEvent.Wait(currTimeout))
            {
                WriteDiagnosticMessage("Connecting to the Twitch IRC server timed out.");
                return(ConnectResult.NetworkFailed);
            }

            currTimeout = timeout - (int)started.Elapsed().TotalMilliseconds;
            /// Wait for the client to be registered.
            if (!m_registeredEvent.Wait(currTimeout))
            {
                // Shouldn't really happen
                WriteDiagnosticMessage("Registration timed out.");
                return(ConnectResult.Failed);
            }

            if (m_loginFailed)
            {
                return(ConnectResult.LoginFailed);
            }

            // Attempt to join the channel.  We'll try for roughly 10 seconds to join.  This really shouldn't ever fail.
            m_client.Channels.Join("#" + m_stream);
            currTimeout = timeout - (int)started.Elapsed().TotalMilliseconds;
            if (!m_joinedEvent.Wait(currTimeout))
            {
                // Shouldn't really happen
                WriteDiagnosticMessage("Failed to join channel {0}.", m_stream);
                return(ConnectResult.Failed);
            }

            TwitchSource.Log.Connected(stream);

            // This command tells twitch that we are a chat bot capable of understanding subscriber/turbo/etc
            // messages.  Without sending this raw command, we would not get that data.
            m_client.SendRawMessage("TWITCHCLIENT 3");

            UpdateMods();
            return(ConnectResult.Success);
        }
Пример #10
0
 public TwitchClient(TwitchUsers data)
 {
     m_data    = data;
     LastEvent = DateTime.Now;
 }
Пример #11
0
 public TwitchUser(TwitchUsers data, string name)
 {
     Name   = name.ToLower();
     m_data = data;
     Debug.Assert(data != null);
 }
Пример #12
0
 internal Args(TwitchUsers users, string text)
 {
     m_users = users;
     FullText = text;
     ParseFlags();
 }
Пример #13
0
 internal Args(WinterBot bot, string text)
 {
     m_users = bot.Users;
     FullText = text;
     ParseFlags();
 }
Пример #14
0
 public TwitchClient(TwitchUsers data)
 {
     m_data = data;
 }
Пример #15
0
 public TwitchUser(TwitchUsers data, string name)
 {
     Name = name.ToLower();
     m_data = data;
     Debug.Assert(data != null);
 }
Пример #16
0
 internal Args(WinterBot bot, string text)
 {
     m_users  = bot.Users;
     FullText = text;
     ParseFlags();
 }
Пример #17
0
        bool Connect()
        {
            if (m_twitch != null)
            {
                m_twitch.InformChatClear -= ClearChatHandler;
                m_twitch.MessageReceived -= ChatMessageReceived;
                m_twitch.ActionReceived -= ChatActionReceived;
                m_twitch.UserSubscribed -= SubscribeHandler;
                m_twitch.StatusUpdate -= StatusUpdate;
            }

            string channel = m_channel.ToLower();
            m_users = new TwitchUsers(channel);
            m_twitch = new TwitchClient(m_users);
            m_twitch.InformChatClear += ClearChatHandler;
            m_twitch.MessageReceived += ChatMessageReceived;
            m_twitch.ActionReceived += ChatActionReceived;
            m_twitch.UserSubscribed += SubscribeHandler;
            m_twitch.StatusUpdate += StatusUpdate;

            bool first = true;
            ConnectResult result;
            const int sleepTime = 5000;
            do
            {
                if (!NativeMethods.IsConnectedToInternet())
                {
                    WriteStatus("Not connected to the internet.");

                    do
                    {
                        Thread.Sleep(sleepTime);
                    } while (!NativeMethods.IsConnectedToInternet());

                    WriteStatus("Re-connected to the internet.");
                }

                if (!first)
                    Thread.Sleep(sleepTime);

                first = false;
                result = m_twitch.Connect(channel, m_options.User, m_options.Pass);

                if (result == ConnectResult.LoginFailed)
                {
                    WriteStatus("Failed to login, please change options.ini and restart the application.");
                    return false;
                }
                else if (result != ConnectResult.Success)
                {
                    WriteStatus("Failed to connect: {0}", result == ConnectResult.NetworkFailed ? "network failed" : "failed");
                }
            } while (result != ConnectResult.Success);

            WriteStatus("Connected to channel {0}.", channel);
            return true;
        }
Пример #18
0
 internal Args(TwitchUsers users, string text)
 {
     m_users  = users;
     FullText = text;
     ParseFlags();
 }
Пример #19
0
 public TwitchClient(TwitchUsers data)
 {
     m_data = data;
     LastEvent = DateTime.Now;
 }
Пример #20
0
        /// <summary>
        /// Connect to the given stream, returns true if we successfully connected.  Note
        /// that this function executes synchronously, and will block until fully connected
        /// to the IRC server.
        /// </summary>
        /// <param name="stream">The stream to connect to.</param>
        /// <param name="user">The twitch username this connection will use.</param>
        /// <param name="auth">The twitch API token used to log in.  This must begin with 'oauth:'.</param>
        public ConnectResult Connect(string stream, string user, string auth, int timeout = 10000)
        {
            if (m_shutdown)
                throw new InvalidOperationException("Attempted to connect while disconnecting.");

            user = user.ToLower();
            m_stream = stream.ToLower();

            if (m_data == null)
                m_data = new TwitchUsers(m_stream);

            // Create client and hook up events.
            m_client = new IrcClient();

            m_client.Connected += client_Connected;
            m_client.UnsuccessfulLogin += m_client_UnsuccessfulLogin;
            m_client.ConnectFailed += client_ConnectFailed;
            m_client.Error += client_Error;
            m_client.Registered += client_Registered;
            m_client.ErrorMessageReceived += client_ErrorMessageReceived;
            m_client.PongReceived += m_client_PongReceived;
            m_client.PingReceived += m_client_PingReceived;

            m_flood = new FloodPreventer(this);
            m_flood.RejectedMessage += m_flood_RejectedMessage;
            m_client.FloodPreventer = m_flood;

            int currTimeout = timeout;
            DateTime started = DateTime.Now;

            m_connectedEvent.Reset();
            m_registeredEvent.Reset();
            m_joinedEvent.Reset();

            // Connect to server.
            m_client.Connect("irc.twitch.tv", 6667, false, new IrcUserRegistrationInfo()
            {
                NickName = user,
                UserName = user,
                RealName = user,
                Password = auth
            });

            // Wait for the server to connect.  The connect function on client operates asynchronously, so we
            // wait on s_connectedEvent which is set when client_Connected is called.
            if (!m_connectedEvent.Wait(currTimeout))
            {
                WriteDiagnosticMessage("Connecting to the Twitch IRC server timed out.");
                return ConnectResult.NetworkFailed;
            }

            currTimeout = timeout - (int)started.Elapsed().TotalMilliseconds;
            /// Wait for the client to be registered.
            if (!m_registeredEvent.Wait(currTimeout))
            {
                // Shouldn't really happen
                WriteDiagnosticMessage("Registration timed out.");
                return ConnectResult.Failed;
            }

            if (m_loginFailed)
                return ConnectResult.LoginFailed;

            // Attempt to join the channel.  We'll try for roughly 10 seconds to join.  This really shouldn't ever fail.
            m_client.Channels.Join("#" + m_stream);
            currTimeout = timeout - (int)started.Elapsed().TotalMilliseconds;
            if (!m_joinedEvent.Wait(currTimeout))
            {
                // Shouldn't really happen
                WriteDiagnosticMessage("Failed to join channel {0}.", m_stream);
                return ConnectResult.Failed;
            }

            TwitchSource.Log.Connected(stream);

            // This command tells twitch that we are a chat bot capable of understanding subscriber/turbo/etc
            // messages.  Without sending this raw command, we would not get that data.
            m_client.SendRawMessage("TWITCHCLIENT 3");

            UpdateMods();
            return ConnectResult.Success;
        }
Пример #21
0
        public WinterBot(Options options, string channel, string user, string oauth)
        {
            m_data = new TwitchUsers(this);
            m_options = options;
            m_channel = channel.ToLower();

            Passive = m_options.Passive;

            MessageReceived += TryProcessCommand;
            LastMessageSent = DateTime.Now;

            m_twitch = new TwitchClient(m_data);
            m_twitch.InformChatClear += ClearChatHandler;
            m_twitch.MessageReceived += ChatMessageReceived;
            m_twitch.UserSubscribed += SubscribeHandler;
            m_twitch.InformModerator += InformModerator;

            LoadExtensions();
        }
Пример #22
0
 public static Args ParseArguments(this string text, TwitchUsers users)
 {
     return(new Args(users, text));
 }