001package com.github.theholywaffle.teamspeak3.commands;
002
003/*
004 * #%L
005 * TeamSpeak 3 Java API
006 * %%
007 * Copyright (C) 2017 Bert De Geyter, Roger Baumgartner
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.ServerGroupType;
030import com.github.theholywaffle.teamspeak3.commands.parameter.ArrayParameter;
031import com.github.theholywaffle.teamspeak3.commands.parameter.KeyValueParam;
032import com.github.theholywaffle.teamspeak3.commands.parameter.OptionParam;
033
034public final class PermissionCommands {
035
036        private PermissionCommands() {
037                throw new Error("No instances");
038        }
039
040        /*
041         * General commands
042         */
043
044        public static Command permFind(String permName) {
045                nonEmptyPermissionName(permName);
046
047                return new CommandBuilder("permfind", 1).add(new KeyValueParam("permsid", permName)).build();
048        }
049
050        public static Command permGet(String... permNames) {
051                nonEmptyPermissionArray(permNames);
052
053                CommandBuilder builder = new CommandBuilder("permget", 1);
054
055                ArrayParameter permissions = new ArrayParameter(permNames.length);
056                for (String permName : permNames) {
057                        nonEmptyPermissionName(permName);
058                        permissions.add(new KeyValueParam("permsid", permName));
059                }
060                builder.add(permissions);
061
062                return builder.build();
063        }
064
065        public static Command permIdGetByName(String permName) {
066                nonEmptyPermissionName(permName);
067
068                return new CommandBuilder("permidgetbyname", 1).add(new KeyValueParam("permsid", permName)).build();
069        }
070
071        public static Command permIdGetByName(String... permNames) {
072                nonEmptyPermissionArray(permNames);
073
074                CommandBuilder builder = new CommandBuilder("permidgetbyname", 1);
075
076                ArrayParameter permissions = new ArrayParameter(permNames.length);
077                for (String permName : permNames) {
078                        nonEmptyPermissionName(permName);
079                        permissions.add(new KeyValueParam("permsid", permName));
080                }
081                builder.add(permissions);
082
083                return builder.build();
084        }
085
086        public static Command permissionList() {
087                return new CommandBuilder("permissionlist").build();
088        }
089
090        public static Command permOverview(int channelId, int clientDBId) {
091                CommandBuilder builder = new CommandBuilder("permoverview", 3);
092                builder.add(new KeyValueParam("cid", channelId));
093                builder.add(new KeyValueParam("cldbid", clientDBId));
094                builder.add(new KeyValueParam("permid", 0));
095                return builder.build();
096        }
097
098        public static Command permReset() {
099                return new CommandBuilder("permreset").build();
100        }
101
102        /*
103         * Client commands
104         */
105
106        public static Command clientAddPerm(int clientDBId, String permName, int permValue, boolean skip) {
107                nonEmptyPermissionName(permName);
108
109                CommandBuilder builder = new CommandBuilder("clientaddperm", 4);
110                builder.add(new KeyValueParam("cldbid", clientDBId));
111                builder.add(new KeyValueParam("permsid", permName));
112                builder.add(new KeyValueParam("permvalue", permValue));
113                builder.add(new KeyValueParam("permskip", skip));
114                return builder.build();
115        }
116
117        public static Command clientDelPerm(int clientDBId, String permName) {
118                nonEmptyPermissionName(permName);
119
120                CommandBuilder builder = new CommandBuilder("clientdelperm", 2);
121                builder.add(new KeyValueParam("cldbid", clientDBId));
122                builder.add(new KeyValueParam("permsid", permName));
123                return builder.build();
124        }
125
126        public static Command clientPermList(int clientDBId) {
127                CommandBuilder builder = new CommandBuilder("clientpermlist", 2);
128                builder.add(new KeyValueParam("cldbid", clientDBId));
129                builder.add(new OptionParam("permsid"));
130                return builder.build();
131        }
132
133        /*
134         * Channel commands
135         */
136
137        public static Command channelAddPerm(int channelId, String permName, int permValue) {
138                nonEmptyPermissionName(permName);
139
140                CommandBuilder builder = new CommandBuilder("channeladdperm", 3);
141                builder.add(new KeyValueParam("cid", channelId));
142                builder.add(new KeyValueParam("permsid", permName));
143                builder.add(new KeyValueParam("permvalue", permValue));
144                return builder.build();
145        }
146
147        public static Command channelDelPerm(int channelId, String permName) {
148                nonEmptyPermissionName(permName);
149
150                CommandBuilder builder = new CommandBuilder("channeldelperm", 2);
151                builder.add(new KeyValueParam("cid", channelId));
152                builder.add(new KeyValueParam("permsid", permName));
153                return builder.build();
154        }
155
156        public static Command channelPermList(int channelId) {
157                CommandBuilder builder = new CommandBuilder("channelpermlist", 2);
158                builder.add(new KeyValueParam("cid", channelId));
159                builder.add(new OptionParam("permsid"));
160                return builder.build();
161        }
162
163        /*
164         * Channel client commands
165         */
166
167        public static Command channelClientAddPerm(int channelId, int clientDBId, String permName, int permValue) {
168                nonEmptyPermissionName(permName);
169
170                CommandBuilder builder = new CommandBuilder("channelclientaddperm", 4);
171                builder.add(new KeyValueParam("cid", channelId));
172                builder.add(new KeyValueParam("cldbid", clientDBId));
173                builder.add(new KeyValueParam("permsid", permName));
174                builder.add(new KeyValueParam("permvalue", permValue));
175                return builder.build();
176        }
177
178        public static Command channelClientDelPerm(int channelId, int clientDBId, String permName) {
179                nonEmptyPermissionName(permName);
180
181                CommandBuilder builder = new CommandBuilder("channelclientdelperm", 3);
182                builder.add(new KeyValueParam("cid", channelId));
183                builder.add(new KeyValueParam("cldbid", clientDBId));
184                builder.add(new KeyValueParam("permsid", permName));
185                return builder.build();
186        }
187
188        public static Command channelClientPermList(int channelId, int clientDBId) {
189                CommandBuilder builder = new CommandBuilder("channelclientpermlist", 3);
190                builder.add(new KeyValueParam("cid", channelId));
191                builder.add(new KeyValueParam("cldbid", clientDBId));
192                builder.add(new OptionParam("permsid"));
193                return builder.build();
194        }
195
196        /*
197         * Channel group commands
198         */
199
200        public static Command channelGroupAddPerm(int groupId, String permName, int permValue) {
201                nonEmptyPermissionName(permName);
202
203                CommandBuilder builder = new CommandBuilder("channelgroupaddperm", 3);
204                builder.add(new KeyValueParam("cgid", groupId));
205                builder.add(new KeyValueParam("permsid", permName));
206                builder.add(new KeyValueParam("permvalue", permValue));
207                return builder.build();
208        }
209
210        public static Command channelGroupDelPerm(int groupId, String permName) {
211                nonEmptyPermissionName(permName);
212
213                CommandBuilder builder = new CommandBuilder("channelgroupdelperm", 2);
214                builder.add(new KeyValueParam("cgid", groupId));
215                builder.add(new KeyValueParam("permsid", permName));
216                return builder.build();
217        }
218
219        public static Command channelGroupPermList(int groupId) {
220                CommandBuilder builder = new CommandBuilder("channelgrouppermlist", 2);
221                builder.add(new KeyValueParam("cgid", groupId));
222                builder.add(new OptionParam("permsid"));
223                return builder.build();
224        }
225
226        /*
227         * Server group commands
228         */
229
230        public static Command serverGroupAddPerm(int groupId, String permName, int permValue,
231                                                 boolean negate, boolean skip) {
232                nonEmptyPermissionName(permName);
233
234                CommandBuilder builder = new CommandBuilder("servergroupaddperm", 5);
235                builder.add(new KeyValueParam("sgid", groupId));
236                builder.add(new KeyValueParam("permsid", permName));
237                builder.add(new KeyValueParam("permvalue", permValue));
238                builder.add(new KeyValueParam("permnegated", negate));
239                builder.add(new KeyValueParam("permskip", skip));
240                return builder.build();
241        }
242
243        public static Command serverGroupAutoAddPerm(ServerGroupType type, String permName, int permValue,
244                                                     boolean negate, boolean skip) {
245                nonNullServerGroupType(type);
246                nonEmptyPermissionName(permName);
247
248                CommandBuilder builder = new CommandBuilder("servergroupautoaddperm", 5);
249                builder.add(new KeyValueParam("sgtype", type.getIndex()));
250                builder.add(new KeyValueParam("permsid", permName));
251                builder.add(new KeyValueParam("permvalue", permValue));
252                builder.add(new KeyValueParam("permnegated", negate));
253                builder.add(new KeyValueParam("permskip", skip));
254                return builder.build();
255        }
256
257        public static Command serverGroupDelPerm(int groupId, String permName) {
258                nonEmptyPermissionName(permName);
259
260                CommandBuilder builder = new CommandBuilder("servergroupdelperm", 2);
261                builder.add(new KeyValueParam("sgid", groupId));
262                builder.add(new KeyValueParam("permsid", permName));
263                return builder.build();
264        }
265
266        public static Command serverGroupAutoDelPerm(ServerGroupType type, String permName) {
267                nonNullServerGroupType(type);
268                nonEmptyPermissionName(permName);
269
270                CommandBuilder builder = new CommandBuilder("servergroupautodelperm", 2);
271                builder.add(new KeyValueParam("sgtype", type.getIndex()));
272                builder.add(new KeyValueParam("permsid", permName));
273                return builder.build();
274        }
275
276        public static Command serverGroupPermList(int groupId) {
277                CommandBuilder builder = new CommandBuilder("servergrouppermlist", 2);
278                builder.add(new KeyValueParam("sgid", groupId));
279                builder.add(new OptionParam("permsid"));
280                return builder.build();
281        }
282
283        /*
284         * Validation
285         */
286
287        private static void nonEmptyPermissionName(String permName) {
288                if (permName == null || permName.isEmpty()) {
289                        throw new IllegalArgumentException("Permission name must be a non-empty string");
290                }
291        }
292
293        private static void nonEmptyPermissionArray(String[] permNames) {
294                if (permNames == null || permNames.length == 0) {
295                        throw new IllegalArgumentException("Permission name array cannot be null or empty");
296                }
297        }
298
299        private static void nonNullServerGroupType(ServerGroupType type) {
300                if (type == null) {
301                        throw new IllegalArgumentException("Server group type cannot be null");
302                }
303        }
304}