Browse Source

Make joinChannel more intelligent. This should refix issue 3112.

Change-Id: I98d9743d99963fbce64ac397c53e74e60a035674
Reviewed-on: http://gerrit.dmdirc.com/398
Reviewed-by: Gregory Holmes <greboid@dmdirc.com>
Tested-by: Gregory Holmes <greboid@dmdirc.com>
tags/0.6.3
Shane Mc Cormack 14 years ago
parent
commit
e19e88fbd5
1 changed files with 45 additions and 23 deletions
  1. 45
    23
      src/com/dmdirc/parser/irc/IRCParser.java

+ 45
- 23
src/com/dmdirc/parser/irc/IRCParser.java View File

@@ -1456,7 +1456,6 @@ public class IRCParser implements SecureParser, Runnable {
1456 1456
      * @param sChannelName Name of channel to join
1457 1457
      * @param autoPrefix Automatically prepend the first channel prefix defined
1458 1458
      *                   in 005 if sChannelName is an invalid channel.
1459
-     *                   **This only applies to the first channel if given a list**
1460 1459
      */
1461 1460
     public void joinChannel(final String sChannelName, final boolean autoPrefix) {
1462 1461
         joinChannel(sChannelName, "", autoPrefix);
@@ -1470,37 +1469,60 @@ public class IRCParser implements SecureParser, Runnable {
1470 1469
 
1471 1470
     /**
1472 1471
      * Join a Channel with a key.
1472
+     * This also allows passing a list of channels such as:
1473
+     * "#channel1 key1,#channel2 key2,#channel3,#channel4,#channel5 key2"
1473 1474
      *
1474
-     * @param channel Name of channel to join
1475
-     * @param key Key to use to try and join the channel
1475
+     * @param channel Name of channel to join or a list of channels.
1476
+     * @param key Key to use to try and join the channel (If a list is given
1477
+     *            then this key will be used for any channels that do not
1478
+     *            specify one themselves.
1476 1479
      * @param autoPrefix Automatically prepend the first channel prefix defined
1477
-     *                   in 005 if sChannelName is an invalid channel.
1478
-     *                   **This only applies to the first channel if given a list**
1480
+     *                   in 005 to any of the channels passsed if they are
1481
+     *                   otherwise invalid channels.
1479 1482
      */
1480 1483
     public void joinChannel(final String channel, final String key, final boolean autoPrefix) {
1481
-        final String channelName;
1482
-        if (isValidChannelName(channel)) {
1483
-            channelName = channel;
1484
-        } else {
1485
-            if (autoPrefix) {
1486
-                if (h005Info.containsKey("CHANTYPES")) {
1487
-                    final String chantypes = h005Info.get("CHANTYPES");
1488
-                    if (chantypes.isEmpty()) {
1489
-                        channelName = "#" + channel;
1484
+        final String[] bits = channel.split(",");
1485
+
1486
+        // We store a map from key->channels to allow intelligent joining of
1487
+        // channels using as few JOIN commands as needed.
1488
+        final Map<String, StringBuffer> joinMap = new HashMap<String, StringBuffer>();
1489
+
1490
+        for (String bit : bits) {
1491
+            // Find any key for this channel
1492
+            final String[] keybits = bit.split(" ", 2);
1493
+            final String channelName = keybits[0];
1494
+            final String thisKey = (keybits.length > 1) ? keybits[0] : key;
1495
+
1496
+            // Make sure we have a list to put stuff in.
1497
+            StringBuffer list = joinMap.get(thisKey);
1498
+            if (list == null) { list = new StringBuffer(); }
1499
+
1500
+            // Add the channel to the list. If the name is invalid and
1501
+            // autoprefix is off we will just skip this channel.
1502
+            if (isValidChannelName(channelName) || autoPrefix) {
1503
+                if (list.length() > 0) { list.append(","); }
1504
+                if (autoPrefix) {
1505
+                    if (h005Info.containsKey("CHANTYPES")) {
1506
+                        final String chantypes = h005Info.get("CHANTYPES");
1507
+                        if (chantypes.isEmpty()) {
1508
+                            list.append('#');
1509
+                        } else {
1510
+                            list.append(chantypes.charAt(0));
1511
+                        }
1490 1512
                     } else {
1491
-                        channelName = chantypes.charAt(0) + channel;
1513
+                        list.append('#');
1492 1514
                     }
1493
-                } else {
1494
-                    return;
1495 1515
                 }
1496
-            } else {
1497
-                return;
1516
+                list.append(channel);
1498 1517
             }
1499 1518
         }
1500
-        if (key.isEmpty()) {
1501
-            sendString("JOIN " + channelName);
1502
-        } else {
1503
-            sendString("JOIN " + channelName + " " + key);
1519
+
1520
+        for (String thisKey : joinMap.keySet()) {
1521
+            if (thisKey.isEmpty()) {
1522
+                sendString("JOIN " + joinMap.get(thisKey).toString());
1523
+            } else {
1524
+                sendString("JOIN " + joinMap.get(thisKey).toString() + " " + thisKey);
1525
+            }
1504 1526
         }
1505 1527
     }
1506 1528
 

Loading…
Cancel
Save