Пример #1
0
            private void DescriptionUpdater(CancellationToken token)
            {
                while (true)
                {
                    var data = Data;
                    if (PlayManager.IsPlaying && data != null)
                    {
                        int      queueLength = PlayManager.Queue.Items.Count - PlayManager.Queue.Index - 1;
                        TimeSpan timeLeft    = Player.Length.Subtract(Player.Position);

                        var description = Data.MakeDescription(queueLength, timeLeft);
                        if (description.Length > 200)
                        {
                            description = description.Take(197) + "...";
                        }

                        Ts3Client.ChangeDescription(description).UnwrapThrow();
                    }

                    if (token.WaitHandle.WaitOne(1000))
                    {
                        return;
                    }
                }
            }
Пример #2
0
    public void LoginStepTwo(SpotifyAccount account, ClientCall invoker)
    {
        //get Email
        string information = "";

        information += "Now your Spotify-E-Mail.";
        Ts3Client.SendMessage(information, invoker.ClientId.Value);

        void handler(object sender, TextMessage textMessage)
        {
            if (textMessage.InvokerId == invoker.ClientId)
            {
                string mail = textMessage.Message.ToString();
                Match  m    = Regex.Match(mail, @"\[URL=.*\](.*)\[\/URL\]");
                if (m.Success)
                {
                    account.email = m.Groups[m.Groups.Count - 1].Value;
                }
                else
                {
                    account.email = mail;
                }

                Ts3Client.OnMessageReceived -= handler;
                LoginStepThree(account, invoker);
            }
        };

        Ts3Client.OnMessageReceived += handler;
    }
Пример #3
0
    public void LoginStepOne(SpotifyAccount account, ClientCall invoker)
    {
        //get Auth-Key
        account.id = invoker.ClientUid.Value;
        var newControl = new SpotifyControl(spotifyPluginConfig, rootConf);

        newControl.firstTimeLogin((link) =>
        {
            string information = "";
            information       += "Grant rights and place the code from GET-Param into this chat.";
            Ts3Client.SendMessage(information, invoker.ClientId.Value);
            Ts3Client.SendMessage(link, invoker.ClientId.Value);

            void handler(object sender, TextMessage textMessage)
            {
                if (textMessage.InvokerId == invoker.ClientId)
                {
                    account.code = textMessage.Message.ToString();
                    Ts3Client.OnMessageReceived -= handler;
                    LoginStepTwo(account, invoker);
                }
            };

            Ts3Client.OnMessageReceived += handler;
        });
    }
Пример #4
0
 public Voting(Player player, Ts3Client client, TsFullClient ts3FullClient, ConfBot config)
 {
     this.player        = player;
     this.client        = client;
     this.config        = config;
     this.ts3FullClient = ts3FullClient;
 }
Пример #5
0
        public static void SendMessage(Ts3Client client, ClientCall cc, string message)
        {
            if (cc?.ClientId == null)
            {
                return;
            }

            client.SendMessage(message, cc.ClientId.Value);
        }
Пример #6
0
        public async Task UpdadeDbIdToUid(Ts3Client ts3Client)
        {
            var upgradedEntries = new List <AudioLogEntry>();
            var dbIdCache       = new Dictionary <uint, (bool valid, Uid uid)>();

            foreach (var audioLogEntry in audioLogEntries.FindAll())
            {
#pragma warning disable CS0612
                if (audioLogEntry.UserInvokeId is null)
                {
                    continue;
                }

                if (audioLogEntry.UserUid != null || audioLogEntry.UserInvokeId.Value == 0)
                {
                    audioLogEntry.UserInvokeId = null;
                    upgradedEntries.Add(audioLogEntry);
                    continue;
                }

                if (!dbIdCache.TryGetValue(audioLogEntry.UserInvokeId.Value, out var data))
                {
                    try
                    {
                        var dbData = await ts3Client.GetDbClientByDbId((ClientDbId)audioLogEntry.UserInvokeId.Value);

                        data = (true, dbData.Uid);
                    }
                    catch (AudioBotException)
                    {
                        Log.Warn("Client DbId {0} could not be found.", audioLogEntry.UserInvokeId.Value);
                        data = (false, Uid.Null);
                    }
                    dbIdCache.Add(audioLogEntry.UserInvokeId.Value, data);
                }

                if (!data.valid)
                {
                    continue;
                }

                audioLogEntry.UserInvokeId = null;
                audioLogEntry.UserUid      = data.uid.Value;
                upgradedEntries.Add(audioLogEntry);
#pragma warning restore CS0612
            }

            if (upgradedEntries.Count > 0)
            {
                audioLogEntries.Update(upgradedEntries);
            }
            Log.Info("Upgraded {0} entries.", upgradedEntries.Count);
        }
