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