Browse Source

Add parser method to update parser with a URI

Issue 3254

Change-Id: I7b7d1ba7daa5d8b5c4ec0838163d67de039b616c
Reviewed-on: http://gerrit.dmdirc.com/565
Automatic-Compile: Gregory Holmes <greboid@dmdirc.com>
Reviewed-by: Gregory Holmes <greboid@dmdirc.com>
tags/0.6.3
Chris Smith 14 years ago
parent
commit
0b69437aad

+ 22
- 3
src/com/dmdirc/parser/interfaces/Parser.java View File

@@ -186,6 +186,25 @@ public interface Parser extends Runnable {
186 186
      */
187 187
     boolean compareURI(final URI uri);
188 188
 
189
+    /**
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
206
+     */
207
+    void updateURI(final URI uri);
189 208
 
190 209
     /**
191 210
      * Retrieves the name of the server that this parser is connected to.
@@ -286,7 +305,7 @@ public interface Parser extends Runnable {
286 305
 
287 306
     /**
288 307
      * Retrieves a list of channel user modes, in descending priority order.
289
-     * 
308
+     *
290 309
      * @return A string containing a list of channel user mode characters
291 310
      */
292 311
     String getChannelUserModes();
@@ -298,11 +317,11 @@ public interface Parser extends Runnable {
298 317
      * @since 0.6.3
299 318
      */
300 319
     String getChannelPrefixes();
301
-    
320
+
302 321
     /**
303 322
      * Retrieves the object which is responsible for managing callbacks for
304 323
      * this parser.
305
-     * 
324
+     *
306 325
      * @return This parser's callback manager
307 326
      */
308 327
     CallbackManager<? extends Parser> getCallbackManager();

+ 22
- 14
src/com/dmdirc/parser/irc/IRCParser.java View File

@@ -269,7 +269,7 @@ public class IRCParser implements SecureParser, Runnable {
269 269
 
270 270
     /** This is the IP we want to bind to. */
271 271
     private String bindIP = "";
272
-    
272
+
273 273
     /** This is list containing 001 - 005 inclusive. */
274 274
     private final List<String> serverInformationLines = new LinkedList<String>();
275 275
 
@@ -352,6 +352,14 @@ public class IRCParser implements SecureParser, Runnable {
352 352
                newURI.getPort() == oldURI.getPort();
353 353
     }
354 354
 
355
+    /** {@inheritDoc} */
356
+    @Override
357
+    public void updateURI(final URI uri) {
358
+        final String channels = new ServerInfo(uri).getChannels();
359
+        if (channels != null) {
360
+            joinChannel(channels, true);
361
+        }
362
+    }
355 363
 
356 364
     /** {@inheritDoc} */
357 365
     @Override
@@ -598,7 +606,7 @@ public class IRCParser implements SecureParser, Runnable {
598 606
     protected synchronized boolean callPost005() {
599 607
         if (post005) { return false; }
600 608
         post005 = true;
601
-        
609
+
602 610
         if (!h005Info.containsKey("CHANTYPES")) { parseChanPrefix(); }
603 611
         if (!h005Info.containsKey("PREFIX")) { parsePrefixModes(); }
604 612
         if (!h005Info.containsKey("USERMODES")) { parseUserModes(); }
@@ -800,7 +808,7 @@ public class IRCParser implements SecureParser, Runnable {
800 808
         final ParserError ei = new ParserError(ParserError.ERROR_ERROR + (isUserError ? ParserError.ERROR_USER : 0), "Exception with server socket", getLastLine());
801 809
         ei.setException(e);
802 810
         callConnectError(ei);
803
-        
811
+
804 812
         if (currentSocketState != SocketState.CLOSED) {
805 813
             currentSocketState = SocketState.CLOSED;
806 814
             callSocketClosed();
@@ -851,7 +859,7 @@ public class IRCParser implements SecureParser, Runnable {
851 859
                 }
852 860
             } catch (IOException e) {
853 861
                 callDebugInfo(DEBUG_SOCKET, "Exception in main loop (" + e.getMessage() + "), Aborted");
854
-                
862
+
855 863
                 if (currentSocketState != SocketState.CLOSED) {
856 864
                     currentSocketState = SocketState.CLOSED;
857 865
                     callSocketClosed();
@@ -1009,7 +1017,7 @@ public class IRCParser implements SecureParser, Runnable {
1009 1017
                 }
1010 1018
             }
1011 1019
         }
1012
-        
1020
+
1013 1021
         return true;
1014 1022
     }
1015 1023
 
@@ -1030,7 +1038,7 @@ public class IRCParser implements SecureParser, Runnable {
1030 1038
     public String getLastLine() {
1031 1039
         return lastLine;
1032 1040
     }
1033
-    
1041
+
1034 1042
     /**
1035 1043
      * Get the list of lines the server gave from 001 - 005.
1036 1044
      *
@@ -1112,7 +1120,7 @@ public class IRCParser implements SecureParser, Runnable {
1112 1120
                             // Some networks may send a NICK message if you nick change before 001
1113 1121
                             // Eat it up so that it isn't treated as a notice auth.
1114 1122
                             if (token[1].equalsIgnoreCase("NICK")) { break; }
1115
-                            
1123
+
1116 1124
                             // Otherwise, send to Notice Auth
1117 1125
                             try { myProcessingManager.process("Notice Auth", token); } catch (ProcessorNotFoundException e) { }
1118 1126
                             break;
@@ -1310,7 +1318,7 @@ public class IRCParser implements SecureParser, Runnable {
1310 1318
         if (chanPrefix.isEmpty()) {
1311 1319
             return "#&";
1312 1320
         }
1313
-        
1321
+
1314 1322
         final StringBuilder builder = new StringBuilder(chanPrefix.size());
1315 1323
 
1316 1324
         for (Character prefix : chanPrefix) {
@@ -1575,7 +1583,7 @@ public class IRCParser implements SecureParser, Runnable {
1575 1583
         } else {
1576 1584
             me.setNickname(nickname);
1577 1585
         }
1578
-        
1586
+
1579 1587
         thinkNickname = nickname;
1580 1588
     }
1581 1589
 
@@ -1712,7 +1720,7 @@ public class IRCParser implements SecureParser, Runnable {
1712 1720
             sendString("QUIT :" + reason);
1713 1721
         }
1714 1722
     }
1715
-    
1723
+
1716 1724
     /** {@inheritDoc} */
1717 1725
     @Override
1718 1726
     public void disconnect(final String message) {
@@ -1877,7 +1885,7 @@ public class IRCParser implements SecureParser, Runnable {
1877 1885
 
1878 1886
         pingTimerSem.release();
1879 1887
     }
1880
-    
1888
+
1881 1889
     /**
1882 1890
      * Stop the pingTimer.
1883 1891
      */
@@ -1906,18 +1914,18 @@ public class IRCParser implements SecureParser, Runnable {
1906 1914
                 pingTimer.cancel();
1907 1915
             }
1908 1916
             pingTimerSem.release();
1909
-            
1917
+
1910 1918
             return;
1911 1919
         }
1912 1920
         if (getPingNeeded()) {
1913 1921
             if (!callPingFailed()) {
1914 1922
                 pingTimerSem.acquireUninterruptibly();
1915
-                
1923
+
1916 1924
                 if (pingTimer != null && pingTimer.equals(timer)) {
1917 1925
                     pingTimer.cancel();
1918 1926
                 }
1919 1927
                 pingTimerSem.release();
1920
-                
1928
+
1921 1929
                 disconnect("Server not responding.");
1922 1930
             }
1923 1931
         } else {

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

@@ -24,7 +24,6 @@ package com.dmdirc.parser.irc;
24 24
 
25 25
 import com.dmdirc.parser.common.ParserError;
26 26
 import com.dmdirc.parser.interfaces.callbacks.ServerReadyListener;
27
-import java.net.URI;
28 27
 
29 28
 /**
30 29
  * Process a 001 message.
@@ -42,7 +41,7 @@ public class Process001 extends IRCProcessor {
42 41
         // << :demon1.uk.quakenet.org 001 Java-Test :Welcome to the QuakeNet IRC Network, Java-Test
43 42
         myParser.serverName = token[0].substring(1,token[0].length());
44 43
         final String sNick = token[2];
45
-                
44
+
46 45
         // myself will be fake if we havn't recieved a 001 yet
47 46
         if (myParser.getLocalClient().isFake()) {
48 47
             // Update stored information
@@ -66,31 +65,26 @@ public class Process001 extends IRCProcessor {
66 65
                 }
67 66
             }
68 67
         }
69
-        
68
+
70 69
         callServerReady();
71 70
         myParser.startPingTimer();
72 71
 
73
-        final URI uri = myParser.server.getURI();
74
-        if (uri != null) {
75
-            String channelString = uri.getPath();
76
-            if (uri.getRawQuery() != null && !uri.getRawQuery().isEmpty()) { channelString += "?" + uri.getRawQuery(); }
77
-            if (uri.getRawFragment() != null && !uri.getRawFragment().isEmpty()) { channelString += "#" + uri.getRawFragment(); }
78
-            if (channelString.startsWith("/")) { channelString = channelString.substring(1); }
79
-            
80
-            myParser.joinChannel(channelString, true);
72
+        final String channels = myParser.server.getChannels();
73
+        if (channels != null) {
74
+            myParser.joinChannel(channels, true);
81 75
         }
82 76
     }
83
-    
77
+
84 78
     /**
85 79
      * Callback to all objects implementing the ServerReady Callback.
86 80
      *
87 81
      * @see IServerReady
88 82
      * @return true if a method was called, false otherwise
89
-     */    
83
+     */
90 84
     protected boolean callServerReady() {
91 85
         return getCallbackManager().getCallbackType(ServerReadyListener.class).call();
92 86
     }
93
-    
87
+
94 88
     /**
95 89
      * What does this IRCProcessor handle.
96 90
      *
@@ -99,8 +93,8 @@ public class Process001 extends IRCProcessor {
99 93
     @Override
100 94
     public String[] handles() {
101 95
         return new String[]{"001"};
102
-    } 
103
-    
96
+    }
97
+
104 98
     /**
105 99
      * Create a new instance of the IRCProcessor Object.
106 100
      *

+ 42
- 22
src/com/dmdirc/parser/irc/ServerInfo.java View File

@@ -27,7 +27,7 @@ import java.net.URISyntaxException;
27 27
 
28 28
 /**
29 29
  * Contains Server information.
30
- * 
30
+ *
31 31
  * @author Shane Mc Cormack
32 32
  * @author Chris Smith
33 33
  * @see IRCParser
@@ -47,7 +47,7 @@ public class ServerInfo {
47 47
     /** Optional password needed to connect to server (Default: ""). */
48 48
     private String password = "";
49 49
     /** Is this an ssl-enabled server (Default: false). */
50
-    private boolean isSSL = false;    
50
+    private boolean isSSL = false;
51 51
     /** Are we using a socks proxy (Default: false). */
52 52
     private boolean useSocksProxy = false;
53 53
     /** Proxy server to connect to (Default: "127.0.0.1"). */
@@ -60,10 +60,10 @@ public class ServerInfo {
60 60
     private String proxyPass = "";
61 61
     /** URI used to create this ServerInfo if applicable */
62 62
     private URI uri = null;
63
-    
63
+
64 64
     /** Constructor using Default values. */
65 65
     public ServerInfo () { }
66
-    
66
+
67 67
     /**
68 68
      * Constructor using specifed host, port and password, SSL/Proxy must be specifed separately.
69 69
      *
@@ -87,7 +87,7 @@ public class ServerInfo {
87 87
     public ServerInfo(final URI uri) {
88 88
         setURI(uri);
89 89
     }
90
-    
90
+
91 91
     /**
92 92
      * Get the URI for this ServerInfo.
93 93
      * This will return a new URI based on this ServerInfo.
@@ -134,7 +134,7 @@ public class ServerInfo {
134 134
             }
135 135
         }
136 136
     }
137
-    
137
+
138 138
     /**
139 139
      * Set the URI for this ServerInfo.
140 140
      * This will overwrite host/port/password and isSSL.
@@ -159,91 +159,91 @@ public class ServerInfo {
159 159
      * @param newValue Value to set to.
160 160
      */
161 161
     public void setHost(final String newValue) { host = newValue; }
162
-    
162
+
163 163
     /**
164 164
      * Get the hostname.
165 165
      *
166 166
      * @return Current hostname
167 167
      */
168 168
     public String getHost() { return host; }
169
-    
169
+
170 170
     /**
171 171
      * Set the port.
172 172
      *
173 173
      * @param newValue Value to set to.
174 174
      */
175 175
     public void setPort(final int newValue) { port = newValue; }
176
-    
176
+
177 177
     /**
178 178
      * Get the port.
179 179
      *
180 180
      * @return Current port
181 181
      */
182 182
     public int getPort() { return port; }
183
-    
183
+
184 184
     /**
185 185
      * Set the password.
186 186
      *
187 187
      * @param newValue Value to set to.
188 188
      */
189 189
     public void setPassword(final String newValue) { password = newValue; }
190
-    
190
+
191 191
     /**
192 192
      * Get the password.
193 193
      *
194 194
      * @return Current Password
195 195
      */
196 196
     public String getPassword() { return password; }
197
-    
197
+
198 198
     /**
199 199
      * Set if the server uses ssl.
200 200
      *
201 201
      * @param newValue true if server uses ssl, else false
202 202
      */
203 203
     public void setSSL(final boolean newValue) { isSSL = newValue; }
204
-    
204
+
205 205
     /**
206 206
      * Get if the server uses ssl.
207 207
      *
208 208
      * @return true if server uses ssl, else false
209 209
      */
210 210
     public boolean getSSL() { return isSSL; }
211
-    
211
+
212 212
     /**
213 213
      * Set if we are connecting via a socks proxy.
214 214
      *
215 215
      * @param newValue true if we are using socks, else false
216 216
      */
217 217
     public void setUseSocks(final boolean newValue) { useSocksProxy = newValue; }
218
-    
218
+
219 219
     /**
220 220
      * Get if we are connecting via a socks proxy.
221 221
      *
222 222
      * @return true if we are using socks, else false
223 223
      */
224 224
     public boolean getUseSocks() { return useSocksProxy; }
225
-    
225
+
226 226
     /**
227 227
      * Set the Proxy hostname.
228 228
      *
229 229
      * @param newValue Value to set to.
230 230
      */
231 231
     public void setProxyHost(final String newValue) { proxyHost = newValue; }
232
-    
232
+
233 233
     /**
234 234
      * Get the Proxy hostname.
235 235
      *
236 236
      * @return Current Proxy hostname
237 237
      */
238 238
     public String getProxyHost() { return proxyHost; }
239
-    
239
+
240 240
     /**
241 241
      * Set the Proxy port.
242 242
      *
243 243
      * @param newValue Value to set to.
244 244
      */
245 245
     public void setProxyPort(final int newValue) { proxyPort = newValue; }
246
-    
246
+
247 247
     /**
248 248
      * Get the Proxy port.
249 249
      *
@@ -257,26 +257,46 @@ public class ServerInfo {
257 257
      * @param newValue Value to set to.
258 258
      */
259 259
     public void setProxyUser(final String newValue) { proxyUser = newValue; }
260
-    
260
+
261 261
     /**
262 262
      * Get the Proxy username.
263 263
      *
264 264
      * @return Current Proxy username
265 265
      */
266 266
     public String getProxyUser() { return proxyUser; }
267
-    
267
+
268 268
     /**
269 269
      * Set the Proxy password.
270 270
      *
271 271
      * @param newValue Value to set to.
272 272
      */
273 273
     public void setProxyPass(final String newValue) { proxyPass = newValue; }
274
-    
274
+
275 275
     /**
276 276
      * Get the Proxy password.
277 277
      *
278 278
      * @return Current Proxy password
279 279
      */
280 280
     public String getProxyPass() { return proxyPass; }
281
+
282
+    /**
283
+     * Retrieves a String describing any channels included in this ServerInfo's
284
+     * URI.
285
+     *
286
+     * @return A channel string for this URI, or null if no URI specified
287
+     * @since 0.6.3
288
+     */
289
+    public String getChannels() {
290
+        if (uri == null) {
291
+            return null;
292
+        }
293
+
294
+        String channelString = uri.getPath();
295
+        if (uri.getRawQuery() != null && !uri.getRawQuery().isEmpty()) { channelString += "?" + uri.getRawQuery(); }
296
+        if (uri.getRawFragment() != null && !uri.getRawFragment().isEmpty()) { channelString += "#" + uri.getRawFragment(); }
297
+        if (channelString.startsWith("/")) { channelString = channelString.substring(1); }
298
+
299
+        return channelString;
300
+    }
281 301
 }
282 302
 

Loading…
Cancel
Save