Exemplo n.º 1
0
 /// <summary>
 /// Get server variables
 /// </summary>
 public List <TS3Models.VirtualServer> ServerVariable(List <TS3Models.VirtualServerProperties> serverProperties)
 {
     if (IsConnected())
     {
         string query = "servervariable ";
         List <TS3Models.VirtualServerProperties> serverProps = serverProperties;
         if (serverProps == null || serverProps.Count == 0)
         {
             serverProps = new List <TS3Models.VirtualServerProperties>();
             foreach (TS3Models.VirtualServerProperties prop in Enum.GetValues(typeof(TS3Models.VirtualServerProperties))) //loop enum
             {
                 if (prop != TS3Models.VirtualServerProperties.virtualserver_status)
                 {
                     serverProps.Add(prop);
                 }
             }
         }
         foreach (TS3Models.VirtualServerProperties prop in serverProps)
         {
             query += string.Format(" {0}", prop);
         }
         ts3Connection.WriteLine(query);
         TS3Models.Result result = TS3Helper.ParseResult(ReadAll(), true);
         if (result != null && result.ErrorId == 0)
         {
             List <TS3Models.VirtualServer> server = new List <TS3Models.VirtualServer>();
             foreach (Dictionary <string, string> res in result.ResultsList)
             {
                 server.Add(new TS3Models.VirtualServer().FillWithResult(result, res));
             }
             return(server);
         }
     }
     return(null);
 }
Exemplo n.º 2
0
 /// <summary>
 /// Returns all clients which are connected to the server
 /// </summary>
 public List <TS3Models.Client> GetClientList()
 {
     if (IsConnected())
     {
         ts3Connection.WriteLine("clientlist");
         TS3Models.Result        result  = TS3Helper.ParseResult(ReadAll(), true);
         List <TS3Models.Client> clients = new List <TS3Models.Client>();
         foreach (Dictionary <string, string> res in result.ResultsList)
         {
             clients.Add(new TS3Models.Client()
             {
                 Result  = result,
                 Channel = new TS3Models.Channel()
                 {
                     CId = Convert.ToInt32(result.GetResultByList(res, "cid"))
                 },
                 ClId             = Convert.ToInt32(result.GetResultByList(res, "clid")),
                 ClientDatabaseId = Convert.ToInt32(result.GetResultByList(res, "client_database_id")),
                 ClientNickname   = result.GetResultByList(res, "client_nickname"),
                 ClientType       = Convert.ToInt32(result.GetResultByList(res, "client_type"))
             });
         }
         return(clients);
     }
     return(null);
 }
Exemplo n.º 3
0
 /// <summary>
 /// Unregister client notification
 /// </summary>
 public TS3Models.Result ClientNotifyUnregister(int schandlerid, TS3Models.Notifications notification)
 {
     if (IsConnected())
     {
         ts3Connection.WriteLine(string.Format("clientnotifyunregister schandlerid={0} event={1}", schandlerid, notification));
         TS3Models.Result result = TS3Helper.ParseResult(ReadAll(), false);
         if (result != null && result.ErrorId == 0)
         {
             Notifier.UnregisterNotification(notification);
         }
         return(result);
     }
     return(null);
 }
Exemplo n.º 4
0
 /// <summary>
 /// Returns all channels on the server
 /// </summary>
 public List <TS3Models.Channel> GetChannelList()
 {
     if (IsConnected())
     {
         ts3Connection.WriteLine("channellist");
         TS3Models.Result         result   = TS3Helper.ParseResult(ReadAll(), true);
         List <TS3Models.Channel> channels = new List <TS3Models.Channel>();
         foreach (Dictionary <string, string> res in result.ResultsList)
         {
             channels.Add(new TS3Models.Channel().FillWithResult(result, res));
         }
         return(channels);
     }
     return(null);
 }
