KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > commands > CmdUnBan


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

37 public class CmdUnBan extends AbstractCommand {
38     public final String JavaDoc cmd= "/uban";
39     private static final ICommand selve=new CmdUnBan();
40
41     private CmdUnBan () { }
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.sender.hasRight(IUserStates.ROLE_VIP)
56                 && !msgState.sender.getGroup().equals(msgState.targetGroup)) {
57                 msgState.msgTemplate="error.noRight.noVipAdmin";
58                 msgState.sender.sendMessage(msgState.mp);
59                 return false;
60             }
61             if (msgState.targetGroup==null) {
62                 msgState.param = parts[1];
63                 msgState.msgTemplate="error.group.notExisting";
64                 msgState.sender.sendMessage(msgState.mp);
65                 return false;
66             }
67         } else
68             msgState.targetGroup = msgState.sender.getGroup ();
69         
70         if (!msgState.sender.hasRight(IUserStates.ROLE_VIP)) {
71             if (msgState.targetGroup.hasState(IGroupState.ENTRANCE)) {
72                 msgState.msgTemplate = "error.noRight.noVipAdmin";
73                 msgState.sender.sendMessage (msgState.mp);
74                 return false;
75             } else if (!msgState.targetGroup.usrIsSu(msgState.sender)) {
76                        msgState.msgTemplate="error.noRight.noSuVipAdmin";
77                        msgState.sender.sendMessage(msgState.mp);
78                        return false;
79             }
80         } else if (!isVip(msgState.sender, msgState.targetGroup, msgState)) {
81             if (msgState.usrList.length > 1)
82                 msgState.msgTemplate = "error.membership.notMember.many";
83             else
84                 msgState.msgTemplate = "error.membership.notMember";
85             msgState.usercontext=null;
86             msgState.sender.sendMessage(msgState.mp);
87             return false;
88         }
89
90         if (param != null && param.indexOf(" ") > 1
91             && !msgState.sender.hasRight (IUserStates.ROLE_VIP)) {
92                 msgState.msgTemplate = "error.mass.noRight.noVipAdmin";
93                 msgState.sender.sendMessage (msgState.mp);
94                 return false;
95         } else if (param == null || param.length () < 1) {
96             msgState.msgTemplate = "error.uban.noArg";
97             msgState.sender.sendMessage (msgState.mp);
98             return false;
99         }
100         msgState.useRenderCache=false;
101         String JavaDoc users[] = param.split (" ");
102         Vector JavaDoc found = new Vector JavaDoc();
103         Vector JavaDoc notfound = new Vector JavaDoc();
104         for (int i = 0; i < users.length; i++) {
105             Object JavaDoc o = UserManager.mgr.getUserByName(users[i]);
106             if (o == null)
107                 o = users[i];
108             if (!msgState.targetGroup.usrIsBaned(users[i])) {
109                 notfound.add(o);
110             } else {
111                 found.add(o);
112                 if (o instanceof User) {
113                     msgState.usercontext = (User) o;
114                     msgState.targetGroup.setBanForUser (msgState.usercontext.getName(), false);
115                     msgState.msgTemplate = "message.uban.personal";
116                     msgState.usercontext.sendMessage (msgState.mp);
117                 } else {
118                     msgState.targetGroup.setBanForUser((String JavaDoc) o, false);
119                 }
120             }
121         }
122         if (notfound.size() == 1) {
123             msgState.msgTemplate = "error.uban.notOnList.singular";
124             Object JavaDoc o = notfound.get(0);
125             if (o instanceof User) {
126                 msgState.usercontext = (User) o;
127                 msgState.param = null;
128             } else {
129                 msgState.usercontext = null;
130                 msgState.param = (String JavaDoc) o;
131             }
132             msgState.sender.sendMessage(msgState.mp);
133         } else if (notfound.size() > 1) {
134             msgState.msgTemplate = "error.uban.notOnList.plural";
135             msgState.usrList = notfound.toArray();
136             msgState.sender.sendMessage (msgState.mp);
137         }
138         if (found.size() == 1) {
139             Object JavaDoc o = found.get(0);
140             if (o instanceof User) {
141                 msgState.usercontext = (User) o;
142                 msgState.param = "";
143             } else {
144                 msgState.usercontext = null;
145                 msgState.param = (String JavaDoc) o;
146             }
147             if (!msgState.sender.getGroup().equals(msgState.targetGroup)) {
148                 msgState.msgTemplate = "message.uban.confirm.singular";
149                 msgState.sender.sendMessage(msgState.mp);
150             }
151             msgState.msgTemplate = "message.uban.singular";
152             msgState.targetGroup.sendModeratedMessage(msgState.mp);
153             return true;
154         } else if (found.size() > 1) {
155             msgState.usrList = found.toArray();
156             if (!msgState.sender.getGroup().equals(msgState.targetGroup)) {
157                 msgState.msgTemplate = "message.uban.confirm.plural";
158                 msgState.sender.sendMessage (msgState.mp);
159             }
160             msgState.msgTemplate = "message.uban.plural";
161             msgState.usrList = found.toArray();
162             msgState.targetGroup.sendModeratedMessage(msgState.mp);
163             return true;
164         }
165         return false;
166     }
167 }
168
Popular Tags