KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > commands > CmdRSu


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 CmdRSu extends AbstractCommand {
40     public final String JavaDoc cmd= "/rsu";
41     private static final ICommand selve=new CmdRSu();
42
43     private CmdRSu () { }
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.rsu.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 (!msgState.sender.hasRight(IUserStates.ROLE_VIP)
62             && sg.hasState(IGroupState.ENTRANCE)) {
63             msgState.msgTemplate = "error.noRight.noVipAdmin";
64             msgState.sender.sendMessage (msgState.mp);
65             return false;
66         }
67         if (!mayChangeRights
68             && !sg.usrIsSu (msgState.sender)) {
69                 msgState.msgTemplate = "error.noRight.noSuVipAdmin";
70                 msgState.sender.sendMessage (msgState.mp);
71                 return false;
72         }
73         if (param.indexOf(" ") > -1
74             && !msgState.sender.hasRight(IUserStates.ROLE_VIP)) {
75                 msgState.msgTemplate="error.mass.noRight.noVipAdmin";
76                 msgState.sender.sendMessage(msgState.mp);
77                 return false;
78         }
79         msgState.useRenderCache = false;
80         GroupUserList gul = new GroupUserList();
81         Vector JavaDoc found = getMultibleUsers(msgState, param);
82         for (Iterator JavaDoc i = found.iterator(); i.hasNext(); ) {
83             User cu = (User) i.next();
84             if (!cu.getGroup().usrIsSu(cu)) {
85                 i.remove();
86                 continue;
87             }
88             msgState.usrList = null;
89             if (setSuRight (msgState, cu, false)) {
90                 msgState.usercontext = cu;
91                 gul.addUser(cu);
92                 if (found.size() > 1) {
93                     msgState.msgTemplate="message.rsu.personal";
94                     cu.sendMessage(msgState.mp);
95                 }
96                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
97                 sb.append (msgState.sender.getName());
98                 sb.append (" took su-rights from user ");
99                 sb.append (cu.getName());
100                 sb.append (" in group ");
101                 sb.append (cu.getGroup().getRawName());
102                 Server.log (this, sb.toString(), Server.MSG_STATE, Server.LVL_MINOR);
103             } else if (!msgState.sender.hasRight(IUserStates.ROLE_VIP)) {
104                 if (sg.hasState (IGroupState.ENTRANCE)) {
105                     msgState.msgTemplate = "error.noRight.noVipAdmin";
106                     msgState.sender.sendMessage (msgState.mp);
107                 } else {
108                     msgState.msgTemplate = "error.noRight.noSuVipAdmin";
109                     msgState.sender.sendMessage (msgState.mp);
110                 }
111                 i.remove();
112             } else if (msgState.usrList != null) { // user doesn't have apropriate membership
113
msgState.targetGroup = cu.getGroup();
114                 if (msgState.usrList.length > 1)
115                     msgState.msgTemplate = "error.membership.notMember.many";
116                 else
117                     msgState.msgTemplate = "error.membership.notMember";
118                 msgState.usercontext=cu;
119                 msgState.sender.sendMessage(msgState.mp);
120                 i.remove();
121                 continue;
122             }
123         }
124         if (found.size() == 0)
125             return false;
126         if (found.size() == 1) {
127             msgState.msgTemplate = "message.rsu.singular";
128             msgState.usercontext = (User) found.get(0);
129             msgState.usercontext.getGroup().sendMessage(msgState.mp);
130             if (!msgState.sender.getGroup().equals(msgState.usercontext.getGroup())) {
131                 msgState.msgTemplate = "message.rsu.confirm.singular";
132                 msgState.sender.sendMessage(msgState.mp);
133             }
134             return true;
135         }
136         msgState.msgTemplate = "message.rsu.confirm.plural";
137         msgState.usrList = found.toArray();
138         msgState.sender.sendMessage(msgState.mp);
139         gul.sendMessage(msgState, "message.rsu", false);
140         return true;
141     }
142     
143     public String JavaDoc toString() {
144         return "[CmdRSu]";
145     }
146 }
147
Popular Tags