Browse Source

Switch DCCManager to ServerCtcpEvent.

Change-Id: I8b55a2e47099629e6fe80344451f45826a44c188
Reviewed-on: http://gerrit.dmdirc.com/3499
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
changes/99/3499/6
Greg Holmes 10 years ago
parent
commit
bab9c077c9
1 changed files with 59 additions and 73 deletions
  1. 59
    73
      src/com/dmdirc/addons/dcc/DCCManager.java

+ 59
- 73
src/com/dmdirc/addons/dcc/DCCManager.java View File

24
 
24
 
25
 import com.dmdirc.ClientModule.GlobalConfig;
25
 import com.dmdirc.ClientModule.GlobalConfig;
26
 import com.dmdirc.FrameContainer;
26
 import com.dmdirc.FrameContainer;
27
-import com.dmdirc.actions.ActionManager;
28
-import com.dmdirc.actions.CoreActionType;
29
 import com.dmdirc.addons.dcc.events.DccChatRequestEvent;
27
 import com.dmdirc.addons.dcc.events.DccChatRequestEvent;
30
 import com.dmdirc.addons.dcc.events.DccSendRequestEvent;
28
 import com.dmdirc.addons.dcc.events.DccSendRequestEvent;
31
 import com.dmdirc.addons.dcc.io.DCC;
29
 import com.dmdirc.addons.dcc.io.DCC;
39
 import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
37
 import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
40
 import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
38
 import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
41
 import com.dmdirc.commandparser.parsers.GlobalCommandParser;
39
 import com.dmdirc.commandparser.parsers.GlobalCommandParser;
42
-import com.dmdirc.interfaces.ActionListener;
40
+import com.dmdirc.events.ServerCtcpEvent;
43
 import com.dmdirc.interfaces.CommandController;
41
 import com.dmdirc.interfaces.CommandController;
44
 import com.dmdirc.interfaces.Connection;
42
 import com.dmdirc.interfaces.Connection;
45
-import com.dmdirc.interfaces.actions.ActionType;
46
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
43
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
47
 import com.dmdirc.interfaces.config.ConfigProvider;
44
 import com.dmdirc.interfaces.config.ConfigProvider;
48
 import com.dmdirc.interfaces.config.IdentityController;
45
 import com.dmdirc.interfaces.config.IdentityController;
57
 import com.dmdirc.util.URLBuilder;
54
 import com.dmdirc.util.URLBuilder;
58
 
55
 
59
 import com.google.common.eventbus.EventBus;
56
 import com.google.common.eventbus.EventBus;
57
+import com.google.common.eventbus.Subscribe;
60
 
58
 
61
 import java.awt.Window;
59
 import java.awt.Window;
62
 import java.io.File;
60
 import java.io.File;
75
  * This plugin adds DCC to DMDirc.
73
  * This plugin adds DCC to DMDirc.
76
  */
74
  */
77
 @Singleton
75
 @Singleton
78
-public class DCCManager implements ActionListener {
76
+public class DCCManager {
79
 
77
 
80
     /** Our DCC Container window. */
78
     /** Our DCC Container window. */
81
     private PlaceholderContainer container;
79
     private PlaceholderContainer container;
321
         return jc.showSaveDialog(mainWindow);
319
         return jc.showSaveDialog(mainWindow);
322
     }
320
     }
323
 
321
 
324
-    @Override
325
-    public void processEvent(final ActionType type, final StringBuffer format,
326
-            final Object... arguments) {
327
-        handleProcessEvent(type, format, false, arguments);
328
-    }
329
-
330
     /**
322
     /**
331
      * Make the given DCC start listening. This will either call dcc.listen() or
323
      * Make the given DCC start listening. This will either call dcc.listen() or
332
      * dcc.listen(startPort, endPort) depending on config.
324
      * dcc.listen(startPort, endPort) depending on config.
336
      * @return True if Socket was opened.
328
      * @return True if Socket was opened.
337
      */
329
      */
