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 15KB

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