View Javadoc

1   /***
2    * Jetrix TetriNET Server
3    * Copyright (C) 2001-2004  Emmanuel Bourg
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  
20  package net.jetrix.protocols;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.StringTokenizer;
25  
26  /***
27   * An IRC message.
28   *
29   * @since 0.2
30   *
31   * @author Emmanuel Bourg
32   * @version $Revision: 794 $, $Date: 2009-02-17 20:08:39 +0100 (Tue, 17 Feb 2009) $
33   */
34  public class IRCMessage
35  {
36      private String nick;
37      private String user;
38      private String host;
39      private String command;
40      private int reply;
41      private List<String> parameters = new ArrayList<String>();
42  
43      public IRCMessage() { }
44  
45      public IRCMessage(String command)
46      {
47          this.command = command;
48      }
49  
50      public IRCMessage(int reply)
51      {
52          this.reply = reply;
53      }
54  
55      /***
56       * Parse the specified line into an IRC message.
57       */
58      public static IRCMessage parse(String line)
59      {
60          IRCMessage message = new IRCMessage();
61  
62          StringTokenizer st = new StringTokenizer(line);
63  
64          if (!st.hasMoreTokens())
65          {
66              return message;
67          }
68  
69          // get the first token
70          String token = st.nextToken();
71  
72          // is this a prefix ?
73          if (token.startsWith(":"))
74          {
75              message.setPrefix(token.substring(1));
76              token = st.nextToken();
77          }
78  
79          // is this a numeric reply ?
80          try
81          {
82              message.setReply(Integer.parseInt(token));
83          }
84          catch (Exception e)
85          {
86              message.setCommand(token);
87          }
88  
89          // parameters
90          boolean completed = false;
91          while (st.hasMoreTokens() && !completed)
92          {
93              token = st.nextToken();
94  
95              if (token.startsWith(":"))
96              {
97                  message.addParameter(line.substring(line.indexOf(":", 1) + 1));
98                  completed = true;
99              }
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 }