/** Check for new client connections */ private void acceptNewConnections() { try { SocketChannel clientChannel; // since sSockChan is non-blocking, this will return immediately // regardless of whether there is a connection available while ((clientChannel = sSockChan.accept()) != null) { if (getContext().getServer().isRedirectActive()) { LOG.debug( "Client redirected to {}: {}", getContext().getServer().getRedirectAddress().getHostAddress(), clientChannel.socket().getInetAddress().getHostAddress()); redirectAndKill(clientChannel.socket()); continue; } Client client = getContext().getClients().addNewClient(clientChannel, readSelector, SEND_BUFFER_SIZE); if (client == null) { continue; } // from this point on, we know that client // has been successfully connected client.sendWelcomeMessage(); LOG.debug("New client connected: {}", client.getIp().getHostAddress()); } } catch (Exception ex) { LOG.error("Exception in acceptNewConnections(): " + ex.getMessage(), ex); } }
private Client getClientByIp(String ip) { for (Client client : clients) { if (client.getIp().equals(ip)) { return client; } } Client newClient = new Client(ip); clients.add(newClient); return newClient; }
/** 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; }