|
@@ -0,0 +1,812 @@
|
|
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
|
+
|
|
23
|
+package com.dmdirc.addons.dcc;
|
|
24
|
+
|
|
25
|
+import com.dmdirc.Server;
|
|
26
|
+import com.dmdirc.actions.ActionManager;
|
|
27
|
+import com.dmdirc.actions.CoreActionType;
|
|
28
|
+import com.dmdirc.addons.dcc.actions.DCCActions;
|
|
29
|
+import com.dmdirc.addons.dcc.io.DCC;
|
|
30
|
+import com.dmdirc.addons.dcc.io.DCCChat;
|
|
31
|
+import com.dmdirc.addons.dcc.io.DCCTransfer;
|
|
32
|
+import com.dmdirc.addons.dcc.kde.KFileChooser;
|
|
33
|
+import com.dmdirc.addons.ui_swing.SwingController;
|
|
34
|
+import com.dmdirc.addons.ui_swing.SwingWindowFactory;
|
|
35
|
+import com.dmdirc.addons.ui_swing.components.frames.ComponentFrame;
|
|
36
|
+import com.dmdirc.config.prefs.PluginPreferencesCategory;
|
|
37
|
+import com.dmdirc.config.prefs.PreferencesCategory;
|
|
38
|
+import com.dmdirc.config.prefs.PreferencesDialogModel;
|
|
39
|
+import com.dmdirc.config.prefs.PreferencesSetting;
|
|
40
|
+import com.dmdirc.config.prefs.PreferencesType;
|
|
41
|
+import com.dmdirc.interfaces.ActionListener;
|
|
42
|
+import com.dmdirc.interfaces.CommandController;
|
|
43
|
+import com.dmdirc.interfaces.actions.ActionType;
|
|
44
|
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
|
|
45
|
+import com.dmdirc.interfaces.config.ConfigProvider;
|
|
46
|
+import com.dmdirc.interfaces.config.IdentityController;
|
|
47
|
+import com.dmdirc.logger.ErrorLevel;
|
|
48
|
+import com.dmdirc.logger.Logger;
|
|
49
|
+import com.dmdirc.messages.MessageSinkManager;
|
|
50
|
+import com.dmdirc.parser.interfaces.ClientInfo;
|
|
51
|
+import com.dmdirc.parser.interfaces.Parser;
|
|
52
|
+import com.dmdirc.plugins.PluginInfo;
|
|
53
|
+import com.dmdirc.ui.WindowManager;
|
|
54
|
+
|
|
55
|
+import java.io.File;
|
|
56
|
+import java.io.IOException;
|
|
57
|
+import java.net.InetAddress;
|
|
58
|
+import java.net.UnknownHostException;
|
|
59
|
+import java.util.Arrays;
|
|
60
|
+import java.util.HashSet;
|
|
61
|
+
|
|
62
|
+import javax.inject.Inject;
|
|
63
|
+import javax.inject.Singleton;
|
|
64
|
+import javax.swing.JFileChooser;
|
|
65
|
+import javax.swing.JOptionPane;
|
|
66
|
+
|
|
67
|
+import lombok.Getter;
|
|
68
|
+
|
|
69
|
+/**
|
|
70
|
+ * This plugin adds DCC to DMDirc.
|
|
71
|
+ */
|
|
72
|
+@Singleton
|
|
73
|
+public class DCCManager implements ActionListener {
|
|
74
|
+
|
|
75
|
+ /** Our DCC Container window. */
|
|
76
|
+ private PlaceholderContainer container;
|
|
77
|
+ /** This plugin's plugin info. */
|
|
78
|
+ private final PluginInfo pluginInfo;
|
|
79
|
+ /** Config manager to read settings from. */
|
|
80
|
+ private final AggregateConfigProvider config;
|
|
81
|
+ /** Parent swing controller. */
|
|
82
|
+ private final SwingController controller;
|
|
83
|
+ /** Identity controller to read settings from. */
|
|
84
|
+ private final IdentityController identityController;
|
|
85
|
+ /** The sink manager to use to despatch messages. */
|
|
86
|
+ private final MessageSinkManager messageSinkManager;
|
|
87
|
+ /** Window Management. */
|
|
88
|
+ private final WindowManager windowManager;
|
|
89
|
+ /** The command controller to use. */
|
|
90
|
+ private final CommandController commandController;
|
|
91
|
+ /** The configuration domain to use. */
|
|
92
|
+ @Getter
|
|
93
|
+ private final String domain;
|
|
94
|
+
|
|
95
|
+ /**
|
|
96
|
+ * Creates a new instance of this plugin.
|
|
97
|
+ *
|
|
98
|
+ * @param controller The controller to register UI implementations with
|
|
99
|
+ * @param pluginInfo This plugin's plugin info
|
|
100
|
+ * @param identityController The Identity controller that provides the current config
|
|
101
|
+ * @param commandController Command controller to register commands
|
|
102
|
+ * @param messageSinkManager The sink manager to use to despatch messages.
|
|
103
|
+ * @param windowManager Window Management
|
|
104
|
+ */
|
|
105
|
+ @Inject
|
|
106
|
+ public DCCManager(
|
|
107
|
+ final SwingController controller,
|
|
108
|
+ final PluginInfo pluginInfo,
|
|
109
|
+ final IdentityController identityController,
|
|
110
|
+ final CommandController commandController,
|
|
111
|
+ final MessageSinkManager messageSinkManager,
|
|
112
|
+ final WindowManager windowManager) {
|
|
113
|
+ this.identityController = identityController;
|
|
114
|
+ this.controller = controller;
|
|
115
|
+ this.messageSinkManager = messageSinkManager;
|
|
116
|
+ this.windowManager = windowManager;
|
|
117
|
+ this.commandController = commandController;
|
|
118
|
+ this.domain = pluginInfo.getDomain();
|
|
119
|
+ config = controller.getGlobalConfig();
|
|
120
|
+ this.pluginInfo = pluginInfo;
|
|
121
|
+ final SwingWindowFactory factory = controller.getWindowFactory();
|
|
122
|
+ factory.registerImplementation(new HashSet<>(Arrays.asList(
|
|
123
|
+ "com.dmdirc.addons.dcc.ui.PlaceholderPanel")),
|
|
124
|
+ ComponentFrame.class);
|
|
125
|
+ factory.registerImplementation(new HashSet<>(Arrays.asList(
|
|
126
|
+ "com.dmdirc.addons.dcc.ui.TransferPanel")),
|
|
127
|
+ ComponentFrame.class);
|
|
128
|
+
|
|
129
|
+ final ConfigProvider defaults = identityController.getAddonSettings();
|
|
130
|
+ defaults.setOption(getDomain(), "receive.savelocation",
|
|
131
|
+ identityController.getConfigurationDirectory() + "downloads"
|
|
132
|
+ + System.getProperty("file.separator"));
|
|
133
|
+ }
|
|
134
|
+
|
|
135
|
+ /**
|
|
136
|
+ * Ask a question, if the answer is the answer required, then recall
|
|
137
|
+ * handleProcessEvent.
|
|
138
|
+ *
|
|
139
|
+ * @param question Question to ask
|
|
140
|
+ * @param title Title of question dialog
|
|
141
|
+ * @param desiredAnswer Answer required
|
|
142
|
+ * @param type Actiontype to pass back
|
|
143
|
+ * @param format StringBuffer to pass back
|
|
144
|
+ * @param arguments arguments to pass back
|
|
145
|
+ */
|
|
146
|
+ public void askQuestion(final String question, final String title,
|
|
147
|
+ final int desiredAnswer, final ActionType type,
|
|
148
|
+ final StringBuffer format, final Object... arguments) {
|
|
149
|
+ // New thread to ask the question in to stop us locking the UI
|
|
150
|
+ new Thread(new Runnable() {
|
|
151
|
+
|
|
152
|
+ /** {@inheritDoc} */
|
|
153
|
+ @Override
|
|
154
|
+ public void run() {
|
|
155
|
+ final int result = JOptionPane.showConfirmDialog(null, question,
|
|
156
|
+ title, JOptionPane.YES_NO_OPTION);
|
|
157
|
+ if (result == desiredAnswer) {
|
|
158
|
+ handleProcessEvent(type, format, true, arguments);
|
|
159
|
+ }
|
|
160
|
+ }
|
|
161
|
+
|
|
162
|
+ }, "QuestionThread: " + title).start();
|
|
163
|
+ }
|
|
164
|
+
|
|
165
|
+ /**
|
|
166
|
+ * Ask the location to save a file, then start the download.
|
|
167
|
+ *
|
|
168
|
+ * @param nickname Person this dcc is from.
|
|
169
|
+ * @param send The DCCSend to save for.
|
|
170
|
+ * @param parser The parser this send was received on
|
|
171
|
+ * @param reverse Is this a reverse dcc?
|
|
172
|
+ * @param token Token used in reverse dcc.
|
|
173
|
+ */
|
|
174
|
+ public void saveFile(final String nickname, final DCCTransfer send,
|
|
175
|
+ final Parser parser, final boolean reverse, final String token) {
|
|
176
|
+ // New thread to ask the user where to save in to stop us locking the UI
|
|
177
|
+ new Thread(new Runnable() {
|
|
178
|
+
|
|
179
|
+ /** {@inheritDoc} */
|
|
180
|
+ @Override
|
|
181
|
+ public void run() {
|
|
182
|
+ final JFileChooser jc = KFileChooser.getFileChooser(config,
|
|
183
|
+ DCCManager.this,
|
|
184
|
+ config.getOption(getDomain(), "receive.savelocation"));
|
|
185
|
+ int result;
|
|
186
|
+ if (config.getOptionBool(getDomain(), "receive.autoaccept")) {
|
|
187
|
+ result = JFileChooser.APPROVE_OPTION;
|
|
188
|
+ } else {
|
|
189
|
+ result = showFileChooser(send, jc);
|
|
190
|
+ }
|
|
191
|
+ if (result != JFileChooser.APPROVE_OPTION) {
|
|
192
|
+ return;
|
|
193
|
+ }
|
|
194
|
+ send.setFileName(jc.getSelectedFile().getPath());
|
|
195
|
+ if (!handleExists(send, jc, nickname, parser,reverse, token)) {
|
|
196
|
+ return;
|
|
197
|
+ }
|
|
198
|
+ final boolean resume = handleResume(jc);
|
|
199
|
+ if (reverse && !token.isEmpty()) {
|
|
200
|
+ TransferContainer container = new TransferContainer(DCCManager.this, send,
|
|
201
|
+ config, "*Receive: " + nickname, nickname, null);
|
|
202
|
+ windowManager.addWindow(getContainer(), container);
|
|
203
|
+ send.setToken(token);
|
|
204
|
+ if (resume) {
|
|
205
|
+ if (config.getOptionBool(getDomain(),
|
|
206
|
+ "receive.reverse.sendtoken")) {
|
|
207
|
+ parser.sendCTCP(nickname, "DCC", "RESUME "
|
|
208
|
+ + send.getShortFileName() + " 0 "
|
|
209
|
+ + jc.getSelectedFile().length() + " "
|
|
210
|
+ + token);
|
|
211
|
+ } else {
|
|
212
|
+ parser.sendCTCP(nickname, "DCC", "RESUME "
|
|
213
|
+ + send.getShortFileName() + " 0 "
|
|
214
|
+ + jc.getSelectedFile().length());
|
|
215
|
+ }
|
|
216
|
+ } else {
|
|
217
|
+ if (listen(send)) {
|
|
218
|
+ parser.sendCTCP(nickname, "DCC", "SEND "
|
|
219
|
+ + send.getShortFileName() + " "
|
|
220
|
+ + DCC.ipToLong(getListenIP(parser))
|
|
221
|
+ + " " + send.getPort() + " "
|
|
222
|
+ + send.getFileSize() + " " + token);
|
|
223
|
+ }
|
|
224
|
+ }
|
|
225
|
+ } else {
|
|
226
|
+ TransferContainer container = new TransferContainer(DCCManager.this, send,
|
|
227
|
+ config, "Receive: " + nickname, nickname, null);
|
|
228
|
+ windowManager.addWindow(getContainer(), container);
|
|
229
|
+ if (resume) {
|
|
230
|
+ parser.sendCTCP(nickname, "DCC", "RESUME "
|
|
231
|
+ + send.getShortFileName() + " "
|
|
232
|
+ + send.getPort() + " "
|
|
233
|
+ + jc.getSelectedFile().length());
|
|
234
|
+ } else {
|
|
235
|
+ send.connect();
|
|
236
|
+ }
|
|
237
|
+ }
|
|
238
|
+ }
|
|
239
|
+
|
|
240
|
+ }, "saveFileThread: " + send.getShortFileName()).start();
|
|
241
|
+ }
|
|
242
|
+
|
|
243
|
+ /**
|
|
244
|
+ * Checks if the selected file exists and prompts the user as required.
|
|
245
|
+ *
|
|
246
|
+ * @param send DCC Transfer
|
|
247
|
+ * @param jc File chooser
|
|
248
|
+ * @param nickname Remote nickname
|
|
249
|
+ * @param parser Parser
|
|
250
|
+ * @param reverse Reverse DCC?
|
|
251
|
+ * @param token DCC token
|
|
252
|
+ *
|
|
253
|
+ * @return true if the user wants to continue, false if they wish to abort
|
|
254
|
+ */
|
|
255
|
+ private boolean handleExists(final DCCTransfer send, final JFileChooser jc,
|
|
256
|
+ final String nickname, final Parser parser, final boolean reverse,
|
|
257
|
+ final String token) {
|
|
258
|
+ if (jc.getSelectedFile().exists() && send.getFileSize() > -1
|
|
259
|
+ && send.getFileSize() <= jc.getSelectedFile().length()) {
|
|
260
|
+ if (config.getOptionBool(getDomain(), "receive.autoaccept")) {
|
|
261
|
+ return false;
|
|
262
|
+ } else {
|
|
263
|
+ JOptionPane.showMessageDialog(controller.getMainFrame(),
|
|
264
|
+ "This file has already "
|
|
265
|
+ + "been completed, or is longer than the file you are "
|
|
266
|
+ + "receiving.\nPlease choose a different file.",
|
|
267
|
+ "Problem with selected file",
|
|
268
|
+ JOptionPane.ERROR_MESSAGE);
|
|
269
|
+ saveFile(nickname, send, parser, reverse, token);
|
|
270
|
+ return false;
|
|
271
|
+ }
|
|
272
|
+ }
|
|
273
|
+ return true;
|
|
274
|
+ }
|
|
275
|
+
|
|
276
|
+ /**
|
|
277
|
+ * Prompts the user to resume a transfer if required.
|
|
278
|
+ *
|
|
279
|
+ * @param jc File chooser
|
|
280
|
+ *
|
|
281
|
+ * @return true if the user wants to continue the transfer false otherwise
|
|
282
|
+ */
|
|
283
|
+ private boolean handleResume(final JFileChooser jc) {
|
|
284
|
+ if (jc.getSelectedFile().exists()) {
|
|
285
|
+ if (config.getOptionBool(getDomain(), "receive.autoaccept")) {
|
|
286
|
+ return true;
|
|
287
|
+ } else {
|
|
288
|
+ final int result = JOptionPane.showConfirmDialog(
|
|
289
|
+ controller.getMainFrame(), "This file exists already"
|
|
290
|
+ + ", do you want to resume an exisiting download?",
|
|
291
|
+ "Resume Download?", JOptionPane.YES_NO_OPTION);
|
|
292
|
+ return (result == JOptionPane.YES_OPTION);
|
|
293
|
+ }
|
|
294
|
+ }
|
|
295
|
+ return false;
|
|
296
|
+ }
|
|
297
|
+
|
|
298
|
+ /**
|
|
299
|
+ * Sets up and display a file chooser.
|
|
300
|
+ *
|
|
301
|
+ * @param send DCCTransfer object sending the file
|
|
302
|
+ * @param jc File chooser
|
|
303
|
+ *
|
|
304
|
+ * @return the return state of the file chooser on popdown:
|
|
305
|
+ * <ul>
|
|
306
|
+ * <li>JFileChooser.CANCEL_OPTION
|
|
307
|
+ * <li>JFileChooser.APPROVE_OPTION
|
|
308
|
+ * <li>JFileChooser.ERROR_OPTION if an error occurs or the
|
|
309
|
+ * dialog is dismissed
|
|
310
|
+ * </ul>
|
|
311
|
+ */
|
|
312
|
+ private int showFileChooser(final DCCTransfer send, final JFileChooser jc) {
|
|
313
|
+ jc.setDialogTitle("Save " + send.getShortFileName() + " As - DMDirc");
|
|
314
|
+ jc.setFileSelectionMode(JFileChooser.FILES_ONLY);
|
|
315
|
+ jc.setMultiSelectionEnabled(false);
|
|
316
|
+ jc.setSelectedFile(new File(send.getFileName()));
|
|
317
|
+ return jc.showSaveDialog(controller.getMainFrame());
|
|
318
|
+ }
|
|
319
|
+
|
|
320
|
+ /** {@inheritDoc} */
|
|
321
|
+ @Override
|
|
322
|
+ public void processEvent(final ActionType type, final StringBuffer format,
|
|
323
|
+ final Object... arguments) {
|
|
324
|
+ handleProcessEvent(type, format, false, arguments);
|
|
325
|
+ }
|
|
326
|
+
|
|
327
|
+ /**
|
|
328
|
+ * Make the given DCC start listening.
|
|
329
|
+ * This will either call dcc.listen() or dcc.listen(startPort, endPort)
|
|
330
|
+ * depending on config.
|
|
331
|
+ *
|
|
332
|
+ * @param dcc DCC to start listening.
|
|
333
|
+ * @return True if Socket was opened.
|
|
334
|
+ */
|
|
335
|
+ protected boolean listen(final DCC dcc) {
|
|
336
|
+ final boolean usePortRange = config.getOptionBool(getDomain(),
|
|
337
|
+ "firewall.ports.usePortRange");
|
|
338
|
+ try {
|
|
339
|
+ if (usePortRange) {
|
|
340
|
+ final int startPort = config.getOptionInt(getDomain(),
|
|
341
|
+ "firewall.ports.startPort");
|
|
342
|
+ final int endPort = config.getOptionInt(getDomain(),
|
|
343
|
+ "firewall.ports.endPort");
|
|
344
|
+ dcc.listen(startPort, endPort);
|
|
345
|
+ } else {
|
|
346
|
+ dcc.listen();
|
|
347
|
+ }
|
|
348
|
+ return true;
|
|
349
|
+ } catch (IOException ioe) {
|
|
350
|
+ return false;
|
|
351
|
+ }
|
|
352
|
+ }
|
|
353
|
+
|
|
354
|
+ /**
|
|
355
|
+ * Process an event of the specified type.
|
|
356
|
+ *
|
|
357
|
+ * @param type The type of the event to process
|
|
358
|
+ * @param format Format of messages that are about to be sent. (May be null)
|
|
359
|
+ * @param dontAsk Don't ask any questions, assume yes.
|
|
360
|
+ * @param arguments The arguments for the event
|
|
361
|
+ */
|
|
362
|
+ public void handleProcessEvent(final ActionType type,
|
|
363
|
+ final StringBuffer format, final boolean dontAsk,
|
|
364
|
+ final Object... arguments) {
|
|
365
|
+ if (config.getOptionBool(getDomain(), "receive.autoaccept") && !dontAsk) {
|
|
366
|
+ handleProcessEvent(type, format, true, arguments);
|
|
367
|
+ return;
|
|
368
|
+ }
|
|
369
|
+
|
|
370
|
+ if (type == CoreActionType.SERVER_CTCP) {
|
|
371
|
+ final String[] ctcpData = ((String) arguments[3]).split(" ");
|
|
372
|
+ if ("DCC".equalsIgnoreCase((String) arguments[2])) {
|
|
373
|
+ if ("chat".equalsIgnoreCase(ctcpData[0])
|
|
374
|
+ && ctcpData.length > 3) {
|
|
375
|
+ handleChat(type, format, dontAsk, ctcpData, arguments);
|
|
376
|
+ } else if ("send".equalsIgnoreCase(ctcpData[0])
|
|
377
|
+ && ctcpData.length > 3) {
|
|
378
|
+ handleSend(type, format, dontAsk, ctcpData, arguments);
|
|
379
|
+ } else if (("resume".equalsIgnoreCase(ctcpData[0])
|
|
380
|
+ || "accept".equalsIgnoreCase(ctcpData[0]))
|
|
381
|
+ && ctcpData.length > 2) {
|
|
382
|
+ handleReceive(ctcpData, arguments);
|
|
383
|
+ }
|
|
384
|
+ }
|
|
385
|
+ }
|
|
386
|
+ }
|
|
387
|
+
|
|
388
|
+ /**
|
|
389
|
+ * Handles a DCC chat request.
|
|
390
|
+ *
|
|
391
|
+ * @param type The type of the event to process
|
|
392
|
+ * @param format Format of messages that are about to be sent. (May be null)
|
|
393
|
+ * @param dontAsk Don't ask any questions, assume yes.
|
|
394
|
+ * @param ctcpData CTCP data bits
|
|
395
|
+ * @param arguments The arguments for the event
|
|
396
|
+ */
|
|
397
|
+ private void handleChat(final ActionType type, final StringBuffer format,
|
|
398
|
+ final boolean dontAsk, final String[] ctcpData,
|
|
399
|
+ final Object... arguments) {
|
|
400
|
+ final String nickname = ((ClientInfo) arguments[1]).getNickname();
|
|
401
|
+ if (dontAsk) {
|
|
402
|
+ final DCCChat chat = new DCCChat();
|
|
403
|
+ try {
|
|
404
|
+ chat.setAddress(Long.parseLong(ctcpData[2]),
|
|
405
|
+ Integer.parseInt(ctcpData[3]));
|
|
406
|
+ } catch (NumberFormatException nfe) {
|
|
407
|
+ return;
|
|
408
|
+ }
|
|
409
|
+ final String myNickname = ((Server) arguments[0]).getParser()
|
|
410
|
+ .getLocalClient().getNickname();
|
|
411
|
+ final DCCFrameContainer f = new ChatContainer(chat, config, commandController,
|
|
412
|
+ "Chat: " + nickname, myNickname, nickname, messageSinkManager);
|
|
413
|
+ windowManager.addWindow(getContainer(), f);
|
|
414
|
+ f.addLine("DCCChatStarting", nickname, chat.getHost(),
|
|
415
|
+ chat.getPort());
|
|
416
|
+ chat.connect();
|
|
417
|
+ } else {
|
|
418
|
+ ActionManager.getActionManager().triggerEvent(
|
|
419
|
+ DCCActions.DCC_CHAT_REQUEST, null, arguments[0],
|
|
420
|
+ nickname);
|
|
421
|
+ askQuestion("User " + nickname + " on "
|
|
422
|
+ + ((Server) arguments[0]).getName()
|
|
423
|
+ + " would like to start a DCC Chat with you.\n\n"
|
|
424
|
+ + "Do you want to continue?",
|
|
425
|
+ "DCC Chat Request", JOptionPane.YES_OPTION,
|
|
426
|
+ type, format, arguments);
|
|
427
|
+ }
|
|
428
|
+ }
|
|
429
|
+
|
|
430
|
+ /**
|
|
431
|
+ * Handles a DCC send request.
|
|
432
|
+ *
|
|
433
|
+ * @param type The type of the event to process
|
|
434
|
+ * @param format Format of messages that are about to be sent. (May be null)
|
|
435
|
+ * @param dontAsk Don't ask any questions, assume yes.
|
|
436
|
+ * @param ctcpData CTCP data bits
|
|
437
|
+ * @param arguments The arguments for the event
|
|
438
|
+ */
|
|
439
|
+ private void handleSend(final ActionType type, final StringBuffer format,
|
|
440
|
+ final boolean dontAsk, final String[] ctcpData,
|
|
441
|
+ final Object... arguments) {
|
|
442
|
+ final String nickname = ((ClientInfo) arguments[1]).getNickname();
|
|
443
|
+ final String filename;
|
|
444
|
+ String tmpFilename;
|
|
445
|
+ // Clients tend to put files with spaces in the name in ""
|
|
446
|
+ final StringBuilder filenameBits = new StringBuilder();
|
|
447
|
+ int i;
|
|
448
|
+ final boolean quoted = ctcpData[1].startsWith("\"");
|
|
449
|
+ if (quoted) {
|
|
450
|
+ for (i = 1; i < ctcpData.length; i++) {
|
|
451
|
+ String bit = ctcpData[i];
|
|
452
|
+ if (i == 1) {
|
|
453
|
+ bit = bit.substring(1);
|
|
454
|
+ }
|
|
455
|
+ if (bit.endsWith("\"")) {
|
|
456
|
+ filenameBits.append(" ")
|
|
457
|
+ .append(bit.substring(0, bit.length() - 1));
|
|
458
|
+ break;
|
|
459
|
+ } else {
|
|
460
|
+ filenameBits.append(" ").append(bit);
|
|
461
|
+ }
|
|
462
|
+ }
|
|
463
|
+ tmpFilename = filenameBits.toString().trim();
|
|
464
|
+ } else {
|
|
465
|
+ tmpFilename = ctcpData[1];
|
|
466
|
+ i = 1;
|
|
467
|
+ }
|
|
468
|
+
|
|
469
|
+ // Try to remove path names if sent.
|
|
470
|
+ // Change file separatorChar from other OSs first
|
|
471
|
+ if (File.separatorChar == '/') {
|
|
472
|
+ tmpFilename = tmpFilename.replace('\\', File.separatorChar);
|
|
473
|
+ } else {
|
|
474
|
+ tmpFilename = tmpFilename.replace('/', File.separatorChar);
|
|
475
|
+ }
|
|
476
|
+ // Then get just the name of the file.
|
|
477
|
+ filename = new File(tmpFilename).getName();
|
|
478
|
+
|
|
479
|
+ final String ip = ctcpData[++i];
|
|
480
|
+ final String port = ctcpData[++i];
|
|
481
|
+ long size;
|
|
482
|
+ if (ctcpData.length + 1 > i) {
|
|
483
|
+ try {
|
|
484
|
+ size = Integer.parseInt(ctcpData[++i]);
|
|
485
|
+ } catch (NumberFormatException nfe) {
|
|
486
|
+ size = -1;
|
|
487
|
+ }
|
|
488
|
+ } else {
|
|
489
|
+ size = -1;
|
|
490
|
+ }
|
|
491
|
+ final String token = (ctcpData.length - 1 > i
|
|
492
|
+ && !ctcpData[i + 1].equals("T")) ? ctcpData[++i] : "";
|
|
493
|
+
|
|
494
|
+ // Ignore incorrect ports, or non-numeric IP/Port
|
|
495
|
+ try {
|
|
496
|
+ final int portInt = Integer.parseInt(port);
|
|
497
|
+ if (portInt > 65535 || portInt < 0) {
|
|
498
|
+ return;
|
|
499
|
+ }
|
|
500
|
+ Long.parseLong(ip);
|
|
501
|
+ } catch (NumberFormatException nfe) {
|
|
502
|
+ return;
|
|
503
|
+ }
|
|
504
|
+
|
|
505
|
+ DCCTransfer send = DCCTransfer.findByToken(token);
|
|
506
|
+
|
|
507
|
+ if (send == null && !dontAsk) {
|
|
508
|
+ if (!token.isEmpty() && !port.equals("0")) {
|
|
509
|
+ // This is a reverse DCC Send that we no longer care about.
|
|
510
|
+ } else {
|
|
511
|
+ ActionManager.getActionManager().triggerEvent(
|
|
512
|
+ DCCActions.DCC_SEND_REQUEST, null,
|
|
513
|
+ arguments[0], nickname, filename);
|
|
514
|
+ askQuestion("User " + nickname + " on "
|
|
515
|
+ + ((Server) arguments[0]).getName()
|
|
516
|
+ + " would like to send you a file over DCC.\n\nFile: "
|
|
517
|
+ + filename + "\n\nDo you want to continue?",
|
|
518
|
+ "DCC Send Request", JOptionPane.YES_OPTION, type,
|
|
519
|
+ format, arguments);
|
|
520
|
+ }
|
|
521
|
+ } else {
|
|
522
|
+ final boolean newSend = send == null;
|
|
523
|
+ if (newSend) {
|
|
524
|
+ send = new DCCTransfer(config.getOptionInt(getDomain(),
|
|
525
|
+ "send.blocksize"));
|
|
526
|
+ send.setTurbo(config.getOptionBool(getDomain(), "send.forceturbo"));
|
|
527
|
+ }
|
|
528
|
+ try {
|
|
529
|
+ send.setAddress(Long.parseLong(ip), Integer.parseInt(port));
|
|
530
|
+ } catch (NumberFormatException nfe) {
|
|
531
|
+ return;
|
|
532
|
+ }
|
|
533
|
+ if (newSend) {
|
|
534
|
+ send.setFileName(filename);
|
|
535
|
+ send.setFileSize(size);
|
|
536
|
+ saveFile(nickname, send, ((Server) arguments[0]).getParser(),
|
|
537
|
+ "0".equals(port), token);
|
|
538
|
+ } else {
|
|
539
|
+ send.connect();
|
|
540
|
+ }
|
|
541
|
+ }
|
|
542
|
+ }
|
|
543
|
+
|
|
544
|
+ /**
|
|
545
|
+ * Handles a DCC chat request.
|
|
546
|
+ *
|
|
547
|
+ * @param ctcpData CTCP data bits
|
|
548
|
+ * @param arguments The arguments for the event
|
|
549
|
+ */
|
|
550
|
+ private void handleReceive(final String[] ctcpData,
|
|
551
|
+ final Object... arguments) {
|
|
552
|
+ final String filename;
|
|
553
|
+ // Clients tend to put files with spaces in the name in ""
|
|
554
|
+ final StringBuilder filenameBits = new StringBuilder();
|
|
555
|
+ int i;
|
|
556
|
+ final boolean quoted = ctcpData[1].startsWith("\"");
|
|
557
|
+ if (quoted) {
|
|
558
|
+ for (i = 1; i < ctcpData.length; i++) {
|
|
559
|
+ String bit = ctcpData[i];
|
|
560
|
+ if (i == 1) {
|
|
561
|
+ bit = bit.substring(1);
|
|
562
|
+ }
|
|
563
|
+ if (bit.endsWith("\"")) {
|
|
564
|
+ filenameBits.append(" ")
|
|
565
|
+ .append(bit.substring(0, bit.length() - 1));
|
|
566
|
+ break;
|
|
567
|
+ } else {
|
|
568
|
+ filenameBits.append(" ").append(bit);
|
|
569
|
+ }
|
|
570
|
+ }
|
|
571
|
+ filename = filenameBits.toString().trim();
|
|
572
|
+ } else {
|
|
573
|
+ filename = ctcpData[1];
|
|
574
|
+ i = 1;
|
|
575
|
+ }
|
|
576
|
+
|
|
577
|
+ final int port;
|
|
578
|
+ final int position;
|
|
579
|
+ try {
|
|
580
|
+ port = Integer.parseInt(ctcpData[++i]);
|
|
581
|
+ position = Integer.parseInt(ctcpData[++i]);
|
|
582
|
+ } catch (NumberFormatException nfe) {
|
|
583
|
+ return;
|
|
584
|
+ }
|
|
585
|
+ final String token = (ctcpData.length - 1 > i) ? " "
|
|
586
|
+ + ctcpData[++i] : "";
|
|
587
|
+
|
|
588
|
+ // Now look for a dcc that matches.
|
|
589
|
+ for (DCCTransfer send : DCCTransfer.getTransfers()) {
|
|
590
|
+ if (send.getPort() == port && (new File(send.getFileName()))
|
|
591
|
+ .getName().equalsIgnoreCase(filename)) {
|
|
592
|
+ if ((!token.isEmpty() && !send.getToken().isEmpty())
|
|
593
|
+ && (!token.equals(send.getToken()))) {
|
|
594
|
+ continue;
|
|
595
|
+ }
|
|
596
|
+ final Parser parser = ((Server) arguments[0]).getParser();
|
|
597
|
+ final String nick = ((ClientInfo) arguments[1]).getNickname();
|
|
598
|
+ if (ctcpData[0].equalsIgnoreCase("resume")) {
|
|
599
|
+ parser.sendCTCP(nick, "DCC", "ACCEPT "+ ((quoted) ? "\""
|
|
600
|
+ + filename + "\"" : filename) + " " + port + " "
|
|
601
|
+ + send.setFileStart(position) + token);
|
|
602
|
+ } else {
|
|
603
|
+ send.setFileStart(position);
|
|
604
|
+ if (port == 0) {
|
|
605
|
+ // Reverse dcc
|
|
606
|
+ if (listen(send)) {
|
|
607
|
+ if (send.getToken().isEmpty()) {
|
|
608
|
+ parser.sendCTCP(nick, "DCC", "SEND "
|
|
609
|
+ + ((quoted) ? "\"" + filename
|
|
610
|
+ + "\"" : filename) + " "
|
|
611
|
+ + DCC.ipToLong(send.getHost())
|
|
612
|
+ + " " + send.getPort()
|
|
613
|
+ + " " + send.getFileSize());
|
|
614
|
+ } else {
|
|
615
|
+ parser.sendCTCP(nick, "DCC", "SEND "
|
|
616
|
+ + ((quoted) ? "\"" + filename
|
|
617
|
+ + "\"" : filename)
|
|
618
|
+ + " " + DCC.ipToLong(send.getHost())
|
|
619
|
+ + " " + send.getPort()
|
|
620
|
+ + " " + send.getFileSize() + " "
|
|
621
|
+ + send.getToken());
|
|
622
|
+ }
|
|
623
|
+ }
|
|
624
|
+ } else {
|
|
625
|
+ send.connect();
|
|
626
|
+ }
|
|
627
|
+ }
|
|
628
|
+ }
|
|
629
|
+ }
|
|
630
|
+ }
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
|
|
634
|
+ /**
|
|
635
|
+ * Retrieves the container for the placeholder.
|
|
636
|
+ *
|
|
637
|
+ * @since 0.6.4
|
|
638
|
+ * @return This plugin's placeholder container
|
|
639
|
+ */
|
|
640
|
+ public synchronized PlaceholderContainer getContainer() {
|
|
641
|
+ if (container == null) {
|
|
642
|
+ createContainer();
|
|
643
|
+ }
|
|
644
|
+
|
|
645
|
+ return container;
|
|
646
|
+ }
|
|
647
|
+
|
|
648
|
+ /**
|
|
649
|
+ * Removes the cached container.
|
|
650
|
+ *
|
|
651
|
+ * @since 0.6.4
|
|
652
|
+ */
|
|
653
|
+ public synchronized void removeContainer() {
|
|
654
|
+ container = null;
|
|
655
|
+ }
|
|
656
|
+
|
|
657
|
+ /**
|
|
658
|
+ * Create the container window.
|
|
659
|
+ */
|
|
660
|
+ protected void createContainer() {
|
|
661
|
+ container = new PlaceholderContainer(this, config, controller);
|
|
662
|
+ windowManager.addWindow(container);
|
|
663
|
+ }
|
|
664
|
+
|
|
665
|
+ /**
|
|
666
|
+ * Called when the plugin is loaded.
|
|
667
|
+ */
|
|
668
|
+ public void onLoad() {
|
|
669
|
+ final File dir = new File(config.getOption(getDomain(),
|
|
670
|
+ "receive.savelocation"));
|
|
671
|
+ if (dir.exists()) {
|
|
672
|
+ if (!dir.isDirectory()) {
|
|
673
|
+ Logger.userError(ErrorLevel.LOW,
|
|
674
|
+ "Unable to create download dir (file exists instead)");
|
|
675
|
+ }
|
|
676
|
+ } else {
|
|
677
|
+ try {
|
|
678
|
+ dir.mkdirs();
|
|
679
|
+ dir.createNewFile();
|
|
680
|
+ } catch (IOException ex) {
|
|
681
|
+ Logger.userError(ErrorLevel.LOW,
|
|
682
|
+ "Unable to create download dir");
|
|
683
|
+ }
|
|
684
|
+ }
|
|
685
|
+
|
|
686
|
+ ActionManager.getActionManager().registerTypes(DCCActions.values());
|
|
687
|
+ ActionManager.getActionManager().registerListener(this,
|
|
688
|
+ CoreActionType.SERVER_CTCP);
|
|
689
|
+ }
|
|
690
|
+
|
|
691
|
+ /**
|
|
692
|
+ * Called when this plugin is Unloaded.
|
|
693
|
+ */
|
|
694
|
+ public synchronized void onUnload() {
|
|
695
|
+ ActionManager.getActionManager().unregisterListener(this);
|
|
696
|
+ if (container != null) {
|
|
697
|
+ container.close();
|
|
698
|
+ }
|
|
699
|
+ }
|
|
700
|
+
|
|
701
|
+ /**
|
|
702
|
+ * Get the IP Address we should send as our listening IP.
|
|
703
|
+ *
|
|
704
|
+ * @return The IP Address we should send as our listening IP.
|
|
705
|
+ */
|
|
706
|
+ public String getListenIP() {
|
|
707
|
+ return getListenIP(null);
|
|
708
|
+ }
|
|
709
|
+
|
|
710
|
+ /**
|
|
711
|
+ * Get the IP Address we should send as our listening IP.
|
|
712
|
+ *
|
|
713
|
+ * @param parser Parser the IRC Parser where this dcc is initiated
|
|
714
|
+ * @return The IP Address we should send as our listening IP.
|
|
715
|
+ */
|
|
716
|
+ public String getListenIP(final Parser parser) {
|
|
717
|
+ final String configIP = config.getOption(getDomain(), "firewall.ip");
|
|
718
|
+ if (!configIP.isEmpty()) {
|
|
719
|
+ try {
|
|
720
|
+ return InetAddress.getByName(configIP).getHostAddress();
|
|
721
|
+ } catch (UnknownHostException ex) { //NOPMD - handled below
|
|
722
|
+ //Continue below
|
|
723
|
+ }
|
|
724
|
+ }
|
|
725
|
+ if (parser != null) {
|
|
726
|
+ final String myHost = parser.getLocalClient().getHostname();
|
|
727
|
+ if (!myHost.isEmpty()) {
|
|
728
|
+ try {
|
|
729
|
+ return InetAddress.getByName(myHost).getHostAddress();
|
|
730
|
+ } catch (UnknownHostException e) { //NOPMD - handled below
|
|
731
|
+ //Continue below
|
|
732
|
+ }
|
|
733
|
+ }
|
|
734
|
+ }
|
|
735
|
+ try {
|
|
736
|
+ return InetAddress.getLocalHost().getHostAddress();
|
|
737
|
+ } catch (UnknownHostException e) {
|
|
738
|
+ // This is almost certainly not what we want, but we can't work out
|
|
739
|
+ // the right one.
|
|
740
|
+ return "127.0.0.1"; //NOPMD
|
|
741
|
+ }
|
|
742
|
+ }
|
|
743
|
+
|
|
744
|
+ /**
|
|
745
|
+ * Adds configuration options for the DCC plugin.
|
|
746
|
+ *
|
|
747
|
+ * @param manager The model to add options to.
|
|
748
|
+ */
|
|
749
|
+ public void showConfig(final PreferencesDialogModel manager) {
|
|
750
|
+ final PreferencesCategory general = new PluginPreferencesCategory(
|
|
751
|
+ pluginInfo, "DCC", "", "category-dcc");
|
|
752
|
+ final PreferencesCategory firewall = new PluginPreferencesCategory(
|
|
753
|
+ pluginInfo, "Firewall", "");
|
|
754
|
+ final PreferencesCategory sending = new PluginPreferencesCategory(
|
|
755
|
+ pluginInfo, "Sending", "");
|
|
756
|
+ final PreferencesCategory receiving = new PluginPreferencesCategory(
|
|
757
|
+ pluginInfo, "Receiving", "");
|
|
758
|
+
|
|
759
|
+ manager.getCategory("Plugins").addSubCategory(general.setInlineAfter());
|
|
760
|
+ general.addSubCategory(firewall.setInline());
|
|
761
|
+ general.addSubCategory(sending.setInline());
|
|
762
|
+ general.addSubCategory(receiving.setInline());
|
|
763
|
+
|
|
764
|
+ firewall.addSetting(new PreferencesSetting(PreferencesType.TEXT,
|
|
765
|
+ getDomain(), "firewall.ip", "Forced IP",
|
|
766
|
+ "What IP should be sent as our IP (Blank = work it out)",
|
|
767
|
+ manager.getConfigManager(), manager.getIdentity()));
|
|
768
|
+ firewall.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
|
|
769
|
+ getDomain(), "firewall.ports.usePortRange", "Use Port Range",
|
|
770
|
+ "Useful if you have a firewall that only forwards specific "
|
|
771
|
+ + "ports", manager.getConfigManager(), manager.getIdentity()));
|
|
772
|
+ firewall.addSetting(new PreferencesSetting(PreferencesType.INTEGER,
|
|
773
|
+ getDomain(), "firewall.ports.startPort", "Start Port",
|
|
774
|
+ "Port to try to listen on first", manager.getConfigManager(),
|
|
775
|
+ manager.getIdentity()));
|
|
776
|
+ firewall.addSetting(new PreferencesSetting(PreferencesType.INTEGER,
|
|
777
|
+ getDomain(), "firewall.ports.endPort", "End Port",
|
|
778
|
+ "Port to try to listen on last", manager.getConfigManager(),
|
|
779
|
+ manager.getIdentity()));
|
|
780
|
+ receiving.addSetting(new PreferencesSetting(PreferencesType.DIRECTORY,
|
|
781
|
+ getDomain(), "receive.savelocation", "Default save location",
|
|
782
|
+ "Where the save as window defaults to?",
|
|
783
|
+ manager.getConfigManager(), manager.getIdentity()));
|
|
784
|
+ sending.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
|
|
785
|
+ getDomain(), "send.reverse", "Reverse DCC",
|
|
786
|
+ "With reverse DCC, the sender connects rather than "
|
|
787
|
+ + "listens like normal dcc", manager.getConfigManager(),
|
|
788
|
+ manager.getIdentity()));
|
|
789
|
+ sending.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
|
|
790
|
+ getDomain(), "send.forceturbo", "Use Turbo DCC",
|
|
791
|
+ "Turbo DCC doesn't wait for ack packets. this is "
|
|
792
|
+ + "faster but not always supported.",
|
|
793
|
+ manager.getConfigManager(), manager.getIdentity()));
|
|
794
|
+ receiving.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
|
|
795
|
+ getDomain(), "receive.reverse.sendtoken",
|
|
796
|
+ "Send token in reverse receive",
|
|
797
|
+ "If you have problems with reverse dcc receive resume,"
|
|
798
|
+ + " try toggling this.", manager.getConfigManager(),
|
|
799
|
+ manager.getIdentity()));
|
|
800
|
+ general.addSetting(new PreferencesSetting(PreferencesType.INTEGER,
|
|
801
|
+ getDomain(), "send.blocksize", "Blocksize to use for DCC",
|
|
802
|
+ "Change the block size for send/receive, this can "
|
|
803
|
+ + "sometimes speed up transfers.", manager.getConfigManager(),
|
|
804
|
+ manager.getIdentity()));
|
|
805
|
+ general.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
|
|
806
|
+ getDomain(), "general.percentageInTitle",
|
|
807
|
+ "Show percentage of transfers in the window title",
|
|
808
|
+ "Show the current percentage of transfers in the DCC window "
|
|
809
|
+ + "title", manager.getConfigManager(), manager.getIdentity()));
|
|
810
|
+ }
|
|
811
|
+
|
|
812
|
+}
|