Пример #1
0
        protected void RunQueries(Action<QueryRunner> action)
        {
            if (action == null)
                throw new ArgumentNullException("action");

            string host = ConfigurationManager.AppSettings["host"];

            if (host == null)
                throw new ConfigurationErrorsException("host could not be found in web.config");

            string portString = ConfigurationManager.AppSettings["port"];

            if (portString == null)
                throw new ConfigurationErrorsException("port could not be found in web.config");

            ushort port;

            if (!ushort.TryParse(portString, NumberStyles.Integer, CultureInfo.InvariantCulture, out port))
                throw new ConfigurationErrorsException("port in web.config has invalid an invalid format.");

            string login = ConfigurationManager.AppSettings["login"];
            string password = ConfigurationManager.AppSettings["password"];


            using (QueryRunner queryRunner = new QueryRunner(new SyncTcpDispatcher(host, port)))  // host and port
            {
                // connection to the TS3-Server is established with the first query command

                if (!login.IsNullOrTrimmedEmpty() && !password.IsNullOrTrimmedEmpty())
                {
                    SimpleResponse loginResponse = queryRunner.Login(login, password); // login using the provided username and password and show a dump-output of the response in a textbox

                    if (loginResponse.IsErroneous)
                        throw new NotSupportedException("Login failed for the given username and password!");
                }

                action(queryRunner);
            }
        }
Пример #2
0
        private void QueryDispatcher_ReadyForSendingCommands(object sender, EventArgs e)
        {
            QueryRunner = new QueryRunner(QueryDispatcher);

            QueryRunner.Login("Obama", "maTf15+J");
            QueryRunner.SelectVirtualServerById(1);

            QueryRunner.Notifications.ServerMessageReceived += Notifications_ServerMessageReceived;
            QueryRunner.RegisterForNotifications(ServerNotifyRegisterEvent.TextServer, 1);

            QueryRunner.Notifications.ChannelMessageReceived += Notifications_ChannelMessageReceived;
            QueryRunner.RegisterForNotifications(ServerNotifyRegisterEvent.TextChannel, 1);

            QueryDispatcher.BanDetected += QueryDispatcher_BanDetected;

            QueryDispatcher.ServerClosedConnection += QueryDispatcher_ServerClosedConnection;
        }
        /// <summary>
        /// Inits the credential.
        /// </summary>
        private void InitCredential()
        {
            TcpDispatcher = new SyncTcpDispatcher(BotInstance.Settings);
            QueryRunner = new QueryRunner(TcpDispatcher);
            QueryRunner.Login(BotInstance.Settings.TeamSpeak.Username, BotInstance.Settings.TeamSpeak.Password);
            if (BotInstance.Settings.TeamSpeak.InstancePort.GetValueOrDefault() > 0)
                QueryRunner.SelectVirtualServerByPort(BotInstance.Settings.TeamSpeak.InstancePort.GetValueOrDefault());
            else
                QueryRunner.SelectVirtualServerById(BotInstance.Settings.TeamSpeak.Instance.GetValueOrDefault());
            Self = QueryRunner.SendWhoAmI();

            WorkerTcpDispatcher = new SyncTcpDispatcher(BotInstance.Settings);
            WorkerQueryRunner = new QueryRunner(WorkerTcpDispatcher);
            WorkerQueryRunner.Login(BotInstance.Settings.TeamSpeak.Username, BotInstance.Settings.TeamSpeak.Password);

            if (BotInstance.Settings.TeamSpeak.InstancePort.GetValueOrDefault() > 0)
                WorkerQueryRunner.SelectVirtualServerByPort(BotInstance.Settings.TeamSpeak.InstancePort.GetValueOrDefault());
            else
                WorkerQueryRunner.SelectVirtualServerById(BotInstance.Settings.TeamSpeak.Instance.GetValueOrDefault());

            WorkerQueryRunner.UpdateCurrentQueryClient(new ClientModification { Nickname = BotInstance.Settings.Global.BotNickname });
            SelfWorker = WorkerQueryRunner.SendWhoAmI();
            WorkerQueryRunner.AddLogEntry(new LogEntryLight(LogLevel.Info, string.Format("TS3-Bot '{0}' connected.", BotInstance.Settings.Global.BotNickname)));

            NotificationTcpDispatcher = new AsyncTcpDispatcher(BotInstance.Settings);
            NotificationQueryRunner = new QueryRunner(NotificationTcpDispatcher);

            NotificationTcpDispatcher.ServerClosedConnection += notificationDispatcher_ServerClosedConnection;
            NotificationTcpDispatcher.ReadyForSendingCommands += notificationDispatcher_ReadyForSendingCommands;
            NotificationQueryRunner.Notifications.ClientJoined += BotInstance.Notifications_ClientJoined;
            NotificationQueryRunner.Notifications.ClientMoved += BotInstance.Notifications_ClientMoved;
            NotificationQueryRunner.Notifications.ClientMoveForced += BotInstance.Notifications_ClientMoveForced;
            NotificationQueryRunner.Notifications.ClientDisconnect += BotInstance.Notifications_ClientDisconnect;
            NotificationQueryRunner.Notifications.ClientConnectionLost += BotInstance.Notifications_ClientConnectionLost;
            NotificationQueryRunner.Notifications.ServerMessageReceived += BotInstance.Notifications_MessageReceived;
            NotificationQueryRunner.Notifications.ChannelMessageReceived += BotInstance.Notifications_MessageReceived;
            NotificationQueryRunner.Notifications.ClientMessageReceived += BotInstance.Notifications_MessageReceived;

            TcpDispatcher.Connect();
            WorkerTcpDispatcher.Connect();
            NotificationTcpDispatcher.Connect();
            Initialized = true;
        }