338
     protected boolean listen(final DCC dcc) {
330
     protected boolean listen(final DCC dcc) {
339
-        final boolean usePortRange = config.getOptionBool(getDomain(),
340
-                "firewall.ports.usePortRange");
331
+        final boolean usePortRange = config.
332
+                getOptionBool(getDomain(), "firewall.ports.usePortRange");
341
         try {
333
         try {
342
             if (usePortRange) {
334
             if (usePortRange) {
343
-                final int startPort = config.getOptionInt(getDomain(),
344
-                        "firewall.ports.startPort");
345
-                final int endPort = config.getOptionInt(getDomain(),
346
-                        "firewall.ports.endPort");
335
+                final int startPort = config.getOptionInt(getDomain(), "firewall.ports.startPort");
336
+                final int endPort = config.getOptionInt(getDomain(), "firewall.ports.endPort");
347
                 dcc.listen(startPort, endPort);
337
                 dcc.listen(startPort, endPort);
348
             } else {
338
             } else {
349
                 dcc.listen();
339
                 dcc.listen();
354
         }
344
         }
355
     }
345
     }
356
 
346
 
357
-    /**
358
-     * Process an event of the specified type.
359
-     *
360
-     * @param type      The type of the event to process
361
-     * @param format    Format of messages that are about to be sent. (May be null)
362
-     * @param dontAsk   Don't ask any questions, assume yes.
363
-     * @param arguments The arguments for the event
364
-     */
365
-    public void handleProcessEvent(final ActionType type,
366
-            final StringBuffer format, final boolean dontAsk,
367
-            final Object... arguments) {
368
-        if (config.getOptionBool(getDomain(), "receive.autoaccept") && !dontAsk) {
369
-            handleProcessEvent(type, format, true, arguments);
347
+    @Subscribe
348
+    public void handleServerCtctpEvent(final ServerCtcpEvent event) {
349
+        final boolean autoAccept = config.getOptionBool(getDomain(), "receive.autoaccept");
350
+        final String[] ctcpData = event.getContent().split(" ");
351
+        if (!"DCC".equalsIgnoreCase(event.getType())) {
370
             return;
352
             return;
371
         }
353
         }
372
-
373
-        if (type == CoreActionType.SERVER_CTCP) {
374
-            final String[] ctcpData = ((String) arguments[3]).split(" ");
375
-            if ("DCC".equalsIgnoreCase((String) arguments[2])) {
376
-                if ("chat".equalsIgnoreCase(ctcpData[0])
377
-                        && ctcpData.length > 3) {
378
-                    handleChat(dontAsk, ctcpData, arguments);
379
-                } else if ("send".equalsIgnoreCase(ctcpData[0])
380
-                        && ctcpData.length > 3) {
381
-                    handleSend(dontAsk, ctcpData, arguments);
382
-                } else if (("resume".equalsIgnoreCase(ctcpData[0])
383
-                        || "accept".equalsIgnoreCase(ctcpData[0]))
384
-                        && ctcpData.length > 2) {
385
-                    handleReceive(ctcpData, arguments);
354
+        switch (event.getType().toLowerCase()) {
355
+            case "chat":
356
+                if (ctcpData.length > 3) {
357
+                    handleChat(autoAccept, ctcpData, event.getClient(), event.getConnection());
386
                 }
358
                 }
387
-            }
359
+                break;
360
+            case "send":
361
+                if (ctcpData.length > 3) {
362
+                    handleSend(autoAccept, ctcpData, event.getClient(), event.getConnection());
363
+                }
364
+                break;
365
+            case "resume":
366
+            //Fallthrough
367
+            case "accept":
368
+                if (ctcpData.length > 2) {
369
+                    handleReceive(ctcpData, event.getClient(), event.getConnection());
370
+                }
371
+                break;
372
+            default:
373
+                break;
388
         }
374
         }
389
     }
375
     }
390
 
376
 
391
     /**
377
     /**
392
      * Handles a DCC chat request.
378
      * Handles a DCC chat request.
393
      *
379
      *
394
-     * @param dontAsk   Don't ask any questions, assume yes.
395
-     * @param ctcpData  CTCP data bits
396
-     * @param arguments The arguments for the event
380
+     * @param dontAsk    Don't ask any questions, assume yes.
381
+     * @param ctcpData   CTCP data bits
382
+     * @param client     Client receiving DCC
383
+     * @param connection Connection DCC received on
397
      */
384
      */
398
     private void handleChat(final boolean dontAsk, final String[] ctcpData,
385
     private void handleChat(final boolean dontAsk, final String[] ctcpData,
399
-            final Object... arguments) {
400
-        final String nickname = ((ClientInfo) arguments[1]).getNickname();
386
+            final ClientInfo client, final Connection connection) {
387
+        final String nickname = client.getNickname();
401
         if (dontAsk) {
388
         if (dontAsk) {
402
-            handleDCCChat(((Connection) arguments[0]).getParser(), nickname, ctcpData);
389
+            handleDCCChat(connection.getParser(), nickname, ctcpData);
403
         } else {
390
         } else {
404
-            eventBus.post(new DccChatRequestEvent((Connection) arguments[0], nickname));
405
-            new ChatRequestDialog(mainWindow, this, (Connection) arguments[0], nickname, ctcpData)
406
-                    .display();
391
+            eventBus.post(new DccChatRequestEvent(connection, nickname));
392
+            new ChatRequestDialog(mainWindow, this, connection, nickname, ctcpData).display();
407
         }
393
         }
408
     }
394
     }
409
 
395
 
439
     /**
425
     /**
440
      * Handles a DCC send request.
426
      * Handles a DCC send request.
441
      *
427
      *
442
-     * @param dontAsk   Don't ask any questions, assume yes.
443
-     * @param ctcpData  CTCP data bits
444
-     * @param arguments The arguments for the event
428
+     * @param dontAsk    Don't ask any questions, assume yes.
429
+     * @param ctcpData   CTCP data bits
430
+     * @param client     Client that received the DCC
431
+     * @param connection Connection the DCC was received on
445
      */
432
      */
446
-    private void handleSend(final boolean dontAsk, final String[] ctcpData,
447
-            final Object... arguments) {
448
-        final String nickname = ((ClientInfo) arguments[1]).getNickname();
433
+    private void handleSend(final boolean dontAsk, final String[] ctcpData, final ClientInfo client,
434
+            final Connection connection) {
435
+        final String nickname = client.getNickname();
449
         final String filename;
436
         final String filename;
450
         String tmpFilename;
437
         String tmpFilename;
451
         // Clients tend to put files with spaces in the name in ""
438
         // Clients tend to put files with spaces in the name in ""
459
                     bit = bit.substring(1);
446
                     bit = bit.substring(1);
460
                 }
447
                 }
461
                 if (bit.endsWith("\"")) {
448
                 if (bit.endsWith("\"")) {
462
-                    filenameBits.append(" ")
463
-                            .append(bit.substring(0, bit.length() - 1));
449
+                    filenameBits.append(" ").append(bit.substring(0, bit.length() - 1));
464
                     break;
450
                     break;
465
                 } else {
451
                 } else {
466
                     filenameBits.append(" ").append(bit);
452
                     filenameBits.append(" ").append(bit);
514
             if (!token.isEmpty() && !port.equals("0")) {
500
             if (!token.isEmpty() && !port.equals("0")) {
515
                 // This is a reverse DCC Send that we no longer care about.
501
                 // This is a reverse DCC Send that we no longer care about.
516
             } else {
502
             } else {
517
-                eventBus.post(
518
-                        new DccSendRequestEvent((Connection) arguments[0], nickname, filename));
503
+                eventBus.post(new DccSendRequestEvent(connection, nickname, filename));
519
                 new SendRequestDialog(mainWindow, this, token, ipLong, portInt, filename, size,
504
                 new SendRequestDialog(mainWindow, this, token, ipLong, portInt, filename, size,
520
-                        nickname, (Connection) arguments[0]).display();
505
+                        nickname, connection).display();
521
             }
506
             }
522
         }
507
         }
523
     }
508
     }
