/** Note: this method is not synchronized! Note2: this method may be called recursively! */ public boolean executeCommand(String command, Client client) { String commandClean = command.trim(); if (commandClean.isEmpty()) { return false; } if (LOG.isTraceEnabled()) { LOG.trace( "[<-{}] \"{}\"", (client.getAccount().getAccess() != Account.Access.NONE) ? client.getAccount().getName() : client.getIp().getHostAddress(), commandClean); } int msgId = Client.NO_MSG_ID; if (commandClean.charAt(0) == '#') { try { if (!commandClean.matches("^#\\d+\\s[\\s\\S]*")) { return false; // malformed command } msgId = Integer.parseInt(commandClean.substring(1).split("\\s")[0]); // remove id field from the rest of command: commandClean = commandClean.replaceFirst("#\\d+\\s", ""); } catch (NumberFormatException ex) { return false; // this means that the command is malformed } catch (PatternSyntaxException ex) { return false; // this means that the command is malformed } } // parse command into tokens: String[] commands = commandClean.split(" "); commands[0] = commands[0].toUpperCase(); client.setSendMsgId(msgId); try { CommandProcessor cp = getContext().getCommandProcessors().get(commands[0]); if (cp != null) { List<String> args = new ArrayList<String>(Arrays.asList(commands)); args.remove(0); try { boolean ret = cp.process(client, args); if (!ret) { return false; } } catch (CommandProcessingException ex) { LOG.debug( cp.getClass().getCanonicalName() + " failed to handle command from client: \"" + Misc.makeSentence(commands) + "\"", ex); return false; } } else if (deprecatedCommands.containsKey(commands[0])) { DeprecatedCommand deprecatedCommand = deprecatedCommands.get(commands[0]); client.sendLine( String.format( "SERVERMSG Command %s is deprecated: %s", deprecatedCommand.getName(), deprecatedCommand.getMessage())); } else { // unknown command! return false; } } finally { client.setSendMsgId(Client.NO_MSG_ID); } return true; }
// TODO make all this stuff non-static private static void add( Map<String, DeprecatedCommand> deprecatedCommands, DeprecatedCommand command) { deprecatedCommands.put(command.getName(), command); }