Exemplo n.º 5
0
 /// <summary>
 /// Get channel variables
 /// Key: cid, Value: list of TS3Models.ChannelProperties
 /// </summary>
 public List <TS3Models.Channel> ChannelVariable(Dictionary <int, List <TS3Models.ChannelProperties> > channelVars)
 {
     if (IsConnected())
     {
         string query = "channelvariable ";
         foreach (KeyValuePair <int, List <TS3Models.ChannelProperties> > cvar in channelVars)
         {
             query += string.Format("cid={0}", cvar.Key);
             List <TS3Models.ChannelProperties> channelProps = cvar.Value;
             if (channelProps == null || channelProps.Count == 0)
             {
                 channelProps = new List <TS3Models.ChannelProperties>();
                 foreach (TS3Models.ChannelProperties prop in Enum.GetValues(typeof(TS3Models.ChannelProperties))) //loop enum
                 {
                     if (prop != TS3Models.ChannelProperties.cpid &&
                         prop != TS3Models.ChannelProperties.channel_flag_temporary &&
                         prop != TS3Models.ChannelProperties.seconds_empty &&
                         prop != TS3Models.ChannelProperties.total_clients &&
                         prop != TS3Models.ChannelProperties.total_clients_family &&
                         prop != TS3Models.ChannelProperties.channel_needed_subscribe_power)
                     {
                         channelProps.Add(prop);
                     }
                 }
             }
             foreach (TS3Models.ChannelProperties prop in channelProps)
             {
                 query += string.Format(" {0}", prop);
             }
             query += "|";
         }
         query = query.Remove(query.Length - 1);
         ts3Connection.WriteLine(query);
         TS3Models.Result result = TS3Helper.ParseResult(ReadAll(), true);
         if (result != null && result.ErrorId == 0)
         {
             List <TS3Models.Channel> channels = new List <TS3Models.Channel>();
             foreach (Dictionary <string, string> res in result.ResultsList)
             {
                 channels.Add(new TS3Models.Channel().FillWithResult(result, res));
             }
             return(channels);
         }
     }
     return(null);
 }
Exemplo n.º 6
0
 internal void ReceiveNotification(string notificationString)
 {
     if (!string.IsNullOrEmpty(notificationString))
     {
         foreach (TS3Models.Notifications noti in registeredNotifications)
         {
             if (notificationString.Contains(noti.ToString()))
             {
                 TS3Models.Result result = TS3Helper.ParseResult(notificationString, true);
                 if (OnNotification != null)
                 {
                     OnNotification(noti, result);
                 }
                 ParseNotification(noti, result);
             }
         }
     }
 }
Exemplo n.º 7
0
 /// <summary>
 /// Returns local client
 /// </summary>
 public TS3Models.Client GetWhoami()
 {
     if (IsConnected())
     {
         ts3Connection.WriteLine("whoami");
         TS3Models.Result result = TS3Helper.ParseResult(ReadAll(), false);
         return(new TS3Models.Client()
         {
             Result = result,
             Channel = new TS3Models.Channel()
             {
                 CId = Convert.ToInt32(result.GetFirstResult("cid"))
             },
             ClId = Convert.ToInt32(result.GetFirstResult("clid"))
         });
     }
     return(null);
 }
Exemplo n.º 8
0
 /// <summary>
 /// List all client tabs
 /// </summary>
 public List <int> ServerConnectionHandlerList()
 {
     if (IsConnected())
     {
         string query = "serverconnectionhandlerlist";
         ts3Connection.WriteLine(query);
         TS3Models.Result result = TS3Helper.ParseResult(ReadAll(), true);
         if (result != null && result.ErrorId == 0)
         {
             List <int> handlers = new List <int>();
             foreach (Dictionary <string, string> res in result.ResultsList)
             {
                 handlers.Add(Convert.ToInt32(result.GetResultByList(res, "schandlerid")));
             }
             return(handlers);
         }
     }
     return(null);
 }
Exemplo n.º 9
0
 /// <summary>
 /// Selects the client tab
 /// </summary>
 public TS3Models.Result Use(int?schandlerid = null)
 {
     if (IsConnected())
     {
         string query = "use";
         if (schandlerid != null)
         {
             query += string.Format(" schandlerid={0}", schandlerid);
         }
         ts3Connection.WriteLine(query);
         TS3Models.Result result = TS3Helper.ParseResult(ReadAll(), false);
         if (result != null && result.ErrorId == 0)
         {
             CurrentHandlerId = Convert.ToInt32(result.GetFirstResult("schandlerid"));
         }
         return(result);
     }
     return(null);
 }
