Procházet zdrojové kódy

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 před 10 roky
rodič
revize
bab9c077c9
1 změnil soubory, kde provedl 59 přidání a 73 odebrání
  1. 59
    73
      src/com/dmdirc/addons/dcc/DCCManager.java

+ 59
- 73
src/com/dmdirc/addons/dcc/DCCManager.java Zobrazit soubor

@@ -24,8 +24,6 @@ package com.dmdirc.addons.dcc;
24 24
 
25 25
 import com.dmdirc.ClientModule.GlobalConfig;
26 26
 import com.dmdirc.FrameContainer;
27
-import com.dmdirc.actions.ActionManager;
28
-import com.dmdirc.actions.CoreActionType;
29 27
 import com.dmdirc.addons.dcc.events.DccChatRequestEvent;
30 28
 import com.dmdirc.addons.dcc.events.DccSendRequestEvent;
31 29
 import com.dmdirc.addons.dcc.io.DCC;
@@ -39,10 +37,9 @@ import com.dmdirc.addons.ui_swing.injection.MainWindow;
39 37
 import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
40 38
 import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
41 39
 import com.dmdirc.commandparser.parsers.GlobalCommandParser;
42
-import com.dmdirc.interfaces.ActionListener;
40
+import com.dmdirc.events.ServerCtcpEvent;
43 41
 import com.dmdirc.interfaces.CommandController;
44 42
 import com.dmdirc.interfaces.Connection;
45
-import com.dmdirc.interfaces.actions.ActionType;
46 43
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
47 44
 import com.dmdirc.interfaces.config.ConfigProvider;
48 45
 import com.dmdirc.interfaces.config.IdentityController;
@@ -57,6 +54,7 @@ import com.dmdirc.ui.input.TabCompleterFactory;
57 54
 import com.dmdirc.util.URLBuilder;
58 55
 
59 56
 import com.google.common.eventbus.EventBus;
57
+import com.google.common.eventbus.Subscribe;
60 58
 
61 59
 import java.awt.Window;
62 60
 import java.io.File;
@@ -75,7 +73,7 @@ import javax.swing.JOptionPane;
75 73
  * This plugin adds DCC to DMDirc.
76 74
  */
77 75
 @Singleton
78
-public class DCCManager implements ActionListener {
76
+public class DCCManager {
79 77
 
80 78
     /** Our DCC Container window. */
81 79
     private PlaceholderContainer container;
@@ -321,12 +319,6 @@ public class DCCManager implements ActionListener {
321 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 323
      * Make the given DCC start listening. This will either call dcc.listen() or
332 324
      * dcc.listen(startPort, endPort) depending on config.
@@ -336,14 +328,12 @@ public class DCCManager implements ActionListener {
336 328
      * @return True if Socket was opened.
337 329
      */
338 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 333
         try {
342 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 337
                 dcc.listen(startPort, endPort);
348 338
             } else {
349 339
                 dcc.listen();
@@ -354,56 +344,52 @@ public class DCCManager implements ActionListener {
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 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 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 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 388
         if (dontAsk) {
402
-            handleDCCChat(((Connection) arguments[0]).getParser(), nickname, ctcpData);
389
+            handleDCCChat(connection.getParser(), nickname, ctcpData);
403 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,13 +425,14 @@ public class DCCManager implements ActionListener {
439 425
     /**
440 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 436
         final String filename;
450 437
         String tmpFilename;
451 438
         // Clients tend to put files with spaces in the name in ""
@@ -459,8 +446,7 @@ public class DCCManager implements ActionListener {
459 446
                     bit = bit.substring(1);
460 447
                 }
461 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 450
                     break;
465 451
                 } else {
466 452
                     filenameBits.append(" ").append(bit);
@@ -514,10 +500,9 @@ public class DCCManager implements ActionListener {
514 500
             if (!token.isEmpty() && !port.equals("0")) {
515 501
                 // This is a reverse DCC Send that we no longer care about.
516 502
             } else {
517
-                eventBus.post(
518
-                        new DccSendRequestEvent((Connection) arguments[0], nickname, filename));
503
+                eventBus.post(new DccSendRequestEvent(connection, nickname, filename));
519 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,11 +530,12 @@ public class DCCManager implements ActionListener {
545 530
     /**
546 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 539
         final String filename;
554 540
         // Clients tend to put files with spaces in the name in ""
555 541
         final StringBuilder filenameBits = new StringBuilder();
@@ -594,8 +580,8 @@ public class DCCManager implements ActionListener {
594 580
                         && (!token.equals(send.getToken()))) {
595 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 585
                 if (ctcpData[0].equalsIgnoreCase("resume")) {
600 586
                     parser.sendCTCP(nick, "DCC", "ACCEPT " + ((quoted) ? "\""
601 587
                             + filename + "\"" : filename) + " " + port + " "
@@ -682,14 +668,14 @@ public class DCCManager implements ActionListener {
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 675
      * Called when this plugin is Unloaded.
690 676
      */
691 677
     public synchronized void onUnload() {
692
-        ActionManager.getActionManager().unregisterListener(this);
678
+        eventBus.unregister(this);
693 679
         if (container != null) {
694 680
             container.close();
695 681
         }

Načítá se…
Zrušit
Uložit