You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DCCCommand.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.dcc;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.addons.dcc.events.DccChatRequestSentEvent;
  25. import com.dmdirc.addons.dcc.events.DccSendRequestEvent;
  26. import com.dmdirc.addons.dcc.io.DCC;
  27. import com.dmdirc.addons.dcc.io.DCCChat;
  28. import com.dmdirc.addons.dcc.io.DCCTransfer;
  29. import com.dmdirc.addons.dcc.kde.KFileChooser;
  30. import com.dmdirc.addons.ui_swing.UIUtilities;
  31. import com.dmdirc.addons.ui_swing.injection.MainWindow;
  32. import com.dmdirc.commandparser.BaseCommandInfo;
  33. import com.dmdirc.commandparser.CommandArguments;
  34. import com.dmdirc.commandparser.CommandInfo;
  35. import com.dmdirc.commandparser.CommandType;
  36. import com.dmdirc.commandparser.commands.Command;
  37. import com.dmdirc.commandparser.commands.IntelligentCommand;
  38. import com.dmdirc.commandparser.commands.context.CommandContext;
  39. import com.dmdirc.commandparser.commands.context.ServerCommandContext;
  40. import com.dmdirc.interfaces.CommandController;
  41. import com.dmdirc.interfaces.Connection;
  42. import com.dmdirc.interfaces.User;
  43. import com.dmdirc.interfaces.WindowModel;
  44. import com.dmdirc.parser.interfaces.Parser;
  45. import com.dmdirc.ui.WindowManager;
  46. import com.dmdirc.ui.input.AdditionalTabTargets;
  47. import com.dmdirc.ui.input.TabCompleterFactory;
  48. import com.dmdirc.ui.input.TabCompletionType;
  49. import com.dmdirc.ui.messages.BackBufferFactory;
  50. import java.awt.Window;
  51. import java.io.File;
  52. import javax.annotation.Nonnull;
  53. import javax.inject.Inject;
  54. import javax.swing.JFileChooser;
  55. import javax.swing.JOptionPane;
  56. /**
  57. * This command allows starting dcc chats/file transfers.
  58. */
  59. public class DCCCommand extends Command implements IntelligentCommand {
  60. /** A command info object for this command. */
  61. public static final CommandInfo INFO = new BaseCommandInfo("dcc",
  62. "dcc <SEND|CHAT> <target> [params] - starts a DCC",
  63. CommandType.TYPE_SERVER);
  64. /** My Plugin. */
  65. private final DCCManager myPlugin;
  66. /** Main window used as the parent for dialogs. */
  67. private final Window mainWindow;
  68. /** Window management. */
  69. private final WindowManager windowManager;
  70. /** The factory to use for tab completers. */
  71. private final TabCompleterFactory tabCompleterFactory;
  72. /** The bus to dispatch events on. */
  73. private final DMDircMBassador eventBus;
  74. private final BackBufferFactory backBufferFactory;
  75. /**
  76. * Creates a new instance of DCCCommand.
  77. */
  78. @Inject
  79. public DCCCommand(
  80. final CommandController controller,
  81. @MainWindow final Window mainWindow,
  82. final DCCManager plugin,
  83. final WindowManager windowManager,
  84. final TabCompleterFactory tabCompleterFactory,
  85. final DMDircMBassador eventBus,
  86. final BackBufferFactory backBufferFactory) {
  87. super(controller);
  88. this.mainWindow = mainWindow;
  89. myPlugin = plugin;
  90. this.windowManager = windowManager;
  91. this.tabCompleterFactory = tabCompleterFactory;
  92. this.eventBus = eventBus;
  93. this.backBufferFactory = backBufferFactory;
  94. }
  95. @Override
  96. public void execute(@Nonnull final WindowModel origin,
  97. final CommandArguments args, final CommandContext context) {
  98. if (args.getArguments().length > 1) {
  99. final String target = args.getArguments()[1];
  100. final Connection connection = ((ServerCommandContext) context).getConnection();
  101. final Parser parser = connection.getParser().get();
  102. final String myNickname = connection.getLocalUser().map(User::getNickname).orElse("Unknown");
  103. if (parser.isValidChannelName(target)
  104. || parser.getStringConverter().equalsIgnoreCase(target,
  105. myNickname)) {
  106. new Thread(() -> {
  107. if (parser.getStringConverter().equalsIgnoreCase(target,
  108. myNickname)) {
  109. JOptionPane.showMessageDialog(null,
  110. "You can't DCC yourself.", "DCC Error",
  111. JOptionPane.ERROR_MESSAGE);
  112. } else {
  113. JOptionPane.showMessageDialog(null,
  114. "You can't DCC a channel.", "DCC Error",
  115. JOptionPane.ERROR_MESSAGE);
  116. }
  117. }, "DCC-Error-Message").start();
  118. return;
  119. }
  120. final String type = args.getArguments()[0];
  121. if ("chat".equalsIgnoreCase(type)) {
  122. startChat(parser, connection, origin, myNickname, target, true);
  123. } else if ("send".equalsIgnoreCase(type)) {
  124. sendFile(target, origin, connection, true,
  125. args.getArgumentsAsString(2));
  126. } else {
  127. showError(origin, args.isSilent(), "Unknown DCC Type: '" + type + '\'');
  128. }
  129. } else {
  130. showUsage(origin, true, INFO.getName(), INFO.getHelp());
  131. }
  132. }
  133. /**
  134. * Starts a DCC Chat.
  135. *
  136. * @param parser Parser from which command originated
  137. * @param connection Server from which command originated
  138. * @param origin Frame container from which command originated
  139. * @param myNickname My current nickname
  140. * @param target Target of the command
  141. * @param isSilent Is this a silent command
  142. */
  143. private void startChat(final Parser parser, final Connection connection,
  144. final WindowModel origin, final String myNickname,
  145. final String target, final boolean isSilent) {
  146. final DCCChat chat = new DCCChat();
  147. if (myPlugin.listen(chat)) {
  148. final ChatContainer window = new ChatContainer(
  149. myPlugin.getContainer(),
  150. chat,
  151. origin.getConfigManager(),
  152. backBufferFactory,
  153. getController(),
  154. "*Chat: " + target,
  155. myNickname,
  156. target,
  157. tabCompleterFactory,
  158. eventBus);
  159. windowManager.addWindow(myPlugin.getContainer(), window);
  160. parser.sendCTCP(target, "DCC", "CHAT chat " + DCC.ipToLong(
  161. myPlugin.getListenIP(parser)) + ' ' + chat.getPort());
  162. eventBus.publish(new DccChatRequestSentEvent(connection, target));
  163. sendLine(origin, isSilent, "DCCChatStarting", target, chat.getHost(), chat.getPort());
  164. window.addLine("DCCChatStarting", target, chat.getHost(), chat.getPort());
  165. } else {
  166. sendLine(origin, isSilent, "DCCChatError",
  167. "Unable to start chat with " + target
  168. + " - unable to create listen socket");
  169. }
  170. }
  171. /**
  172. * Ask for the file to send, then start the send.
  173. *
  174. * @param target Person this dcc is to.
  175. * @param origin The InputWindow this command was issued on
  176. * @param connection The server instance that this command is being executed on
  177. * @param isSilent Whether this command is silenced or not
  178. * @param filename The file to send
  179. *
  180. * @since 0.6.3m1
  181. */
  182. public void sendFile(final String target, final WindowModel origin,
  183. final Connection connection, final boolean isSilent, final String filename) {
  184. // New thread to ask the user what file to send
  185. final File givenFile = new File(filename);
  186. final File selectedFile = UIUtilities.invokeAndWait(() -> {
  187. final JFileChooser jc = givenFile.exists()
  188. ? KFileChooser.getFileChooser(origin.getConfigManager(),
  189. myPlugin, givenFile)
  190. : KFileChooser.getFileChooser(origin.getConfigManager(),
  191. myPlugin);
  192. final int result = showFileChooser(givenFile, target, jc);
  193. if (result != JFileChooser.APPROVE_OPTION
  194. || !handleInvalidItems(jc)) {
  195. return null;
  196. }
  197. return jc.getSelectedFile();
  198. });
  199. if (selectedFile == null) {
  200. return;
  201. }
  202. new Thread(() -> {
  203. final DCCTransfer send = new DCCTransfer(origin
  204. .getConfigManager().getOptionInt(myPlugin.getDomain(),
  205. "send.blocksize"));
  206. send.setTurbo(origin.getConfigManager().getOptionBool(
  207. myPlugin.getDomain(), "send.forceturbo"));
  208. send.setType(DCCTransfer.TransferType.SEND);
  209. eventBus.publish(new DccSendRequestEvent(connection, target, selectedFile.
  210. getAbsolutePath()));
  211. showOutput(origin, isSilent, "Starting DCC Send with: " + target);
  212. send.setFileName(selectedFile.getAbsolutePath());
  213. send.setFileSize(selectedFile.length());
  214. if (origin.getConfigManager().getOptionBool(
  215. myPlugin.getDomain(), "send.reverse")) {
  216. final Parser parser = connection.getParser().get();
  217. final TransferContainer container = new TransferContainer(myPlugin, send,
  218. origin.getConfigManager(), backBufferFactory, "Send: " + target,
  219. target, connection, eventBus);
  220. windowManager.addWindow(myPlugin.getContainer(), container);
  221. parser.sendCTCP(target, "DCC", "SEND \""
  222. + selectedFile.getName() + "\" "
  223. + DCC.ipToLong(myPlugin.getListenIP(parser))
  224. + " 0 " + send.getFileSize() + " "
  225. + send.makeToken()
  226. + (send.isTurbo() ? " T" : ""));
  227. } else {
  228. final Parser parser = connection.getParser().get();
  229. if (myPlugin.listen(send)) {
  230. final TransferContainer container = new TransferContainer(myPlugin, send,
  231. origin.getConfigManager(), backBufferFactory, "*Send: "
  232. + target, target, connection, eventBus);
  233. windowManager.addWindow(myPlugin.getContainer(), container);
  234. parser.sendCTCP(target, "DCC", "SEND \""
  235. + selectedFile.getName() + "\" "
  236. + DCC.ipToLong(myPlugin.getListenIP(parser))
  237. + " " + send.getPort() + " " + send.getFileSize()
  238. + (send.isTurbo() ? " T" : ""));
  239. } else {
  240. sendLine(origin, isSilent, "DCCSendError",
  241. "Unable to start dcc send with " + target
  242. + " - unable to create listen socket");
  243. }
  244. }
  245. }, "openFileThread").start();
  246. }
  247. /**
  248. * Checks for invalid items.
  249. *
  250. * @param jc File chooser to check
  251. *
  252. * @return true iif the selection was valid
  253. */
  254. private boolean handleInvalidItems(final JFileChooser jc) {
  255. if (jc.getSelectedFile().length() == 0) {
  256. JOptionPane.showMessageDialog(null,
  257. "You can't send empty files over DCC.", "DCC Error",
  258. JOptionPane.ERROR_MESSAGE);
  259. return false;
  260. } else if (!jc.getSelectedFile().exists()) {
  261. JOptionPane.showMessageDialog(null, "Invalid file specified",
  262. "DCC Error", JOptionPane.ERROR_MESSAGE);
  263. return false;
  264. }
  265. return true;
  266. }
  267. /**
  268. * Sets up and display a file chooser.
  269. *
  270. * @param givenFile File to display
  271. * @param target DCC target
  272. * @param jc File chooser
  273. *
  274. * @return the return state of the file chooser on popdown:
  275. * <ul>
  276. * <li>JFileChooser.CANCEL_OPTION
  277. * <li>JFileChooser.APPROVE_OPTION
  278. * <li>JFileChooser.ERROR_OPTION if an error occurs or the dialog is dismissed
  279. * </ul>
  280. */
  281. private int showFileChooser(final File givenFile, final String target,
  282. final JFileChooser jc) {
  283. if (givenFile.exists() && givenFile.isFile()) {
  284. jc.setSelectedFile(givenFile);
  285. return JFileChooser.APPROVE_OPTION;
  286. } else {
  287. jc.setDialogTitle("Send file to " + target + " - DMDirc ");
  288. jc.setFileSelectionMode(JFileChooser.FILES_ONLY);
  289. jc.setMultiSelectionEnabled(false);
  290. return jc.showOpenDialog(mainWindow);
  291. }
  292. }
  293. @Override
  294. public AdditionalTabTargets getSuggestions(final int arg,
  295. final IntelligentCommandContext context) {
  296. final AdditionalTabTargets res = new AdditionalTabTargets();
  297. if (arg == 0) {
  298. res.add("SEND");
  299. res.add("CHAT");
  300. res.excludeAll();
  301. } else if (arg == 1) {
  302. res.exclude(TabCompletionType.COMMAND);
  303. res.exclude(TabCompletionType.CHANNEL);
  304. } else {
  305. res.excludeAll();
  306. }
  307. return res;
  308. }
  309. }