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

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