Esempio n. 1
0
        public static int GetClientsCountWithGroup(int groupid)
        {
            int count = 0;

            foreach (User user in User.AllUsers)
            {
                if (user.ConnectionType == User.ConnectionTypeEnum.Query)
                {
                    continue;
                }
                string info         = TelnetConnector.Instance.Execute($"clientinfo clid={user.Id}");
                var    servergroups = new List <int>();
                string tmp          = TeamspeakTools.GetParameter <string>(info, "client_servergroups");
                foreach (string s in tmp.Split(','))
                {
                    if (!int.TryParse(s, out int gr))
                    {
                        continue;
                    }
                    if (gr != 0)
                    {
                        servergroups.Add(gr);
                    }
                }

                if (servergroups.IndexOf(groupid) != -1)
                {
                    count++;
                }
            }
            return(count);
        }
Esempio n. 2
0
        private bool InitializeBot()
        {
            TelnetConnector.Instance.Execute($"clientupdate client_nickname={botConfiguration.BotNickname.ConvertToTeamspeakString()}"); // changing bot nickname
            string output = TelnetConnector.Instance.Execute("whoami");

            if (TeamspeakTools.GetParameter <string>(output, "client_nickname").CompareTo(botConfiguration.BotNickname.ConvertToTeamspeakString()) != 0)
            {
                TelnetConnector.Instance.Execute($"clientupdate client_nickname={botConfiguration.SecondBotNickname.ConvertToTeamspeakString()}");
            }

            if (!TeamspeakTools.TryGetParameter(TelnetConnector.Instance.Execute("whoami"), "client_id", out int botId))
            {
                return(false);
            }
            ConsoleEx.WriteLine($"Bot has logged in successfully! (Bot ID: {botId} )");
            User.MarkUserAsBot(User.FromId(botId));
            User.Bot.Move(new Channel(botConfiguration.BotChannelID));

            ConsoleEx.WriteLine($"Users currently online: { ServerInfo.Instance.ClientsOnline }/{ ServerInfo.Instance.MaxClients }");

            return(true);
        }
Esempio n. 3
0
        protected T GetParameter <T>(string parameter)
        {
            string output = TelnetConnector.Instance.Execute(GetParameterCommand);

            return(TeamspeakTools.GetParameter <T>(output, parameter));
        }
