Browse Source

Add joinChannels method and repurpose updateURI()

Fixes issue 3582
Fixes issue 3641
Fixes issue 3863 (dev error)
Issue 3623
Issue 3600

Change-Id: Ia0b147cfe1af5d5d5d1eae34c82e50ce23578d10
Depends-On: I1dac30977798780148eb27b8279a56a86b3bf890
Reviewed-on: http://gerrit.dmdirc.com/979
Automatic-Compile: Chris Smith <chris@dmdirc.com>
Reviewed-by: Gregory Holmes <greg@dmdirc.com>
tags/0.6.4
Chris Smith 14 years ago
parent
commit
5c89ed6898

+ 78
- 0
src/com/dmdirc/parser/common/ChannelJoinRequest.java View File

@@ -0,0 +1,78 @@
1
+/*
2
+ * Copyright (c) 2006-2010 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.parser.common;
24
+
25
+/**
26
+ * Describes the information needed to try and join a channel.
27
+ *
28
+ * @author chris
29
+ * @since 0.6.4
30
+ */
31
+public class ChannelJoinRequest {
32
+
33
+    /** The name of the channel to join (required). */
34
+    private final String name;
35
+    /** The name of the password to use (optional). */
36
+    private final String password;
37
+
38
+    /**
39
+     * Creates a new ChannelJoinRequest for a password-less channel with
40
+     * the specified name.
41
+     *
42
+     * @param name The name of the channel to join
43
+     */
44
+    public ChannelJoinRequest(final String name) {
45
+        this(name, null);
46
+    }
47
+
48
+    /**
49
+     * Creates a new ChannelJoinRequest for a channel with the specified
50
+     * password.
51
+     *
52
+     * @param name The name of the channel to join
53
+     * @param password The password to use
54
+     */
55
+    public ChannelJoinRequest(final String name, final String password) {
56
+        this.name = name;
57
+        this.password = password;
58
+    }
59
+
60
+    /**
61
+     * Retrieves the name of the channel this request will try to join.
62
+     *
63
+     * @return The name of the channel in this request
64
+     */
65
+    public String getName() {
66
+        return name;
67
+    }
68
+
69
+    /**
70
+     * Retrieves the password which will be used to try to join the channel.
71
+     *
72
+     * @return The password to use, or null if none specified
73
+     */
74
+    public String getPassword() {
75
+        return password;
76
+    }
77
+
78
+}

+ 14
- 17
src/com/dmdirc/parser/interfaces/Parser.java View File

@@ -24,6 +24,7 @@ package com.dmdirc.parser.interfaces;
24 24
 
25 25
 import com.dmdirc.parser.common.IgnoreList;
26 26
 import com.dmdirc.parser.common.CallbackManager;
27
+import com.dmdirc.parser.common.ChannelJoinRequest;
27 28
 
28 29
 import com.dmdirc.parser.common.QueuePriority;
29 30
 import java.net.URI;
