KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > server > ChannelManager


1 /*- ChannelManager.java -------------------------------------------+
2  | |
3  | Copyright (C) 2002-2003 Joseph Monti, LlamaChat |
4  | countjoe@users.sourceforge.net |
5  | http://www.42llamas.com/LlamaChat/ |
6  | |
7  | This program is free software; you can redistribute it and/or |
8  | modify it under the terms of the GNU General Public License |
9  | as published by the Free Software Foundation; either version 2 |
10  | of the License, or (at your option) any later version |
11  | |
12  | This program is distributed in the hope that it will be useful, |
13  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15  | GNU General Public License for more details. |
16  | |
17  | A copy of the GNU General Public License may be found in the |
18  | installation directory named "GNUGPL.txt" |
19  | |
20  +-----------------------------------------------------------------+
21  */

22
23 package server;
24
25 import java.util.Vector JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Enumeration JavaDoc;
28
29 /* -------------------- JavaDoc Information ----------------------*/
30 /**
31  * This class manages the channels for the server. It allows 3 types
32  * of channels. The first of which, defaultChannel, is limited to
33  * one and is the default channel that users are sent to when
34  * connecting. The second, systemChannels, is a collection of channels
35  * that are perminent and created upon program initialization from
36  * the configuration file. The third, userChannels, are created by users
37  * and can only exist if specified in the system configuration file. When
38  * a userChannel is empty it is deleted.
39  * @author Joseph Monti <a HREF="mailto:countjoe@users.sourceforge.net">countjoe@users.sourceforge.net</a>
40  * @version 0.8
41  */

