1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.chat.business;
35
36 import java.util.Date;
37 import java.util.Enumeration;
38 import java.util.Hashtable;
39 import java.util.Vector;
40
41
42
43
44
45 public class ChatRoom
46 {
47 public static final int INVALID_ROOM = -1;
48 public static final int USER_ADDED = 0;
49 public static final int USER_ALREADY_EXISTS = 1;
50 public static final int USER_IS_BANNED = 2;
51 private String _strName;
52 private String _strDescription;
53 private String _strAdminPassword;
54 private String _strBgColor;
55 private String _strButtonBgColor;
56 private String _strButtonFgColor;
57 private String _strFieldBgColor;
58 private Hashtable _htUsers = new Hashtable( );
59 private Hashtable _htBannedUsers = new Hashtable( );
60 private Vector _vRoomEntries = new Vector( );
61
62
63
64
65
66
67
68 public ChatRoom( String strName, String strDescription )
69 {
70 _strName = strName;
71 _strDescription = strDescription;
72 }
73
74
75
76
77
78
79
80 public synchronized void addChatEntry( ChatEntry entry, ChatUser userRecipient )
81 {
82 addChatEntry( null, entry, userRecipient );
83 }
84
85
86
87
88
89
90
91
92 public synchronized void addChatEntry( ChatUser userSender, ChatEntry entry, ChatUser userRecipient )
93 {
94 if ( userRecipient != null )
95 {
96 userRecipient.addChatEntry( entry );
97 }
98 else
99 {
100
101 Enumeration e = getUsers( );
102
103 while ( e.hasMoreElements( ) )
104 {
105 ChatUser u = (ChatUser) e.nextElement( );
106 u.addChatEntry( entry );
107 }
108 }
109
110 if ( userSender != null )
111 {
112 userSender.addSentData( entry.getChatMessage( ) );
113 }
114 }
115
116
117
118
119
120
121
122
123 public synchronized int addUser( ChatUser user )
124 {
125 if ( _htUsers.containsKey( user.getNickname( ) ) )
126 {
127 return USER_ALREADY_EXISTS;
128 }
129
130 if ( _htBannedUsers.containsKey( user.getIpAddress( ) ) )
131 {
132 return USER_IS_BANNED;
133 }
134
135 _htUsers.put( user.getNickname( ), user );
136
137 return USER_ADDED;
138 }
139
140
141
142
143
144
145
146
147 public int changePseudo( String strPseudo, String strNewNickname )
148 {
149 ChatUser user = getUser( strPseudo );
150 user.setNickname( strNewNickname );
151
152 int nError = addUser( user );
153
154 if ( nError != USER_ADDED )
155 {
156 return nError;
157 }
158
159 user.setNewPseudo( true );
160
161 return USER_ADDED;
162 }
163
164
165
166
167
168
169 public void removeOldPseudo( String strPseudo )
170 {
171 ChatUser user = getUser( strPseudo );
172 user.setNewPseudo( false );
173 removeUser( strPseudo );
174 }
175
176
177
178
179
180
181 public int getUserCount( )
182 {
183 return _htUsers.size( );
184 }
185
186
187
188
189
190
191 public void removeUser( String strPseudo )
192 {
193 _htUsers.remove( strPseudo );
194 }
195
196
197
198
199
200
201
202 public void banUser( String strPseudo, String strComment )
203 {
204 ChatUser user = (ChatUser) _htUsers.get( strPseudo );
205 _htBannedUsers.put( user.getIpAddress( ), user );
206 user.kick( strComment );
207 }
208
209
210
211
212
213
214 public void debanUser( String strIpAddress )
215 {
216 _htBannedUsers.remove( strIpAddress );
217 }
218
219
220
221
222
223
224 public Enumeration getUsers( )
225 {
226 return _htUsers.elements( );
227 }
228
229
230
231
232
233
234 public Vector getRoomEntries( )
235 {
236 return _vRoomEntries;
237 }
238
239
240
241
242
243
244 public Enumeration getBannedUsers( )
245 {
246 return _htBannedUsers.elements( );
247 }
248
249
250
251
252
253
254 public String getDescription( )
255 {
256 return _strDescription;
257 }
258
259
260
261
262
263
264 public String getName( )
265 {
266 return _strName;
267 }
268
269
270
271
272
273
274 public void setLastAccessTime( String strPseudo )
275 {
276 ChatUser user = (ChatUser) _htUsers.get( strPseudo );
277 user.setLastAccessTime( new Date( ) );
278 }
279
280
281
282
283
284
285
286 public ChatUser getUser( String strPseudo )
287 {
288 return (ChatUser) _htUsers.get( strPseudo );
289 }
290
291
292
293
294
295
296 public void setAdminPassword( String strAdminPassword )
297 {
298 _strAdminPassword = strAdminPassword;
299 }
300
301
302
303
304
305
306 public String getAdminPassword( )
307 {
308 return _strAdminPassword;
309 }
310
311
312
313
314
315
316 public void setDescription( String strDescription )
317 {
318 _strDescription = strDescription;
319 }
320
321
322
323
324
325
326 public void setBgColor( String strBgColor )
327 {
328 _strBgColor = strBgColor;
329 }
330
331
332
333
334
335
336 public String getBgColor( )
337 {
338 return _strBgColor;
339 }
340
341
342
343
344
345
346 public void setButtonBgColor( String strButtonBgColor )
347 {
348 _strButtonBgColor = strButtonBgColor;
349 }
350
351
352
353
354
355
356 public String getButtonBgColor( )
357 {
358 return _strButtonBgColor;
359 }
360
361
362
363
364
365
366 public void setButtonFgColor( String strButtonFgColor )
367 {
368 _strButtonFgColor = strButtonFgColor;
369 }
370
371
372
373
374
375
376 public String getButtonFgColor( )
377 {
378 return _strButtonFgColor;
379 }
380
381
382
383
384
385
386 public void setFieldBgColor( String strFieldBgColor )
387 {
388 _strFieldBgColor = strFieldBgColor;
389 }
390
391
392
393
394
395
396 public String getFieldBgColor( )
397 {
398 return _strFieldBgColor;
399 }
400 }