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

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