545
     /**
530
     /**
546
      * Handles a DCC chat request.
531
      * Handles a DCC chat request.
547
      *
532
      *
548
-     * @param ctcpData  CTCP data bits
549
-     * @param arguments The arguments for the event
533
+     * @param ctcpData   CTCP data bits
534
+     * @param client     Client receiving the DCC
535
+     * @param connection Connection the DCC was received on
550
      */
536
      */
551
-    private void handleReceive(final String[] ctcpData,
552
-            final Object... arguments) {
537
+    private void handleReceive(final String[] ctcpData, final ClientInfo client,
538
+            final Connection connection) {
553
         final String filename;
539
         final String filename;
554
         // Clients tend to put files with spaces in the name in ""
540
         // Clients tend to put files with spaces in the name in ""
555
         final StringBuilder filenameBits = new StringBuilder();
541
         final StringBuilder filenameBits = new StringBuilder();
594
                         && (!token.equals(send.getToken()))) {
580
                         && (!token.equals(send.getToken()))) {
595
                     continue;
581
                     continue;
596
                 }
582
                 }
597
-                final Parser parser = ((Connection) arguments[0]).getParser();
598
-                final String nick = ((ClientInfo) arguments[1]).getNickname();
583
+                final Parser parser = connection.getParser();
584
+                final String nick = client.getNickname();
599
                 if (ctcpData[0].equalsIgnoreCase("resume")) {
585
                 if (ctcpData[0].equalsIgnoreCase("resume")) {
600
                     parser.sendCTCP(nick, "DCC", "ACCEPT " + ((quoted) ? "\""
586
                     parser.sendCTCP(nick, "DCC", "ACCEPT " + ((quoted) ? "\""
601
                             + filename + "\"" : filename) + " " + port + " "
587
                             + filename + "\"" : filename) + " " + port + " "
682
             }
668
             }
683
         }
669
         }
684
 
670
 
685
-        ActionManager.getActionManager().registerListener(this, CoreActionType.SERVER_CTCP);
671
+        eventBus.register(this);
686
     }
672
     }
687
 
673
 
688
     /**
674
     /**
689
      * Called when this plugin is Unloaded.
675
      * Called when this plugin is Unloaded.
690
      */
676
      */
691
     public synchronized void onUnload() {
677
     public synchronized void onUnload() {
692
-        ActionManager.getActionManager().unregisterListener(this);
678
+        eventBus.unregister(this);
693
         if (container != null) {
679
         if (container != null) {
694
             container.close();
680
             container.close();
695
         }
681
         }

Loading…
Cancel
Save