KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > commands > CmdBan


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.IGroupState;
27 import freecs.content.MessageState;
28 import freecs.core.GroupManager;
29 import freecs.core.User;
30 import freecs.interfaces.IUserStates;
31
32 /**
33  * @author Manfred Andres
34  *
35  * freecs.commands
36  */

37 public class CmdBan extends AbstractCommand {
38     public final String JavaDoc cmd= "/ban";
39     private static final ICommand selve= new CmdBan();
40
41     private CmdBan () { }
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         String JavaDoc parts[] = param.split (":");
51         param = parts[0];
52         msgState.targetGroup = null;
53         if (parts.length > 1) {
54             msgState.targetGroup = GroupManager.mgr.getGroup (parts[1]);
55             if (msgState.targetGroup==null) {
56                 msgState.param = parts[1];
57                 msgState.msgTemplate="error.group.notExisting";
58                 msgState.sender.sendMessage(msgState.mp);
59                 return false;
60             }
61         } else
62             msgState.targetGroup = msgState.sender.getGroup ();
63         if (!msgState.sender.hasRight(IUserStates.ROLE_VIP)) {
64             if (msgState.targetGroup.hasState(IGroupState.ENTRANCE)) {
65                 msgState.msgTemplate = "error.noRight.noVipAdmin";
66                 msgState.sender.sendMessage (msgState.mp);
67                 return false;
68             } else if (!msgState.targetGroup.usrIsSu(msgState.sender)) {
69                        msgState.msgTemplate="error.noRight.noSuVipAdmin";
70                        msgState.sender.sendMessage(msgState.mp);
71                        return false;
72             }
73         }
74         if (param == null || param.length () < 1) {
75             msgState.msgTemplate = "error.ban.noArg";
76             msgState.sender.sendMessage (msgState.mp);
77             return false;
78         }
79         msgState.useRenderCache=false;
80         ICommand ic = CommandSet.getCommandSet().getCommand("/k");
81         Vector JavaDoc found = getMultibleUsers(msgState, param);
82         MessageState bkup = (MessageState) msgState.clone();
83         for (Iterator JavaDoc e = found.iterator(); e.hasNext(); ) {
84             User cu = (User) e.next();
85             if (cu.hasRight (IUserStates.PROTECTED_FROM_BAN)
86                 && !msgState.sender.hasRight(IUserStates.ROLE_GOD)) {
87                 User luc = msgState.usercontext;
88                 msgState.usercontext = cu;
89                 msgState.msgTemplate = "error.noRight.noAdmin";
90                 msgState.sender.sendMessage (msgState.mp);
91                 msgState.usercontext=luc;
92                 e.remove();
93                 continue;
94             }
95             if (msgState.targetGroup.equals(cu.getGroup())) {
96                 if (!ic.execute(msgState, cu.getName())) {
97                     e.remove();
98                     msgState.inhale(bkup);
99                     continue;
100                 }
101                 msgState.inhale(bkup);
102             }
103             msgState.usercontext = cu;
104             msgState.targetGroup.setBanForUser (cu.getName(), true);
105             msgState.msgTemplate = "message.ban.personal";
106             cu.sendMessage (msgState.mp);
107         }
108         if (found.size()==0)
109             return false;
110         if (found.size()==1) {
111             msgState.msgTemplate = "message.ban.singular";
112             msgState.targetGroup.sendModeratedMessage (msgState.mp);
113             if (!msgState.sender.getGroup().equals(msgState.targetGroup)) {
114                 msgState.msgTemplate= "message.ban.confirm.singular";
115                 msgState.sender.sendMessage (msgState.mp);
116             }
117         } else {
118             msgState.usrList = found.toArray();
119             msgState.msgTemplate = "message.ban.plural";
120             msgState.targetGroup.sendModeratedMessage (msgState.mp);
121             if (!msgState.sender.getGroup().equals(msgState.targetGroup)) {
122                 msgState.msgTemplate= "message.ban.confirm.plural";
123                 msgState.sender.sendMessage (msgState.mp);
124             }
125         }
126         return true;
127     }
128 }
129
Popular Tags