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.io.UnsupportedEncodingException; 032import java.net.URLEncoder; 033import java.util.Date; 034import java.util.Map; 035 036public class Client extends Wrapper { 037 038 public Client(Map<String, String> map) { 039 super(map); 040 } 041 042 public boolean canTalk() { 043 return getBoolean(ClientProperty.CLIENT_IS_TALKER); 044 } 045 046 public String getAwayMessage() { 047 return get(ClientProperty.CLIENT_AWAY_MESSAGE); 048 } 049 050 public String[] getBadgeGUIDs() { 051 String raw = get(ClientProperty.CLIENT_BADGES); 052 String[] properties = raw.split("[\\s:;]"); 053 for (String property : properties) { 054 if (!property.startsWith("badges=")) continue; 055 String commaSepBadges = property.substring("badges=".length()); 056 return commaSepBadges.split(","); 057 } 058 return new String[0]; 059 } 060 061 public int getChannelGroupId() { 062 return getInt(ClientProperty.CLIENT_CHANNEL_GROUP_ID); 063 } 064 065 public int getChannelId() { 066 return getInt(ClientProperty.CID); 067 } 068 069 /** 070 * Utility method. This method will return a client URI. 071 * A client URI can be used to reference a client in descriptions or just via chat. 072 * Example: {@code client://<clientId>/<clientUId>~<clientNickname>} 073 * 074 * @return Client's URI 075 */ 076 public String getClientURI() { 077 StringBuilder sb = new StringBuilder(); 078 sb.append("client://").append(getId()).append('/'); 079 sb.append(getUniqueIdentifier()).append('~'); 080 try { 081 // We will encode the nickname, so characters like spaces work with this. 082 sb.append(URLEncoder.encode(getNickname(), "UTF-8")); 083 } catch (UnsupportedEncodingException e) { 084 throw new IllegalStateException("JVM doesn't support UTF-8", e); 085 } 086 return sb.toString(); 087 } 088 089 public String getCountry() { 090 return get(ClientProperty.CLIENT_COUNTRY); 091 } 092 093 public Date getCreatedDate() { 094 return new Date(getLong(ClientProperty.CLIENT_CREATED) * 1000); 095 } 096 097 public int getDatabaseId() { 098 return getInt(ClientProperty.CLIENT_DATABASE_ID); 099 } 100 101 public String getEstimatedLocation() { 102 return get(ClientProperty.CLIENT_ESTIMATED_LOCATION); 103 } 104 105 public long getIconId() { 106 return getLong(ClientProperty.CLIENT_ICON_ID); 107 } 108 109 public int getId() { 110 return getInt(ClientProperty.CLID); 111 } 112 113 public long getIdleTime() { 114 return getLong(ClientProperty.CLIENT_IDLE_TIME); 115 } 116 117 public int getInheritedChannelGroupId() { 118 return getInt(ClientProperty.CLIENT_CHANNEL_GROUP_INHERITED_CHANNEL_ID); 119 } 120 121 public String getIp() { 122 return get(ClientProperty.CONNECTION_CLIENT_IP); 123 } 124 125 public Date getLastConnectedDate() { 126 return new Date(getLong(ClientProperty.CLIENT_LASTCONNECTED) * 1000); 127 } 128 129 public String getNickname() { 130 return get(ClientProperty.CLIENT_NICKNAME); 131 } 132 133 public String getPlatform() { 134 return get(ClientProperty.CLIENT_PLATFORM); 135 } 136 137 public int[] getServerGroups() { 138 final String str = get(ClientProperty.CLIENT_SERVERGROUPS); 139 final String[] arr = str.split(","); 140 final int[] groups = new int[arr.length]; 141 for (int i = 0; i < groups.length; i++) { 142 groups[i] = Integer.parseInt(arr[i]); 143 } 144 return groups; 145 } 146 147 public int getTalkPower() { 148 return getInt(ClientProperty.CLIENT_TALK_POWER); 149 } 150 151 public int getType() { 152 return getInt(ClientProperty.CLIENT_TYPE); 153 } 154 155 public String getUniqueIdentifier() { 156 return get(ClientProperty.CLIENT_UNIQUE_IDENTIFIER); 157 } 158 159 public String getVersion() { 160 return get(ClientProperty.CLIENT_VERSION); 161 } 162 163 public boolean hasOverwolf() { 164 String raw = get(ClientProperty.CLIENT_BADGES); 165 String[] properties = raw.split("[\\s:;]"); 166 for (String property : properties) { 167 if (!(property.startsWith("overwolf=") || property.startsWith("Overwolf="))) continue; 168 String overwolfValue = property.substring("overwolf=".length()); 169 return overwolfValue.equals("1"); 170 } 171 return false; 172 } 173 174 public boolean isAway() { 175 return getBoolean(ClientProperty.CLIENT_AWAY); 176 } 177 178 public boolean isChannelCommander() { 179 return getBoolean(ClientProperty.CLIENT_IS_CHANNEL_COMMANDER); 180 } 181 182 /** 183 * Utility method that does a linear search on the array of server group IDs returned 184 * by {@link #getServerGroups()} and returns {@code true} if that array contains 185 * the given server group ID. 186 * 187 * @param serverGroupId 188 * the ID of the server group to search for 189 * 190 * @return whether this client is a member of the given server group 191 */ 192 public boolean isInServerGroup(int serverGroupId) { 193 int[] serverGroupIds = getServerGroups(); 194 for (int s : serverGroupIds) { 195 if (s == serverGroupId) return true; 196 } 197 return false; 198 } 199 200 /** 201 * Utility method that does a linear search on the array of server group IDs returned 202 * by {@link #getServerGroups()} and returns {@code true} if that array contains 203 * the ID of the given server group. 204 * 205 * @param serverGroup 206 * the server group to search for 207 * 208 * @return whether this client is a member of the given server group 209 */ 210 public boolean isInServerGroup(ServerGroup serverGroup) { 211 return isInServerGroup(serverGroup.getId()); 212 } 213 214 public boolean isInputHardware() { 215 return getBoolean(ClientProperty.CLIENT_INPUT_HARDWARE); 216 } 217 218 public boolean isInputMuted() { 219 return getBoolean(ClientProperty.CLIENT_INPUT_MUTED); 220 } 221 222 public boolean isOutputHardware() { 223 return getBoolean(ClientProperty.CLIENT_OUTPUT_HARDWARE); 224 } 225 226 public boolean isOutputMuted() { 227 return getBoolean(ClientProperty.CLIENT_OUTPUT_MUTED); 228 } 229 230 public boolean isPrioritySpeaker() { 231 return getBoolean(ClientProperty.CLIENT_IS_PRIORITY_SPEAKER); 232 } 233 234 public boolean isRecording() { 235 return getBoolean(ClientProperty.CLIENT_IS_RECORDING); 236 } 237 238 public boolean isRegularClient() { 239 return getType() == 0; 240 } 241 242 public boolean isServerQueryClient() { 243 return getType() == 1; 244 } 245 246 public boolean isTalking() { 247 return getBoolean(ClientProperty.CLIENT_FLAG_TALKING); 248 } 249}