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.PermissionGroupType;
030
031import java.util.Map;
032
033/**
034 * Describes a single permission that is assigned to a varying target.
035 * <p>
036 * This class is used when a lot of permissions are sent at once.
037 * To reduce bandwidth usage, the TS3 server only transmit the numeric
038 * permission ID and not the permission name.
039 * </p><p>
040 * For a complete description of the TS3 permission system, refer to
041 * <a href="http://forum.teamspeak.com/threads/49581-The-New-Permission-Documentataions">
042 * this post</a> on the TeamSpeak forums.
043 * </p>
044 */
045public class PermissionAssignment extends Wrapper {
046
047        public PermissionAssignment(Map<String, String> map) {
048                super(map);
049        }
050
051        /**
052         * Where this permission assignment originates from.
053         *
054         * @return the type of this permission assignment
055         */
056        public PermissionGroupType getType() {
057                final int type = getInt("t");
058                for (final PermissionGroupType p : PermissionGroupType.values()) {
059                        if (p.getIndex() == type) {
060                                return p;
061                        }
062                }
063                return null;
064        }
065
066        /**
067         * Specifies where this permission assignment originates from,
068         * depending on the {@linkplain #getType() type} of this assignment.
069         * <p>
070         * {@code x -> y} := <i>In case {@code type} is {@code x}, {@code majorId} means {@code y}</i>
071         * </p><p>
072         * {@linkplain PermissionGroupType#SERVER_GROUP SERVER_GROUP} {@code ->} {@linkplain ServerGroup#getId() Server group ID}<br>
073         * {@linkplain PermissionGroupType#GLOBAL_CLIENT GLOBAL_CLIENT} {@code ->} {@linkplain Client#getDatabaseId() Client database ID}<br>
074         * {@linkplain PermissionGroupType#CHANNEL CHANNEL} {@code ->} {@linkplain Channel#getId() Channel ID}<br>
075         * {@linkplain PermissionGroupType#CHANNEL_GROUP CHANNEL_GROUP} {@code ->} {@linkplain Channel#getId() Channel ID}<br>
076         * {@linkplain PermissionGroupType#CHANNEL_CLIENT CHANNEL_CLIENT} {@code ->} {@linkplain Channel#getId() Channel ID}
077         * </p>
078         *
079         * @return the major ID of the source of this assignment as described above
080         */
081        public int getMajorId() {
082                return getInt("id1");
083        }
084
085        /**
086         * Specifies where this permission assignment originates from,
087         * depending on the {@linkplain #getType() type} of this assignment.
088         * <p>
089         * {@code x -> y} := <i>In case {@code type} is {@code x}, {@code minorId} means {@code y}</i>
090         * </p><p>
091         * {@linkplain PermissionGroupType#CHANNEL_GROUP CHANNEL_GROUP} {@code ->} {@linkplain ChannelGroup#getId() Channel group ID}<br>
092         * {@linkplain PermissionGroupType#CHANNEL_CLIENT CHANNEL_CLIENT} {@code ->} {@linkplain Client#getDatabaseId() Client database ID}
093         * </p><p>
094         * Otherwise {@code getMinorId()} is undefined should return {@code 0}.
095         * </p>
096         *
097         * @return the minor ID of the source of this assignment as described above
098         */
099        public int getMinorId() {
100                return getInt("id2");
101        }
102
103        /**
104         * Gets the numerical ID of this permission.
105         *
106         * @return this permission's numerical ID
107         */
108        public int getId() {
109                return getInt("p");
110        }
111
112        /**
113         * Gets the value of this permission assignment.
114         * <p>
115         * Please note that this value doesn't necessarily have to be
116         * the effective permission value for a client, as this assignment
117         * can be overridden by another assignment.
118         * </p><p>
119         * Integer permissions usually have values between 0 and 100,
120         * but any integer value is theoretically valid.
121         * </p><p>
122         * Boolean permissions have a value of {@code 0} to represent
123         * {@code false} and {@code 1} to represent {@code true}.
124         * </p>
125         *
126         * @return the value of this permission assignment
127         */
128        public int getValue() {
129                return getInt("v");
130        }
131
132        /**
133         * Returns {@code true} if this permission is negated.
134         * <p>
135         * Negated means that instead of the highest value, the lowest
136         * value will be selected for this permission instead.
137         * </p>
138         *
139         * @return whether this permission is negated or not
140         */
141        public boolean isNegated() {
142                return getBoolean("n");
143        }
144
145        /**
146         * Returns {@code true} if this permission is skipped.
147         * <p>
148         * Skipped only exists for server group and client permissions,
149         * therefore this value will always be false for channel group permissions.
150         * </p><p>
151         * If a client permission is skipped, it won't be overridden by channel
152         * group permissions.<br>
153         * If a server group permission is skipped, it won't be overridden by
154         * channel group or client permissions.
155         * </p>
156         *
157         * @return whether this permission is negated or not
158         */
159        public boolean isSkipped() {
160                return getBoolean("s");
161        }
162}