View Javadoc
1   /*
2    * Copyright (c) 2002-2017, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * This class provides methods for the ChatRoom objects
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       * Creates a new ChatRoom object.
64       *
65       * @param strName The name of the room
66       * @param strDescription The description of the room
67       */
68      public ChatRoom( String strName, String strDescription )
69      {
70          _strName = strName;
71          _strDescription = strDescription;
72      }
73  
74      /**
75       * Adds an entry to this chat room
76       *
77       * @param entry The chat entry to add to the room
78       * @param userRecipient The user which receives the message
79       */
80      public synchronized void addChatEntry( ChatEntry entry, ChatUser userRecipient )
81      {
82          addChatEntry( null, entry, userRecipient );
83      }
84  
85      /**
86       * Adds an entry to this chat room
87       *
88       * @param userSender The user which sends the message
89       * @param entry The chat entry to add to the room
90       * @param userRecipient The user which receives the message
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             // Send to all
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      * Checks if the user specified in parameter is not already existent in the room and if it is not banned. If not,
118      * it is added to the room. It returns an int code which describes the status of the user.
119      *
120      * @param user The user to add to the room
121      * @return an int code which describes the result of the process
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      * Modifies the nick name of the user
142      *
143      * @param strPseudo The old pseudo
144      * @param strNewNickname The new pseudo
145      * @return The result of the process
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      * Removes the user which has this pseudo in the room.
166      *
167      * @param strPseudo The pseudo of the user to delete from the room
168      */
169     public void removeOldPseudo( String strPseudo )
170     {
171         ChatUser user = getUser( strPseudo );
172         user.setNewPseudo( false );
173         removeUser( strPseudo );
174     }
175 
176     /**
177      * Returns the numbers of users in the room
178      *
179      * @return The number of users as an int
180      */
181     public int getUserCount(  )
182     {
183         return _htUsers.size(  );
184     }
185 
186     /**
187      * Removes the user which has this pseudo in the room.
188      *
189      * @param strPseudo The pseudo of the user to delete from the room
190      */
191     public void removeUser( String strPseudo )
192     {
193         _htUsers.remove( strPseudo );
194     }
195 
196     /**
197      * Bannes a user from the room and comments it.
198      *
199      * @param strPseudo The pseudo to ban
200      * @param strComment The comment to add to this ban
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      * Removes an ip address from the list of those which are banned from the room
211      *
212      * @param strIpAddress The ip address to authorize again
213      */
214     public void debanUser( String strIpAddress )
215     {
216         _htBannedUsers.remove( strIpAddress );
217     }
218 
219     /**
220      * Returns the list of the users of this room
221      *
222      * @return The enumeration which contains the users of the room
223      */
224     public Enumeration getUsers(  )
225     {
226         return _htUsers.elements(  );
227     }
228 
229     /**
230      * The list of the entries of the room
231      *
232      * @return A vector which contains the list of the entries
233      */
234     public Vector getRoomEntries(  )
235     {
236         return _vRoomEntries;
237     }
238 
239     /**
240      * Returns the list of the users banned from this room
241      *
242      * @return An enumeration of the banned users
243      */
244     public Enumeration getBannedUsers(  )
245     {
246         return _htBannedUsers.elements(  );
247     }
248 
249     /**
250      * Retruns the description of this room
251      *
252      * @return The string description of this room
253      */
254     public String getDescription(  )
255     {
256         return _strDescription;
257     }
258 
259     /**
260      * The name of this room
261      *
262      * @return The string name of this room
263      */
264     public String getName(  )
265     {
266         return _strName;
267     }
268 
269     /**
270      * Sets the last access to the room of the user which corresponds to the pseudo specified in parameter
271      *
272      * @param strPseudo The user's pseudo
273      */
274     public void setLastAccessTime( String strPseudo )
275     {
276         ChatUser user = (ChatUser) _htUsers.get( strPseudo );
277         user.setLastAccessTime( new Date(  ) );
278     }
279 
280     /**
281      * Returns the ChatUser object whose pseudo is specified in parameter
282      *
283      * @param strPseudo The pseudo of the user to get
284      * @return The ChatUser object
285      */
286     public ChatUser getUser( String strPseudo )
287     {
288         return (ChatUser) _htUsers.get( strPseudo );
289     }
290 
291     /**
292      * Sets the password of the room
293      *
294      * @param strAdminPassword The new password of the admin
295      */
296     public void setAdminPassword( String strAdminPassword )
297     {
298         _strAdminPassword = strAdminPassword;
299     }
300 
301     /**
302      * The password of the admin
303      *
304      * @return The admin password
305      */
306     public String getAdminPassword(  )
307     {
308         return _strAdminPassword;
309     }
310 
311     /**
312      * Sets the description of this room whith the specified string
313      *
314      * @param strDescription The description of the room
315      */
316     public void setDescription( String strDescription )
317     {
318         _strDescription = strDescription;
319     }
320 
321     /**
322      * Sets the background color of the room
323      *
324      * @param strBgColor The background color
325      */
326     public void setBgColor( String strBgColor )
327     {
328         _strBgColor = strBgColor;
329     }
330 
331     /**
332      * Returns the background color of the room
333      *
334      * @return The background color of the room as a String
335      */
336     public String getBgColor(  )
337     {
338         return _strBgColor;
339     }
340 
341     /**
342      * Sets the button background color
343      *
344      * @param strButtonBgColor The button's background color
345      */
346     public void setButtonBgColor( String strButtonBgColor )
347     {
348         _strButtonBgColor = strButtonBgColor;
349     }
350 
351     /**
352      * Returns the background color of the button
353      *
354      * @return The background color of the button as a String
355      */
356     public String getButtonBgColor(  )
357     {
358         return _strButtonBgColor;
359     }
360 
361     /**
362      * Sets the foreground color of the button
363      *
364      * @param strButtonFgColor The button's foreground color
365      */
366     public void setButtonFgColor( String strButtonFgColor )
367     {
368         _strButtonFgColor = strButtonFgColor;
369     }
370 
371     /**
372      * Returns the foreground color of the button
373      *
374      * @return The foreground color of the button as a String
375      */
376     public String getButtonFgColor(  )
377     {
378         return _strButtonFgColor;
379     }
380 
381     /**
382      * Sets the background color of the text field
383      *
384      * @param strFieldBgColor The text field's background color
385      */
386     public void setFieldBgColor( String strFieldBgColor )
387     {
388         _strFieldBgColor = strFieldBgColor;
389     }
390 
391     /**
392      * Returns the background color of the text field
393      *
394      * @return The background color of the text field as a String
395      */
396     public String getFieldBgColor(  )
397     {
398         return _strFieldBgColor;
399     }
400 }