Esempio n. 1
0
        private void Reconnect()
        {
            if (IrcClient != null) {
                IrcClient.Disconnect();
                IrcClient = null;
            }

            IrcClient = new IrcClient();
            IrcClient.Connected += IrcClient_Connected;
            IrcClient.ProtocolError += IrcClient_ProtocolError;
            IrcClient.Error += IrcClient_Error;
            IrcClient.Disconnected += IrcClient_Disconnected;
            IrcClient.RawMessageReceived += IrcClient_RawMessageReceived;
            IrcClient.ConnectFailed += IrcClient_ConnectFailed;

            Random rnd = new Random();
            string s = "justinfan" + rnd.Next(100000000);
            regInfo = new IrcUserRegistrationInfo() {
                NickName = s,
                UserName = s,
                Password = "******",
                RealName = s
            };

            Header = "http://twitch.tv, Подключаемся к " + StreamerNick;

            if (!string.IsNullOrEmpty(_directAdress)) {
                string[] dat = _directAdress.Split(':');
                try {
                    int port = int.Parse(dat[1]);
                    IrcClient.Connect(dat[0], port, false, regInfo);
                } catch {
                    Header = "http://twitch.tv, Ошибка " + StreamerNick;
                }
            } else {
                IrcClient.Connect(StreamerNick + ".jtvirc.com", 6667, false, regInfo);
            }
        }
Esempio n. 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;
                        }
                    }
                }
            }
        }