001 /**
002 * Jetrix TetriNET Server
003 * Copyright (C) 2001-2004 Emmanuel Bourg
004 *
005 * This program is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU General Public License
007 * as published by the Free Software Foundation; either version 2
008 * of the License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013 * GNU General Public License for more details.
014 *
015 * You should have received a copy of the GNU General Public License
016 * along with this program; if not, write to the Free Software
017 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018 */
019
020 package net.jetrix.protocols;
021
022 import java.util.ArrayList;
023 import java.util.List;
024 import java.util.StringTokenizer;
025
026 /**
027 * An IRC message.
028 *
029 * @since 0.2
030 *
031 * @author Emmanuel Bourg
032 * @version $Revision: 794 $, $Date: 2009-02-17 20:08:39 +0100 (Tue, 17 Feb 2009) $
033 */
034 public class IRCMessage
035 {
036 private String nick;
037 private String user;
038 private String host;
039 private String command;
040 private int reply;
041 private List<String> parameters = new ArrayList<String>();
042
043 public IRCMessage() { }
044
045 public IRCMessage(String command)
046 {
047 this.command = command;
048 }
049
050 public IRCMessage(int reply)
051 {
052 this.reply = reply;
053 }
054
055 /**
056 * Parse the specified line into an IRC message.
057 */
058 public static IRCMessage parse(String line)
059 {
060 IRCMessage message = new IRCMessage();
061
062 StringTokenizer st = new StringTokenizer(line);
063
064 if (!st.hasMoreTokens())
065 {
066 return message;
067 }
068
069 // get the first token
070 String token = st.nextToken();
071
072 // is this a prefix ?
073 if (token.startsWith(":"))
074 {
075 message.setPrefix(token.substring(1));
076 token = st.nextToken();
077 }
078
079 // is this a numeric reply ?
080 try
081 {
082 message.setReply(Integer.parseInt(token));
083 }
084 catch (Exception e)
085 {
086 message.setCommand(token);
087 }
088
089 // parameters
090 boolean completed = false;
091 while (st.hasMoreTokens() && !completed)
092 {
093 token = st.nextToken();
094
095 if (token.startsWith(":"))
096 {
097 message.addParameter(line.substring(line.indexOf(":", 1) + 1));
098 completed = true;
099 }
100 else
101 {
102 message.addParameter(token);
103 }
104 }
105
106 return message;
107 }
108
109 public String getNick()
110 {
111 return nick;
112 }
113
114 public void setNick(String nick)
115 {
116 this.nick = nick;
117 }
118
119 public String getUser()
120 {
121 return user;
122 }
123
124 public void setUser(String user)
125 {
126 this.user = user;
127 }
128
129 public String getHost()
130 {
131 return host;
132 }
133
134 public void setHost(String host)
135 {
136 this.host = host;
137 }
138
139 public String getPrefix()
140 {
141 StringBuilder prefix = new StringBuilder();
142 prefix.append(nick);
143
144 if (user != null)
145 {
146 prefix.append("!").append(user);
147 }
148
149 if (host != null)
150 {
151 prefix.append("@").append(host);
152 }
153
154 return prefix.toString();
155 }
156
157 public void setPrefix(String prefix)
158 {
159 if (prefix != null)
160 {
161 int separator1 = prefix.indexOf("!");
162 int separator2 = prefix.indexOf("@");
163
164 if (separator1 == -1 && separator2 == -1)
165 {
166 // missing user & host
167 nick = prefix;
168 }
169 else if (separator2 == -1)
170 {
171 // missing host
172 nick = prefix.substring(0, separator1);
173 user = prefix.substring(separator1 + 1);
174 }
175 else if (separator1 == -1)
176 {
177 // missing user
178 nick = prefix.substring(0, separator2);
179 host = prefix.substring(separator2 + 1);
180 }
181 else
182 {
183 nick = prefix.substring(0, separator1);
184 user = prefix.substring(separator1 + 1, separator2);
185 host = prefix.substring(separator2 + 1);
186 }
187 }
188 }
189
190 public String getCommand()
191 {
192 return command;
193 }
194
195 public void setCommand(String command)
196 {
197 this.command = command;
198 }
199
200 public boolean isCommand(String command)
201 {
202 return command.equals(this.command);
203 }
204
205 public int getReply()
206 {
207 return reply;
208 }
209
210 public void setReply(int reply)
211 {
212 this.reply = reply;
213 }
214
215 public List<String> getParameters()
216 {
217 return parameters;
218 }
219
220 public void setParameters(List<String> parameters)
221 {
222 this.parameters = parameters;
223 }
224
225 public String getParameter(int i)
226 {
227 return parameters.get(i);
228 }
229
230 public void addParameter(String param)
231 {
232 parameters.add(param);
233 }
234
235 public int getParameterCount()
236 {
237 return parameters.size();
238 }
239
240 /**
241 * Return the string representation of this message. The resulting string
242 * complies with the IRC specification and can be used directly in the
243 * communication between servers and clients.
244 */
245 public String toString()
246 {
247 StringBuilder buffer = new StringBuilder();
248
249 // prefix
250 if (nick != null)
251 {
252 buffer.append(":").append(getPrefix());
253 buffer.append(" ");
254 }
255
256 // command
257 if (command != null)
258 {
259 buffer.append(command);
260 }
261 else
262 {
263 buffer.append(reply);
264 }
265
266 // parameters
267 for (int i = 0; i < parameters.size() - 1; i++)
268 {
269 buffer.append(" ").append(parameters.get(i));
270 }
271
272 // add the last parameter
273 if (parameters.size() > 0)
274 {
275 buffer.append(" ");
276
277 String lastParam = String.valueOf(parameters.get(parameters.size() - 1));
278 if (lastParam.indexOf(" ") != -1 || lastParam.startsWith(":"))
279 {
280 buffer.append(":");
281 }
282 buffer.append(lastParam);
283 }
284
285 return buffer.toString();
286 }
287 }