KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > commands > CmdKick


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

38 public class CmdKick extends AbstractCommand {
39     public final String JavaDoc cmd= "/k";
40     private static final ICommand selve=new CmdKick();
41
42     private CmdKick () { }
43     
44     public static ICommand getInstance () {
45         return selve;
46     }
47     
48     public boolean execute (MessageState msgState, String JavaDoc param) {
49         if (isPunished (msgState))
50             return false;
51         // check if sender does have the right to kick a user
52
Group sg = msgState.sender.getGroup ();
53         boolean mayKick = msgState.sender.hasRight(IUserStates.MAY_KICK);
54         int pos1 = param.indexOf(" ");
55         int pos2 = param.indexOf(":");
56         boolean multiUser = (pos1!=-1 && (pos2 > pos1+1 || pos2==-1));
57         if (multiUser && !msgState.sender.hasRight(IUserStates.ROLE_VIP)) {
58             msgState.msgTemplate="error.mass.noRight.noVipAdmin";
59             msgState.sender.sendMessage (msgState.mp);
60             return false;
61         }
62         // get the reason for this kick if a reason was given
63
if (pos2>-1) {
64             String JavaDoc parts[] = param.split (":");
65             param = parts[0];
66             msgState.reason = parts[1];
67         }
68         msgState.targetGroup = GroupManager.mgr.getGroup ("exil");
69         Vector JavaDoc found = getMultibleUsers(msgState, param);
70         if (found.size() > 1
71                 && !msgState.sender.hasRight(IUserStates.ROLE_VIP)) {
72             msgState.msgTemplate = "error.noRight.noVipAdmin";
73             msgState.sender.sendMessage(msgState.mp);
74             return false; // none-vip's may only use single-user-version
75
}
76         GroupUserList gul = new GroupUserList();
77         msgState.useRenderCache=false;
78         for (Iterator JavaDoc i = found.iterator(); i.hasNext(); ) {
79             User cu = (User) i.next();
80             msgState.sourceGroup = cu.getGroup();
81             if (!cu.hasRight(IUserStates.FREELY_KICKABLE)) {
82                  if (!msgState.sender.hasRight(IUserStates.ROLE_VIP)
83                          && (msgState.sourceGroup==null
84                                  || !msgState.sourceGroup.equals(msgState.sender.getGroup()))) {
85                      msgState.usercontext = cu;
86                      msgState.msgTemplate = "error.noRight.noVipAdmin";
87                      msgState.sender.sendMessage(msgState.mp);
88                      return false; // none-vip's may only use single-user-version
89
} else if (!msgState.sender.hasRight(IUserStates.ROLE_VIP)
90                          && !msgState.sourceGroup.usrIsSu(msgState.sender)) {
91                      msgState.usercontext = cu;
92                      msgState.msgTemplate = "error.noRight.noSuVipAdmin";
93                      msgState.sender.sendMessage(msgState.mp);
94                      return false; // none-vip's may only use single-user-version
95
} else if (cu.hasRight(IUserStates.ROLE_GOD)) {
96                      msgState.usercontext = cu;
97                      msgState.msgTemplate = "error.noRight.isAdmin";
98                      msgState.sender.sendMessage(msgState.mp);
99                      i.remove();
100                      continue;
101                  } else if (cu.hasRight(IUserStates.ROLE_VIP)
102                          && !msgState.sender.hasRight(IUserStates.ROLE_GOD)){
103                      msgState.usercontext = cu;
104                      msgState.msgTemplate = "error.noRight.isVip";
105                      msgState.sender.sendMessage(msgState.mp);
106                      i.remove();
107                      continue;
108                  }
109             }
110             if (msgState.targetGroup == null) {
111                 msgState.targetGroup = GroupManager.mgr.openGroup ("exil", "punished users here", null, msgState.sender);
112                 if (msgState.targetGroup == null) {
113                     msgState.param = "exil";
114                     msgState.usercontext = cu;
115                     msgState.msgTemplate = "error.k.noGroupRight";
116                     msgState.sender.sendMessage(msgState.mp);
117                     return false;
118                 }
119                 msgState.targetGroup.removeFromSusers(cu);
120             } else if (!msgState.targetGroup.usrMayJoin(msgState.sender)) {
121                 msgState.param = "";
122                 msgState.usercontext = cu;
123                 msgState.msgTemplate = "error.k.noGroupRight";
124                 msgState.sender.sendMessage (msgState.mp);
125                 return false;
126             }
127             msgState.usercontext = cu;
128             gul.addUser(cu);
129             msgState.targetGroup.addUser(cu, msgState.sender);
130             msgState.msgTemplate = "message.k.personal";
131             cu.sendMessage(msgState.mp);
132         }
133         if (found.size()==0)
134             return false;
135         if (found.size()==1) {
136             msgState.msgTemplate="message.k.destination.singular";
137             msgState.targetGroup.exclusiveSendMessage(msgState.mp, found);
138             if (!msgState.sender.getGroup().equals(msgState.sourceGroup)
139                     && !msgState.sender.equals(msgState.usercontext)) {
140                 msgState.msgTemplate="message.k.confirm.singular";
141                 msgState.sender.sendMessage(msgState.mp);
142             }
143             msgState.msgTemplate="message.k.singular";
144             msgState.sourceGroup.sendMessage(msgState.mp);
145             return true;
146         }
147         msgState.usrList = found.toArray();
148         gul.sendMessage(msgState, "message.k", false);
149         msgState.msgTemplate="message.k.destination.plural";
150         msgState.targetGroup.exclusiveSendMessage(msgState.mp, found);
151         msgState.msgTemplate="message.k.confirm.plural";
152         msgState.sender.sendMessage(msgState.mp);
153         return true;
154     }
155 }
Popular Tags