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 long getIconId() { 102 return getLong(ClientProperty.CLIENT_ICON_ID); 103 } 104 105 public int getId() { 106 return getInt("clid"); 107 } 108 109 public long getIdleTime() { 110 return getLong(ClientProperty.CLIENT_IDLE_TIME); 111 } 112 113 public int getInheritedChannelGroupId() { 114 return getInt(ClientProperty.CLIENT_CHANNEL_GROUP_INHERITED_CHANNEL_ID); 115 } 116 117 public String getIp() { 118 return get(ClientProperty.CONNECTION_CLIENT_IP); 119 } 120 121 public Date getLastConnectedDate() { 122 return new Date(getLong(ClientProperty.CLIENT_LASTCONNECTED) * 1000); 123 } 124 125 public String getNickname() { 126 return get(ClientProperty.CLIENT_NICKNAME); 127 } 128 129 public String getPlatform() { 130 return get(ClientProperty.CLIENT_PLATFORM); 131 } 132 133 public int[] getServerGroups() { 134 final String str = get(ClientProperty.CLIENT_SERVERGROUPS); 135 final String[] arr = str.split(","); 136 final int[] groups = new int[arr.length]; 137 for (int i = 0; i < groups.length; i++) { 138 groups[i] = Integer.parseInt(arr[i]); 139 } 140 return groups; 141 } 142 143 public int getTalkPower() { 144 return getInt(ClientProperty.CLIENT_TALK_POWER); 145 } 146 147 public int getType() { 148 return getInt(ClientProperty.CLIENT_TYPE); 149 } 150 151 public String getUniqueIdentifier() { 152 return get(ClientProperty.CLIENT_UNIQUE_IDENTIFIER); 153 } 154 155 public String getVersion() { 156 return get(ClientProperty.CLIENT_VERSION); 157 } 158 159 public boolean hasOverwolf() { 160 String raw = get(ClientProperty.CLIENT_BADGES); 161 String[] properties = raw.split("[\\s:;]"); 162 for (String property : properties) { 163 if (!(property.startsWith("overwolf=") || property.startsWith("Overwolf="))) continue; 164 String overwolfValue = property.substring("overwolf=".length()); 165 return overwolfValue.equals("1"); 166 } 167 return false; 168 } 169 170 public boolean isAway() { 171 return getBoolean(ClientProperty.CLIENT_AWAY); 172 } 173 174 public boolean isChannelCommander() { 175 return getBoolean(ClientProperty.CLIENT_IS_CHANNEL_COMMANDER); 176 } 177 178 /** 179 * Utility method that does a linear search on the array of server group IDs returned 180 * by {@link #getServerGroups()} and returns {@code true} if that array contains 181 * the given server group ID. 182 * 183 * @param serverGroupId 184 * the ID of the server group to search for 185 * 186 * @return whether this client is a member of the given server group 187 */ 188 public boolean isInServerGroup(int serverGroupId) { 189 int[] serverGroupIds = getServerGroups(); 190 for (int s : serverGroupIds) { 191 if (s == serverGroupId) return true; 192 } 193 return false; 194 } 195 196 /** 197 * Utility method that does a linear search on the array of server group IDs returned 198 * by {@link #getServerGroups()} and returns {@code true} if that array contains 199 * the ID of the given server group. 200 * 201 * @param serverGroup 202 * the server group to search for 203 * 204 * @return whether this client is a member of the given server group 205 */ 206 public boolean isInServerGroup(ServerGroup serverGroup) { 207 return isInServerGroup(serverGroup.getId()); 208 } 209 210 public boolean isInputHardware() { 211 return getBoolean(ClientProperty.CLIENT_INPUT_HARDWARE); 212 } 213 214 public boolean isInputMuted() { 215 return getBoolean(ClientProperty.CLIENT_INPUT_MUTED); 216 } 217 218 public boolean isOutputHardware() { 219 return getBoolean(ClientProperty.CLIENT_OUTPUT_HARDWARE); 220 } 221 222 public boolean isOutputMuted() { 223 return getBoolean(ClientProperty.CLIENT_OUTPUT_MUTED); 224 } 225 226 public boolean isPrioritySpeaker() { 227 return getBoolean(ClientProperty.CLIENT_IS_PRIORITY_SPEAKER); 228 } 229 230 public boolean isRecording() { 231 return getBoolean(ClientProperty.CLIENT_IS_RECORDING); 232 } 233 234 public boolean isRegularClient() { 235 return getType() == 0; 236 } 237 238 public boolean isServerQueryClient() { 239 return getType() == 1; 240 } 241 242 public boolean isTalking() { 243 return getBoolean(ClientProperty.CLIENT_FLAG_TALKING); 244 } 245}