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.api.ClientProperty;
030
031import java.util.Date;
032import java.util.Map;
033
034public class Client extends Wrapper {
035
036        public Client(Map<String, String> map) {
037                super(map);
038        }
039
040        public boolean canTalk() {
041                return getBoolean(ClientProperty.CLIENT_IS_TALKER);
042        }
043
044        public String getAwayMessage() {
045                return get(ClientProperty.CLIENT_AWAY_MESSAGE);
046        }
047
048        public int getChannelGroupId() {
049                return getInt(ClientProperty.CLIENT_CHANNEL_GROUP_ID);
050        }
051
052        public int getChannelId() {
053                return getInt(ClientProperty.CID);
054        }
055
056        public String getCountry() {
057                return get(ClientProperty.CLIENT_COUNTRY);
058        }
059
060        public Date getCreatedDate() {
061                return new Date(getLong(ClientProperty.CLIENT_CREATED) * 1000);
062        }
063
064        public int getDatabaseId() {
065                return getInt(ClientProperty.CLIENT_DATABASE_ID);
066        }
067
068        public long getIconId() {
069                return getLong(ClientProperty.CLIENT_ICON_ID);
070        }
071
072        public int getId() {
073                return getInt("clid");
074        }
075
076        public long getIdleTime() {
077                return getLong(ClientProperty.CLIENT_IDLE_TIME);
078        }
079
080        public int getInheritedChannelGroupId() {
081                return getInt(ClientProperty.CLIENT_CHANNEL_GROUP_INHERITED_CHANNEL_ID);
082        }
083
084        public String getIp() {
085                return get(ClientProperty.CONNECTION_CLIENT_IP);
086        }
087
088        public Date getLastConnectedDate() {
089                return new Date(getLong(ClientProperty.CLIENT_LASTCONNECTED) * 1000);
090        }
091
092        public String getNickname() {
093                return get(ClientProperty.CLIENT_NICKNAME);
094        }
095
096        public String getPlatform() {
097                return get(ClientProperty.CLIENT_PLATFORM);
098        }
099
100        public int[] getServerGroups() {
101                final String str = get(ClientProperty.CLIENT_SERVERGROUPS);
102                final String[] arr = str.split(",");
103                final int[] groups = new int[arr.length];
104                for (int i = 0; i < groups.length; i++) {
105                        groups[i] = Integer.parseInt(arr[i]);
106                }
107                return groups;
108        }
109
110        public int getTalkPower() {
111                return getInt(ClientProperty.CLIENT_TALK_POWER);
112        }
113
114        public int getType() {
115                return getInt(ClientProperty.CLIENT_TYPE);
116        }
117
118        public String getUniqueIdentifier() {
119                return get(ClientProperty.CLIENT_UNIQUE_IDENTIFIER);
120        }
121
122        public String getVersion() {
123                return get(ClientProperty.CLIENT_VERSION);
124        }
125
126        public boolean isAway() {
127                return getBoolean(ClientProperty.CLIENT_AWAY);
128        }
129
130        public boolean isChannelCommander() {
131                return getBoolean(ClientProperty.CLIENT_IS_CHANNEL_COMMANDER);
132        }
133
134        /**
135         * Utility method that does a linear search on the array of server group IDs returned
136         * by {@link #getServerGroups()} and returns {@code true} if that array contains
137         * the given server group ID.
138         *
139         * @param serverGroupId
140         *              the ID of the server group to search for
141         *
142         * @return whether this client is a member of the given server group
143         */
144        public boolean isInServerGroup(int serverGroupId) {
145                int[] serverGroupIds = getServerGroups();
146                for (int s : serverGroupIds) {
147                        if (s == serverGroupId) return true;
148                }
149                return false;
150        }
151
152        /**
153         * Utility method that does a linear search on the array of server group IDs returned
154         * by {@link #getServerGroups()} and returns {@code true} if that array contains
155         * the ID of the given server group.
156         *
157         * @param serverGroup
158         *              the server group to search for
159         *
160         * @return whether this client is a member of the given server group
161         */
162        public boolean isInServerGroup(ServerGroup serverGroup) {
163                return isInServerGroup(serverGroup.getId());
164        }
165
166        public boolean isInputHardware() {
167                return getBoolean(ClientProperty.CLIENT_INPUT_HARDWARE);
168        }
169
170        public boolean isInputMuted() {
171                return getBoolean(ClientProperty.CLIENT_INPUT_MUTED);
172        }
173
174        public boolean isOutputHardware() {
175                return getBoolean(ClientProperty.CLIENT_OUTPUT_HARDWARE);
176        }
177
178        public boolean isOutputMuted() {
179                return getBoolean(ClientProperty.CLIENT_OUTPUT_MUTED);
180        }
181
182        public boolean isPrioritySpeaker() {
183                return getBoolean(ClientProperty.CLIENT_IS_PRIORITY_SPEAKER);
184        }
185
186        public boolean isRecording() {
187                return getBoolean(ClientProperty.CLIENT_IS_RECORDING);
188        }
189
190        public boolean isRegularClient() {
191                return getType() == 0;
192        }
193
194        public boolean isServerQueryClient() {
195                return getType() == 1;
196        }
197
198        public boolean isTalking() {
199                return getBoolean("client_flag_talking");
200        }
201}