Esempio n. 4
0
        public void HandleQueryEvents()
        {
            try
            {
                var queryEvents = new QueryEvent[EventsQueue.Count];
                EventsQueue.CopyTo(queryEvents);
                //ConsoleEx.WriteDebug($"Handling Query Events... Count: {QueryEvents.Count}");
                foreach (QueryEvent queryEvent in queryEvents)
                {
                    EventsQueue.Remove(queryEvent);
                    //ConsoleEx.WriteDebug($"{queryEvent.QueryEventType} - {queryEvent.EventMessage}");

                    if (queryEvent.QueryEventType == QueryEventType.UnknownNotify)
                    {
                        ConsoleEx.Debug($"Unknown notify detected: {queryEvent.EventMessage}");
                    }
                    else if (queryEvent.QueryEventType == QueryEventType.PrivateMessage)
                    {
                        string msg = TeamspeakTools.GetParameter <string>(queryEvent.EventMessage, "msg");
                        if (!TeamspeakTools.TryGetParameter(queryEvent.EventMessage, "invokerid", out int userid))
                        {
                            continue;
                        }
                        if (userid == User.Bot.Id)
                        {
                            continue;                       // don't parse own messages
                        }
                        if (msg.IndexOf("!") == 0)
                        {
                            luaController.Events.OnUserSentComand(User.FromId(userid), msg);
                            LiveCommands.Add(userid, msg);
                        }
                        else
                        {
                            luaController.Events.OnUserSentPrivateMessage(User.FromId(userid), msg);
                        }
                    }
                    else if (queryEvent.QueryEventType == QueryEventType.ClientJoinedServer)
                    {
                        if (TeamspeakTools.GetParameter <int>(queryEvent.EventMessage, "cfid") != 0)
                        {
                            continue;
                        }
                        if (!TeamspeakTools.TryGetParameter(queryEvent.EventMessage, "clid", out int userid))
                        {
                            continue;
                        }
                        if (userid == User.Bot.Id)
                        {
                            continue;                       // don't parse myself
                        }
                        User.InvalidateAllUsers();
                        luaController.Events.OnUserJoinedTheServer(User.FromId(userid));
                    }
                    else if (queryEvent.QueryEventType == QueryEventType.ClientLeftServer)
                    {
                        if (TeamspeakTools.GetParameter <int>(queryEvent.EventMessage, "ctid") != 0)
                        {
                            continue;
                        }
                        if (!TeamspeakTools.TryGetParameter(queryEvent.EventMessage, "clid", out int userid))
                        {
                            continue;
                        }
                        var user = User.FromId(userid);
                        if (user == null)
                        {
                            continue;
                        }
                        User.InvalidateAllUsers();
                        if (userid == User.Bot.Id)
                        {
                            continue;                       // don't parse myself
                        }
                        luaController.Events.OnUserLeftTheServer(user);
                    }
                    else if (queryEvent.QueryEventType == QueryEventType.ClientMoved)
                    {
                        if (!TeamspeakTools.TryGetParameter(queryEvent.EventMessage, "clid", out int userid))
                        {
                            continue;
                        }
                        var user = User.FromId(userid);
                        if (!TeamspeakTools.TryGetParameter(queryEvent.EventMessage, "ctid", out int channelid))
                        {
                            continue;
                        }
                        if (userid == User.Bot.Id)
                        {
                            continue;                       // don't parse myself
                        }
                        luaController.Events.OnUserSwitchedChannel(user, channelid);
                    }
                    else if (queryEvent.QueryEventType == QueryEventType.ServerEdited)
                    {
                        string reason = TeamspeakTools.GetParameter <string>(queryEvent.EventMessage, "reasonid");
                        if (!TeamspeakTools.TryGetParameter(queryEvent.EventMessage, "invokerid", out int userid))
                        {
                            continue;
                        }
                        if (userid == User.Bot.Id)
                        {
                            continue;                       // don't parse own edits
                        }
                        ConsoleEx.Debug($"Server was edited by {User.FromId(userid).Nickname}");
                    }
                    else if (queryEvent.QueryEventType == QueryEventType.ChannelEdited)
                    {
                        if (!TeamspeakTools.TryGetParameter(queryEvent.EventMessage, "cid", out int channelId))
                        {
                            continue;
                        }
                        if (!TeamspeakTools.TryGetParameter(queryEvent.EventMessage, "invokerid", out int userid))
                        {
                            continue;
                        }
                        if (userid == User.Bot.Id)
                        {
                            continue;                       // don't parse own edits
                        }
                        ConsoleEx.Debug($"Channel {new Channel(channelId).Name} was edited by {User.FromId(userid).Nickname}");
                    }
                    else if (queryEvent.QueryEventType == QueryEventType.ChannelDescriptionChanged)
                    {
                        if (!TeamspeakTools.TryGetParameter(queryEvent.EventMessage, "cid", out int channelId))
                        {
                            continue;
                        }
                    }
                    else if (queryEvent.QueryEventType == QueryEventType.ChannelPasswordChanged)
                    {
                        if (!TeamspeakTools.TryGetParameter(queryEvent.EventMessage, "cid", out int channelId))
                        {
                            continue;
                        }
                    }
                    else if (queryEvent.QueryEventType == QueryEventType.ChannelCreated)
                    {
                        if (!TeamspeakTools.TryGetParameter(queryEvent.EventMessage, "cid", out int channelId))
                        {
                            continue;
                        }
                        if (!TeamspeakTools.TryGetParameter(queryEvent.EventMessage, "invokerid", out int userid))
                        {
                            continue;
                        }
                        if (userid == User.Bot.Id)
                        {
                            continue;                       // don't parse own edits
                        }
                        ConsoleEx.Debug($"Channel {new Channel(channelId).Name} was created by {User.FromId(userid).Nickname}");
                    }
                    else if (queryEvent.QueryEventType == QueryEventType.ChannelDeleted)
                    {
                        if (!TeamspeakTools.TryGetParameter(queryEvent.EventMessage, "cid", out int channelId))
                        {
                            continue;
                        }
                        if (!TeamspeakTools.TryGetParameter(queryEvent.EventMessage, "invokerid", out int userid))
                        {
                            continue;
                        }
                        if (userid == User.Bot.Id)
                        {
                            continue;                       // don't parse own edits
                        }
                        ConsoleEx.Debug($"Channel with id {channelId} was deleted by {User.FromId(userid).Nickname}");
                    }
                }
            }
            catch (Exception e)
            {
                ConsoleEx.Error($"QueryEvents Exception: {e.Message}\n{e.StackTrace}");
            }
        }
