File Source: IrcAddress.java
/*
P/P * Method: com.dmdirc.util.IrcAddress__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 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.util;
24
25 import com.dmdirc.Server;
26 import com.dmdirc.ServerManager;
27 import com.dmdirc.config.Identity;
28 import com.dmdirc.config.IdentityManager;
29
30 import java.io.Serializable;
31 import java.net.URI;
32 import java.net.URISyntaxException;
33 import java.util.ArrayList;
34 import java.util.List;
35
36 /**
37 * Parses an IRC address. IRC addresses take the following form:
38 * irc[s]://[[username][:password]@]<server>[:[+]port][/channel1[,channel2[,...]]]
39 *
40 * @author Chris
41 */
42 public class IrcAddress implements Serializable {
43
44 /**
45 * A version number for this class. It should be changed whenever the class
46 * structure is changed (or anything else that would prevent serialized
47 * objects being unserialized with the new class).
48 */
49 private final static long serialVersionUID = 1;
50
51 /** Whether or not this address uses SSL. */
52 private boolean usesSSL;
53 /** The server name for this address. */
54 private String server;
55 /** The port number for this address. */
56 private int port = 6667;
57 /** A list of channels to auto-connect to. */
58 private List<String> channels = new ArrayList<String>();
59 /** The password for this address. */
60 private String pass = "";
61
62 /**
63 * Creates a new instance of IrcAddress.
64 *
65 * @param address The address to parse
66 * @throws InvalidAddressException If an invalid address is passed
67 */
/*
P/P * Method: void com.dmdirc.util.IrcAddress(String)
*
* Preconditions:
* address != null
*
* Presumptions:
* java.lang.String:equalsIgnoreCase(...)@86 == 1
* java.net.URI:getHost(...)@102 != null
* java.net.URI:getScheme(...)@84 != null
* java.net.URI:getScheme(...)@86 != null
*
* Postconditions:
* this.channels == &new ArrayList(IrcAddress#1)
* init'ed(this.pass)
* init'ed(this.port)
* init'ed(this.server)
* possibly_updated(this.usesSSL)
* new ArrayList(IrcAddress#1) num objects == 1
*
* Test Vectors:
* java.lang.String:equalsIgnoreCase(...)@84: {0}, {1}
* java.net.URI:getPort(...)@98: {-231..-1}, {0..232-1}
* java.net.URI:getScheme(...)@84: Addr_Set{null}, Inverse{null}
* java.net.URI:getUserInfo(...)@90: Addr_Set{null}, Inverse{null}
*/
68 public IrcAddress(final String address) throws InvalidAddressException {
69 URI uri;
70 String myAddress;
71
72 // Check for +ports (SSL)
73 myAddress = address.replaceFirst(":\\+([0-9]+)", ":$1");
74 if (myAddress.length() < address.length()) {
75 usesSSL = true;
76 }
77
78 try {
79 uri = new URI(myAddress);
80 } catch (URISyntaxException ex) {
81 throw new InvalidAddressException("Unable to parse URI", ex);
82 }
83
84 if (uri.getScheme() != null && uri.getScheme().equalsIgnoreCase("ircs")) {
85 usesSSL = true;
86 } else if (uri.getScheme() == null || !uri.getScheme().equalsIgnoreCase("irc")) {
87 throw new InvalidAddressException("Invalid protocol specified");
88 }
89
90 if (uri.getUserInfo() != null) {
91 doPass(uri.getUserInfo());
92 }
93
94 doChannels(uri.getPath() + (uri.getQuery() == null ? "" :
95 "?" + uri.getQuery()) + (uri.getFragment() == null ? "" :
96 "#" + uri.getFragment()));
97
98 if (uri.getPort() > -1) {
99 doPort(uri.getPort());
100 }
101
102 if (uri.getHost() == null) {
103 throw new InvalidAddressException("Invalid host or port specified");
104 } else {
105 doServer(uri.getHost());
106 }
107 }
108
109 /**
110 * Processes the password part of this address.
111 *
112 * @param pass The password part of this address
113 */
114 private void doPass(final String pass) {
/*
P/P * Method: void doPass(String)
*
* Postconditions:
* this.pass == pass
* init'ed(this.pass)
*/
115 this.pass = pass;
116 }
117
118 /**
119 * Processes the channels part of this address.
120 *
121 * @param channels The channels part of this address
122 */
123 private void doChannels(final String channels) {
/*
P/P * Method: void doChannels(String)
*
* Test Vectors:
* channels: Addr_Set{null}, Inverse{null}
* java.lang.String:charAt(...)@124: {47}, {0..46, 48..216-1}
* java.lang.String:length(...)@124: {0}, {1..232-1}
*/
124 if (channels == null || channels.length() == 0 || channels.charAt(0) != '/') {
125 return;
126 }
127
128 for (String channel : channels.substring(1).split(",")) {
129 if (!channel.equalsIgnoreCase("needpass") &&
130 !channel.equalsIgnoreCase("needkey") &&
131 !channel.equalsIgnoreCase("isnick") && !channel.isEmpty()) {
132 this.channels.add(channel);
133 }
134 }
135 }
136
137 /**
138 * Processes the port part of this address.
139 *
140 * @param port The port part of this address
141 * @throws InvalidAddressException if the port is non-numeric
142 */
143 private void doPort(final int port) throws InvalidAddressException {
/*
P/P * Method: void doPort(int)
*
* Postconditions:
* this.port == port
* init'ed(this.port)
*/
144 this.port = port;
145 }
146
147 /**
148 * Processes the server part of this address.
149 *
150 * @param server The server part of this address
151 */
152 private void doServer(final String server) {
/*
P/P * Method: void doServer(String)
*
* Postconditions:
* this.server == server
* init'ed(this.server)
*/
153 this.server = server;
154 }
155
156 /**
157 * Determines if this address requires the use of SSL or not.
158 *
159 * @return True if the address requires SSL, false otherwise
160 */
161 public boolean isSSL() {
/*
P/P * Method: bool isSSL()
*
* Preconditions:
* init'ed(this.usesSSL)
*
* Postconditions:
* return_value == this.usesSSL
* init'ed(return_value)
*/
162 return usesSSL;
163 }
164
165 /**
166 * Retrieves the server from this address.
167 *
168 * @return This address's server
169 */
170 public String getServer() {
/*
P/P * Method: String getServer()
*
* Preconditions:
* init'ed(this.server)
*
* Postconditions:
* return_value == this.server
* init'ed(return_value)
*/
171 return server;
172 }
173
174 /**
175 * Retrieves the port used for this address.
176 *
177 * @return This address's port
178 */
179 public int getPort() {
/*
P/P * Method: int getPort()
*
* Preconditions:
* init'ed(this.port)
*
* Postconditions:
* return_value == this.port
* init'ed(return_value)
*/
180 return port;
181 }
182
183 /**
184 * Retrieves the password used for this address.
185 *
186 * @return This address's password
187 */
188 public String getPassword() {
/*
P/P * Method: String getPassword()
*
* Preconditions:
* init'ed(this.pass)
*
* Postconditions:
* return_value == this.pass
* init'ed(return_value)
*/
189 return pass;
190 }
191
192 /**
193 * Retrieves the list of channels for this address.
194 *
195 * @return This address's channels
196 */
197 public List<String> getChannels() {
/*
P/P * Method: List getChannels()
*
* Preconditions:
* init'ed(this.channels)
*
* Postconditions:
* return_value == this.channels
* init'ed(return_value)
*/
198 return channels;
199 }
200
201 /**
202 * Connects to a server represented by this address.
203 */
204 public void connect() {
/*
P/P * Method: void connect()
*
* Preconditions:
* init'ed(this.channels)
* init'ed(this.server)
* (soft) init'ed(this.pass)
* (soft) init'ed(this.port)
* (soft) init'ed(this.usesSSL)
*
* Presumptions:
* com.dmdirc.config.IdentityManager:getProfiles(...)@205 != null
*/
205 connect(IdentityManager.getProfiles().get(0));
206 }
207
208 /**
209 * Connects to a server represented by this address.
210 *
211 * @param profile Profile to use when connecting
212 */
213 public void connect(final Identity profile) {
/*
P/P * Method: void connect(Identity)
*
* Preconditions:
* init'ed(this.channels)
* init'ed(this.server)
* (soft) init'ed(this.pass)
* (soft) init'ed(this.port)
* (soft) init'ed(this.usesSSL)
*
* Presumptions:
* com.dmdirc.ServerManager:getServerManager(...)@214 != null
* com.dmdirc.ServerManager:getServersByAddress(...)@214 != null
* java.util.ArrayList:iterator(...)@221 != null
* java.util.List:get(...)@220 != null
*
* Test Vectors:
* java.util.Iterator:hasNext(...)@221: {0}, {1}
* java.util.List:isEmpty(...)@216: {0}, {1}
*/
214 final List<Server> servers = ServerManager.getServerManager().
215 getServersByAddress(getServer());
216 if (servers.isEmpty()) {
217 new Server(getServer(), getPort(), getPassword(), isSSL(),
218 profile, getChannels());
219 } else {
220 final Server thisServer = servers.get(0);
221 for (String channel : new ArrayList<String>(getChannels())) {
222 thisServer.join(channel);
223 }
224 }
225 }
226 }
SofCheck Inspector Build Version : 2.17854
| IrcAddress.java |
2009-Jun-25 01:54:24 |
| IrcAddress.class |
2009-Sep-02 17:04:15 |