42 public final class ChannelManager {
43     public static char allowUserChannels;
44     public String JavaDoc defaultChannel;
45     private int defaultCount;
46     private static Hashtable JavaDoc systemChannels;
47     private static Hashtable JavaDoc userChannels;
48     public static final String JavaDoc eName = "Invalid Channel Name";
49     public static final String JavaDoc eAllowed = "User Channels not allowed";
50     public static final String JavaDoc eAdmin = "Admin only operation";
51
52     /**
53      * Constructor to setup variables
54      */

55     ChannelManager() {
56         allowUserChannels = 'y';
57         defaultChannel = "Lobby";
58         defaultCount = 0;
59         systemChannels = null;
60         userChannels = null;
61     }
62
63     /**
64      * sets the default channel for the server
65      * @param name the name of the channel
66      * @return true on success, false otherwise (if its not a valid channeL)
67      */

68     public boolean setDefaultChannel(String JavaDoc name) {
69         if (!name.equals(defaultChannel) && !isValidChannel(name)) {
70             return false;
71         }
72         defaultChannel = name;
73         return true;
74     }
75
76     /**
77      * creates a new system channel (if valid name)
78      * @param name the name of the channel
79          * @param pass the password for the channel, can be null if no
80          * password required
81      * @return true on success, false otherwise (if its not a valid channeL)
82      */

83     public boolean addSystemChannel(String JavaDoc name, String JavaDoc pass) {
84         if (!isValidChannel(name)) {
85             return false;
86         }
87         if (systemChannels == null) {
88             systemChannels = new Hashtable JavaDoc();
89         }
90         systemChannels.put(name, new ChannelManagerItem(pass));
91         return true;
92     }
93
94     /**
95      * creates a new user channel (if valid name and is allowed)
96      * @param name the name of the channel
97      * @param pass the password for the channel, can be null if no
98      * password required
99      * @param cc the client requesting the channel
100      * @return null on success, reason otherwise (if its not a valid channeL)
101      */

102     public String JavaDoc addUserChannel(String JavaDoc name, String JavaDoc pass, ClientConnection cc) {
103         if (allowUserChannels == 'n') {
104             return eAllowed;
105         }
106         if (allowUserChannels == 'a' && !cc.isAdmin()) {
107             return eAdmin;
108         }
109         if (!isValidChannel(name)) {
110             return eName;
111         }
112         if (userChannels == null) {
113             userChannels = new Hashtable JavaDoc();
114         }
115         userChannels.put(name, new ChannelManagerItem(pass));
116         return null;
117     }
118
119     /**
120      * adds a user to the specified channel
121      * @param name the name of the channel
122          * @param pass the password to use for the channel, can be null
123      * @return true on success, false otherwise (if its not a valid channel,
124          * or invalied passphrase)
125      */

126     public boolean userAdd(String JavaDoc name, String JavaDoc pass) {
127         if (defaultChannel.equals(name)) {
128             defaultCount++;
129             return true;
130         }
131
132         if (systemChannels != null && systemChannels.containsKey(name)) {
133            ChannelManagerItem value = (ChannelManagerItem) systemChannels.get(name);
134             if (value == null) {
135                 return false;
136             }
137            if (value.pass == null || value.pass.equals(pass)) {
138                 value.countpp();
139                 return true;
140             }
141             return false;
142         }
143         if (userChannels != null && userChannels.containsKey(name)) {
144             ChannelManagerItem value = (ChannelManagerItem) userChannels.get(name);
145             if (value == null) {
146                 return false;
147             }
148             if (value.pass == null || value.pass.equals(pass)) {
149                 value.countpp();
150                 return true;
151             }
152         }
153         return false;
154     }
155
156     /**
157      * removes a user from the specified channel, only used on user channels
158      * @param name the name of the channel
159      * @return true when the operation emptied the channel and the channel
160      * was removed, false if failed or did not empty channel
161      */

162     public boolean userDel(String JavaDoc name) {
163         if (defaultChannel.equals(name)) {
164             defaultCount--;
165             return false;
166         }
167
168         if (systemChannels != null && systemChannels.containsKey(name)) {
169             ChannelManagerItem value = (ChannelManagerItem) systemChannels.get(name);
170             if (value != null) {
171                 value.countmm();
172             }
173             return false;
174         }
175         if (userChannels != null && userChannels.containsKey(name)) {
176             ChannelManagerItem value = (ChannelManagerItem) userChannels.get(name);
177             if (value == null) {
178                 return false;
179             }
180             if (value.countmm()) {
181                 userChannels.remove(name);
182                 return true;
183             }
184         }
185         return false;
186     }
187
188     /**
189      * checks to see if the channel can be created
190      * @param name the name of the channel
191      * @return true if valid channel
192      */

193     public boolean isValidChannel(String JavaDoc name) {
194         if (defaultChannel.equals(name) ||
195                 (systemChannels != null && systemChannels.containsKey(name)) ||
196                 (userChannels != null && userChannels.containsKey(name)) ||
197                 name.length() > 12 || !name.matches("[\\w_-]+?")) {
198             return false;
199         }
200         return true;
201     }
202
203     /**
204      * checks to see if the channel exists
205      * @param name the name of the channel
206          * @return true if channel exists
207      */

208     public boolean channelExists(String JavaDoc name) {
209         if (defaultChannel.equals(name) ||
210                 (systemChannels != null && systemChannels.containsKey(name)) ||
211                 (userChannels != null && userChannels.containsKey(name))) {
212             return true;
213         }
214         return false;
215     }
216     
217     /**
218      * checks to see if channel has password
219      * @return "" if name has pass, null otherwise
220      */

221     public String JavaDoc channelHasPass(String JavaDoc name) {
222         if (defaultChannel.equals(name)) {
223             return null;
224         } else if (systemChannels != null && systemChannels.containsKey(name)) {
225             ChannelManagerItem value = (ChannelManagerItem) systemChannels.get(name);
226             if (value != null) {
227                 if (value.pass != null) {
228                     return "";
229                 }
230             }
231             return null;
232         } else if (userChannels != null && userChannels.containsKey(name)) {
233             ChannelManagerItem value = (ChannelManagerItem) userChannels.get(name);
234             if (value != null) {
235                 if (value.pass != null) {
236                     return "";
237                 }
238             }
239             return null;
240         }
241         return null;
242     }
243
244     /**
245      * returns an enumeration of all the contained channels
246      * @return an enumeration of all contained channels
247      */

248     public Enumeration JavaDoc enumerate() {
249         Vector JavaDoc all = new Vector JavaDoc();
250         all.add(defaultChannel);
251         if (systemChannels != null) {
252             Enumeration JavaDoc e = systemChannels.keys();
253             while (e.hasMoreElements()) {
254                 all.add(e.nextElement());
255             }
256         }
257         if (userChannels != null) {
258             Enumeration JavaDoc e = userChannels.keys();
259             while (e.hasMoreElements()) {
260                 all.add(e.nextElement());
261             }
262         }
263         return all.elements();
264     }
265
266     /**
267      * storage class for placing in a HashTable
268      */

269     public class ChannelManagerItem {
270         /**
271          * number of users connected
272          */

273         public int count;
274
275         /**
276          * password for channel, null for no password
277          */

278         public String JavaDoc pass;
279
280         /**
281          * default constructor for default initialization
282          */

283         ChannelManagerItem() {
284             count = 0;
285             pass = null;
286         }
287
288         /**
289          * creates a new channelmanageritem with p as its password
290          * @param p the password
291          */

292         ChannelManagerItem(String JavaDoc p) {
293             count = 0;
294             pass = (p == null ? null : new String JavaDoc(p));
295         }
296
297         /**
298          * increment the user count
299          */

300         public void countpp() {
301             count++;
302         }
303
304         /**
305          * decrement the user count
306          * @return true if decrement emptied room
307          */

308         public boolean countmm() {
309             count--;
310             if (count == 0)
311                 return true;
312             return false;
313         }
314     }
315 }
316
317
Popular Tags