001 /**
002 * Jetrix TetriNET Server
003 * Copyright (C) 2001-2003 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.messages.channel;
021
022 import java.util.*;
023
024 import net.jetrix.*;
025
026 /**
027 * A /command.
028 *
029 * @author Emmanuel Bourg
030 * @version $Revision: 798 $, $Date: 2009-02-18 16:24:28 +0100 (Wed, 18 Feb 2009) $
031 */
032 public class CommandMessage extends TextMessage
033 {
034 private String command;
035 private List<String> parameters;
036
037 public CommandMessage()
038 {
039 parameters = new ArrayList<String>();
040 }
041
042 public String getCommand()
043 {
044 return command;
045 }
046
047 public void setCommand(String command)
048 {
049 this.command = command;
050 }
051
052 public String getParameter(int i)
053 {
054 return parameters.get(i);
055 }
056
057 /**
058 * Return an integer parameter, or the default value if the specified
059 * parameter doesn't map to an integer value.
060 *
061 * @param i the index of the parameter
062 * @param defaultValue the default value
063 */
064 public int getIntParameter(int i, int defaultValue)
065 {
066 int value;
067
068 try
069 {
070 value = Integer.parseInt(parameters.get(i));
071 }
072 catch (Exception e)
073 {
074 value = defaultValue;
075 }
076
077 return value;
078 }
079
080 /**
081 * Return an integer parameter, or null if the specified parameter
082 * doesn't map to an integer value.
083 *
084 * @param i the index of the parameter
085 */
086 public Integer getIntegerParameter(int i)
087 {
088 Integer value;
089
090 try
091 {
092 value = Integer.valueOf(parameters.get(i));
093 }
094 catch (Exception e)
095 {
096 value = null;
097 }
098
099 return value;
100 }
101
102 /**
103 * Return the Client object associated to the i-th parameter of the command.
104 * The client can be specified as a slot number if he is in the same channel
105 * as the client issuing the command, or as a case insensitive name. If no
106 * client matches the specified parameter a null value is returned.
107 */
108 public Client getClientParameter(int i)
109 {
110 Client client = null;
111 String param = getParameter(i);
112
113 // check if the parameter is a slot number
114 try
115 {
116 int slot = Integer.parseInt(param);
117 if (slot >= 1 && slot <= 6)
118 {
119 // find the channel of the client issuing this command
120 if (getSource() instanceof Client)
121 {
122 Channel channel = ((Client) getSource()).getChannel();
123 client = channel.getClient(slot);
124 }
125 }
126 }
127 catch (NumberFormatException e) { }
128
129 if (client == null)
130 {
131 // the client is still null, the parameter may be a playername
132 ClientRepository repository = ClientRepository.getInstance();
133 client = repository.getClient(param);
134 }
135
136 return client;
137 }
138
139 /**
140 * Returns the channel associated to the i-th parameter of the command. The
141 * channel can be specified by a partial name or by its number. If no channel
142 * matches the specified parameter a null value is returned.
143 */
144 public Channel getChannelParameter(int i)
145 {
146 Channel channel = null;
147 String param = getParameter(i);
148
149 try
150 {
151 // trying to parse the number
152 int num = Integer.parseInt(param) - 1;
153 channel = ChannelManager.getInstance().getChannel(num);
154 }
155 catch (NumberFormatException e)
156 {
157 channel = ChannelManager.getInstance().getChannel(param, true);
158 }
159
160 return channel;
161 }
162
163 /**
164 * Add a parameter to the command.
165 */
166 public void addParameter(String obj)
167 {
168 parameters.add(obj);
169 }
170
171 /**
172 * Return the number of parameters on this command.
173 */
174 public int getParameterCount()
175 {
176 return parameters.size();
177 }
178 }