Exemple #1
0
        void twitchIrc_RawMessageReceived(object sender, IrcRawMessageEventArgs e)
        {
            lock (lockTwitchMessage)
            {

                if (e.RawContent.Contains("Login failed") || e.RawContent.ToLower().Contains("login unsuccessful"))
                {
                    if (!twitchTriedOAuth && !settings.TwitchPassword.Contains("oauth:"))
                    {
                        TwitchWeb toauth = new TwitchWeb(settings.TwitchUser.ToLower(), settings.TwitchPassword);
                        twitchTriedOAuth = true;

                        if (toauth.Login())
                        {
                            settings.twitchOAuthKey = toauth.ChatOAuthKey;
                            return;
                        }
                    }
                    SendMessage(new UbiMessage("Twitch login failed! Check settings!", EndPoint.TwitchTV, EndPoint.Error));
                }
                else if (settings.twitchDebugMessages)
                {
                    SendMessage(new UbiMessage(e.RawContent, EndPoint.TwitchTV, EndPoint.SteamAdmin));
                }
            }
        }
Exemple #2
0
        private void ConnectTwitchIRC()
        {
            lock (lockTwitchConnect)
            {
                if (isClosing)
                    return;

                //twitchIrc.FloodPreventer = new IrcStandardFloodPreventer(4, 1000);
                if (settings.TwitchUser.Length <= 0 ||
                    !settings.twitchEnabled)
                    return;


                try
                {
                    if (twitchIrc != null)
                    {
                        if (twitchIrc.IsConnected)
                            return;

                        twitchIrc.Disconnect();
                        twitchIrc = null;
                    }
                }
                catch
                {
                    Debug.Print("Exception in ConnectTwitchIRC()");
                }

                //TwitchAPI ttvApi = new TwitchAPI();
                //ttvApi.GetToken( settings.TwitchUser.ToLower(), settings.TwitchPassword);
                twitchPing = new System.Threading.Timer(new TimerCallback(TwitchPingTick), null, Timeout.Infinite, Timeout.Infinite);
                twitchDisconnect = new System.Threading.Timer(new TimerCallback(TwitchDisconnectNoPong), null, Timeout.Infinite, Timeout.Infinite);

                twitchIrc = new IrcClient();

                twitchIrc.Connected += OnTwitchConnect;
                twitchIrc.Registered += OnTwitchRegister;
                twitchIrc.Disconnected += OnTwitchDisconnect;
                twitchIrc.Error += new EventHandler<IrcErrorEventArgs>(twitchIrc_Error);
                twitchIrc.RawMessageReceived += new EventHandler<IrcRawMessageEventArgs>(twitchIrc_RawMessageReceived);
                
                
                var twitchDnsName = settings.TwitchUser.ToLower() + "." + twitchIRCDomain;

                try
                {
                    twitchServers = Dns.GetHostEntry("irc.twitch.tv");
                }
                catch
                {
                    SendMessage(new UbiMessage("Twitch: error resolving twitch hostname. Using harcoded list of IPs", EndPoint.TwitchTV, EndPoint.Error));
                    twitchServers = new IPHostEntry()
                    {
                        AddressList = new IPAddress[] {
                        IPAddress.Parse("199.9.253.199"),
                        IPAddress.Parse("199.9.253.210"),
                        IPAddress.Parse("199.9.250.239"),
                        IPAddress.Parse("199.9.250.229")
                    }
                    };
                }
                using (var connectedEvent = new ManualResetEventSlim(false))
                {
                    twitchIrc.Connected += (sender2, e2) => connectedEvent.Set();

                    var tmpNextServer = twitchServers.AddressList.SkipWhile(p => p.ToString() == nextTwitchIP.ToString()).FirstOrDefault();


                    if (tmpNextServer == null)
                        tmpNextServer = twitchServers.AddressList.FirstOrDefault();

                    if (nextTwitchIP.Address != 0)
                        SendMessage(new UbiMessage("Twitch: cycling to the next server " + tmpNextServer.ToString(), EndPoint.TwitchTV, EndPoint.Error));

                    nextTwitchIP = tmpNextServer;


                    if (!settings.TwitchPassword.ToLower().Contains("oauth:"))
                    {
                        twitchWeb = new TwitchWeb(settings.TwitchUser.ToLower(), settings.TwitchPassword);
                        twitchWeb.OnDescriptionSetError += new EventHandler<EventArgs>(twitchWeb_OnDescriptionSetError);
                        if (twitchWeb.Login())
                        {
                            settings.twitchOAuthKey = twitchWeb.ChatOAuthKey;
                            twitchWeb.GetDescription();
                            if( String.IsNullOrEmpty( settings.twitch_ShortDescription ) )
                                settings.twitch_ShortDescription = twitchWeb.ShortDescription;
                            if( String.IsNullOrEmpty( settings.twitch_Game ))
                                settings.twitch_Game = twitchWeb.Game;
                        }
                    }
                    else
                    {
                        settings.twitchOAuthKey = settings.TwitchPassword;
                    }

                    if (!String.IsNullOrEmpty(settings.twitchOAuthKey))
                    {
                        twitchIrc.Connect(nextTwitchIP, false, new IrcUserRegistrationInfo()
                        {
                            NickName = settings.TwitchUser.ToLower(),
                            UserName = settings.TwitchUser.ToLower(),
                            RealName = "Twitch bot of " + settings.TwitchUser.ToLower(),
                            Password = settings.twitchOAuthKey
                        });

                        if (!connectedEvent.Wait(60000))
                        {
                            SendMessage(new UbiMessage("Twitch: connection timeout!", EndPoint.TwitchTV, EndPoint.Error));
                            return;
                        }
                    }
                }
            }
        }