KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > commands > CmdJoinClosed


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  * Created on 28.09.2003
19  */

20
21 package freecs.commands;
22 import freecs.Server;
23 import freecs.interfaces.ICommand;
24 import freecs.interfaces.IGroupState;
25 import freecs.interfaces.IUserStates;
26 import freecs.core.GroupManager;
27 import freecs.content.MessageState;
28
29 /**
30  * @author Manfred Andres
31  *
32  * freecs.commands
33  */

34 public class CmdJoinClosed extends AbstractCommand {
35     public final String JavaDoc cmd= "/jclosed";
36     private static final ICommand selve=new CmdJoinClosed();
37
38     private CmdJoinClosed () { }
39     
40     public static ICommand getInstance () {
41         return selve;
42     }
43     
44     public boolean execute (MessageState msgState, String JavaDoc param) {
45         if (!msgState.sender.hasRight(IUserStates.MAY_JOIN_LOCKED_GROUP)) {
46             msgState.msgTemplate="error.noRight.noAdmin";
47             msgState.sender.sendMessage (msgState.mp);
48             return false;
49         }
50         if (param.length () < 1) {
51             msgState.msgTemplate="error.j.noArg";
52             msgState.sender.sendMessage (msgState.mp);
53             return false;
54         }
55         msgState.sourceGroup=msgState.sender.getGroup();
56         String JavaDoc groupname;
57         String JavaDoc topic = null;
58         int pos = param.indexOf (":");
59         if (pos > -1) { // joincommand issued with topic
60
topic = param.substring (pos+1).trim ();
61             groupname = param.substring (0, pos).trim ();
62         } else {
63             groupname = param;
64         }
65         if (groupname.length() <1 ) {
66             msgState.msgTemplate="error.j.noArg";
67             msgState.sender.sendMessage (msgState.mp);
68             return false;
69         }
70         if (Server.srv.MAX_GROUPNAME_LENGTH > 0
71                 && groupname.length() > Server.srv.MAX_GROUPNAME_LENGTH)
72                 groupname = groupname.substring (0,Server.srv.MAX_GROUPNAME_LENGTH);
73         if (msgState.sourceGroup.getRawName ().equalsIgnoreCase (groupname)) {
74             msgState.targetGroup=msgState.sourceGroup;
75             msgState.msgTemplate="error.j.alreadyHere";
76             msgState.sender.sendMessage (msgState.mp);
77             return false;
78         }
79         msgState.sourceGroup = msgState.sender.getGroup ();
80         boolean senderWasSu = msgState.sourceGroup.usrIsSu(msgState.sender);
81         msgState.targetGroup = GroupManager.mgr.getGroup (groupname);
82         if (msgState.targetGroup==null) {
83             msgState.param = groupname;
84             msgState.msgTemplate="error.jclosed.groupNotExisting";
85             msgState.sender.sendMessage(msgState.mp);
86             return false;
87         }
88         if (!msgState.targetGroup.hasState(IGroupState.ENTRANCE)
89             && isPunished (msgState))
90                 return false;
91         if (msgState.sender.isPunished()) {
92             if (msgState.targetGroup != null && msgState.targetGroup.usrMayJoinPunished(msgState.sender)){
93                 msgState.targetGroup.incrementJoinPunishedCounter();
94             } else {
95                 msgState.msgTemplate = "error.user.punished";
96                 msgState.sender.sendMessage(msgState.mp);
97                 return false;
98             }
99         }
100         if (!msgState.sender.isPunished())
101             msgState.sourceGroup.resetJoinPunishedCounter();
102         if (msgState.targetGroup.usrIsBaned (msgState.sender.getName())) {
103             msgState.msgTemplate = "error.j.banned";
104             msgState.sender.sendMessage (msgState.mp);
105             return false;
106         }
107         if (!msgState.targetGroup.usrMayJoin(msgState.sender)) {
108             msgState.msgTemplate = "error.j.noRight";
109             msgState.sender.sendMessage (msgState.mp);
110             return false;
111         }
112         Server.log(this, msgState.sender.getName()
113                 + " joines closed group"
114                 + msgState.targetGroup.getRawName(), Server.MSG_STATE, Server.LVL_MINOR);
115         msgState.msgTemplate = "message.user.join.closedGroup";
116         msgState.targetGroup.sendMessage (msgState.mp);
117         msgState.targetGroup.addUser (msgState.sender);
118         if (msgState.sourceGroup.size() > 0) {
119             msgState.msgTemplate = "message.user.leaving.group";
120             msgState.sourceGroup.sendMessage (msgState.mp);
121         }
122         msgState.msgTemplate = "message.jclosed";
123         msgState.sender.sendMessage (msgState.mp);
124         return true;
125     }
126 }
Popular Tags