KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > commands > CmdAck


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

34 public class CmdAck extends AbstractCommand {
35     public final String JavaDoc cmd= "/ack";
36     private static final ICommand selve=new CmdAck();
37
38     private CmdAck () { }
39     
40     public static ICommand getInstance () {
41         return selve;
42     }
43     
44     public boolean execute (MessageState msgState, String JavaDoc param) {
45         if (!msgState.sender.hasRight(IUserStates.IS_MODERATOR)) {
46             msgState.msgTemplate = "error.noRight.noModAdmin";
47             msgState.sender.sendMessage (msgState.mp);
48             return false;
49         }
50         if (!msgState.sender.getGroup().hasState(IGroupState.MODERATED)) {
51                     msgState.targetGroup=msgState.sender.getGroup();
52                     msgState.msgTemplate = "error.ack.groupNotModerated";
53                     msgState.sender.sendMessage (msgState.mp);
54                     return false;
55                 }
56         if (param.length () < 1) {
57             msgState.msgTemplate = "error.ack.noArg";
58             msgState.sender.sendMessage (msgState.mp);
59             return false;
60         }
61         
62         int pos = param.indexOf(" ");
63         if (pos == -1) {
64             msgState.msgTemplate = "error.ack.noMessage";
65             msgState.sender.sendMessage (msgState.mp);
66             return false;
67         }
68         String JavaDoc uname = param.substring(0,pos).trim();
69         User u = getUser(msgState, uname);
70         if (u == null)
71             return false;
72         String JavaDoc msg = param.substring (pos).trim();
73         User moderator = msgState.sender;
74         Group g = moderator.getGroup();
75         if (!g.equals (u.getGroup())) {
76             msgState.msgTemplate = "error.ack.userNotInGroup";
77             moderator.sendMessage (msgState.mp);
78             return false;
79         }
80         msgState.sender = u;
81         if (msg.startsWith ("/")) {
82             pos = msg.indexOf (" ");
83             ICommand ic = CommandSet.getCommandSet().getCommand(msg.substring (0,pos));
84             ic.execute (msgState, msg.substring (pos).trim ());
85         } else {
86             msgState.msgTemplate = "message.send";
87             msgState.message = msg;
88             g.sendModeratedMessage (msgState.mp);
89         }
90         msgState.msgTemplate="message.ack";
91         msgState.message = msg;
92         msgState.sender = moderator;
93         moderator.sendMessage (msgState.mp);
94         return true;
95     }
96 }
97
Popular Tags