Exemplo n.º 10
0
 /// <summary>
 /// Get client variables
 /// Key: clid, Value: list of TS3Models.ClientProperties
 /// </summary>
 public List <TS3Models.Client> ClientVariable(Dictionary <int, List <TS3Models.ClientProperties> > clientVars)
 {
     if (IsConnected())
     {
         string query = "clientvariable ";
         foreach (KeyValuePair <int, List <TS3Models.ClientProperties> > cvar in clientVars)
         {
             query += string.Format("clid={0}", cvar.Key);
             List <TS3Models.ClientProperties> clientProps = cvar.Value;
             if (clientProps == null || clientProps.Count == 0)
             {
                 clientProps = new List <TS3Models.ClientProperties>();
                 foreach (TS3Models.ClientProperties prop in Enum.GetValues(typeof(TS3Models.ClientProperties))) //loop enum
                 {
                     if (prop != TS3Models.ClientProperties.pid &&
                         prop != TS3Models.ClientProperties.client_base64HashClientUID)
                     {
                         clientProps.Add(prop);
                     }
                 }
             }
             foreach (TS3Models.ClientProperties prop in clientProps)
             {
                 query += string.Format(" {0}", prop);
             }
             query += "|";
         }
         query = query.Remove(query.Length - 1);
         ts3Connection.WriteLine(query);
         TS3Models.Result result = TS3Helper.ParseResult(ReadAll(), true);
         if (result != null && result.ErrorId == 0)
         {
             List <TS3Models.Client> clients = new List <TS3Models.Client>();
             foreach (Dictionary <string, string> res in result.ResultsList)
             {
                 clients.Add(new TS3Models.Client().FillWithResult(result, res));
             }
             return(clients);
         }
     }
     return(null);
 }
Exemplo n.º 11
0
        internal bool Connect(string hostname = "localhost", int port = 25639)
        {
            if (!IsConnected())
            {
                try
                {
                    tcpClient = new TcpClient(hostname, port);
                    if (!IsConnected())
                    {
                        throw new Exception("Could not connect.");
                    }
                }
                catch
                {
                    return(false);
                }

                networkStream = tcpClient.GetStream();
                streamReader  = new StreamReader(networkStream);
                streamWriter  = new StreamWriter(networkStream)
                {
                    NewLine = "\n"
                };

                // Ignore welcome message
                streamReader.ReadLine();
                streamReader.ReadLine();
                streamReader.ReadLine();
                streamReader.ReadLine();

                StartResponseLoop();
                TS3Models.Result result = TS3Helper.ParseResult(ReadAll(), false);
                if (result != null && result.ErrorId == 0)
                {
                    ts3Client.CurrentHandlerId = Convert.ToInt32(result.GetFirstResult("schandlerid"));
                }
                return(true);
            }
            return(false);
        }
Exemplo n.º 12
0
        internal static TS3Models.Result ParseResult(string resultString, bool isMany)
        {
            if (!string.IsNullOrEmpty(resultString))
            {
                TS3Models.Result result = new TS3Models.Result();
                result.ResultString = resultString;

                Match errorMatch = errorRegex.Match(resultString);
                if (errorMatch.Success)
                {
                    result.ErrorId  = Convert.ToInt32(errorMatch.Groups[1].Value);
                    result.ErrorMsg = UnescapeString(errorMatch.Groups[2].Value);
                    resultString    = errorRegex.Replace(resultString, "");
                }

                if (!string.IsNullOrEmpty(resultString))
                {
                    result.ResultsList = GetParamList(resultString, isMany);
                }
                return(result);
            }
            return(null);
        }