Пример #7
0
 public Autofill(
     Ts3Client ts3Client,
     Player player,
     PlayManager playManager,
     PlaylistManager playlistManager,
     TsFullClient ts3FullClient)
 {
     Ts3Client       = ts3Client;
     Player          = player;
     PlayManager     = playManager;
     PlaylistManager = playlistManager;
     Ts3FullClient   = ts3FullClient;
 }
Пример #8
0
            public DescriptionThread(
                Player player, Ts3Client ts3Client, PlayManager playManager, CancellationToken token)
            {
                Player      = player;
                Ts3Client   = ts3Client;
                PlayManager = playManager;

                var descUpdaterThread = new Thread(() => DescriptionUpdater(token))
                {
                    IsBackground = true
                };

                descUpdaterThread.Start();
            }
Пример #9
0
        public void UpdadeDbIdToUid(Ts3Client ts3Client)
        {
            var upgradedEntries = new List <AudioLogEntry>();
            var dbIdCache       = new Dictionary <uint, (bool valid, string uid)>();

            foreach (var audioLogEntry in audioLogEntries.FindAll())
            {
#pragma warning disable CS0612
                if (!audioLogEntry.UserInvokeId.HasValue)
                {
                    continue;
                }

                if (audioLogEntry.UserUid != null || audioLogEntry.UserInvokeId.Value == 0)
                {
                    audioLogEntry.UserInvokeId = null;
                    upgradedEntries.Add(audioLogEntry);
                    continue;
                }

                if (!dbIdCache.TryGetValue(audioLogEntry.UserInvokeId.Value, out var data))
                {
                    var result = ts3Client.GetDbClientByDbId(audioLogEntry.UserInvokeId.Value);
                    data.uid = (data.valid = result.Ok) ? result.Value.Uid : null;
                    if (!data.valid)
                    {
                        Log.Warn("Client DbId {0} could not be found.", audioLogEntry.UserInvokeId.Value);
                    }
                    dbIdCache.Add(audioLogEntry.UserInvokeId.Value, data);
                }

                if (!data.valid)
                {
                    continue;
                }

                audioLogEntry.UserInvokeId = null;
                audioLogEntry.UserUid      = data.uid;
                upgradedEntries.Add(audioLogEntry);
#pragma warning restore CS0612
            }

            if (upgradedEntries.Count > 0)
            {
                audioLogEntries.Update(upgradedEntries);
            }
            Log.Info("Upgraded {0} entries.", upgradedEntries.Count);
        }
Пример #10
0
    public void LoginStepFour(SpotifyAccount account, ClientCall invoker, string password)
    {
        //Start Librespot and check Password
        string information = "";

        information += "Lets check it quickly.";
        Ts3Client.SendMessage(information, invoker.ClientId.Value);

        SpotifyInstance newInstance = new SpotifyInstance(spotifyPluginConfig, account);

        newInstance.useLogin(account.email, password);
        newInstance.startProcess();

        int tries = 0;

        while (!newInstance.connected && tries++ < 4)
        {
            Thread.Sleep(500);
        }

        if (!newInstance.connected || newInstance.hasExited())
        {
            Ts3Client.SendMessage("Credentials not right.", invoker.ClientId.Value);
            return;
        }


        SpotifyControl newControl = new SpotifyControl(spotifyPluginConfig, rootConf);

        newControl.logintoken(account.code, (bool success, bool tell, string accessToken, string refreshToken) =>
        {
            if (success)
            {
                account.refreshToken = refreshToken;
                account.accessToken  = accessToken;

                spotifyPluginConfig.accountList.Add(account);
                saveConfig();

                Ts3Client.SendMessage("You can use Spotify now.", invoker.ClientId.Value);
            }
            else
            {
                Ts3Client.SendMessage("Something went wrong check your Auth-Token.", invoker.ClientId.Value);
            }
        });
    }
