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

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