KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > core > Group


1 /**
2  * Copyright (C) 2003 Manfred Andres
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */

18 package freecs.core;
19
20 import freecs.Server;
21 import freecs.interfaces.*;
22 import freecs.util.EntityDecoder;
23
24 import java.util.List JavaDoc;
25 import java.util.Vector JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 public class Group implements IGroupState, IMessageDestination {
29    private IGroupPlugin[] plugins;
30    private String JavaDoc suForbiddenMembership;
31    private String JavaDoc name, key;
32    private String JavaDoc theme;
33    private String JavaDoc saveName, saveTheme;
34    private int state;
35    private int minRight=IUserStates.MAY_JOIN_GROUP;
36    private int questionCntr = 0, joinpunishedCntr = 0;
37    private int timelockSec = 60;
38    private int minSetSuRole = IUserStates.ROLE_USER;
39    private User[] usrArr;
40    private Vector JavaDoc usr;
41    private Vector JavaDoc susers;
42    private Vector JavaDoc banList;
43    private Vector JavaDoc autoSuList;
44    private Membership[] memberRoom;
45    private volatile boolean valid;
46
47    public Group (String JavaDoc name, String JavaDoc theme) {
48       this.name = name;
49       this.saveName = EntityDecoder.charToHtml(name);
50       this.key = name.toLowerCase ().trim();
51       this.theme = theme;
52       this.saveTheme = EntityDecoder.charToHtml(theme);
53       this.state = OPEN | AUTO_SU_FIRST | ALLOW_SU;
54       this.usr = new Vector JavaDoc ();
55       this.susers = new Vector JavaDoc ();
56       this.banList = new Vector JavaDoc ();
57       this.valid = true;
58       if (Server.TRACE_CREATE_AND_FINALIZE)
59           Server.log (this, "++++++++++++++++++++++++++++++++++++++++CREATE", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
60    }
61
62    public Group (String JavaDoc name, String JavaDoc theme, int state) {
63       this.name = name;
64       this.saveName = EntityDecoder.charToHtml(name);
65       this.key = name.toLowerCase ().trim();
66       this.theme = theme;
67       this.saveTheme = EntityDecoder.charToHtml(theme);
68       this.state = state;
69       this.usr = new Vector JavaDoc ();
70       this.susers = new Vector JavaDoc ();
71       this.banList = new Vector JavaDoc ();
72       this.valid = true;
73       if (Server.TRACE_CREATE_AND_FINALIZE)
74           Server.log (this, "++++++++++++++++++++++++++++++++++++++++CREATE", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
75    }
76
77    /**
78     * returns the name of this group
79     * @return the name of this group
80     */

81    public String JavaDoc getName () {
82       return saveName;
83    }
84    
85     public String JavaDoc getRawName() {
86         return name;
87     }
88    
89    /**
90     * @return the key of this group
91     */

92    public String JavaDoc getKey() {
93        return key;
94    }
95
96
97    public void setPlugins (IGroupPlugin[] plugins) {
98        this.plugins = plugins;
99    }
100    public IGroupPlugin[] getPlugins () {
101        return plugins;
102    }
103     /**
104      * set the theme of this group
105      * @param t the theme to use for this group
106      */

107    public void setTheme (String JavaDoc t) {
108         theme = t;
109         saveTheme = EntityDecoder.charToHtml(t);
110    }
111    /**
112     * returns the theme of this group
113     * @return the theme of this group
114     */

115    public String JavaDoc getTheme () {
116       return saveTheme;
117    }
118
119     public void setMemberRoom (Membership[] memberRoom) {
120         this.memberRoom = memberRoom;
121     }
122     
123     public Membership[] getMemberships () {
124         return memberRoom;
125     }
126     /**
127      * adds this user to this group
128      * @param u The user joining this Group
129      */

130     public boolean addUser (User u) {
131         GroupManager.mgr.updateGroupListLastModified();
132         return (addUser (u, u, false));
133     }
134    
135     public void addLoginUser (User u) {
136         synchronized (this) {
137             usr.addElement(u);
138             usrArr=(User[]) usr.toArray(new User[0]);
139         }
140         
141         u.setGroup(this);
142         if (autoSuList != null
143                 && autoSuList.contains(u.getName().toLowerCase())) {
144                     addToSusers(u);
145             }
146         if (plugins!=null) {
147             for (int i = 0; i<plugins.length; i++) {
148                 try {
149                     plugins[i].usrJoin(u);
150                 } catch (Exception JavaDoc e) {
151                     Server.debug (plugins[i], "catched exception from plugin", e, Server.MSG_ERROR, Server.LVL_MINOR);
152                 }
153             }
154         }
155     }
156    /**
157     * adds this user to this group using the right of another user
158     * @param u The user to join to this group
159     * @param ru The user to check for the rights for joining ths group
160     * @return ture on success, false if joining is not allowed
161     */

162     public boolean addUser (User u, User ru) {
163         return this.addUser (u, ru, false);
164     }
165     public boolean addUser (User u, User ru, boolean invited) {
166         if (u == null || usr.contains (u))
167             return true;
168         if (!usrMayJoin(ru)
169             && (u.equals(ru) || !invited))
170             return false;
171         boolean isFirst;
172         synchronized (this) {
173             isFirst = usr.isEmpty();
174             usr.addElement (u);
175             usrArr=(User[]) usr.toArray(new User[0]);
176         }
177         if ((isFirst
178                 && this.hasState(IGroupState.AUTO_SU_FIRST | IGroupState.ALLOW_SU)
179                 && !this.hasState(IGroupState.ENTRANCE) && !this.hasState(NO_SU_FIRST))
180             || (autoSuList != null
181                 && autoSuList.contains(u.getName().toLowerCase()) && this.hasState(AUTO_SU_FIRST) )) {
182             addToSusers (u);
183         }
184         u.setGroup(this);
185         if (plugins!=null) {
186             for (int i = 0; i<plugins.length; i++) {
187                 try {
188                     plugins[i].usrJoin(u);
189                 } catch (Exception JavaDoc e) {
190                     Server.debug (plugins[i], "catched exception from plugin", e, Server.MSG_ERROR, Server.LVL_MINOR);
191                 }
192             }
193         }
194         return true;
195     }
196
197     /**
198      * removes this user from this group
199      * @param u the user to remove from this group
200      */

201     public void removeUser (User u) {
202         synchronized (this) {
203             if (usr==null) {
204                 return;
205             }
206             usr.remove (u);
207             if (usr.size() <= 0) {
208                 GroupManager.mgr.removeGroup (this);
209                 return;
210             }
211             usrArr=(User[]) usr.toArray(new User[0]);
212         }
213         while (susers.contains (u)) {
214             susers.remove (u);
215         }
216         if (susers.size()<1
217                 && !this.hasState(OPEN)) {
218             this.setState(OPEN);
219             MessageParser mp = new MessageParser ();
220             mp.setSender(u);
221             mp.setMessageTemplate("message.ul");
222             this.sendModeratedMessage(mp);
223         }
224         if (plugins!=null) {
225             for (int i = 0; i<plugins.length; i++) {
226                 try {
227                     plugins[i].usrLeaving(u);
228                 } catch (Exception JavaDoc e) {
229                     Server.debug (plugins[i], "catched exception from plugin", e, Server.MSG_ERROR, Server.LVL_MINOR);
230                 }
231             }
232         }
233     }
234
235    /**
236     * adds user to su-user-list of this room
237     * @param u The user to add to the su-user-list of this group
238     */

239     public boolean addToSusers (User u) {
240        if (!this.hasState(AUTO_SU_FIRST))
241             return false;
242         if (susers.contains (u))
243             return true;
244         susers.addElement (u);
245         return true;
246     }
247
248     /**
249      * removes a user from the su-user-list of this group
250      * @param u The user to remove from the su-user-list
251      */

252     public void removeFromSusers (User u) {
253         while (susers.contains(u))
254             susers.removeElement (u);
255     }
256
257     /**
258      * send an excluseive message. Message goes to everybody within this group, excluding the users contained within the given vector
259      * @param mc the message
260      * @param exclude users which will not recieve this message
261      */

262     public void exclusiveSendMessage (IContainer mc, List JavaDoc exclude) {
263         if (usr == null || usrArr==null || usr.size () < 1)
264             return;
265         User[] uarr = usrArr;
266         for (int i = 0; i < uarr.length; i++) {
267             User cu = (User) uarr[i];
268             if (exclude.contains(cu))
269                 continue;
270             cu.sendMessage (mc);
271         }
272         if (mc instanceof MessageParser && plugins!=null) {
273             MessageParser mp = (MessageParser) mc;
274             for (int i = 0; i<plugins.length; i++) {
275                 try {
276                     plugins[i].usrAction(mp);
277                 } catch (Exception JavaDoc e) {
278                     Server.debug (plugins[i], "catched exception from plugin", e, Server.MSG_ERROR, Server.LVL_MINOR);
279                 }
280             }
281         }
282     }
283
284    /**
285     * Interface IMessageDestination used for sending messages to this group
286     * meaning all users of this group
287     */

288
289    /**
290     * sends a message to all users of this group. If the group is moderated,
291     * only the moderator will see the message. A moderator may send a users
292     * message by typing /ack username theMessageOfTheUser. This will be done
293     * by a template, causing the message to have a link which sends this
294     * acknowledgement.
295     * @param mc the IContainer containing the content to send to all users of this group
296     */

297     public void sendMessage (IContainer mc) {
298         if (usr==null || usrArr==null || usr.size () < 1)
299             return;
300         boolean messageToModerate = false;
301         if (mc instanceof MessageParser) {
302             User sender = ((MessageParser) mc).getSender();
303             messageToModerate = !(sender != null && (sender.hasRight(IUserStates.IS_MODERATOR) || sender.hasRight(IUserStates.IS_GUEST)));
304         }
305         if (messageToModerate &&
306             this.hasState(IGroupState.MODERATED)) {
307                 User[] uarr = usrArr;
308                 for (int i = 0; i<uarr.length; i++) {
309                 User cu = uarr[i];
310                 if (!cu.hasRight(IUserStates.IS_MODERATOR))
311                     continue;
312                 cu.sendMessage (mc);
313             }
314             return;
315         }
316         sendMsg(mc);
317     }
318    
319     private void sendMsg (IContainer mc) {
320         if (usrArr==null)
321             return;
322         User[] uarr = usrArr;
323         for (int i = 0; i<uarr.length; i++) {
324             User cu = uarr[i];
325             cu.sendMessage (mc);
326         }
327         if (mc instanceof MessageParser && plugins!=null) {
328             MessageParser mp = (MessageParser) mc;
329             for (int i = 0; i<plugins.length; i++) {
330                 try {
331                     plugins[i].usrAction(mp);
332                 } catch (Exception JavaDoc e) {
333                     Server.debug (plugins[i], "catched exception from plugin", e, Server.MSG_ERROR, Server.LVL_MINOR);
334                 }
335             }
336         }
337     }
338     
339     public void sendModeratorMessage (IContainer mc) {
340         if (usr==null || usrArr==null || usr.size () < 1)
341             return;
342         User sender = ((MessageParser) mc).getSender();
343         User[] uarr = usrArr;
344         for (int i=0; i<uarr.length; i++) {
345             User cu = uarr[i];
346             if (sender.equals(cu))
347                 continue;
348             cu.sendMessage (mc);
349         }
350         if (mc instanceof MessageParser && plugins!=null) {
351             MessageParser mp = (MessageParser) mc;
352             for (int i = 0; i<plugins.length; i++) {
353                 try {
354                     plugins[i].usrAction(mp);
355                 } catch (Exception JavaDoc e) {
356                     Server.debug (plugins[i], "catched exception from plugin", e, Server.MSG_ERROR, Server.LVL_MINOR);
357                 }
358             }
359         }
360     }
361     
362    /**
363     * sends a message to all users of this group
364     * @param mc the IContainer containing the content to send to all users of this group
365     */

366     public void sendModeratedMessage (IContainer mc) {
367         if (usr==null || usr.size () < 1)
368             return;
369         sendMsg(mc);
370    }
371
372    /**
373     * returns an Iterator containing all users of this group
374     * @return an Iterator for looping over all groupmembers of this group
375     */

376    public Iterator JavaDoc users () {
377       return usr.iterator ();
378    }
379
380     public User[] getUserArray() {
381         return usrArr;
382     }
383
384     /**
385      * Check if the given user may join this group
386      * @param u The user to check the rights from
387      * @return true if the user is allowed to join, false if not
388      */

389    public boolean usrMayJoin (User u) {
390         if (u==null)
391             return false;
392         if (!hasState(OPEN)
393             && !u.hasRight (IUserStates.MAY_JOIN_LOCKED_GROUP)
394             && !susers.contains (u))
395                 return false;
396         String JavaDoc uname = u.getName().toLowerCase();
397         if (banList != null && banList.contains(uname))
398             return false;
399         return true;
400     }
401     
402     public boolean usrMaySetSu (User u) {
403         if ((!this.equals (u.getGroup ())
404                 && !u.hasRight (IUserStates.MAY_CHANGE_RIGHT))
405              || !u.hasRight (this.minSetSuRole)
406              || !usrIsMember(u))
407            return false;
408         return true;
409     }
410     
411     public boolean usrIsMember (User u) {
412         if (this.memberRoom == null
413                 || this.memberRoom.length == 0)
414             return true;
415         for (int i = 0; i < this.memberRoom.length; i++)
416             if (u.getMembership(this.memberRoom[i].key)!= null)
417                 return true;
418         return false;
419     }
420     
421     /**
422      * returns true if the given user has fulfills all creterias to join this group
423      * @param u the user to join
424      * @return true if all creterias are fulfilled, false if not
425      */

426     public boolean usrMayLock (User u) {
427         if (this.hasState(IGroupState.LOCKPROTECTED))
428             return false;
429         if (!u.hasRight(IUserStates.MAY_LOCK_GROUP)
430             && !susers.contains(u))
431                 return false;
432         if (this.hasState(IGroupState.ENTRANCE)
433             && !u.hasRight(IUserStates.MAY_LOCK_STARTING_GROUP))
434                 return false;
435         if (hasState(IGroupState.MODERATED)
436             && !u.hasRight(IUserStates.MAY_LOCK_MODERATED_GROUP))
437                 return false;
438         if (!usrIsMember(u))
439             return false;
440         return true;
441     }
442     
443     public boolean usrMayJoinPunished (User u) {
444         if (this.joinpunishedCntr >= Server.srv.JOIN_PUNISHED_COUNTER)
445             return false;
446         return true;
447     }
448     
449     public void incrementJoinPunishedCounter() {
450         if (joinpunishedCntr == Integer.MAX_VALUE)
451             joinpunishedCntr=1;
452         else
453             joinpunishedCntr++;
454     }
455     
456     public void resetJoinPunishedCounter() {
457         joinpunishedCntr=0;
458     }
459     
460     /**
461      * increments the question-counter, counting the questions asked within this group
462      */

463     public void incrementQuestionCounter() {
464         if (questionCntr == Integer.MAX_VALUE)
465             questionCntr=1;
466         else
467             questionCntr++;
468     }
469
470     /**
471      * returns the number of questions asked within this group
472      * @return number of questions asked within this group
473      */

474     public int getQuestionCounter() {
475         return questionCntr;
476     }
477     
478     public void resetQuestionCounter() {
479         questionCntr=0;
480     }
481
482     /**
483      * Set the group-ban-state for a user
484      * @param u The user to set the state for
485      * @param on If true, the user will be banned, if false the user will be unbanned
486      */

487     public void setBanForUser (String JavaDoc u, boolean on) {
488         if (!this.isValid())
489             return;
490         String JavaDoc uname = u.toLowerCase();
491         if (on && !banList.contains (uname))
492             banList.addElement (uname);
493         else if (!on)
494             banList.removeElement (uname);
495     }
496     
497     public Vector JavaDoc bannedUsers () {
498         return (Vector JavaDoc) banList.clone();
499     }
500
501     /**
502      * Check if the given user is banned
503      * @param u The user to check the ban-state for this room
504      * @return true if the user is banned, false if not
505      */

506     public boolean usrIsBaned (User u) {
507         return usrIsBaned(u.getName());
508     }
509     public boolean usrIsBaned (String JavaDoc u) {
510         if (banList==null)
511             return false;
512         String JavaDoc uname = u.toLowerCase();
513         return banList.contains (uname);
514     }
515
516     /**
517      * Checks if a specific user is insied this room
518      * @param u The user to search for
519      * @return true if the user is within this group, false if not
520      */

521    public boolean usrIsPresent (User u) {
522        if (usr==null)
523            return false;
524       return usr.contains (u);
525    }
526
527     /**
528      * Checks if the given user is in the SU-list for this group
529      * @param u The user to check for
530      * @return true if the user is contained in the SU-list, false if not
531      */

532     public boolean usrIsSu (User u) {
533         if (susers==null)
534             return false;
535         return susers.contains (u);
536     }
537
538     /**
539      * returns the number of superusers in this grou
540      * @return number of superusers in this group
541      */

542     public int suUserCount () {
543         if (susers==null)
544             return 0;
545         return susers.size();
546     }
547     
548     /**
549      * returns the number of users within this group
550      * @return the number of users within this group
551      */

552     public int size() {
553         if (usr==null)
554             return 0;
555         return usr.size();
556     }
557     
558     /**
559      * set the min-right to join/open this group
560      * @param r
561      */

562     public void setMinRight(int r) {
563         this.minRight = r;
564     }
565
566     /**
567      * set the array with user-names which automatically get su-rights
568      * @param usrs string-array containing the usernames which recieve su-rights on joining this group
569      */

570     public void setAutoSu (String JavaDoc[] usrs) {
571         if (autoSuList == null)
572             autoSuList = new Vector JavaDoc();
573         for (int i = 0; i < usrs.length; i++) {
574             String JavaDoc usr = usrs[i].trim().toLowerCase();
575             if (!autoSuList.contains(usr))
576                 autoSuList.add(usr);
577         }
578     }
579     
580     public void setSuForbiddenMembership (String JavaDoc ship) {
581         suForbiddenMembership = ship ;
582     }
583     public String JavaDoc getSuForbiddenMembership (){
584         return suForbiddenMembership ;
585     }
586     /**
587      * checks if a state is true for this group
588      * @param state the state which must be given
589      * @return true if the state is given, false if not
590      */

591    public boolean hasState (int state) {
592       return (this.state & state) == state;
593    }
594
595     /**
596      * enables the given state
597      * @param state the state to enable
598      */

599    public void setState (int state) {
600       this.state = this.state | (state - (this.state & state));
601    }
602
603     /**
604      * disables the given state
605      * @param state the state to disable
606      */

607    public void unsetState (int state) {
608       this.state = this.state - (this.state & state);
609    }
610
611     public synchronized void setMinRightSu (int minRight) {
612         this.minSetSuRole = minRight;
613     }
614     public boolean isValid () {
615         return valid;
616     }
617     
618     public void invalidate() {
619         usrArr=null;
620         usr=null;
621         susers=null;
622         banList=null;
623         autoSuList=null;
624         valid=false;
625     }
626     /**
627      * checks if the given group is equal to this group
628      * @return true if the two groups are equal
629      */

630     public boolean equals (Object JavaDoc g) {
631         if (this == g)
632             return true;
633         if (g==null)
634             return false;
635         if (!(g instanceof Group))
636             return false;
637         String JavaDoc n = ((Group) g).getKey ();
638         if (n == null)
639             return false;
640         return (n.equalsIgnoreCase (this.key));
641     }
642     
643     public String JavaDoc toString () {
644         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
645         sb.append ("[Group ");
646         sb.append (key);
647         sb.append (" / state: ");
648         sb.append (state);
649         sb.append ("]");
650         return sb.toString();
651     }
652     
653     public int hasCode() {
654         return this.key.hashCode();
655     }
656
657     public void finalize() {
658         if (Server.TRACE_CREATE_AND_FINALIZE)
659             Server.log(this, "----------------------------------------FINALIZED", Server.MSG_STATE, Server.LVL_VERY_VERBOSE);
660     }
661     /**
662      * @return Returns the timelockSec.
663      */

664     public int getTimelockSec() {
665         return timelockSec;
666     }
667     /**
668      * @param timelockSec The timelockSec to set.
669      */

670     public void setTimelockSec(int timelockSec) {
671         this.timelockSec = timelockSec;
672     }
673 }
Popular Tags