Пример #11
0
    public void CommandSpotifyAccount(ClientCall invoker)
    {
        SpotifyAccount account            = spotifyPluginConfig.getAccount(invoker.ClientUid.Value);
        string         accountInformation = "";

        if (account.Exists())
        {
            accountInformation += "Your Accountinformation:\n\n";
            accountInformation += "Your E-Mail: " + account.email + "\n";
            accountInformation += "Your Code: " + account.code + "\n";
        }
        else
        {
            accountInformation += "Yout dont have an Spotify-Account stored. Type !spotify account add";
        }
        Ts3Client.SendMessage(accountInformation, invoker.ClientId.Value);
    }
Пример #12
0
        public static IEnumerable <Client> GetListeningClients(Ts3Client ts3Client, TsFullClient ts3FullClient)
        {
            if (ts3FullClient.Book.CurrentChannel() == null)
            {
                return(new List <Client>());
            }

            return(GetClientsInChannel(ts3FullClient, ts3FullClient.Book.CurrentChannel().Id)
                   .Where(currentClient => {
                if (ts3FullClient.ClientId == currentClient.Id)                         // exclude bot
                {
                    return false;
                }

                var data = ts3Client.GetClientInfoById(currentClient.Id);
                return data.Ok && !data.Value.OutputMuted;
            }));
        }
Пример #13
0
    public void LoginStepThree(SpotifyAccount account, ClientCall invoker)
    {
        //get password
        string information = "";

        information += "Now your Spotify-Password.";
        Ts3Client.SendMessage(information, invoker.ClientId.Value);

        void handler(object sender, TextMessage textMessage)
        {
            if (textMessage.InvokerId == invoker.ClientId)
            {
                Ts3Client.OnMessageReceived -= handler;
                LoginStepFour(account, invoker, textMessage.Message.ToString());
            }
        };

        Ts3Client.OnMessageReceived += handler;
    }
Пример #14
0
    public void CommandSpotifyAccountAdd(ClientCall invoker)
    {
        SpotifyAccount account = spotifyPluginConfig.getAccount(invoker.ClientUid.Value);

        bool cancel = false;

        if (spotifyPluginConfig.returnUrl.Length < 10)
        {
            Ts3Client.SendMessage("Return-URL is not set", invoker.ClientId.Value);
            cancel = true;
        }
        if (spotifyPluginConfig.clientSecret.Length < 10)
        {
            Ts3Client.SendMessage("clientSecret is not set", invoker.ClientId.Value);
            cancel = true;
        }
        if (spotifyPluginConfig.clientId.Length < 10)
        {
            Ts3Client.SendMessage("clientId is not set", invoker.ClientId.Value);
            cancel = true;
        }

        if (cancel == true)
        {
            Ts3Client.SendMessage("aborting", invoker.ClientId.Value);
            return;
        }

        if (account.Exists())
        {
            CommandSpotifyAccount(invoker);
        }
        else
        {
            string information = "Follow instructions";
            Ts3Client.SendMessage(information, invoker.ClientId.Value);
            LoginStepOne(account, invoker);
        }
    }
Пример #15
0
 public void info(string message)
 {
     Log.Info("* " + message);
     Ts3Client.SendChannelMessage("* Spotify: " + message);
 }
