001package com.github.theholywaffle.teamspeak3.api.wrapper;
002
003/*
004 * #%L
005 * TeamSpeak 3 Java API
006 * %%
007 * Copyright (C) 2014 Bert De Geyter
008 * %%
009 * Permission is hereby granted, free of charge, to any person obtaining a copy
010 * of this software and associated documentation files (the "Software"), to deal
011 * in the Software without restriction, including without limitation the rights
012 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
013 * copies of the Software, and to permit persons to whom the Software is
014 * furnished to do so, subject to the following conditions:
015 * 
016 * The above copyright notice and this permission notice shall be included in
017 * all copies or substantial portions of the Software.
018 * 
019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
020 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
021 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
022 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
023 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
024 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
025 * THE SOFTWARE.
026 * #L%
027 */
028
029import com.github.theholywaffle.teamspeak3.TS3Api;
030import com.github.theholywaffle.teamspeak3.TS3ApiAsync;
031import com.github.theholywaffle.teamspeak3.api.ClientProperty;
032import com.github.theholywaffle.teamspeak3.api.VirtualServerProperty;
033import com.github.theholywaffle.teamspeak3.api.VirtualServerStatus;
034
035import java.util.Map;
036
037/**
038 * A class containing information about a server query, returned by the
039 * API methods {@link TS3Api#whoAmI()} and {@link TS3ApiAsync#whoAmI()}.
040 */
041public class ServerQueryInfo extends Wrapper {
042
043        /**
044         * Creates a new {@code ServerQueryInfo} from the information present in the provided map.
045         *
046         * @param map
047         *              the map containing the key-value pairs abstracted by this object
048         */
049        public ServerQueryInfo(Map<String, String> map) {
050                super(map);
051        }
052
053        /**
054         * Gets the ID of the channel the server query is currently in.
055         *
056         * @return the ID of the current channel
057         */
058        public int getChannelId() {
059                return getInt("client_channel_id");
060        }
061
062        /**
063         * Gets the ID of the server query in the database.
064         * <p>
065         * In case this server query account was created by a client,
066         * its database ID will be identical to the client's database ID.
067         * </p>
068         *
069         * @return the server query's database ID
070         */
071        public int getDatabaseId() {
072                return getInt(ClientProperty.CLIENT_DATABASE_ID);
073        }
074
075        /**
076         * Gets the client ID of the server query.
077         *
078         * @return the server query's client ID
079         */
080        public int getId() {
081                return getInt("client_id");
082        }
083
084        /**
085         * Gets the username that was used when logging the query in (using a username-password combination).
086         * <p>
087         * This username was set when creating the server query login and doesn't have to be related to
088         * the client who created the server query login.
089         * <br>
090         * In case a server query is not logged in yet, this method will return an empty string.
091         * </p>
092         *
093         * @return the username used when logging this query in
094         *
095         * @see TS3Api#login(String, String) TS3Api#login(username, password) - logging in server queries
096         */
097        public String getLoginName() {
098                return get(ClientProperty.CLIENT_LOGIN_NAME);
099        }
100
101        /**
102         * Gets the nickname currently used by the server query.
103         * Unless explicitly set, the nickname will be formatted like
104         * <pre><i>username</i> from <i>ip</i>:<i>port</i></pre>
105         * <p>
106         * Nicknames are only assigned after a virtual server has been selected.
107         * Until then, this method will return an empty string.
108         * </p>
109         *
110         * @return the current nickname of the server query
111         */
112        public String getNickname() {
113                return get(ClientProperty.CLIENT_NICKNAME);
114        }
115
116        /**
117         * Gets the ID of the virtual server on which the server query login was created.
118         * <p>
119         * This method will return {@code 0} (the ID of the template server) if a server query is
120         * not logged in or using the {@code serveradmin} login.
121         * </p>
122         *
123         * @return the ID of the virtual server this server query belongs to
124         */
125        public int getOriginServerId() {
126                return getInt("client_origin_server_id");
127        }
128
129        /**
130         * Gets the unique identifier of the server query.
131         * <p>
132         * In case this server query account was created by a client,
133         * its unique ID will be identical to the client's unique ID.
134         * </p>
135         *
136         * @return the server query's unique ID
137         */
138        public String getUniqueIdentifier() {
139                return get(ClientProperty.CLIENT_UNIQUE_IDENTIFIER);
140        }
141
142        /**
143         * Gets the ID of the currently selected virtual server.
144         * <p>
145         * If used on a non-commercial TeamSpeak instance which can only host 1 virtual server,
146         * this ID will always be 1.
147         * <br>
148         * If no virtual server has been selected yet, this method will return {@code 0}.
149         * </p>
150         *
151         * @return the ID of the current virtual server or {@code 0} if none is selected
152         */
153        public int getVirtualServerId() {
154                return getInt(VirtualServerProperty.VIRTUALSERVER_ID);
155        }
156
157        /**
158         * Gets the port used by the currently selected virtual server.
159         * <p>
160         * If no virtual server has been selected yet, this method will return {@code 0}.
161         * </p>
162         *
163         * @return the port used by the current virtual server or {@code 0} if none is selected
164         */
165        public int getVirtualServerPort() {
166                return getInt(VirtualServerProperty.VIRTUALSERVER_PORT);
167        }
168
169        /**
170         * Gets the status of the currently selected virtual server.
171         * <p>
172         * If no virtual server has been selected yet, this method will return {@link VirtualServerStatus#UNKNOWN}.
173         * </p>
174         *
175         * @return the status of the current virtual server
176         */
177        public VirtualServerStatus getVirtualServerStatus() {
178                final String status = get(VirtualServerProperty.VIRTUALSERVER_STATUS);
179                for (final VirtualServerStatus s : VirtualServerStatus.values()) {
180                        if (s.getName().equals(status)) {
181                                return s;
182                        }
183                }
184                return VirtualServerStatus.UNKNOWN;
185        }
186
187        /**
188         * Gets the unique identifier of the currently selected virtual server.
189         * <p>
190         * If no virtual server has been selected yet, this method will return an empty string.
191         * </p>
192         *
193         * @return the unique ID of the current virtual server
194         */
195        public String getVirtualServerUniqueIdentifier() {
196                return get(VirtualServerProperty.VIRTUALSERVER_UNIQUE_IDENTIFIER);
197        }
198}