diff options
Diffstat (limited to 'juick-commands/src')
5 files changed, 167 insertions, 0 deletions
diff --git a/juick-commands/src/main/java/com/juick/command/Command.java b/juick-commands/src/main/java/com/juick/command/Command.java new file mode 100644 index 00000000..3d974bba --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/Command.java @@ -0,0 +1,16 @@ +package com.juick.command; + +import com.juick.User; +import com.juick.server.protocol.ProtocolListener; + +import java.util.regex.Pattern; + +/** + * @author ma1uta + */ +public interface Command { + + Pattern pattern(); + + String execute(User sender, ProtocolListener protocolListener, String command); +} diff --git a/juick-commands/src/main/java/com/juick/command/MultiArgsCommand.java b/juick-commands/src/main/java/com/juick/command/MultiArgsCommand.java new file mode 100644 index 00000000..5fde6775 --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/MultiArgsCommand.java @@ -0,0 +1,24 @@ +package com.juick.command; + +import com.juick.User; +import com.juick.server.protocol.ProtocolListener; + +import java.util.regex.Matcher; + +/** + * @author ma1uta + */ +public abstract class MultiArgsCommand implements Command { + + @Override + public String execute(User sender, ProtocolListener protocolListener, String command) { + Matcher matcher = pattern().matcher(command.trim()); + String[] arguments = new String[matcher.groupCount()]; + for (int i = 1; i <= matcher.groupCount(); i++) { + arguments[i - 1] = (matcher.group(i)); + } + return execute(sender, protocolListener, arguments); + } + + protected abstract String execute(User sender, ProtocolListener protocolListener, String... arguments); +} diff --git a/juick-commands/src/main/java/com/juick/command/Ping.java b/juick-commands/src/main/java/com/juick/command/Ping.java new file mode 100644 index 00000000..7d081b41 --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/Ping.java @@ -0,0 +1,26 @@ +package com.juick.command; + +import com.juick.User; +import com.juick.server.protocol.ProtocolListener; +import org.springframework.stereotype.Component; + +import java.util.regex.Pattern; + +/** + * @author ma1uta + */ +@Component +public class Ping implements Command { + + private static final Pattern PATTERN = Pattern.compile("^ping$", Pattern.CASE_INSENSITIVE); + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + public String execute(User sender, ProtocolListener protocolListener, String command) { + return "PONG"; + } +} diff --git a/juick-commands/src/main/java/com/juick/command/PrivateMessage.java b/juick-commands/src/main/java/com/juick/command/PrivateMessage.java new file mode 100644 index 00000000..1e02f1da --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/PrivateMessage.java @@ -0,0 +1,53 @@ +package com.juick.command; + +import com.juick.User; +import com.juick.server.protocol.ProtocolListener; +import com.juick.service.PMQueriesService; +import com.juick.service.UserService; +import lombok.Getter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.regex.Pattern; + +/** + * @author ma1uta + */ +@Component +@Getter +public class PrivateMessage extends MultiArgsCommand { + + private static final Pattern PATTERN = Pattern.compile("^@(\\S+)\\s+([\\s\\S]+)$", Pattern.CASE_INSENSITIVE); + + private final UserService userService; + private final PMQueriesService pmQueriesService; + + @Autowired + public PrivateMessage(UserService userService, PMQueriesService pmQueriesService) { + this.userService = userService; + this.pmQueriesService = pmQueriesService; + } + + @Override + public Pattern pattern() { + return PATTERN; + } + + @Override + protected String execute(User sender, ProtocolListener protocolListener, String... arguments) { + String addressee = arguments[0]; + String body = arguments[1]; + + User toUser = getUserService().getUserByName(addressee); + + if (toUser.getUid() > 0) { + if (!getUserService().isInBLAny(toUser.getUid(), sender.getUid())) { + if (getPmQueriesService().createPM(sender.getUid(), toUser.getUid(), body)) { + protocolListener.privateMessage(sender, toUser, body); + return "Private message sent"; + } + } + } + return "Error"; + } +} diff --git a/juick-commands/src/main/java/com/juick/command/Processor.java b/juick-commands/src/main/java/com/juick/command/Processor.java new file mode 100644 index 00000000..bf38a1b3 --- /dev/null +++ b/juick-commands/src/main/java/com/juick/command/Processor.java @@ -0,0 +1,48 @@ +package com.juick.command; + +import com.juick.User; +import com.juick.server.protocol.ProtocolListener; +import lombok.Getter; +import lombok.Setter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.Set; + +/** + * @author ma1uta + */ +@Component +@Getter +public class Processor { + + private static final Logger LOGGER = LoggerFactory.getLogger(Processor.class); + + private final Set<Command> commands; + @Setter + private ProtocolListener protocolListener; + + @Autowired + public Processor(Set<Command> commands) { + this.commands = commands; + } + + public String execute(User sender, String command) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Execute command: {}", command); + } + for (Command commandItem : getCommands()) { + if (commandItem.pattern().matcher(command).matches()) { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("Found command processor: {}", commandItem.getClass().getName()); + } + return commandItem.execute(sender, command); + } + } + + LOGGER.debug("Command processor not found, return help"); + return ""; + } +} |