diff options
author | Anatoliy Sablin | 2017-11-06 15:45:55 +0300 |
---|---|---|
committer | Anatoliy Sablin | 2017-11-06 15:45:55 +0300 |
commit | 41b885d84958d07ade536850829b3ba394a4f8f9 (patch) | |
tree | 93661b9f558c4234afd4e5a84b0442fdb27454a2 /juick-commands/src/main/java/com/juick/command/Processor.java | |
parent | ce0ed9ac9e82b19763b7dfd4814d327e0b49ddb7 (diff) |
Added a few commands.
Diffstat (limited to 'juick-commands/src/main/java/com/juick/command/Processor.java')
-rw-r--r-- | juick-commands/src/main/java/com/juick/command/Processor.java | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/juick-commands/src/main/java/com/juick/command/Processor.java b/juick-commands/src/main/java/com/juick/command/Processor.java index bf38a1b3..51ce2830 100644 --- a/juick-commands/src/main/java/com/juick/command/Processor.java +++ b/juick-commands/src/main/java/com/juick/command/Processor.java @@ -10,6 +10,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Set; +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import javax.annotation.PostConstruct; /** * @author ma1uta @@ -23,26 +26,48 @@ public class Processor { private final Set<Command> commands; @Setter private ProtocolListener protocolListener; + private String help; @Autowired public Processor(Set<Command> commands) { this.commands = commands; } + @PostConstruct + public void init() { + this.help = + "HELP - returns this help message.\n" + getCommands().stream().map(Command::help).sorted().collect(Collectors.joining("\n")); + } + public String execute(User sender, String command) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Execute command: {}", command); } + command = command != null ? command.trim() : ""; + if ("help".equalsIgnoreCase(command)) { + LOGGER.debug("Show help"); + return getHelp(); + } + Command defaultCommand = null; for (Command commandItem : getCommands()) { - if (commandItem.pattern().matcher(command).matches()) { + Pattern pattern = commandItem.pattern(); + if (pattern != null && pattern.matcher(command).matches()) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Found command processor: {}", commandItem.getClass().getName()); } - return commandItem.execute(sender, command); + return commandItem.execute(sender, getProtocolListener(), command); + } else { + defaultCommand = commandItem; } } + if (defaultCommand != null) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Found default command processor: {}", defaultCommand.getClass().getName()); + } + return defaultCommand.execute(sender, getProtocolListener(), command); + } LOGGER.debug("Command processor not found, return help"); - return ""; + return getHelp(); } } |