KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > commands > CmdChangeForeignColor


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

36 public class CmdChangeForeignColor extends AbstractCommand {
37     public final String JavaDoc cmd= "/fcol";
38     private static final ICommand selve=new CmdChangeForeignColor();
39
40     private CmdChangeForeignColor () { }
41     
42     public static ICommand getInstance () {
43         return selve;
44     }
45     
46     public boolean execute (MessageState msgState, String JavaDoc param) {
47         if (isPunished (msgState))
48             return false;
49         if (!msgState.sender.hasRight(IUserStates.ROLE_VIP)) {
50             msgState.msgTemplate = "error.noRight.noVipAdmin";
51             msgState.sender.sendMessage(msgState.mp);
52             return false;
53         }
54         if (param==null
55             || param.length() < 1) {
56             msgState.msgTemplate = "error.fcol.noArg";
57             msgState.sender.sendMessage(msgState.mp);
58             return false;
59         }
60         int pos = param.indexOf(" ");
61         if (pos == -1) {
62             msgState.msgTemplate = "error.fcol.noUser";
63             msgState.sender.sendMessage(msgState.mp);
64             return false;
65         }
66         msgState.param = param.substring(0,pos);
67         String JavaDoc colcode = msgState.param;
68         param = param.substring (pos+1);
69         if (!isColorCodeValid(msgState)) {
70             msgState.sender.sendMessage(msgState.mp);
71             return false;
72         }
73         Vector JavaDoc found = getMultibleUsers(msgState, param);
74         GroupUserList gul = new GroupUserList();
75         for (Iterator JavaDoc i = found.iterator(); i.hasNext(); ) {
76             User u = (User) i.next();
77             msgState.param = colcode;
78             if (u.equals(msgState.sender)) {
79                 i.remove();
80                 continue;
81             } else if (isVip(u, u.getGroup(), msgState)
82                     && !isVip(msgState.sender, u.getGroup(), msgState)) {
83                         msgState.msgTemplate = "error.noRight.noVipAdmin";
84                         msgState.sender.sendMessage(msgState.mp);
85                         i.remove();
86                         continue;
87             } else if (!isVip(msgState.sender, u.getGroup(), msgState)
88                     && !msgState.sender.hasRight(IUserStates.ROLE_GOD)) {
89                         msgState.targetGroup = u.getGroup();
90                         if (msgState.usrList.length > 1)
91                             msgState.msgTemplate = "error.membership.notMember.many";
92                         else
93                             msgState.msgTemplate = "error.membership.notMember";
94                             msgState.usercontext=u;
95                             msgState.sender.sendMessage(msgState.mp);
96                             i.remove();
97                             continue;
98             }
99             u.setColCode(msgState.param);
100             msgState.msgTemplate = "message.fcol.personal";
101             msgState.usrList = found.toArray();
102             msgState.usercontext=u;
103             if (found.size() > 1)
104                 u.sendMessage(msgState.mp);
105             gul.addUser(u);
106         }
107         if (found.size() == 0)
108             return false;
109         if (found.size() == 1) {
110             msgState.usercontext = (User) found.get(0);
111             msgState.msgTemplate = "message.fcol.singular";
112             msgState.targetGroup = msgState.usercontext.getGroup();
113             if (msgState.targetGroup != null) {
114                 msgState.targetGroup.sendMessage(msgState.mp);
115                 if (!msgState.targetGroup.equals(msgState.sender.getGroup())) {
116                     msgState.msgTemplate = "message.fcol.confirm.singular";
117                     msgState.sender.sendMessage(msgState.mp);
118                 }
119             }
120             return true;
121         }
122         msgState.usrList = found.toArray();
123         msgState.msgTemplate = "message.fcol.confirm.plural";
124         msgState.sender.sendMessage(msgState.mp);
125         gul.sendMessage(msgState, "message.fcol", true);
126         return true;
127     }
128 }
129
Popular Tags