1 20 21 package freecs.commands; 22 import java.util.Vector ; 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 40 public abstract class AbstractCommand implements ICommand { 41 private final String cmd = "Abstract"; 42 private final String toStringValue = createToStringValue(); 43 44 public boolean execute (MessageState msgState, String 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 66 User getUser (MessageState msgState, String 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 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 getUserList (MessageState msgState, boolean shorten) { 120 TemplateSet ts = msgState.sender.getTemplateSet (); 121 String s = generateUserList (msgState, shorten, ts); 122 if (s.length () == 0) 123 return null; 124 msgState.msgTemplate = "list.users"; 125 msgState.message = s; 126 String tpl = msgState.sender.getTemplateSet().getMessageTemplate("list.users"); 127 return MessageRenderer.renderTemplate (msgState, tpl); 128 } 129 130 public String generateUserList (MessageState msgState, boolean shorten, TemplateSet ts) { 131 if (ts == null) { 132 ts = TemplateManager.mgr.getTemplateSet(); 133 } 134 Group g = msgState.sourceGroup; 135 StringBuffer sb = new StringBuffer (); 136 String tplcnt = ts.getMessageTemplate (shorten ? "message.user.short" : "message.user.overview"); 137 String 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 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 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 180 public Vector getMultibleUsers (MessageState msgState, String param) { 181 String usrs[] = param.split(" "); 182 Vector found = new Vector (); 183 Vector notfound = new Vector (); 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 ) 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 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 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 toString () { 238 return toStringValue; 239 } 240 241 public static short _isColorCodeValid (String 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 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 createToStringValue() { 272 String fullName = getClass().getName(); 273 return "[Command " + cmd + "]"; 274 } 275 } 276 | Popular Tags |