KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > commands > CmdPunish


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.ScheduledAction;
29 import freecs.core.User;
30 import freecs.core.UserManager;
31 import freecs.interfaces.ICommand;
32 import freecs.interfaces.IUserStates;
33 import freecs.util.GroupUserList;
34
35 /**
36  * @author Manfred Andres
37  *
38  * freecs.commands
39  */

40 public class CmdPunish extends AbstractCommand {
41     public final String JavaDoc cmd= "/gag";
42     private static final ICommand selve=new CmdPunish();
43
44     private CmdPunish () { }
45     
46     public static ICommand getInstance () {
47         return selve;
48     }
49     
50     public boolean execute (MessageState msgState, String JavaDoc param) {
51         if (isPunished (msgState))
52             return false;
53         boolean mayPunish = msgState.sender.hasRight (IUserStates.MAY_PUNISH);
54         Group sg = msgState.sender.getGroup ();
55         boolean isSu = sg.usrIsSu (msgState.sender);
56         if (!mayPunish && !isSu) {
57             msgState.msgTemplate = "error.noRight.noSuVipAdmin";
58             msgState.sender.sendMessage (msgState.mp);
59             return false;
60         }
61         if (param.length () < 1) {
62             msgState.msgTemplate = "error.gag.noArg";
63             msgState.sender.sendMessage (msgState.mp);
64             return false;
65         }
66         int pos = param.indexOf(":");
67         if (pos != -1) {
68             msgState.reason = param.substring (pos+1);
69             param = param.substring (0, pos);
70         } else
71             msgState.reason = "";
72
73         if (param.indexOf(" ") > -1
74             && !mayPunish) {
75             msgState.msgTemplate = "error.mass.noRight.noVipAdmin";
76             msgState.sender.sendMessage (msgState.mp);
77             return false;
78         }
79         
80         // try to treat the last argument before the ":" as timeamount
81
// after this time, the punishment will be removed
82
pos = param.lastIndexOf(" ");
83         long secs = -1;
84         if (pos > -1) try {
85             secs = Long.parseLong (param.substring (pos+1), 10);
86             param = param.substring (0, pos);
87         } catch (NumberFormatException JavaDoc nfe) {
88             // wasn't a number so treat it as user
89
}
90         GroupUserList gul = new GroupUserList();
91         Vector JavaDoc found = getMultibleUsers(msgState, param);
92         msgState.useRenderCache = false;
93         for (Iterator JavaDoc i = found.iterator(); i.hasNext (); ) {
94             User cu = (User) i.next();
95             if (cu.equals(msgState.sender)) {
96                 i.remove();
97                 continue;
98             }
99             if (!mayPunish
100                 && !msgState.sender.getGroup().equals(cu.getGroup())) {
101                 msgState.msgTemplate = "error.noRight.noVipAdmin";
102                 User u = msgState.usercontext;
103                 msgState.usercontext = cu;
104                 msgState.sender.sendMessage (msgState.mp);
105                 msgState.usercontext = u;
106                 i.remove();
107                 continue;
108             }
109             /**
110             if (!cs.setPunishment (msgState, cu, true)) {
111                 i.remove();
112                 continue;
113             }*/

114             
115             boolean p = true;
116             if ((p && msgState.sender.isPunished())
117                     || cu == null || cu.isPunished () == p)
118                         return false;
119                 if (!cu.hasRight(IUserStates.FREELY_PUNISHABLE)) {
120                     if (cu.hasRight(IUserStates.ROLE_GOD)) {
121                         msgState.msgTemplate = "error.noRight.isAdmin";
122                         msgState.usercontext = cu;
123                         msgState.sender.sendMessage (msgState.mp);
124                         i.remove();
125                         continue;
126                     }
127                     if (cu.hasRight(IUserStates.ROLE_VIP)
128                         && !msgState.sender.hasRight(IUserStates.ROLE_VIP)) {
129                             msgState.msgTemplate = "error.noRight.isVip";
130                             msgState.usercontext = cu;
131                             msgState.sender.sendMessage (msgState.mp);
132                             i.remove();
133                             continue;
134                     }
135                     if (cu.hasRight (IUserStates.PROTECTED_FROM_PUNISH)
136                         && !msgState.sender.hasRight(IUserStates.ROLE_GOD)) {
137                             msgState.msgTemplate = "error.gag.protected";
138                             msgState.usercontext = cu;
139                             msgState.sender.sendMessage (msgState.mp);
140                             i.remove();
141                             continue;
142                     }
143                 }
144             cu.setPunish (p);
145             if (found.size() > 1) {
146                 msgState.msgTemplate= "message.gag.personal";
147                 cu.sendMessage (msgState.mp);
148             }
149             gul.addUser(cu);
150             msgState.usercontext = cu;
151             if (secs > 0) {
152                 long millsecs = secs * 1000 + System.currentTimeMillis();
153                 UserManager.mgr.scheduleAction(ScheduledAction.UNPUNISH, millsecs, cu, msgState.sender);
154             }
155         }
156         if (found.size()==0)
157             return false;
158         if (found.size()==1) {
159             msgState.msgTemplate="message.gag.singular";
160             if (msgState.usercontext.getGroup() != null)
161                 msgState.usercontext.getGroup().sendMessage(msgState.mp);
162             if (!msgState.sender.getGroup().equals(msgState.usercontext.getGroup())) {
163                 msgState.msgTemplate="message.gag.confirm.singular";
164                 msgState.sender.sendMessage(msgState.mp);
165             }
166             return true;
167         }
168         msgState.msgTemplate="message.gag.confirm.plural";
169         msgState.usrList = found.toArray();
170         msgState.sender.sendMessage (msgState.mp);
171         gul.sendMessage(msgState, "message.gag", false);
172         return true;
173     }
174 }
175
Popular Tags