KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > commands > AbstractCommand


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.Vector JavaDoc;
23
24 import freecs.Server;
25 import freecs.interfaces.ICommand;
26 import freecs.interfaces.IUserStates;
27 import freecs.layout.TemplateManager;
28 import freecs.layout.TemplateSet;
29 import freecs.content.MessageState;
30 import freecs.core.Group;
31 import freecs.core.MessageRenderer;
32 import freecs.core.User;
33 import freecs.core.UserManager;
34
35 /**
36  * @author Manfred Andres
37  *
38  * freecs.commands
39  */

40 public abstract class AbstractCommand implements ICommand {
41     private final String JavaDoc cmd = "Abstract";
42     private final String JavaDoc toStringValue = createToStringValue();
43     
44     public boolean execute (MessageState msgState, String JavaDoc param) {
45         Server.log (this, "not implemented", Server.MSG_ERROR, Server.LVL_MINOR);
46         return false;
47     }
48     
49     
50     boolean isPunished (MessageState msgState) {
51         if (!msgState.sender.isPunished ())
52             return false;
53         msgState.msgTemplate = "error.user.punished";
54         msgState.sender.sendMessage(msgState.mp);
55         return true;
56     }
57     
58     /**
59      * Utility method for retrieving an User. If the user
60      * hasn't been found the apropriate Message will be sent
61      * to the sender.
62      * @param msgState
63      * @param uname
64      * @return
65      */

66     User getUser (MessageState msgState, String JavaDoc uname) {
67         User ru = UserManager.mgr.getUserByName (uname);
68         if (ru == null) {
69            msgState.msgTemplate = "error.user.notOnServer.singular";
70            msgState.param = uname;
71            msgState.sender.sendMessage (msgState.mp);
72         }
73         return ru;
74     }
75
76     /**
77      * Utility method setting the invitation-state for one specific user
78      * @param msgState The message-state to operate on
79      * @param cu The user to set the invitation for
80      * @return boolean true if invitation was successful, false if not
81      */

82     boolean setInvited (MessageState msgState, User cu) {
83         User tusr = msgState.usercontext;
84         msgState.usercontext = cu;
85         if (msgState.sender.equals(cu.invitedBy())
86             && msgState.targetGroup.equals(cu.invitedTo())) {
87             msgState.msgTemplate = "error.i.alreadyInvited";
88             msgState.sender.sendMessage (msgState.mp);
89             msgState.usercontext = tusr;
90             return false;
91         }
92         if (msgState.targetGroup.equals (cu.getGroup())) {
93             msgState.msgTemplate = "error.i.alreadyHere";
94             msgState.sender.sendMessage (msgState.mp);
95             msgState.usercontext = tusr;
96             return false;
97         }
98         if (!cu.invitedFrom (msgState.sender)) {
99             msgState.usercontext = tusr;
100             return false;
101         }
102         if (msgState.msgTemplate==null)
103             return true;
104         cu.sendMessage(msgState.mp);
105         return true;
106     }
107     
108     boolean isVip (User u, Group g, MessageState msgState) {
109         if (u.hasRight(IUserStates.ROLE_GOD))
110             return true;
111         if (!u.hasRight(IUserStates.ROLE_VIP))
112             return false;
113         if (g.usrIsMember(u))
114             return true;
115         msgState.usrList = g.getMemberships();
116         return false;
117     }
118
119     String JavaDoc getUserList (MessageState msgState, boolean shorten) {
120         TemplateSet ts = msgState.sender.getTemplateSet ();
121         String JavaDoc s = generateUserList (msgState, shorten, ts);
122         if (s.length () == 0)
123             return null;
124         msgState.msgTemplate = "list.users";
125         msgState.message = s;
126         String JavaDoc tpl = msgState.sender.getTemplateSet().getMessageTemplate("list.users");
127         return MessageRenderer.renderTemplate (msgState, tpl);
128     }
129
130     public String JavaDoc generateUserList (MessageState msgState, boolean shorten, TemplateSet ts) {
131         if (ts == null) {
132             ts = TemplateManager.mgr.getTemplateSet();
133         }
134         Group g = msgState.sourceGroup;
135         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
136         String JavaDoc tplcnt = ts.getMessageTemplate (shorten ? "message.user.short" : "message.user.overview");
137         String JavaDoc seperator = ts.getMessageTemplate ("message.user.short.seperator");
138         if (!shorten)
139             sb.append ("<br>");
140         if (seperator != null)
141             seperator = MessageRenderer.renderTemplate (msgState, seperator, false);
142         int cnt = 0;
143         boolean success = false;
144         User[] usrs = g.getUserArray();
145         if (usrs==null)
146             return "";
147         for (int i = 0; i < usrs.length; i++) {
148             msgState.usercontext = usrs[i];
149             String JavaDoc snippet = MessageRenderer.renderTemplate (msgState, tplcnt, false);
150             sb.append (snippet == null ? msgState.usercontext.getName () : snippet);
151             if (i<(usrs.length-1) && (shorten || snippet == null)) {
152                 if (seperator != null)
153                     sb.append (seperator);
154                 else
155                     sb.append (", ");
156             }
157         }
158         return sb.toString();
159     }
160
161     boolean sendUserList (MessageState msgState, boolean shorten) {
162         TemplateSet ts = msgState.sender.getTemplateSet ();
163         String JavaDoc s = generateUserList (msgState, shorten, ts);
164         if (s.length () == 0)
165             return false;
166         msgState.msgTemplate = "list.users";
167         msgState.message = s;
168         msgState.sender.sendMessage (msgState.mp);
169         return true;
170     }
171
172     /**
173      * Splits up a " " (space)- seperated userlist and returns a vector containing the
174      * found user-objects. Not-found-users will be collected and an "error.user.notOnServer.plural"
175      * will be sent to the sender.
176      * @param msgState the message-state to operate on
177      * @param param the string containing the users
178      * @return a vector containing all found user-objects
179      */

180     public Vector JavaDoc getMultibleUsers (MessageState msgState, String JavaDoc param) {
181         String JavaDoc usrs[] = param.split(" ");
182         Vector JavaDoc found = new Vector JavaDoc();
183         Vector JavaDoc notfound = new Vector JavaDoc();
184         for (int i = 0; i < usrs.length; i++) {
185             User cu = UserManager.mgr.getUserByName(usrs[i]);
186             if (cu == null) {
187                 notfound.add(usrs[i]);
188                 continue;
189             }
190             found.add(cu);
191         }
192
193         if (notfound.size() > 1) {
194             msgState.usrList = notfound.toArray();
195             msgState.msgTemplate = "error.user.notOnServer.plural";
196             msgState.sender.sendMessage(msgState.mp);
197         } else if (notfound.size() == 1) {
198             msgState.param = (String JavaDoc) notfound.get(0);
199             msgState.msgTemplate = "error.user.notOnServer.singular";
200             msgState.sender.sendMessage(msgState.mp);
201         }
202         return found;
203     }
204
205     public void sendPrivateMessage (MessageState msgState, User cu, String JavaDoc message) {
206         msgState.sender.setPrivateUser (cu);
207         cu.setForeignPrivateUser (msgState.sender);
208         msgState.usercontext = cu;
209         msgState.msgTemplate = "message.m";
210         cu.sendMessage (msgState.mp);
211         if (cu.isAway ()) msgState.msgTemplate = "message.m.away";
212         else msgState.msgTemplate = "message.m.confirm";
213         msgState.sender.sendMessage (msgState.mp);
214     }
215
216     /**
217      * .) check the permissions of sender for the right to change su-state for this user
218      * .) set the su-state wanted
219      * @param msgState the message-state-object containing all infos
220      * @param cu the user which will be changed
221      * @param give give or take su
222      * @return true on success, false otherwise
223      */

224     boolean setSuRight (MessageState msgState, User cu, boolean give) {
225         if (cu.hasRight (IUserStates.PROTECTED_FROM_RIGHTCHANGE))
226             return false;
227         Group cug = cu.getGroup ();
228         if (!cug.usrMaySetSu (msgState.sender))
229             return false;
230         if (give)
231            return cug.addToSusers (cu);
232         else
233            cug.removeFromSusers (cu);
234        return true;
235     }
236     
237     public String JavaDoc toString () {
238         return toStringValue;
239     }
240
241     public static short _isColorCodeValid (String JavaDoc col) {
242         if (col.length() != 6)
243             return 1;
244         for (int i = 0; i < col.length (); i++) {
245             char c = col.charAt (i);
246             if (c != '0' && c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6' && c != '7' && c != '8' && c != '9' && c != 'a' && c != 'b' && c != 'c' && c != 'd' && c != 'e' && c != 'f') {
247                return 1;
248             }
249         }
250         int r = Integer.parseInt (col.substring (0,2), 16);
251         int g = Integer.parseInt (col.substring (2,4), 16);
252         int b = Integer.parseInt (col.substring (4,6), 16);
253         int sum = r + g + b;
254         if ((765 - sum) < 64)
255            return 2;
256         return 0;
257     }
258
259     public boolean isColorCodeValid (MessageState msgState) {
260         String JavaDoc colcode = msgState.param.trim().toLowerCase();
261         short result = _isColorCodeValid(colcode);
262         if (result==0)
263             return true;
264         if (result==1)
265             msgState.msgTemplate = "error.col.wrongCode";
266         else if (result==2)
267             msgState.msgTemplate = "error.col.lockedColor";
268         return false;
269     }
270
271     private String JavaDoc createToStringValue() {
272         String JavaDoc fullName = getClass().getName();
273         return "[Command " + cmd + "]";
274     }
275 }
276
Popular Tags