KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > commands > CmdKickToRoom


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.Iterator JavaDoc;
23 import java.util.Vector 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 CmdKickToRoom extends AbstractCommand {
39     public final String JavaDoc cmd= "/kc";
40     private static final ICommand selve = new CmdKickToRoom();
41
42     private CmdKickToRoom () { }
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
String JavaDoc grpname = null;
53         Group sg = msgState.sender.getGroup ();
54         boolean mayKick = msgState.sender.hasRight(IUserStates.MAY_KICK);
55         if (!mayKick) {
56             msgState.msgTemplate = "error.noRight.noVipAdmin";
57             msgState.sender.sendMessage(msgState.mp);
58             return false;
59         }
60         int pos1 = param.indexOf(" ");
61         int pos2 = param.indexOf(":");
62         boolean multiUser = (pos1!=-1 && (pos2 > pos1+1 || pos2==-1));
63         if (multiUser
64                 && !msgState.sender.hasRight(IUserStates.ROLE_VIP)) {
65             msgState.msgTemplate="error.mass.noRight.noVipAdmin";
66             msgState.sender.sendMessage (msgState.mp);
67             return false;
68         }
69         if (pos2>-1) {
70             String JavaDoc parts[] = param.split (":");
71             param = parts[0];
72             msgState.targetGroup = GroupManager.mgr.getGroup(parts[1]);
73             grpname = parts[1];
74         } else {
75             msgState.msgTemplate = "error.kc.noroom";
76             msgState.sender.sendMessage(msgState.mp);
77             return false;
78         }
79         Vector JavaDoc found = getMultibleUsers(msgState, param);
80         boolean created = false;
81         GroupUserList gul = new GroupUserList();
82         msgState.useRenderCache=false;
83         for (Iterator JavaDoc i = found.iterator(); i.hasNext(); ) {
84             User cu = (User) i.next();
85             msgState.sourceGroup = cu.getGroup();
86             if (cu.hasRight(IUserStates.ROLE_GOD)) {
87                 msgState.usercontext = cu;
88                 msgState.msgTemplate = "error.noRight.isAdmin";
89                 msgState.sender.sendMessage(msgState.mp);
90                 i.remove();
91                 msgState.param = grpname;
92                 msgState.msgTemplate = "message.kc.godinfo";
93                 cu.sendMessage(msgState.mp);
94                 continue;
95             } else if (cu.hasRight(IUserStates.ROLE_VIP)
96                        && !msgState.sender.hasRight(IUserStates.ROLE_GOD)) {
97                 msgState.usercontext = cu;
98                 msgState.msgTemplate = "error.noRight.isVip";
99                 msgState.sender.sendMessage(msgState.mp);
100                 i.remove();
101                 continue;
102             } else if (cu.hasRight(IUserStates.PROTECTED_FROM_KICK)
103                        && !msgState.sender.hasRight(IUserStates.ROLE_GOD)) {
104                 msgState.msgTemplate = "error.k.protected";
105                 msgState.usercontext = cu;
106                 msgState.sender.sendMessage(msgState.mp);
107                 i.remove();
108                 continue;
109             } else if (!msgState.sender.hasRight(IUserStates.ROLE_GOD)
110                     && !isVip (msgState.sender, cu.getGroup(), msgState)) {
111                 msgState.targetGroup = cu.getGroup();
112                 if (msgState.usrList.length > 1)
113                     msgState.msgTemplate = "error.membership.notMember.many";
114                 else
115                     msgState.msgTemplate = "error.membership.notMember";
116                 msgState.usercontext=cu;
117                 msgState.sender.sendMessage(msgState.mp);
118                 i.remove();
119                 continue;
120             }
121             if (msgState.targetGroup == null) {
122                 msgState.targetGroup = GroupManager.mgr.openGroup (grpname, "punished users here", null, msgState.sender);
123                 if (msgState.targetGroup == null) {
124                     msgState.param = "";
125                     msgState.usercontext = cu;
126                     msgState.msgTemplate = "error.k.noGroupRight";
127                     msgState.sender.sendMessage(msgState.mp);
128                     return false;
129                 }
130                 created=true;
131                
132             } else if (!msgState.targetGroup.usrMayJoin(msgState.sender)) {
133                 msgState.param = "";
134                 msgState.usercontext = cu;
135                 msgState.msgTemplate = "error.k.noGroupRight";
136                 msgState.sender.sendMessage (msgState.mp);
137                 return false;
138             }
139             msgState.usercontext = cu;
140             gul.addUser(cu);
141             msgState.targetGroup.addUser(cu, msgState.sender);
142             msgState.targetGroup.removeFromSusers(cu);
143             msgState.msgTemplate = "message.kc.personal";
144             cu.sendMessage(msgState.mp);
145         }
146         if (found.size()==0)
147             return false;
148         if (found.size()==1) {
149             msgState.msgTemplate="message.kc.destination.singular";
150             msgState.targetGroup.exclusiveSendMessage(msgState.mp, found);
151             if (!msgState.sender.getGroup().equals(msgState.sourceGroup)) {
152                 msgState.msgTemplate="message.kc.confirm.singular";
153                 msgState.sender.sendMessage(msgState.mp);
154             }
155             if (msgState.sourceGroup != null) {
156                 msgState.msgTemplate="message.kc.singular";
157                 msgState.sourceGroup.sendMessage(msgState.mp);
158             }
159             return true;
160         }
161         msgState.usrList = found.toArray();
162         gul.sendMessage(msgState, "message.kc", false);
163         msgState.msgTemplate="message.kc.destination.plural";
164         msgState.targetGroup.exclusiveSendMessage(msgState.mp, found);
165         msgState.msgTemplate="message.kc.confirm.plural";
166         msgState.sender.sendMessage(msgState.mp);
167         return true;
168     }
169 }
170
Popular Tags