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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.Main;
  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.kde.KFileChooser;
  28. import com.dmdirc.commandparser.CommandArguments;
  29. import com.dmdirc.commandparser.CommandManager;
  30. import com.dmdirc.commandparser.commands.IntelligentCommand;
  31. import com.dmdirc.commandparser.commands.ServerCommand;
  32. import com.dmdirc.config.IdentityManager;
  33. import com.dmdirc.parser.interfaces.Parser;
  34. import com.dmdirc.ui.input.AdditionalTabTargets;
  35. import com.dmdirc.ui.input.TabCompletionType;
  36. import com.dmdirc.ui.interfaces.InputWindow;
  37. import java.io.File;
  38. import java.util.List;
  39. import javax.swing.JFileChooser;
  40. import javax.swing.JFrame;
  41. import javax.swing.JOptionPane;
  42. /**
  43. * This command allows starting dcc chats/file transfers
  44. *
  45. * @author Shane "Dataforce" Mc Cormack
  46. */
  47. public final class DCCCommand extends ServerCommand implements IntelligentCommand {
  48. /** My Plugin */
  49. private final DCCPlugin myPlugin;
  50. /**
  51. * Creates a new instance of DCCCommand.
  52. *
  53. * @param plugin The DCC Plugin that this command belongs to
  54. */
  55. public DCCCommand(final DCCPlugin plugin) {
  56. super();
  57. myPlugin = plugin;
  58. CommandManager.registerCommand(this);
  59. }
  60. /** {@inheritDoc} */
  61. @Override
  62. public void execute(final InputWindow origin, final Server server,
  63. final boolean isSilent, final CommandArguments args) {
  64. if (args.getArguments().length > 1) {
  65. final String type = args.getArguments()[0];
  66. final String target = args.getArguments()[1];
  67. final Parser parser = server.getParser();
  68. final String myNickname = parser.getLocalClient().getNickname();
  69. if (parser.isValidChannelName(target) || parser.getStringConverter().equalsIgnoreCase(target, myNickname)) {
  70. final Thread errorThread = new Thread(new Runnable() {
  71. /** {@inheritDoc} */
  72. @Override
  73. public void run() {
  74. if (parser.getStringConverter().equalsIgnoreCase(target, myNickname)) {
  75. JOptionPane.showMessageDialog(null, "You can't DCC yourself.", "DCC Error", JOptionPane.ERROR_MESSAGE);
  76. } else {
  77. JOptionPane.showMessageDialog(null, "You can't DCC a channel.", "DCC Error", JOptionPane.ERROR_MESSAGE);
  78. }
  79. }
  80. });
  81. errorThread.start();
  82. return;
  83. }
  84. if (type.equalsIgnoreCase("chat")) {
  85. final DCCChat chat = new DCCChat();
  86. if (myPlugin.listen(chat)) {
  87. final DCCChatWindow window = new DCCChatWindow(myPlugin, chat, "*Chat: " + target, myNickname, target);
  88. parser.sendCTCP(target, "DCC", "CHAT chat " + DCC.ipToLong(myPlugin.getListenIP(parser)) + " " + chat.getPort());
  89. ActionManager.processEvent(DCCActions.DCC_CHAT_REQUEST_SENT, null, server, target);
  90. sendLine(origin, isSilent, "DCCChatStarting", target, chat.getHost(), chat.getPort());
  91. window.getFrame().addLine("DCCChatStarting", target, chat.getHost(), chat.getPort());
  92. } else {
  93. sendLine(origin, isSilent, "DCCChatError", "Unable to start chat with " + target + " - unable to create listen socket");
  94. }
  95. } else if (type.equalsIgnoreCase("send")) {
  96. sendFile(target, origin, server, isSilent, args.getArgumentsAsString(2));
  97. } else {
  98. sendLine(origin, isSilent, FORMAT_ERROR, "Unknown DCC Type: '" + type + "'");
  99. }
  100. } else {
  101. sendLine(origin, isSilent, FORMAT_ERROR, "Syntax: dcc <type> <target> [params]");
  102. }
  103. }
  104. /**
  105. * Ask for the file to send, then start the send.
  106. *
  107. * @param target Person this dcc is to.
  108. * @param origin The InputWindow this command was issued on
  109. * @param server The server instance that this command is being executed on
  110. * @param isSilent Whether this command is silenced or not
  111. * @param filename The file to send
  112. * @since 0.6.3m1
  113. */
  114. public void sendFile(final String target, final InputWindow origin, final Server server, final boolean isSilent, final String filename) {
  115. // New thread to ask the user what file to send
  116. final File givenFile = new File(filename);
  117. final Thread dccThread = new Thread(new Runnable() {
  118. /** {@inheritDoc} */
  119. @Override
  120. public void run() {
  121. final JFileChooser jc = (givenFile.exists()) ? KFileChooser.getFileChooser(myPlugin, givenFile) : KFileChooser.getFileChooser(myPlugin);
  122. int result;
  123. if (!givenFile.exists() || !givenFile.isFile()) {
  124. jc.setDialogTitle("Send file to " + target + " - DMDirc ");
  125. jc.setFileSelectionMode(JFileChooser.FILES_ONLY);
  126. jc.setMultiSelectionEnabled(false);
  127. result = jc.showOpenDialog((JFrame) Main.getUI().getMainWindow());
  128. } else {
  129. jc.setSelectedFile(givenFile);
  130. result = JFileChooser.APPROVE_OPTION;
  131. }
  132. if (result == JFileChooser.APPROVE_OPTION) {
  133. if (jc.getSelectedFile().length() == 0) {
  134. JOptionPane.showMessageDialog(null, "You can't send empty files over DCC.", "DCC Error", JOptionPane.ERROR_MESSAGE);
  135. return;
  136. } else if (!jc.getSelectedFile().exists()) {
  137. JOptionPane.showMessageDialog(null, "Invalid file specified", "DCC Error", JOptionPane.ERROR_MESSAGE);
  138. return;
  139. }
  140. final Parser parser = server.getParser();
  141. DCCSend send = new DCCSend(IdentityManager.getGlobalConfig().getOptionInt(myPlugin.getDomain(), "send.blocksize"));
  142. send.setTurbo(IdentityManager.getGlobalConfig().getOptionBool(myPlugin.getDomain(), "send.forceturbo"));
  143. send.setType(DCCSend.TransferType.SEND);
  144. ActionManager.processEvent(DCCActions.DCC_SEND_REQUEST_SENT, null, server, target, jc.getSelectedFile());
  145. sendLine(origin, isSilent, FORMAT_OUTPUT, "Starting DCC Send with: " + target);
  146. send.setFileName(jc.getSelectedFile().getAbsolutePath());
  147. send.setFileSize(jc.getSelectedFile().length());
  148. if (IdentityManager.getGlobalConfig().getOptionBool(myPlugin.getDomain(), "send.reverse")) {
  149. new DCCSendWindow(myPlugin, send, "Send: " + target, target, server);
  150. parser.sendCTCP(target, "DCC", "SEND \"" + jc.getSelectedFile().getName() + "\" " + DCC.ipToLong(myPlugin.getListenIP(parser)) + " 0 " + send.getFileSize() + " " + send.makeToken() + ((send.isTurbo()) ? " T" : ""));
  151. } else {
  152. if (myPlugin.listen(send)) {
  153. new DCCSendWindow(myPlugin, send, "*Send: " + target, target, server);
  154. parser.sendCTCP(target, "DCC", "SEND \"" + jc.getSelectedFile().getName() + "\" " + DCC.ipToLong(myPlugin.getListenIP(parser)) + " " + send.getPort() + " " + send.getFileSize() + ((send.isTurbo()) ? " T" : ""));
  155. } else {
  156. sendLine(origin, isSilent, "DCCSendError", "Unable to start dcc send with " + target + " - unable to create listen socket");
  157. }
  158. }
  159. }
  160. }
  161. }, "openFileThread");
  162. // Start the thread
  163. dccThread.start();
  164. }
  165. /**
  166. * Returns this command's name.
  167. *
  168. * @return The name of this command
  169. */
  170. @Override
  171. public String getName() {
  172. return "dcc";
  173. }
  174. /**
  175. * Returns whether or not this command should be shown in help messages.
  176. *
  177. * @return True iff the command should be shown, false otherwise
  178. */
  179. @Override
  180. public boolean showInHelp() {
  181. return true;
  182. }
  183. /**
  184. * Returns a string representing the help message for this command.
  185. *
  186. * @return the help message for this command
  187. */
  188. @Override
  189. public String getHelp() {
  190. return "dcc - Allows DCC";
  191. }
  192. /**
  193. * Returns a list of suggestions for the specified argument, given the list
  194. * of previous arguments.
  195. * @param arg The argument that is being completed
  196. * @param previousArgs The contents of the previous arguments, if any
  197. * @return A list of suggestions for the argument
  198. */
  199. @Override
  200. public AdditionalTabTargets getSuggestions(final int arg, final List<String> previousArgs) {
  201. final AdditionalTabTargets res = new AdditionalTabTargets();
  202. if (arg == 0) {
  203. res.add("SEND");
  204. res.add("CHAT");
  205. res.excludeAll();
  206. } else if (arg == 1) {
  207. res.exclude(TabCompletionType.COMMAND);
  208. res.exclude(TabCompletionType.CHANNEL);
  209. } else {
  210. res.excludeAll();
  211. }
  212. return res;
  213. }
  214. }