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.

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