KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > commands > CmdUnPunish


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

38 public class CmdUnPunish extends AbstractCommand {
39     public final String JavaDoc cmd= "/rgag";
40     private static final ICommand selve=new CmdUnPunish();
41
42     private CmdUnPunish () { }
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         boolean mayPunish = msgState.sender.hasRight (IUserStates.MAY_PUNISH);
52         Group sg = msgState.sender.getGroup ();
53         boolean isSu = sg.usrIsSu (msgState.sender);
54         if (!mayPunish && !isSu) {
55             msgState.msgTemplate = "error.noRight.noSuVipAdmin";
56             msgState.sender.sendMessage (msgState.mp);
57             return false;
58         }
59         if (param.length () < 1) {
60             msgState.msgTemplate = "error.rgag.noArg";
61             msgState.sender.sendMessage (msgState.mp);
62             return false;
63         }
64         int pos = param.indexOf(":");
65         if (pos != -1) {
66             msgState.reason = param.substring (pos+1);
67             param = param.substring (0, pos);
68         } else
69             msgState.reason = "";
70
71         // only vip's are allowd to punish more than one user
72
if (param.indexOf(" ") > -1
73             && !msgState.sender.hasRight(IUserStates.ROLE_VIP)) {
74             msgState.msgTemplate = "error.mass.noRight.noVipAdmin";
75             msgState.sender.sendMessage (msgState.mp);
76             return false;
77         }
78         GroupUserList gul = new GroupUserList();
79         Vector JavaDoc found = getMultibleUsers(msgState, param);
80         msgState.useRenderCache = false;
81         for (Iterator JavaDoc i = found.iterator(); i.hasNext (); ) {
82             User cu = (User) i.next();
83             if (cu.equals(msgState.sender)) {
84                 i.remove();
85                 continue;
86             }
87             // user must be >vip to unpunish users within other groups
88
if (!mayPunish
89                     && (cu.getGroup() == null
90                             || !cu.getGroup().equals(msgState.sender.getGroup()))) {
91                 msgState.msgTemplate = "error.noRight.noVipAdmin";
92                 User u = msgState.usercontext;
93                 msgState.usercontext = cu;
94                 msgState.sender.sendMessage (msgState.mp);
95                 msgState.usercontext = u;
96                 i.remove();
97                 continue;
98             }
99             boolean p = false;
100             if ((p && msgState.sender.isPunished())
101                     || cu == null || cu.isPunished () == p)
102                         return false;
103                 if (!cu.hasRight(IUserStates.FREELY_PUNISHABLE)) {
104                     if (cu.hasRight(IUserStates.ROLE_GOD)) {
105                         msgState.msgTemplate = "error.noRight.isAdmin";
106                         msgState.usercontext = cu;
107                         msgState.sender.sendMessage (msgState.mp);
108                         i.remove();
109                         continue;
110                     }
111                     if (cu.hasRight(IUserStates.ROLE_VIP)
112                         && !msgState.sender.hasRight(IUserStates.ROLE_VIP)) {
113                             msgState.msgTemplate = "error.noRight.isVip";
114                             msgState.usercontext = cu;
115                             msgState.sender.sendMessage (msgState.mp);
116                             i.remove();
117                             continue;
118                     }
119                     if (cu.hasRight (IUserStates.PROTECTED_FROM_PUNISH)
120                         && !msgState.sender.hasRight(IUserStates.ROLE_GOD)) {
121                             msgState.msgTemplate = "error.gag.protected";
122                             msgState.usercontext = cu;
123                             msgState.sender.sendMessage (msgState.mp);
124                             i.remove();
125                             continue;
126                     }
127                 }
128             cu.setPunish (p);
129             if (found.size() > 1) {
130                msgState.msgTemplate= "message.rgag.personal";
131                cu.sendMessage (msgState.mp);
132             }
133             gul.addUser(cu);
134             msgState.usercontext = cu;
135         }
136         if (found.size()==0)
137             return false;
138         if (found.size()==1) {
139             msgState.msgTemplate="message.rgag.singular";
140             msgState.usercontext.getGroup().sendMessage(msgState.mp);
141             if (!msgState.sender.getGroup().equals(msgState.usercontext.getGroup())) {
142                 msgState.msgTemplate="message.rgag.confirm.singular";
143                 msgState.sender.sendMessage(msgState.mp);
144             }
145             return true;
146         }
147         msgState.msgTemplate="message.rgag.confirm.plural";
148         msgState.usrList = found.toArray();
149         msgState.sender.sendMessage (msgState.mp);
150         gul.sendMessage(msgState, "message.rgag", false);
151         return true;
152     }
153 }
154
Popular Tags