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