@@ -62,6 +63,14 @@ public interface Parser extends Runnable {
62 63
      */
63 64
     void joinChannel(String channel, String key);
64 65
 
66
+    /**
67
+     * Joins the specified channels.
68
+     *
69
+     * @since 0.6.4
70
+     * @param channels The channels to be joined
71
+     */
72
+    void joinChannels(ChannelJoinRequest ... channels);
73
+
65 74
     /**
66 75
      * Retrieves a channel information object for the specified channel.
67 76
      *
@@ -187,24 +196,12 @@ public interface Parser extends Runnable {
187 196
     boolean compareURI(final URI uri);
188 197
 
189 198
     /**
190
-     * Updates this parser with a new URI. The new URI should be fundamentally
191
-     * the same as the one the parser connected to (to the extent that
192
-     * {@link #compareURI(java.net.URI)} returns true), but it may contain
193
-     * extra information (such as an additional list of channels to join).
194
-     * This operation should be non-destructive; that is, if the URI provided
195
-     * contains less information than a previous URI, the 'missing' information
196
-     * should not be treated as an instruction to remove it.
197
-     * <p>
198
-     * For example, if the parser was originally constructed with a URI
199
-     * <code>irc://irc.quakenet.org/chan1,chan2</code>, and this method was
200
-     * subsequently called with an argument of
201
-     * <code>irc://irc.quakenet.org/chan3</code>, the IRC parser should join
202
-     * the channel "chan3" and would also remain in chan1 and chan2.
203
-     *
204
-     * @param uri The URI to update this parser with
205
-     * @since 0.6.3
199
+     * Extracts any channels present in the specified URI.
200
+     *
201
+     * @param uri The URI to extract channels from
202
+     * @since 0.6.4
206 203
      */
207
-    void updateURI(final URI uri);
204
+    Collection<? extends ChannelJoinRequest> extractChannels(final URI uri);
208 205
 
209 206
     /**
210 207
      * Retrieves the name of the server that this parser is connected to.

+ 39
- 35
src/com/dmdirc/parser/irc/IRCParser.java View File

@@ -39,6 +39,7 @@ import com.dmdirc.parser.interfaces.callbacks.Post005Listener;
39 39
 import com.dmdirc.parser.interfaces.callbacks.ServerErrorListener;
40 40
 import com.dmdirc.parser.interfaces.callbacks.SocketCloseListener;
41 41
 import com.dmdirc.parser.common.CallbackManager;
42
+import com.dmdirc.parser.common.ChannelJoinRequest;
42 43
 import com.dmdirc.parser.common.QueuePriority;
43 44
 
44 45
 import java.io.BufferedReader;
@@ -53,6 +54,7 @@ import java.net.UnknownHostException;
53 54
 import java.security.KeyManagementException;
54 55
 import java.security.NoSuchAlgorithmException;
55 56
 import java.security.cert.X509Certificate;
57
+import java.util.ArrayList;
56 58
 import java.util.Arrays;
57 59
 import java.util.Collection;
58 60
 import java.util.HashMap;
@@ -357,11 +359,33 @@ public class IRCParser implements SecureParser, Runnable {
357 359
 
358 360
     /** {@inheritDoc} */
359 361
     @Override
360
-    public void updateURI(final URI uri) {
361
-        final String channels = new ServerInfo(uri).getChannels();
362
-        if (channels != null) {
363
-            joinChannel(channels, true);
362
+    public Collection<? extends ChannelJoinRequest> extractChannels(final URI uri) {
363
+        return extractChannels(new ServerInfo(uri).getChannels());
364
+    }
365
+
366
+    /**
367
+     * Extracts a set of channels and optional keys from the specified String.
368
+     * Channels are separated by commas, and keys are separated from their
369
+     * channels by a space.
370
+     *
371
+     * @since 0.6.4
372
+     * @param channels The string of channels to parse
373
+     * @return A corresponding collection of join request objects
374
+     */
375
+    protected Collection<? extends ChannelJoinRequest> extractChannels(final String channels) {
376
+        final List<ChannelJoinRequest> res = new ArrayList<ChannelJoinRequest>();
377
+
378
+        for (String channel : channels.split(",")) {
379
+            final String[] parts = channel.split(" ", 2);
380
+
381
+            if (parts.length == 2) {
382
+                res.add(new ChannelJoinRequest(parts[0], parts[1]));
383
+            } else {
384
+                res.add(new ChannelJoinRequest(parts[0]));
385
+            }
364 386
         }
387
+
388
+        return res;
365 389
     }
366 390
 
367 391
     /** {@inheritDoc} */
@@ -1468,24 +1492,13 @@ public class IRCParser implements SecureParser, Runnable {
1468 1492
     /** {@inheritDoc} */
1469 1493
     @Override
1470 1494
     public void joinChannel(final String channel) {
1471
-        joinChannel(channel, "", true);
1472
-    }
1473
-
1474
-    /**
1475
-     * Join a Channel.
1476
-     *
1477
-     * @param sChannelName Name of channel to join
1478
-     * @param autoPrefix Automatically prepend the first channel prefix defined
1479
-     *                   in 005 if sChannelName is an invalid channel.
1480
-     */
1481
-    public void joinChannel(final String sChannelName, final boolean autoPrefix) {
1482
-        joinChannel(sChannelName, "", autoPrefix);
1495
+        joinChannels(extractChannels(channel).toArray(new ChannelJoinRequest[0]));
1483 1496
     }
1484 1497
 
1485 1498
     /** {@inheritDoc} */
1486 1499
     @Override
1487 1500
     public void joinChannel(final String channel, final String key) {
1488
-        joinChannel(channel, key, true);
1501
+        joinChannels(new ChannelJoinRequest(channel, key));
1489 1502
     }
1490 1503
 
1491 1504
     /**
@@ -1497,35 +1510,26 @@ public class IRCParser implements SecureParser, Runnable {
1497 1510
      * @param key Key to use to try and join the channel (If a list is given
1498 1511
      *            then this key will be used for any channels that do not
1499 1512
      *            specify one themselves.
1500
-     * @param autoPrefix Automatically prepend the first channel prefix defined
1501
-     *                   in 005 to any of the channels passsed if they are
1502
-     *                   otherwise invalid channels.
1503 1513
      */
1504
-    public void joinChannel(final String channel, final String key, final boolean autoPrefix) {
1505
-        final String[] bits = channel.split(",");
1506
-
1514
+    @Override
1515
+    public void joinChannels(final ChannelJoinRequest ... channels) {
1507 1516
         // We store a map from key->channels to allow intelligent joining of
1508 1517
         // channels using as few JOIN commands as needed.
1509 1518
         final Map<String, StringBuffer> joinMap = new HashMap<String, StringBuffer>();
1510 1519
 
1511
-        for (String bit : bits) {
1512
-            // Find any key for this channel
1513
-            final String[] keybits = bit.split(" ", 2);
1514
-            final String channelName = keybits[0];
1515
-            final String thisKey = (keybits.length > 1) ? keybits[1] : key;
1516
-
1520
+        for (ChannelJoinRequest channel : channels) {
1517 1521
             // Make sure we have a list to put stuff in.
1518
-            StringBuffer list = joinMap.get(thisKey);
1522
+            StringBuffer list = joinMap.get(channel.getPassword());
1519 1523
             if (list == null) {
1520 1524
                 list = new StringBuffer();
1521
-                joinMap.put(thisKey, list);
1525
+                joinMap.put(channel.getPassword(), list);
1522 1526
             }
1523 1527
 
1524 1528
             // Add the channel to the list. If the name is invalid and
1525 1529
             // autoprefix is off we will just skip this channel.
1526
-            if (channelName.length() > 0 && (isValidChannelName(channelName) || autoPrefix)) {
1530
+            if (!channel.getName().isEmpty()) {
1527 1531
                 if (list.length() > 0) { list.append(","); }
1528
-                if (!isValidChannelName(channelName) && autoPrefix) {
1532
+                if (!isValidChannelName(channel.getName())) {
1529 1533
                     if (h005Info.containsKey("CHANTYPES")) {
1530 1534
                         final String chantypes = h005Info.get("CHANTYPES");
1531 1535
                         if (chantypes.isEmpty()) {
@@ -1537,7 +1541,7 @@ public class IRCParser implements SecureParser, Runnable {
1537 1541
                         list.append('#');
1538 1542
                     }
1539 1543
                 }
1540
-                list.append(channelName);
1544
+                list.append(channel.getName());
1541 1545
             }
1542 1546
         }
1543 1547
 
@@ -1545,7 +1549,7 @@ public class IRCParser implements SecureParser, Runnable {
1545 1549
             final String thisKey = entrySet.getKey();
1546 1550
             final String channelString = entrySet.getValue().toString();
1547 1551
             if (!channelString.isEmpty()) {
1548
-                if (thisKey.isEmpty()) {
1552
+                if (thisKey == null || thisKey.isEmpty()) {
1549 1553
                     sendString("JOIN " + channelString);
1550 1554
                 } else {
1551 1555
                     sendString("JOIN " + channelString + " " + thisKey);

+ 1
- 1
src/com/dmdirc/parser/irc/Process001.java View File

@@ -71,7 +71,7 @@ public class Process001 extends IRCProcessor {
71 71
 
72 72
         final String channels = myParser.server.getChannels();
73 73
         if (channels != null) {
74
-            myParser.joinChannel(channels, true);
74
+            myParser.joinChannel(channels);
75 75
         }
76 76
     }
77 77
 

Loading…
Cancel
Save