KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > commands > CmdSepa


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

34 public class CmdSepa extends AbstractCommand {
35     public final String JavaDoc cmd= "/sepa";
36     private static final ICommand selve=new CmdSepa();
37
38     private CmdSepa () { }
39
40     public static ICommand getInstance () {
41         return selve;
42     }
43     
44     public boolean execute (MessageState msgState, String JavaDoc param) {
45         if (isPunished (msgState))
46             return false;
47         if (param.length () < 1) {
48             msgState.msgTemplate = "error.sepa.noArg";
49             msgState.sender.sendMessage (msgState.mp);
50             return false;
51         }
52         int pos = param.indexOf (":");
53         String JavaDoc joinGroup;
54         String JavaDoc topic = null;
55         if (pos > -1) {
56             joinGroup = param.substring (0,pos).trim();
57             topic = param.substring (pos+1).trim();
58         } else {
59             joinGroup = param;
60         }
61         if (joinGroup.length() <1 ) {
62             msgState.msgTemplate="error.sepa.noArg";
63             msgState.sender.sendMessage (msgState.mp);
64             return false;
65         }
66         if (Server.srv.MAX_GROUPNAME_LENGTH > 0
67                 && joinGroup.length() > Server.srv.MAX_GROUPNAME_LENGTH)
68             joinGroup = joinGroup.substring (0,Server.srv.MAX_GROUPNAME_LENGTH);
69         if (joinGroup.equalsIgnoreCase ("exil") ||
70             !msgState.sender.hasRight (IUserStates.MAY_OPEN_GROUP)) {
71             msgState.param = joinGroup;
72             msgState.msgTemplate = "error.sepa.noRight";
73             msgState.sender.sendMessage (msgState.mp);
74             return false;
75         }
76         msgState.sourceGroup = msgState.sender.getGroup ();
77         msgState.targetGroup = GroupManager.mgr.getGroup(joinGroup);
78         if (msgState.sender.getGroup().equals(msgState.targetGroup)) {
79             msgState.msgTemplate = "error.sepa.alreadyHere";
80             msgState.sender.sendMessage(msgState.mp);
81             return false;
82         }
83         if (msgState.targetGroup != null) {
84             msgState.msgTemplate = "error.sepa.alreadyExists";
85             msgState.sender.sendMessage(msgState.mp);
86             return false;
87         }
88         msgState.targetGroup = GroupManager.mgr.openGroup (joinGroup, topic, msgState.sender);
89         if (msgState.targetGroup == null) {
90             msgState.param = joinGroup;
91             msgState.msgTemplate = "error.sepa.noRight";
92             msgState.sender.sendMessage (msgState.mp);
93             return false;
94         }
95         if (!msgState.targetGroup.usrMayLock(msgState.sender)) {
96             msgState.msgTemplate = "message.user.leaving.group";
97             msgState.sourceGroup.sendMessage(msgState.mp);
98             msgState.msgTemplate = "message.j";
99             msgState.sender.sendMessage(msgState.mp);
100             msgState.msgTemplate = "error.sepa.l.noRight";
101             msgState.sender.sendMessage (msgState.mp);
102             return false;
103         }
104         msgState.targetGroup.unsetState(IGroupState.OPEN);
105         msgState.msgTemplate="message.sepa";
106         msgState.sourceGroup.sendMessage(msgState.mp);
107         msgState.msgTemplate="message.sepa.confirm";
108         msgState.sender.sendMessage(msgState.mp);
109         return true;
110     }
111 }
112
Popular Tags