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 clientAddPerm(int clientDBId, String permName, boolean permValue, boolean skip) {
118                nonEmptyPermissionName(permName);
119
120                CommandBuilder builder = new CommandBuilder("clientaddperm", 4);
121                builder.add(new KeyValueParam("cldbid", clientDBId));
122                builder.add(new KeyValueParam("permsid", permName));
123                builder.add(new KeyValueParam("permvalue", permValue));
124                builder.add(new KeyValueParam("permskip", skip));
125                return builder.build();
126        }
127
128        public static Command clientDelPerm(int clientDBId, String permName) {
129                nonEmptyPermissionName(permName);
130
131                CommandBuilder builder = new CommandBuilder("clientdelperm", 2);
132                builder.add(new KeyValueParam("cldbid", clientDBId));
133                builder.add(new KeyValueParam("permsid", permName));
134                return builder.build();
135        }
136
137        public static Command clientPermList(int clientDBId) {
138                CommandBuilder builder = new CommandBuilder("clientpermlist", 2);
139                builder.add(new KeyValueParam("cldbid", clientDBId));
140                builder.add(new OptionParam("permsid"));
141                return builder.build();
142        }
143
144        /*
145         * Channel commands
146         */
147
148        public static Command channelAddPerm(int channelId, String permName, int permValue) {
149                nonEmptyPermissionName(permName);
150
151                CommandBuilder builder = new CommandBuilder("channeladdperm", 3);
152                builder.add(new KeyValueParam("cid", channelId));
153                builder.add(new KeyValueParam("permsid", permName));
154                builder.add(new KeyValueParam("permvalue", permValue));
155                return builder.build();
156        }
157
158        public static Command channelDelPerm(int channelId, String permName) {
159                nonEmptyPermissionName(permName);
160
161                CommandBuilder builder = new CommandBuilder("channeldelperm", 2);
162                builder.add(new KeyValueParam("cid", channelId));
163                builder.add(new KeyValueParam("permsid", permName));
164                return builder.build();
165        }
166
167        public static Command channelPermList(int channelId) {
168                CommandBuilder builder = new CommandBuilder("channelpermlist", 2);
169                builder.add(new KeyValueParam("cid", channelId));
170                builder.add(new OptionParam("permsid"));
171                return builder.build();
172        }
173
174        /*
175         * Channel client commands
176         */
177
178        public static Command channelClientAddPerm(int channelId, int clientDBId, String permName, int permValue) {
179                nonEmptyPermissionName(permName);
180
181                CommandBuilder builder = new CommandBuilder("channelclientaddperm", 4);
182                builder.add(new KeyValueParam("cid", channelId));
183                builder.add(new KeyValueParam("cldbid", clientDBId));
184                builder.add(new KeyValueParam("permsid", permName));
185                builder.add(new KeyValueParam("permvalue", permValue));
186                return builder.build();
187        }
188
189        public static Command channelClientDelPerm(int channelId, int clientDBId, String permName) {
190                nonEmptyPermissionName(permName);
191
192                CommandBuilder builder = new CommandBuilder("channelclientdelperm", 3);
193                builder.add(new KeyValueParam("cid", channelId));
194                builder.add(new KeyValueParam("cldbid", clientDBId));
195                builder.add(new KeyValueParam("permsid", permName));
196                return builder.build();
197        }
198
199        public static Command channelClientPermList(int channelId, int clientDBId) {
200                CommandBuilder builder = new CommandBuilder("channelclientpermlist", 3);
201                builder.add(new KeyValueParam("cid", channelId));
202                builder.add(new KeyValueParam("cldbid", clientDBId));
203                builder.add(new OptionParam("permsid"));
204                return builder.build();
205        }
206
207        /*
208         * Channel group commands
209         */
210
211        public static Command channelGroupAddPerm(int groupId, String permName, int permValue) {
212                nonEmptyPermissionName(permName);
213
214                CommandBuilder builder = new CommandBuilder("channelgroupaddperm", 3);
215                builder.add(new KeyValueParam("cgid", groupId));
216                builder.add(new KeyValueParam("permsid", permName));
217                builder.add(new KeyValueParam("permvalue", permValue));
218                return builder.build();
219        }
220
221        public static Command channelGroupDelPerm(int groupId, String permName) {
222                nonEmptyPermissionName(permName);
223
224                CommandBuilder builder = new CommandBuilder("channelgroupdelperm", 2);
225                builder.add(new KeyValueParam("cgid", groupId));
226                builder.add(new KeyValueParam("permsid", permName));
227                return builder.build();
228        }
229
230        public static Command channelGroupPermList(int groupId) {
231                CommandBuilder builder = new CommandBuilder("channelgrouppermlist", 2);
232                builder.add(new KeyValueParam("cgid", groupId));
233                builder.add(new OptionParam("permsid"));
234                return builder.build();
235        }
236
237        /*
238         * Server group commands
239         */
240
241        public static Command serverGroupAddPerm(int groupId, String permName, int permValue,
242                                                 boolean negate, boolean skip) {
243                nonEmptyPermissionName(permName);
244
245                CommandBuilder builder = new CommandBuilder("servergroupaddperm", 5);
246                builder.add(new KeyValueParam("sgid", groupId));
247                builder.add(new KeyValueParam("permsid", permName));
248                builder.add(new KeyValueParam("permvalue", permValue));
249                builder.add(new KeyValueParam("permnegated", negate));
250                builder.add(new KeyValueParam("permskip", skip));
251                return builder.build();
252        }
253
254        public static Command serverGroupAutoAddPerm(ServerGroupType type, String permName, int permValue,
255                                                     boolean negate, boolean skip) {
256                nonNullServerGroupType(type);
257                nonEmptyPermissionName(permName);
258
259                CommandBuilder builder = new CommandBuilder("servergroupautoaddperm", 5);
260                builder.add(new KeyValueParam("sgtype", type.getIndex()));
261                builder.add(new KeyValueParam("permsid", permName));
262                builder.add(new KeyValueParam("permvalue", permValue));
263                builder.add(new KeyValueParam("permnegated", negate));
264                builder.add(new KeyValueParam("permskip", skip));
265                return builder.build();
266        }
267
268        public static Command serverGroupDelPerm(int groupId, String permName) {
269                nonEmptyPermissionName(permName);
270
271                CommandBuilder builder = new CommandBuilder("servergroupdelperm", 2);
272                builder.add(new KeyValueParam("sgid", groupId));
273                builder.add(new KeyValueParam("permsid", permName));
274                return builder.build();
275        }
276
277        public static Command serverGroupAutoDelPerm(ServerGroupType type, String permName) {
278                nonNullServerGroupType(type);
279                nonEmptyPermissionName(permName);
280
281                CommandBuilder builder = new CommandBuilder("servergroupautodelperm", 2);
282                builder.add(new KeyValueParam("sgtype", type.getIndex()));
283                builder.add(new KeyValueParam("permsid", permName));
284                return builder.build();
285        }
286
287        public static Command serverGroupPermList(int groupId) {
288                CommandBuilder builder = new CommandBuilder("servergrouppermlist", 2);
289                builder.add(new KeyValueParam("sgid", groupId));
290                builder.add(new OptionParam("permsid"));
291                return builder.build();
292        }
293
294        /*
295         * Validation
296         */
297
298        private static void nonEmptyPermissionName(String permName) {
299                if (permName == null || permName.isEmpty()) {
300                        throw new IllegalArgumentException("Permission name must be a non-empty string");
301                }
302        }
303
304        private static void nonEmptyPermissionArray(String[] permNames) {
305                if (permNames == null || permNames.length == 0) {
306                        throw new IllegalArgumentException("Permission name array cannot be null or empty");
307                }
308        }
309
310        private static void nonNullServerGroupType(ServerGroupType type) {
311                if (type == null) {
312                        throw new IllegalArgumentException("Server group type cannot be null");
313                }
314        }
315}