KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > commands > CmdSu


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.util.GroupUserList;
27 import freecs.core.Group;
28 import freecs.core.User;
29 import freecs.content.MessageState;
30
31 import java.util.Iterator JavaDoc;
32 import java.util.Vector JavaDoc;
33
34 /**
35  * @author Manfred Andres
36  *
37  * freecs.commands
38  */

39 public class CmdSu extends AbstractCommand {
40     public final String JavaDoc cmd= "/su";
41     private static final ICommand selve=new CmdSu();
42
43     private CmdSu () { }
44     
45     public static ICommand getInstance () {
46         return selve;
47     }
48     
49     public boolean execute (MessageState msgState, String JavaDoc param) {
50         if (isPunished (msgState)) return false;
51         if (param.length () < 1) {
52             msgState.msgTemplate = "error.su.noArg";
53             msgState.sender.sendMessage (msgState.mp);
54             return false;
55         }
56         msgState.targetGroup = msgState.sender.getGroup ();
57         Group sg = msgState.sender.getGroup();
58         if (sg == null)
59             return false;
60         boolean mayChangeRights = msgState.sender.hasRight (IUserStates.MAY_CHANGE_RIGHT);
61         if (!mayChangeRights
62             && !sg.usrIsSu (msgState.sender)) {
63                 msgState.msgTemplate = "error.noRight.noSuVipAdmin";
64                 msgState.sender.sendMessage (msgState.mp);
65                 return false;
66         }
67         if (param.indexOf(" ") > -1
68             && !msgState.sender.hasRight(IUserStates.ROLE_VIP)) {
69                 msgState.msgTemplate="error.mass.noRight.noVipAdmin";
70                 msgState.sender.sendMessage(msgState.mp);
71                 return false;
72         }
73         msgState.useRenderCache = false;
74         GroupUserList gul = new GroupUserList();
75         Vector JavaDoc found = getMultibleUsers(msgState, param);
76         for (Iterator JavaDoc i = found.iterator(); i.hasNext(); ) {
77             User cu = (User) i.next();
78             if (cu.hasRight(IUserStates.ROLE_GOD)) {
79                 msgState.msgTemplate="error.noRight.isAdmin";
80                 msgState.usercontext=cu;
81                 msgState.sender.sendMessage(msgState.mp);
82                 i.remove();
83                 continue;
84             } else if (cu.hasRight(IUserStates.ROLE_VIP)) {
85                 msgState.msgTemplate="error.noRight.isVip";
86                 msgState.usercontext=cu;
87                 msgState.sender.sendMessage(msgState.mp);
88                 i.remove();
89                 continue;
90             }
91             if (!mayChangeRights &&
92                    !msgState.sender.getGroup().equals(cu.getGroup())) {
93                 msgState.msgTemplate="error.noRight.noVipAdmin";
94                 msgState.sender.sendMessage(msgState.mp);
95                 i.remove();
96                 continue;
97             }
98             if (cu.getMembership(sg.getSuForbiddenMembership())!=null) {
99                 msgState.msgTemplate="error.noRight.isSuForbiddenMembership";
100                 msgState.usercontext=cu;
101                 msgState.sender.sendMessage(msgState.mp);
102                 i.remove();
103                 continue;
104             }
105             if (cu.getGroup() == null // user is currently changing groups...
106
|| cu.getGroup().usrIsSu(cu)) {
107                 i.remove();
108                 continue;
109             }
110             if (cu.getGroup().hasState(IGroupState.ENTRANCE)
111                 && !mayChangeRights
112                 && sg.suUserCount() >= Server.srv.MAX_SUUSERS_PER_STARTGROUP) {
113                     msgState.msgTemplate = "error.su.tooManyForStartgroup";
114                     msgState.param = String.valueOf(Server.srv.MAX_SUUSERS_PER_STARTGROUP);
115                     msgState.sender.sendMessage(msgState.mp);
116                     i.remove();
117                     while (i.hasNext()) {
118                         Object JavaDoc o = i.next();
119                         i.remove();
120                     }
121                     break;
122             }
123             msgState.usrList=null;
124             if (setSuRight (msgState, cu, true)) {
125                 msgState.usercontext = cu;
126                 gul.addUser(cu);
127                 if (found.size() > 1) {
128                 msgState.msgTemplate="message.su.personal";
129                 cu.sendMessage(msgState.mp);
130                 }
131                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
132                 sb.append (msgState.sender.getName());
133                 sb.append (" gave su-rights to user ");
134                 sb.append (cu.getName());
135                 sb.append (" in group ");
136                 sb.append (cu.getGroup().getRawName());
137                 Server.log (this, sb.toString(), Server.MSG_STATE, Server.LVL_MINOR);
138             } else if (!sg.usrIsSu (msgState.sender)
139                         && !msgState.sender.hasRight (IUserStates.ROLE_VIP)) {
140                 User u = msgState.usercontext;
141                 msgState.usercontext = cu;
142                 msgState.msgTemplate = "error.noRight.noSuVipAdmin";
143                 msgState.sender.sendMessage (msgState.mp);
144                 i.remove();
145                 msgState.usercontext = u;
146             } else if (msgState.usrList != null) { // user doesn't have apropriate membership
147
msgState.targetGroup = cu.getGroup();
148                 if (msgState.usrList.length > 1)
149                     msgState.msgTemplate = "error.membership.notMember.many";
150                 else
151                     msgState.msgTemplate = "error.membership.notMember";
152                 msgState.usercontext=cu;
153                 msgState.sender.sendMessage(msgState.mp);
154                 i.remove();
155                 continue;
156             } else {
157                 User u = msgState.usercontext;
158                 msgState.usercontext = cu;
159                 msgState.msgTemplate = "error.noRight.deactivated";
160                 msgState.sender.sendMessage(msgState.mp);
161                 i.remove();
162                 msgState.usercontext = u;
163             }
164         }
165         if (found.size() == 0)
166             return false;
167         if (found.size() == 1) {
168             msgState.msgTemplate = "message.su.singular";
169             msgState.usercontext = (User) found.get(0);
170             Group g = msgState.usercontext.getGroup();
171             g.sendMessage(msgState.mp);
172             if (!msgState.sender.getGroup().equals(g)) {
173                 msgState.msgTemplate = "message.su.confirm.singular";
174                 msgState.sender.sendMessage(msgState.mp);
175             }
176             return true;
177         }
178         msgState.msgTemplate = "message.su.confirm.plural";
179         msgState.usrList = found.toArray();
180         msgState.sender.sendMessage(msgState.mp);
181         gul.sendMessage(msgState, "message.su", false);
182         return true;
183     }
184     
185     public String JavaDoc toString() {
186         return "[CmdSu]";
187     }
188 }
189
Popular Tags