Exemplo n.º 13
0
        private void ParseNotification(TS3Models.Notifications notification, TS3Models.Result result)
        {
            switch (notification)
            {
                #region Client
            case TS3Models.Notifications.notifytextmessage:
                if (OnTextMessage != null)
                {
                    OnTextMessage(new TS3Models.TextMessage()
                    {
                        Result     = result,
                        TargetMode = (TS3Models.TextMessageTargetMode)Convert.ToInt32(result.GetFirstResult("targetmode")),
                        Msg        = result.GetFirstResult("msg"),
                        Target     = Convert.ToInt32(result.GetFirstResult("target")),
                        Invoker    = new TS3Models.Client()
                        {
                            ClId                   = Convert.ToInt32(result.GetFirstResult("invokerid")),
                            ClientNickname         = result.GetFirstResult("invokername"),
                            ClientUniqueIdentifier = result.GetFirstResult("invokeruid"),
                        }
                    });
                }
                break;

            case TS3Models.Notifications.notifyclientpoke:
                if (OnClientPoke != null)
                {
                    OnClientPoke(new TS3Models.ClientPoke()
                    {
                        Result  = result,
                        Msg     = result.GetFirstResult("msg"),
                        Invoker = new TS3Models.Client()
                        {
                            ClId                   = Convert.ToInt32(result.GetFirstResult("invokerid")),
                            ClientNickname         = result.GetFirstResult("invokername"),
                            ClientUniqueIdentifier = result.GetFirstResult("invokeruid"),
                        }
                    });
                }
                break;

            case TS3Models.Notifications.notifycliententerview:
                if (OnClientEnterView != null)
                {
                    OnClientEnterView(new TS3Models.ClientEnterView()
                    {
                        Result = result,
                        CfId   = Convert.ToInt32(result.GetFirstResult("cfid")),
                        CtId   = Convert.ToInt32(result.GetFirstResult("ctid")),
                        Reason = (TS3Models.ReasonIdentifier)Convert.ToInt32(result.GetFirstResult("reasonid")),
                        Client = new TS3Models.Client().FillWithResult(result)
                    });
                }
                break;

            case TS3Models.Notifications.notifyclientleftview:
                if (OnClientLeftView != null)
                {
                    OnClientLeftView(new TS3Models.ClientLeftView()
                    {
                        Result  = result,
                        CfId    = Convert.ToInt32(result.GetFirstResult("cfid")),
                        CtId    = Convert.ToInt32(result.GetFirstResult("ctid")),
                        Reason  = (TS3Models.ReasonIdentifier)Convert.ToInt32(result.GetFirstResult("reasonid")),
                        Invoker = new TS3Models.Client()
                        {
                            ClId                   = Convert.ToInt32(result.GetFirstResult("invokerid")),
                            ClientNickname         = result.GetFirstResult("invokername"),
                            ClientUniqueIdentifier = result.GetFirstResult("invokeruid"),
                        },
                        ReasonMsg = result.GetFirstResult("reasonmsg"),
                        BanTime   = Convert.ToInt32(result.GetFirstResult("bantime")),
                        ClId      = Convert.ToInt32(result.GetFirstResult("clid"))
                    });
                }
                break;

            case TS3Models.Notifications.notifyclientmoved:
                if (OnClientMoved != null)
                {
                    List <TS3Models.Client> clients = new List <TS3Models.Client>();
                    foreach (Dictionary <string, string> list in result.ResultsList)
                    {
                        string id = result.GetResultByList(list, "clid");
                        if (!string.IsNullOrEmpty(id))
                        {
                            clients.Add(new TS3Models.Client()
                            {
                                ClId = Convert.ToInt32(id)
                            });
                        }
                    }

                    OnClientMoved(new TS3Models.ClientMoved()
                    {
                        Result  = result,
                        Channel = new TS3Models.Channel()
                        {
                            CId = Convert.ToInt32(result.GetFirstResult("ctid"))
                        },
                        Reason  = (TS3Models.ReasonIdentifier)Convert.ToInt32(result.GetFirstResult("reasonid")),
                        Invoker = new TS3Models.Client()
                        {
                            ClId                   = Convert.ToInt32(result.GetFirstResult("invokerid")),
                            ClientNickname         = result.GetFirstResult("invokername"),
                            ClientUniqueIdentifier = result.GetFirstResult("invokeruid"),
                        },
                        Clients = clients
                    });
                }
                break;

                #endregion
                #region Channel
            case TS3Models.Notifications.notifychannelmoved:
                if (OnChannelMoved != null)
                {
                    OnChannelMoved(new TS3Models.ChannelMoved()
                    {
                        Result  = result,
                        Reason  = (TS3Models.ReasonIdentifier)Convert.ToInt32(result.GetFirstResult("reasonid")),
                        Invoker = new TS3Models.Client()
                        {
                            ClId                   = Convert.ToInt32(result.GetFirstResult("invokerid")),
                            ClientNickname         = result.GetFirstResult("invokername"),
                            ClientUniqueIdentifier = result.GetFirstResult("invokeruid"),
                        },
                        Channel = new TS3Models.Channel().FillWithResult(result)
                    });
                }
                break;

            case TS3Models.Notifications.notifychanneledited:
                if (OnChannelEdited != null)
                {
                    OnChannelEdited(new TS3Models.ChannelEdited()
                    {
                        Result  = result,
                        Reason  = (TS3Models.ReasonIdentifier)Convert.ToInt32(result.GetFirstResult("reasonid")),
                        Invoker = new TS3Models.Client()
                        {
                            ClId                   = Convert.ToInt32(result.GetFirstResult("invokerid")),
                            ClientNickname         = result.GetFirstResult("invokername"),
                            ClientUniqueIdentifier = result.GetFirstResult("invokeruid"),
                        },
                        Channel = new TS3Models.Channel().FillWithResult(result)
                    });
                }
                break;

            case TS3Models.Notifications.notifychannelcreated:
                if (OnChannelCreated != null)
                {
                    OnChannelCreated(new TS3Models.ChannelCreated()
                    {
                        Result  = result,
                        Invoker = new TS3Models.Client()
                        {
                            ClId                   = Convert.ToInt32(result.GetFirstResult("invokerid")),
                            ClientNickname         = result.GetFirstResult("invokername"),
                            ClientUniqueIdentifier = result.GetFirstResult("invokeruid"),
                        },
                        Channel = new TS3Models.Channel().FillWithResult(result)
                    });
                }
                break;

            case TS3Models.Notifications.notifychanneldeleted:
                if (OnChannelDeleted != null)
                {
                    OnChannelDeleted(new TS3Models.ChannelDeleted()
                    {
                        Result  = result,
                        Invoker = new TS3Models.Client()
                        {
                            ClId                   = Convert.ToInt32(result.GetFirstResult("invokerid")),
                            ClientNickname         = result.GetFirstResult("invokername"),
                            ClientUniqueIdentifier = result.GetFirstResult("invokeruid"),
                        },
                        Channel = new TS3Models.Channel().FillWithResult(result)
                    });
                }
                break;

                #endregion
                #region Server
            case TS3Models.Notifications.notifyserveredited:
                if (OnServerEdited != null)
                {
                    OnServerEdited(new TS3Models.ServerEdited()
                    {
                        Result  = result,
                        Reason  = (TS3Models.ReasonIdentifier)Convert.ToInt32(result.GetFirstResult("reasonid")),
                        Invoker = new TS3Models.Client()
                        {
                            ClId                   = Convert.ToInt32(result.GetFirstResult("invokerid")),
                            ClientNickname         = result.GetFirstResult("invokername"),
                            ClientUniqueIdentifier = result.GetFirstResult("invokeruid"),
                        },
                        VirtualServer = new TS3Models.VirtualServer().FillWithResult(result)
                    });
                }
                break;

            case TS3Models.Notifications.notifyservergrouplist:
                if (OnServerGroupList != null)
                {
                    List <TS3Models.ServerGroup> serverGroups = new List <TS3Models.ServerGroup>();
                    foreach (Dictionary <string, string> res in result.ResultsList)
                    {
                        serverGroups.Add(new TS3Models.ServerGroup().FillWithResult(result, res));
                    }
                    OnServerGroupList(serverGroups);
                }
                break;
                #endregion
            }
        }