1 /*****************************************************/
\r
2 /* This java file is a part of the */
\r
4 /* - Plouf's Java IRC Client - */
\r
6 /* Copyright (C) 2002 - 2004 Philippe Detournay */
\r
8 /* All contacts : theplouf@yahoo.com */
\r
10 /* PJIRC is free software; you can redistribute */
\r
11 /* it and/or modify it under the terms of the GNU */
\r
12 /* General Public License as published by the */
\r
13 /* Free Software Foundation; version 2 or later of */
\r
16 /* PJIRC is distributed in the hope that it will */
\r
17 /* be useful, but WITHOUT ANY WARRANTY; without */
\r
18 /* even the implied warranty of MERCHANTABILITY or */
\r
19 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
\r
20 /* General Public License for more details. */
\r
22 /* You should have received a copy of the GNU */
\r
23 /* General Public License along with PJIRC; if */
\r
24 /* not, write to the Free Software Foundation, */
\r
25 /* Inc., 59 Temple Place, Suite 330, Boston, */
\r
26 /* MA 02111-1307 USA */
\r
28 /*****************************************************/
\r
32 import java.util.Vector;
\r
37 public class ModeHandler {
\r
38 private String _mode;
\r
39 private Vector _parameters;
\r
40 private char[][] _modes;
\r
41 private char[] _prefix;
\r
44 * Create a new ModeHandler with empty mode.
\r
47 * A,B,C,D known modes. This is an array of four arrays of chars.
\r
51 public ModeHandler(char[][] modes, char[] prefix) {
\r
52 this("", modes, prefix);
\r
56 * Create a new ModeHandler with given mode.
\r
61 * A,B,C,D know modes.
\r
65 public ModeHandler(String mode, char[][] modes, char[] prefix) {
\r
69 _parameters = new Vector();
\r
70 if (mode.startsWith("+"))
\r
71 mode = mode.substring(1);
\r
76 * Reset current mode.
\r
78 public void reset() {
\r
83 * Get password (+k), if defined.
\r
87 public String getPassword() {
\r
89 return findParameter('k');
\r
94 * Get user limit (+l), if defined.
\r
96 * @return the user limit.
\r
98 public int getLimit() {
\r
100 return new Integer(findParameter('l')).intValue();
\r
104 private boolean inside(char[] list, char c) {
\r
105 for (int i = 0; i < list.length; i++)
\r
111 private String findParameter(char k) {
\r
112 int index = getParameterIndex(k);
\r
114 return (String) _parameters.elementAt(index);
\r
118 private boolean hasParameter(boolean positive, char c) {
\r
119 if (inside(_prefix, c))
\r
121 else if (inside(_modes[0], c))
\r
123 else if (inside(_modes[1], c))
\r
125 else if (inside(_modes[2], c))
\r
131 private int getParameterIndex(char k) {
\r
132 if (_parameters.size() == 0)
\r
135 for (int i = 0; i < _mode.length(); i++) {
\r
136 char c = _mode.charAt(i);
\r
137 if (hasParameter(true, c)) {
\r
141 if (index >= _parameters.size())
\r
151 private void addMode(char mode, String param) {
\r
153 removeMode(mode, param);
\r
155 if (hasParameter(true, mode))
\r
156 _parameters.insertElementAt(param, _parameters.size());
\r
159 private void removeMode(char mode, String param) {
\r
160 if (!hasMode(mode))
\r
162 if (hasParameter(true, mode))
\r
163 _parameters.removeElementAt(getParameterIndex(mode));
\r
164 int pos = _mode.indexOf(mode);
\r
165 _mode = _mode.substring(0, pos) + _mode.substring(pos + 1);
\r
168 private void applyOne(boolean positive, char mode, String param) {
\r
170 addMode(mode, param);
\r
172 removeMode(mode, param);
\r
176 * Apply a new mode on the current mode.
\r
179 * new mode to apply.
\r
181 public void apply(String mode) {
\r
182 String[] params = (new StringParser()).parseString(mode);
\r
183 boolean positive = true;
\r
186 for (int i = 0; i < params[0].length(); i++) {
\r
187 char c = params[0].charAt(i);
\r
190 } else if (c == '-') {
\r
194 if (hasParameter(positive, c)) {
\r
196 if (index >= params.length)
\r
198 param = params[index];
\r
200 applyOne(positive, c, param);
\r
206 * Test wether the given mode is set.
\r
210 * @return true if mode is set, false otherwise.
\r
212 public boolean hasMode(char mode) {
\r
213 return _mode.indexOf(mode) != -1;
\r
217 * Get a string representation of the current mode.
\r
219 * @return string representation of current mode.
\r
221 public String getMode() {
\r
222 String ans = _mode;
\r
223 for (int i = 0; i < _parameters.size(); i++)
\r
224 ans += " " + _parameters.elementAt(i);
\r