KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > commands > CmdKickHard


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.User;
29 import freecs.content.MessageState;
30 import freecs.Server;
31
32 /**
33  * @author Manfred Andres
34  *
35  * freecs.commands
36  */

37 public class CmdKickHard extends AbstractCommand {
38     public final String JavaDoc cmd= "/kh";
39     private static final ICommand selve=new CmdKickHard();
40
41     private CmdKickHard () { }
42     
43     public static ICommand getInstance () {
44         return selve;
45     }
46     
47     public boolean execute (MessageState msgState, String JavaDoc param) {
48         if (isPunished (msgState))
49             return false;
50         if (!msgState.sender.hasRight(IUserStates.MAY_KICK_HARD)) {
51             msgState.msgTemplate = "error.noRight.noVipAdmin";
52             msgState.sender.sendMessage(msgState.mp);
53             return false;
54         }
55         int pos1 = param.indexOf (" ");
56         int pos2 = param.indexOf (":");
57         if ((pos1!=-1 && (pos2 > pos1+1 || pos2==-1))
58             && !msgState.sender.hasRight(IUserStates.ROLE_VIP)) {
59             msgState.msgTemplate="error.mass.noRight.noVipAdmin";
60             msgState.sender.sendMessage (msgState.mp);
61             return false;
62         }
63         if (pos2 > 0) {
64             msgState.reason = param.substring(pos2+1);
65             msgState.message = param.substring(pos2+1);
66             param = param.substring(0,pos2).trim();
67         }
68         long time = Server.srv.DEFAULT_BAN_DURATION;
69         pos1 = param.trim().lastIndexOf(" ");
70         if (pos1 != -1) {
71             String JavaDoc timeStrg = param.substring(pos1).trim();
72             long customTime=-1;
73             try {
74                 customTime = Long.parseLong(timeStrg,10);
75                 time = customTime;
76                 param = param.substring(0,pos1).trim();
77             } catch (NumberFormatException JavaDoc nfe) {
78                 /* last arg is not a number so it will be treated as a user */
79             }
80         }
81         if (time > Server.srv.MAX_BAN_DURATION)
82             time = Server.srv.MAX_BAN_DURATION;
83         time = time * 60 * 1000;
84         
85         Vector JavaDoc found = getMultibleUsers(msgState, param);
86         GroupUserList gul = new GroupUserList();
87         msgState.useRenderCache=false;
88         for (Iterator JavaDoc i = found.iterator(); i.hasNext(); ) {
89             User cu = (User) i.next();
90             if (cu.hasRole(IUserStates.ROLE_GOD)) {
91                 msgState.usercontext= cu;
92                 msgState.msgTemplate = "error.noRight.isAdmin";
93                 msgState.sender.sendMessage(msgState.mp);
94                 msgState.msgTemplate = "message.kh.godinfo";
95                 cu.sendMessage(msgState.mp);
96                 i.remove();
97                 continue;
98             } else if (cu.hasRight(IUserStates.PROTECTED_FROM_KICK)
99                     && !msgState.sender.hasRole(IUserStates.ROLE_GOD)) {
100                  msgState.usercontext= cu;
101                  msgState.msgTemplate = "error.noRight.isVip";
102                  msgState.sender.sendMessage(msgState.mp);
103                  i.remove();
104                  continue;
105             } else if (!msgState.sender.hasRight(IUserStates.ROLE_GOD)
106                     && !isVip (msgState.sender, cu.getGroup(), msgState)) {
107                 msgState.targetGroup = cu.getGroup();
108                 if (msgState.usrList.length > 1)
109                     msgState.msgTemplate = "error.membership.notMember.many";
110                 else
111                     msgState.msgTemplate = "error.membership.notMember";
112                 msgState.usercontext=cu;
113                 msgState.sender.sendMessage(msgState.mp);
114                 i.remove();
115                 continue;
116             }
117             msgState.sourceGroup=cu.getGroup();
118             gul.addUser(cu);
119         }
120         if (found.size()==0)
121             return false;
122         if (found.size()==1) {
123             msgState.msgTemplate="message.kh.singular";
124             msgState.usercontext=(User) found.get(0);
125             if (msgState.sourceGroup != null)
126                 msgState.sourceGroup.sendMessage(msgState.mp);
127             Server.srv.banUser (found, msgState.reason, time, msgState.sender.getName());
128             if (!msgState.sender.getGroup().equals (msgState.sourceGroup)) {
129                 msgState.msgTemplate = "message.kh.confirm.singular";
130                 msgState.sender.sendMessage(msgState.mp);
131             }
132             return true;
133         }
134         msgState.usrList = found.toArray();
135         gul.sendMessage(msgState, "message.kh", false);
136         Server.srv.banUser (found, msgState.reason, time, msgState.sender.getName());
137         msgState.msgTemplate = "message.kh.confirm.plural";
138         msgState.sender.sendMessage(msgState.mp);
139         return true;
140     }
141 }
142
Popular Tags