Esempio n. 5
0
        public void GetListOfGroupsTest(string testCase, List <int> expectedResults)
        {
            IEnumerable <int> result = TeamspeakTools.GetListOfGroups(testCase);

            Assert.IsTrue(Enumerable.SequenceEqual(result, expectedResults));
        }
Esempio n. 6
0
        public void GetParameterTest <T>(string output, string param, T expectedResult)
        {
            T result = TeamspeakTools.GetParameter <T>(output, param);

            Assert.AreEqual(result, expectedResult);
        }
Esempio n. 7
0
#pragma warning restore

        public Channel Create()
        {
            if (name == null)
            {
                return(null);
            }
            var str = new StringBuilder($"channelcreate channel_name={name.ConvertToTeamspeakString()}");

            if (topic != null)
            {
                str.Append($" channel_topic={topic.ConvertToTeamspeakString()}");
            }
            if (description != null)
            {
                str.Append($" channel_description={description.ConvertToTeamspeakString()}");
            }
            if (password != null)
            {
                str.Append($" channel_password={password.ConvertToTeamspeakString()}");
            }
            if (codec != null)
            {
                str.Append($" channel_codec={(int)codec}");
            }
            if (codecQuality != null)
            {
                str.Append($" channel_codec_quality={codecQuality}");
            }
            if (maxClients != null)
            {
                str.Append($" channel_maxclients={maxClients}");
            }
            if (maxFamilyClients != null)
            {
                str.Append($" channel_maxfamilyclients={maxFamilyClients}");
            }
            if (order != null)
            {
                str.Append($" channel_order={order}");
            }
            if (parentId != null)
            {
                str.Append($" cpid={parentId}");
            }
            switch (type)
            {
            case ChannelType.Default:
                str.Append($" channel_flag_permanent=1 channel_flag_default=1");
                break;

            case ChannelType.Permanent:
                str.Append($" channel_flag_permanent=1");
                break;

            case ChannelType.SemiPermanent:
                str.Append($" channel_flag_semi_permanent=1");
                break;

            case ChannelType.Temporary:
                str.Append($" channel_flag_temporary=1");
                break;
            }
            if (hasMaxClientsUnlimited != null)
            {
                str.Append($" channel_flag_maxclients_unlimited={(hasMaxClientsUnlimited.Value ? "1" : "0")}");
            }
            if (hasMaxFamilyClientsUnlimited != null)
            {
                str.Append($" channel_flag_maxfamilyclients_unlimited={(hasMaxFamilyClientsUnlimited.Value ? "1" : "0")}");
            }
            if (hasMaxFamilyClientsInherited != null)
            {
                str.Append($" channel_flag_maxfamilyclients_inherited={(hasMaxFamilyClientsInherited.Value ? "1" : "0")}");
            }
            if (neededTalkPower != null)
            {
                str.Append($" channel_needed_talk_power={neededTalkPower}");
            }
            if (phoneticName != null)
            {
                str.Append($" channel_name_phonetic={phoneticName}");
            }
            if (iconId != null)
            {
                str.Append($" channel_icon_id={iconId}");
            }
            if (isUnencrypted != null)
            {
                str.Append($" channel_codec_is_unencrypted={(isUnencrypted.Value ? "1" : "0")}");
            }

            string output = TelnetConnector.Instance.Execute(str.ToString());

            return(TeamspeakTools.TryGetParameter(output, "cid", out int cid) ? new Channel(cid) : null);
        }