KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > commands > CmdJoin


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.core.GroupManager;
26 import freecs.content.MessageState;
27
28 /**
29  * @author Manfred Andres
30  *
31  * freecs.commands
32  */

33 public class CmdJoin extends AbstractCommand {
34     public final String JavaDoc cmd= "/j";
35     private static final ICommand selve=new CmdJoin();
36
37     private CmdJoin () { }
38     
39     public static ICommand getInstance () {
40         return selve;
41     }
42     
43     public boolean execute (MessageState msgState, String JavaDoc param) {
44         if (param.length () < 1) {
45             msgState.msgTemplate="error.j.noArg";
46             msgState.sender.sendMessage (msgState.mp);
47             return false;
48         }
49         msgState.sourceGroup=msgState.sender.getGroup();
50         String JavaDoc groupname;
51         String JavaDoc topic = null;
52         int pos = param.indexOf (":");
53         if (pos > -1) { // joincommand issued with topic
54
topic = param.substring (pos+1).trim ();
55             groupname = param.substring (0, pos).trim ();
56         } else {
57             groupname = param;
58         }
59         if (groupname.length() <1 ) {
60             msgState.msgTemplate="error.j.noArg";
61             msgState.sender.sendMessage (msgState.mp);
62             return false;
63         }
64         if (Server.srv.MAX_GROUPNAME_LENGTH > 0
65             && groupname.length() > Server.srv.MAX_GROUPNAME_LENGTH)
66             groupname = groupname.substring (0,Server.srv.MAX_GROUPNAME_LENGTH);
67         if (msgState.sourceGroup.getRawName ().equalsIgnoreCase (groupname)) {
68             msgState.targetGroup=msgState.sourceGroup;
69             msgState.msgTemplate="error.j.alreadyHere";
70             msgState.sender.sendMessage (msgState.mp);
71             return false;
72         }
73         msgState.sourceGroup = msgState.sender.getGroup ();
74         boolean senderWasSu = msgState.sourceGroup.usrIsSu(msgState.sender);
75         msgState.targetGroup = GroupManager.mgr.getGroup (groupname);
76         if (!GroupManager.mgr.isStartingGroup(groupname)
77             && isPunished (msgState))
78                 return false;
79         
80         if (msgState.sender.isPunished() ) {
81             if (msgState.targetGroup != null && msgState.targetGroup.usrMayJoinPunished(msgState.sender)){
82                 msgState.targetGroup.incrementJoinPunishedCounter();
83             } else {
84                 msgState.msgTemplate = "error.user.punished";
85                 msgState.sender.sendMessage(msgState.mp);
86                 return false;
87             }
88         }
89         if (!msgState.sender.isPunished())
90             msgState.sourceGroup.resetJoinPunishedCounter();
91         if (msgState.targetGroup != null
92             && msgState.targetGroup.usrIsBaned (msgState.sender.getName())) {
93                 msgState.msgTemplate = "error.j.banned";
94                 msgState.sender.sendMessage (msgState.mp);
95                 return false;
96         }
97         boolean created = false;
98         if (msgState.targetGroup == null) {
99             msgState.targetGroup = GroupManager.mgr.openGroup (groupname, topic, msgState.sender);
100             if (msgState.targetGroup == null) {
101                 msgState.param = groupname;
102                 msgState.msgTemplate = "error.j.noRightToOpen";
103                 msgState.sender.sendMessage (msgState.mp);
104                 return false;
105             }
106             created=true;
107         } else if (!msgState.targetGroup.hasState(IGroupState.OPEN)) {
108             msgState.msgTemplate = "error.j.closed";
109             msgState.sender.sendMessage (msgState.mp);
110             return false;
111         } else if (!msgState.targetGroup.usrMayJoin(msgState.sender)) {
112             msgState.msgTemplate = "error.j.noRight";
113             msgState.sender.sendMessage (msgState.mp);
114             return false;
115         }
116         if (!created) {
117             msgState.msgTemplate = "message.user.join.group";
118             msgState.targetGroup.sendMessage (msgState.mp);
119             msgState.targetGroup.addUser (msgState.sender);
120             msgState.msgTemplate = "message.j";
121             msgState.sender.sendMessage (msgState.mp);
122         } else {
123             msgState.msgTemplate = "message.j.created";
124             msgState.sender.sendMessage(msgState.mp);
125         }
126         if (msgState.sourceGroup.size() > 0) {
127             msgState.msgTemplate = "message.user.leaving.group";
128             msgState.sourceGroup.sendMessage (msgState.mp);
129         }
130         return true;
131     }
132 }
133
Popular Tags