Пример #16
0
        public UpdateWebSocket(
            KDFCommandsPlugin kdf,
            Player player,
            PlayManager playManager,
            ResolveContext resolver,
            Ts3Client ts3Client,
            TsFullClient ts3FullClient,
            ConfWebSocket confWebSocket
            )
        {
            this.kdf           = kdf;
            this.player        = player;
            this.playManager   = playManager;
            this.resolver      = resolver;
            this.ts3Client     = ts3Client;
            this.ts3FullClient = ts3FullClient;

            this.playManager.Queue.OnQueueChange      += QueueChanged;
            this.playManager.Queue.OnQueueIndexChange += UpdateRecentlyPlayed;
            this.kdf.Autofill.OnStateChange           += AutofillChanged;
            this.kdf.Voting.OnSkipVoteChanged         -= SkipVoteChanged;

            server = new WebSocketServer(IPAddress.Loopback, 2021, confWebSocket);
            server.OnClientConnected += ClientConnected;

            running = true;
            var thread = new Thread(() => {
                JsonValue <Dictionary <string, IList <(string, string)> > > listeners = null;
                JsonValue <SongInfo> song = null;
                bool frozen      = false;
                long frozenSince = -1;

                while (running)
                {
                    // Check for listener change
                    var newListeners = KDFCommandsPlugin.CommandListeners(ts3Client, ts3FullClient, player);
                    if (listeners == null || !ListenersEqual(listeners, newListeners))
                    {
                        SendListenerUpdate(newListeners);
                    }
                    listeners = newListeners;

                    // Check if song should be updated
                    JsonValue <SongInfo> newSong = null;
                    try {
                        newSong = MainCommands.CommandSong(playManager, player, resolver);
                    } catch (CommandException) {
                        // Don't crash just because nothing is playing
                    }

                    if (newSong != null)
                    {
                        var ts = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                        bool frozenStateChanged = ts - player.WebSocketPipe.LastDataSentTimestamp > 500 != frozen;
                        if (frozenStateChanged)
                        {
                            frozen      = !frozen;
                            frozenSince = frozen ? DateTimeOffset.Now.ToUnixTimeSeconds() : -1;
                        }

                        if (
                            song == null ||
                            newSong.Value.Position < song.Value.Position ||
                            newSong.Value.Length != song.Value.Length ||
                            newSong.Value.Link != song.Value.Link ||
                            newSong.Value.Paused != song.Value.Paused ||
                            frozenStateChanged && !frozen
                            )
                        {
                            if (Log.IsTraceEnabled)
                            {
                                string reason = "Unexpected reason.";
                                if (song == null)
                                {
                                    reason = "First song started playing.";
                                }
                                else if (newSong.Value.Position < song.Value.Position)
                                {
                                    reason = "Position < previous position.";
                                }
                                else if (newSong.Value.Length != song.Value.Length)
                                {
                                    reason = "Length changed.";
                                }
                                else if (newSong.Value.Link != song.Value.Link)
                                {
                                    reason = "Resource URL changed.";
                                }
                                else if (newSong.Value.Paused != song.Value.Paused)
                                {
                                    reason = "Pause state changed.";
                                }
                                else if (frozenStateChanged & !frozen)
                                {
                                    reason = "Song was frozen for over 500ms and now unfroze.";
                                }

                                Log.Trace("Song update sent. Reason: " + reason);
                            }

                            SendSongUpdate(newSong);
                        }

                        // Send resync update if frozen every 5 seconds.
                        if (frozen && (DateTimeOffset.Now.ToUnixTimeSeconds() - frozenSince) % 5 == 4)
                        {
                            Log.Trace("Song update sent. Reason: Update in frozen state.");
                            SendSongUpdate(newSong);
                        }
                    }
                    else if (song != null)
                    {
                        // newSong is null but previous song was not --> music stopped
                        SendToAll("song", null);
                        Log.Trace("Song update sent. Reason: Music stopped.");
                    }

                    song = newSong;

                    Thread.Sleep(1000);
                }
            })
            {
                IsBackground = true
            };

            thread.Start();
        }
Пример #17
0
 public void informChannel(Ts3Client ts3Client, string message)
 {
     ts3Client.SendChannelMessage("* Spotify: " + message);
 }