1 /* $Id$ */
2 /***************************************************************************
3  *                   (C) Copyright 2003-2010 - Stendhal                    *
4  ***************************************************************************
5  ***************************************************************************
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or modify  *
8  *   it under the terms of the GNU General Public License as published by  *
9  *   the Free Software Foundation; either version 2 of the License, or     *
10  *   (at your option) any later version.                                   *
11  *                                                                         *
12  ***************************************************************************/
13 package games.stendhal.client.gui.login;
14 
15 /**
16  * User login profile.
17  */
18 public class Profile {
19 	/** Default server port. */
20 	static final int DEFAULT_SERVER_PORT = 32160;
21 
22 	/** Old server names to remap. */
23 	private static final String[] OLD_SERVER_HOSTS = { "stendhal.ath.cx", "stendhal.game-host.org" };
24 
25 	/** Default server name to replace old ones with. */
26 	private static final String NEW_SERVER_HOST = "stendhalgame.org";
27 
28 	private String host;
29 
30 	private int port;
31 
32 	private String user;
33 
34 	private String password;
35 
36 	private String character;
37 
38 	private String seed;
39 
Profile()40 	public Profile() {
41 		this("", DEFAULT_SERVER_PORT, "", "");
42 	}
43 
Profile(final String host, final int port, final String user, final String password)44 	private Profile(final String host, final int port, final String user, final String password) {
45 		this.host = host;
46 		this.port = port;
47 		this.user = user;
48 		this.password = password;
49 
50 	}
51 
52 	//
53 	// Profile
54 	//
55 
56 	/**
57 	 * Encode the login profile as a string.
58 	 *
59 	 * @return A string excoded form (with newlines).
60 	 */
encode()61 	String encode() {
62 		StringBuilder sbuf;
63 
64 		sbuf = new StringBuilder();
65 		sbuf.append(getHost());
66 		sbuf.append('\n');
67 		sbuf.append(getUser());
68 		sbuf.append('\n');
69 		sbuf.append(getPassword());
70 		sbuf.append('\n');
71 		sbuf.append(getPort());
72 		sbuf.append('\n');
73 
74 		// TCP
75 		sbuf.append(true);
76 
77 		return sbuf.toString();
78 	}
79 
getHost()80 	public String getHost() {
81 		return host;
82 	}
83 
getPassword()84 	public String getPassword() {
85 		return password;
86 	}
87 
getPort()88 	public int getPort() {
89 		return port;
90 	}
91 
getUser()92 	public String getUser() {
93 		return user;
94 	}
95 
getCharacter()96 	public String getCharacter() {
97 		return character;
98 	}
99 
getSeed()100 	public String getSeed() {
101 		return seed;
102 	}
103 
104 	/**
105 	 * Decode a login profile from a string.
106 	 *
107 	 * @param info
108 	 *            The string encoded profile.
109 	 *
110 	 * @return A login profile.
111 	 */
decode(final String info)112 	static Profile decode(final String info) {
113 		String[] params;
114 		Profile profile;
115 		String s;
116 
117 		params = info.split("\n");
118 
119 		profile = new Profile();
120 
121 		/*
122 		 * Server Host
123 		 */
124 		if (params.length > 0) {
125 			s = params[0];
126 
127 			for (final String host : OLD_SERVER_HOSTS) {
128 				if (s.equals(host)) {
129 					s = NEW_SERVER_HOST;
130 					break;
131 				}
132 			}
133 
134 			if (s.length() != 0) {
135 				profile.setHost(s);
136 			}
137 		}
138 
139 		/*
140 		 * User
141 		 */
142 		if (params.length > 1) {
143 			s = params[1];
144 
145 			if (s.length() != 0) {
146 				profile.setUser(s);
147 			}
148 		}
149 
150 		/*
151 		 * Password
152 		 */
153 		if (params.length > 2) {
154 			s = params[2];
155 
156 			if (s.length() != 0) {
157 				profile.setPassword(s);
158 			}
159 		}
160 
161 		/*
162 		 * Server Port
163 		 */
164 		if (params.length > 3) {
165 			s = params[3];
166 
167 			if (s.length() != 0) {
168 				try {
169 					profile.setPort(Integer.parseInt(s));
170 				} catch (final NumberFormatException ex) {
171 					// use default port if port is not a number number
172 				}
173 			}
174 		}
175 
176 		/*
177 		 * Server Protocol (TCP/UDP)
178 		 */
179 		// params[4]
180 		//
181 		// ignore this token for compatibility reasons
182 		// just add what every you want behind it
183 		return profile;
184 	}
185 
186 	/**
187 	 * create a profile based on command line arguments
188 	 * <ul>
189 	 * <li>-u: username</li>
190 	 * <li>-p: password</li>
191 	 * <li>-c: character name (defaults to username)</li>
192 	 * <li>-h: hostname</li>
193 	 * <li>-P: port</li>
194 	 * <li>-S: pre authentication seed</li>
195 	 * </ul>
196 	 *
197 	 * @param args command line arguments
198 	 * @return profile
199 	 */
createFromCommandline(String[] args)200 	public static Profile createFromCommandline(String[] args) {
201 		Profile profile = new Profile();
202 		int i = 0;
203 		while (i != args.length) {
204 			if (args[i].equals("-u")) {
205 				profile.setUser(args[i + 1]);
206 			} else if (args[i].equals("-p")) {
207 				profile.setPassword(args[i + 1]);
208 			} else if (args[i].equals("-c")) {
209 				profile.setCharacter(args[i + 1]);
210 			} else if (args[i].equals("-h")) {
211 				profile.setHost(args[i + 1]);
212 			} else if (args[i].equals("-P")) {
213 				profile.setPort(Integer.parseInt(args[i + 1]));
214 			} else if (args[i].equals("-S")) {
215 				profile.setSeed(args[i + 1]);
216 			}
217 			i++;
218 		}
219 		if (profile.getCharacter() == null) {
220 			profile.setCharacter(profile.getUser());
221 		}
222 		return profile;
223 	}
224 
225 	/**
226 	 * checks whether all required attributes are defined
227 	 *
228 	 * @return true if all required attributes are defined, false if some are missing.
229 	 */
isValid()230 	public boolean isValid() {
231 		return (host != null) && (user != null) && (password != null)
232 			&& !host.equals("") && !user.equals("") && !password.equals("");
233 	}
234 
setHost(final String host)235 	public void setHost(final String host) {
236 		this.host = host;
237 	}
238 
setPassword(final String password)239 	public void setPassword(final String password) {
240 		this.password = password;
241 	}
242 
setPort(final int port)243 	public void setPort(final int port) {
244 		this.port = port;
245 	}
246 
setUser(final String user)247 	public void setUser(final String user) {
248 		this.user = user;
249 	}
250 
251 
setCharacter(String character)252 	public void setCharacter(String character) {
253 		this.character = character;
254 	}
255 
256 
setSeed(String seed)257 	public void setSeed(String seed) {
258 		this.seed = seed;
259 	}
260 
261 	@Override
equals(final Object obj)262 	public boolean equals(final Object obj) {
263 		if (!(obj instanceof Profile)) {
264 			return false;
265 		}
266 
267 		final Profile profile = (Profile) obj;
268 
269 		if (!getHost().equals(profile.getHost())) {
270 			return false;
271 		}
272 
273 		if (getPort() != profile.getPort()) {
274 			return false;
275 		}
276 
277 		if (!getUser().equals(profile.getUser())) {
278 			return false;
279 		}
280 
281 		return true;
282 	}
283 
284 	@Override
hashCode()285 	public int hashCode() {
286 		return getHost().hashCode() ^ getUser().hashCode();
287 	}
288 
289 	/**
290 	 * Get the label string. This label is used for the profile selection list.
291 	 *
292 	 * @return The label in the form of <em>user</em><strong>@</strong><em>server-host</em>[<strong>:</strong><em>port</em>].
293 	 */
294 	@Override
toString()295 	public String toString() {
296 		StringBuilder sbuf;
297 
298 		sbuf = new StringBuilder();
299 		sbuf.append(getUser());
300 		if (getCharacter() != null) {
301 			sbuf.append("/");
302 			sbuf.append(getCharacter());
303 		}
304 		sbuf.append('@');
305 		sbuf.append(getHost());
306 
307 		if (getPort() != DEFAULT_SERVER_PORT) {
308 			sbuf.append(':');
309 			sbuf.append(getPort());
310 		}
311 
312 		return sbuf.toString();
313 	}
314 
315 }
316