Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

DCCCommand.java 9.1KB

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