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.commands.parameter.ArrayParameter;
030import com.github.theholywaffle.teamspeak3.commands.parameter.KeyValueParam;
031
032public final class FileCommands {
033
034        private FileCommands() {
035                throw new Error("No instances");
036        }
037
038        public static Command ftCreateDir(String path, int channelId, String channelPassword) {
039                if (path == null) throw new IllegalArgumentException("File path cannot be null");
040
041                CommandBuilder builder = new CommandBuilder("ftcreatedir", 3);
042                builder.add(new KeyValueParam("cid", channelId));
043                builder.add(new KeyValueParam("cpw", channelPassword));
044                builder.add(new KeyValueParam("dirname", path.startsWith("/") ? path : "/" + path));
045                return builder.build();
046        }
047
048        public static Command ftDeleteFile(int channelId, String channelPassword, String... filePaths) {
049                if (filePaths == null || filePaths.length == 0) {
050                        throw new IllegalArgumentException("File array cannot be null or empty");
051                }
052
053                CommandBuilder builder = new CommandBuilder("ftdeletefile", 3);
054                builder.add(new KeyValueParam("cid", channelId));
055                builder.add(new KeyValueParam("cpw", channelPassword));
056
057                ArrayParameter files = new ArrayParameter(filePaths.length);
058                for (String filePath : filePaths) {
059                        if (filePath == null) throw new IllegalArgumentException("File path cannot be null");
060                        files.add(new KeyValueParam("name", filePath.startsWith("/") ? filePath : "/" + filePath));
061                }
062                builder.add(files);
063
064                return builder.build();
065        }
066
067        public static Command ftGetFileInfo(int channelId, String channelPassword, String... filePaths) {
068                if (filePaths == null || filePaths.length == 0) {
069                        throw new IllegalArgumentException("File array cannot be null or empty");
070                }
071
072                CommandBuilder builder = new CommandBuilder("ftgetfileinfo", 1);
073                ArrayParameter files = new ArrayParameter(filePaths.length, 3);
074                for (String filePath : filePaths) {
075                        if (filePath == null) throw new IllegalArgumentException("File path cannot be null");
076                        files.add(new KeyValueParam("cid", channelId));
077                        files.add(new KeyValueParam("cpw", channelPassword));
078                        files.add(new KeyValueParam("name", filePath));
079                }
080                builder.add(files);
081
082                return builder.build();
083        }
084
085        public static Command ftGetFileInfo(int[] channelIds, String[] channelPasswords, String[] filePaths) {
086                if (channelIds == null || channelIds.length == 0) {
087                        throw new IllegalArgumentException("Channel ID array cannot be null or empty");
088                }
089                if (filePaths == null || filePaths.length == 0) {
090                        throw new IllegalArgumentException("File array cannot be null or empty");
091                }
092                if (channelIds.length != filePaths.length) {
093                        throw new IllegalArgumentException("Channel IDs length doesn't match file paths length");
094                }
095                if (channelPasswords != null && filePaths.length != channelPasswords.length) {
096                        throw new IllegalArgumentException("Passwords length doesn't match file paths length");
097                }
098
099                CommandBuilder builder = new CommandBuilder("ftgetfileinfo", 1);
100                ArrayParameter files = new ArrayParameter(filePaths.length, 3);
101                for (int i = 0; i < filePaths.length; ++i) {
102                        final String password = channelPasswords == null ? null : channelPasswords[i];
103                        final String path = filePaths[i];
104                        if (path == null) throw new IllegalArgumentException("File path cannot be null");
105
106                        files.add(new KeyValueParam("cid", channelIds[i]));
107                        files.add(new KeyValueParam("cpw", password));
108                        files.add(new KeyValueParam("name", path.startsWith("/") ? path : "/" + path));
109                }
110                builder.add(files);
111
112                return builder.build();
113        }
114
115        public static Command ftGetFileList(String directoryPath, int channelId, String channelPassword) {
116                if (directoryPath == null) throw new IllegalArgumentException("Directory path cannot be null");
117
118                CommandBuilder builder = new CommandBuilder("ftgetfilelist", 3);
119                builder.add(new KeyValueParam("cid", channelId));
120                builder.add(new KeyValueParam("cpw", channelPassword));
121
122                String path = directoryPath; // Make sure path starts and ends with /
123                if (!path.startsWith("/")) path = "/" + path;
124                if (!path.endsWith("/")) path += "/";
125                builder.add(new KeyValueParam("path", path));
126
127                return builder.build();
128        }
129
130        public static Command ftInitDownload(int transferId, String path, int channelId, String channelPassword) {
131                if (path == null) throw new IllegalArgumentException("File path cannot be null");
132
133                CommandBuilder builder = new CommandBuilder("ftinitdownload", 6);
134                builder.add(new KeyValueParam("clientftfid", transferId));
135                builder.add(new KeyValueParam("name", path.startsWith("/") ? path : "/" + path));
136                builder.add(new KeyValueParam("cid", channelId));
137                builder.add(new KeyValueParam("cpw", channelPassword));
138                builder.add(new KeyValueParam("seekpos", 0));
139                builder.add(new KeyValueParam("proto", 0)); // Use current (old) protocol for as long as possible
140                return builder.build();
141        }
142
143        public static Command ftInitUpload(int transferId, String path, int channelId, String channelPassword,
144                                           long size, boolean overwrite) {
145                if (path == null) throw new IllegalArgumentException("File path cannot be null");
146
147                CommandBuilder builder = new CommandBuilder("ftinitupload", 8);
148                builder.add(new KeyValueParam("clientftfid", transferId));
149                builder.add(new KeyValueParam("name", path.startsWith("/") ? path : "/" + path));
150                builder.add(new KeyValueParam("cid", channelId));
151                builder.add(new KeyValueParam("cpw", channelPassword));
152                builder.add(new KeyValueParam("size", size));
153                builder.add(new KeyValueParam("overwrite", overwrite));
154                builder.add(new KeyValueParam("resume", 0));
155                builder.add(new KeyValueParam("proto", 0)); // Use current (old) protocol for as long as possible
156                return builder.build();
157        }
158
159        public static Command ftList() {
160                return new CommandBuilder("ftlist").build();
161        }
162
163        public static Command ftRenameFile(String oldPath, String newPath,
164                                           int channelId, String channelPassword) {
165                if (oldPath == null) throw new IllegalArgumentException("Old file path cannot be null");
166                if (newPath == null) throw new IllegalArgumentException("New file path cannot be null");
167
168                CommandBuilder builder = new CommandBuilder("ftrenamefile", 4);
169                builder.add(new KeyValueParam("cid", channelId));
170                builder.add(new KeyValueParam("cpw", channelPassword));
171                builder.add(new KeyValueParam("oldname", oldPath.startsWith("/") ? oldPath : "/" + oldPath));
172                builder.add(new KeyValueParam("newname", newPath.startsWith("/") ? newPath : "/" + newPath));
173                return builder.build();
174        }
175
176        public static Command ftRenameFile(String oldPath, String newPath,
177                                           int oldChannelId, String oldChannelPassword,
178                                           int newChannelId, String newChannelPassword) {
179                if (oldPath == null) throw new IllegalArgumentException("Old file path cannot be null");
180                if (newPath == null) throw new IllegalArgumentException("New file path cannot be null");
181
182                CommandBuilder builder = new CommandBuilder("ftrenamefile", 6);
183                builder.add(new KeyValueParam("cid", oldChannelId));
184                builder.add(new KeyValueParam("cpw", oldChannelPassword));
185                builder.add(new KeyValueParam("tcid", newChannelId));
186                builder.add(new KeyValueParam("tcpw", newChannelPassword));
187                builder.add(new KeyValueParam("oldname", oldPath.startsWith("/") ? oldPath : "/" + oldPath));
188                builder.add(new KeyValueParam("newname", newPath.startsWith("/") ? newPath : "/" + newPath));
189                return builder.build();
190        }
191}