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.ArrayList;
37  import java.util.Date;
38  import java.util.Enumeration;
39  import java.util.Iterator;
40  import java.util.Vector;
41  
42  
43  /**
44   * This class provides methods for the management of the chat users
45   */
46  public class ChatUser
47  {
48      public static final int MODE_USER = 0;
49      public static final int MODE_VOICE = 1;
50      public static final int MODE_OP = 2;
51      public static final int MODE_BAN = 3;
52      public static final int MODE_DEBAN = 4;
53      private String _strNickname;
54      private String _strIpAddress;
55      private String _strHostName;
56      private Vector _vChatEntries = new Vector(  );
57      private int _nMaxMessages = 20;
58      private Date _dateLastAccess;
59      private Date _dateJoin;
60      private int _nMode;
61      private boolean _bAway;
62      private String _strAwayComment;
63      private boolean _bNewPseudo;
64      private String _strKickComment;
65      private boolean _bKicked;
66  
67      ////////////////////////////////////////////////////////////////////////////
68      // Flood management
69      private ArrayList _listSentData = new ArrayList(  );
70  
71      /**
72       * Creates a new ChatUser object
73       *
74       * @param strNickname The nick name of the user
75       */
76      public ChatUser( String strNickname )
77      {
78          setNickname( strNickname );
79          _dateJoin = new Date(  );
80          _nMode = MODE_USER;
81      }
82  
83      /**
84       * Sets the nickname of this user with the specified parameter.
85           * This method is declared final because it is called by the constructor.
86       *
87       * @param strNickname The type as an int
88       */
89      public final void setNickname( String strNickname )
90      {
91          _strNickname = strNickname.replace( ' ', '_' );
92      }
93  
94      /**
95       * Returns the nickname of this user
96       *
97       * @return The nick name
98       */
99      public String getNickname(  )
100     {
101         return _strNickname;
102     }
103 
104     /**
105      * Sets the ip address of this user with the specified parameter
106      *
107      * @param strIpAddress The String ip address
108      */
109     public void setIpAddress( String strIpAddress )
110     {
111         _strIpAddress = strIpAddress;
112     }
113 
114     /**
115      * Returns the ip address of this user
116      *
117      * @return The String ip address
118      */
119     public String getIpAddress(  )
120     {
121         return _strIpAddress;
122     }
123 
124     /**
125      * Sets the host name of this user with the specified parameter
126      *
127      * @param strHostName The String host name
128      */
129     public void setHostName( String strHostName )
130     {
131         _strHostName = strHostName;
132     }
133 
134     /**
135      * Returns the host name of this user
136      *
137      * @return The String host name
138      */
139     public String getHostName(  )
140     {
141         return _strHostName;
142     }
143 
144     /**
145      * Sets the host name of this user with the specified parameter
146      *
147      * @param dateLastAccess The String host name
148      */
149     public void setLastAccessTime( Date dateLastAccess )
150     {
151         _dateLastAccess = dateLastAccess;
152     }
153 
154     /**
155      * Returns the date of the last access of this user to the chat room
156      *
157      * @return The date last access
158      */
159     public Date getLastAccessTime(  )
160     {
161         return _dateLastAccess;
162     }
163 
164     /**
165      * Returns the time when this user entered the chat room
166      *
167      * @return The Long join time
168      */
169     public long getJoinTime(  )
170     {
171         return _dateJoin.getTime(  );
172     }
173 
174     /**
175      * Adds an entry to the ChatEntries Vector
176      *
177      * @param entry The ChatEntry object
178      */
179     public synchronized void addChatEntry( ChatEntry entry )
180     {
181         _vChatEntries.addElement( entry );
182 
183         if ( _vChatEntries.size(  ) > _nMaxMessages )
184         {
185             _vChatEntries.removeElementAt( 0 );
186         }
187     }
188 
189     /**
190      * Return the entries of the chat
191      *
192      * @return The entries as an Enumeration object
193      */
194     public Enumeration getChatEntries(  )
195     {
196         return _vChatEntries.elements(  );
197     }
198 
199     /**
200      * Sets the mode of this user
201      *
202      * @param nMode nMode The mode of this user
203      */
204     public void setMode( int nMode )
205     {
206         _nMode = nMode;
207     }
208 
209     /**
210      * Return the mode of this user
211      *
212      * @return The user mode as an int
213      */
214     public int getMode(  )
215     {
216         return _nMode;
217     }
218 
219     /**
220      * Sets the attribute which define if this user is absent
221      *
222      * @param bAway The new value
223      */
224     public void setAway( boolean bAway )
225     {
226         _bAway = bAway;
227         _strAwayComment = null;
228     }
229 
230     /**
231      * Sets this user as absent and add a comment
232      *
233      * @param strComment the comment of the absence
234      */
235     public void setAway( String strComment )
236     {
237         _bAway = true;
238         _strAwayComment = strComment;
239     }
240 
241     /**
242      * Returns the attribute which define if this user is absent
243      *
244      * @return The boolean attribute _bAway
245      */
246     public boolean isAway(  )
247     {
248         return _bAway;
249     }
250 
251     /**
252      * Returns the absence comment
253      *
254      * @return The absence comment as a String
255      */
256     public String getAwayComment(  )
257     {
258         return _strAwayComment;
259     }
260 
261     /**
262      * Returns the boolean which define if the user has set a new pseudonym
263      *
264      * @return The boolean attribute _bNewPseudo
265      */
266     public boolean hasNewPseudo(  )
267     {
268         return _bNewPseudo;
269     }
270 
271     /**
272      * Set the boolean which define if the user has set a new pseudonym
273      *
274      * @param bNewPseudo the boolean which define if the user has set a new pseudonym
275      */
276     public void setNewPseudo( boolean bNewPseudo )
277     {
278         _bNewPseudo = bNewPseudo;
279     }
280 
281     /**
282      * Kicks this user out of the chat room
283      *
284      * @param strComment A comment about the kick
285      */
286     public void kick( String strComment )
287     {
288         _bKicked = true;
289         _strKickComment = strComment;
290     }
291 
292     /**
293      * Returns the boolean which shows if this user has been kicked out the chat room
294      *
295      * @return the boolean attribte _bKicked
296      */
297     public boolean isKicked(  )
298     {
299         return _bKicked;
300     }
301 
302     /**
303      * Returns the comment shown when this user is kicked out of the chat room
304      *
305      * @return The comment as a String
306      */
307     public String getKickComment(  )
308     {
309         return _strKickComment;
310     }
311 
312     /**
313      * Adds text to the user's list of sent data
314      *
315      * @param strText The text to add
316      */
317     public void addSentData( String strText )
318     {
319         SentData sd = new SentData( strText );
320         _listSentData.add( sd );
321     }
322 
323     /**
324      * Returns the amount of data sent for the last n seconds.
325          *
326      * @param lSeconds The number of seconds
327      * @return The size of the sent data
328      */
329     public int getSentDataSizeSince( long lSeconds )
330     {
331         long lTime = new Date(  ).getTime(  ) - ( 1000 * lSeconds );
332         Iterator i = _listSentData.iterator(  );
333         int nSize = 0;
334 
335         while ( i.hasNext(  ) )
336         {
337             SentData sd = (SentData) i.next(  );
338 
339             if ( sd._date.getTime(  ) > lTime )
340             {
341                 nSize += ( sd._text.length(  ) + 50 );
342             }
343         }
344 
345         return nSize;
346     }
347 
348     /**
349      * This class represents sent data.
350      */
351     private class SentData
352     {
353         String _text;
354         Date _date;
355 
356         /**
357          * Creates a new SentData object.
358          *
359          * @param strText  @param DOCUMENT ME
360          */
361         SentData( String strText )
362         {
363             _text = strText;
364             _date = new Date(  );
365         }
366     }
367 }