1 /***
2 * Jetrix TetriNET Server
3 * Copyright (C) 2001-2003 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.messages.channel;
21
22 import java.util.*;
23
24 import net.jetrix.*;
25
26 /***
27 * A /command.
28 *
29 * @author Emmanuel Bourg
30 * @version $Revision: 798 $, $Date: 2009-02-18 16:24:28 +0100 (Wed, 18 Feb 2009) $
31 */
32 public class CommandMessage extends TextMessage
33 {
34 private String command;
35 private List<String> parameters;
36
37 public CommandMessage()
38 {
39 parameters = new ArrayList<String>();
40 }
41
42 public String getCommand()
43 {
44 return command;
45 }
46
47 public void setCommand(String command)
48 {
49 this.command = command;
50 }
51
52 public String getParameter(int i)
53 {
54 return parameters.get(i);
55 }
56
57 /***
58 * Return an integer parameter, or the default value if the specified
59 * parameter doesn't map to an integer value.
60 *
61 * @param i the index of the parameter
62 * @param defaultValue the default value
63 */
64 public int getIntParameter(int i, int defaultValue)
65 {
66 int value;
67
68 try
69 {
70 value = Integer.parseInt(parameters.get(i));
71 }
72 catch (Exception e)
73 {
74 value = defaultValue;
75 }
76
77 return value;
78 }
79
80 /***
81 * Return an integer parameter, or null if the specified parameter
82 * doesn't map to an integer value.
83 *
84 * @param i the index of the parameter
85 */
86 public Integer getIntegerParameter(int i)
87 {
88 Integer value;
89
90 try
91 {
92 value = Integer.valueOf(parameters.get(i));
93 }
94 catch (Exception e)
95 {
96 value = null;
97 }
98
99 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
114 try
115 {
116 int slot = Integer.parseInt(param);
117 if (slot >= 1 && slot <= 6)
118 {
119
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
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
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 }