From ce0ed9ac9e82b19763b7dfd4814d327e0b49ddb7 Mon Sep 17 00:00:00 2001 From: Anatoliy Sablin Date: Sun, 5 Nov 2017 10:33:34 +0300 Subject: juick-commands --- .../src/main/java/com/juick/command/Command.java | 16 +++++++ .../java/com/juick/command/MultiArgsCommand.java | 24 ++++++++++ .../src/main/java/com/juick/command/Ping.java | 26 +++++++++++ .../java/com/juick/command/PrivateMessage.java | 53 ++++++++++++++++++++++ .../src/main/java/com/juick/command/Processor.java | 48 ++++++++++++++++++++ 5 files changed, 167 insertions(+) create mode 100644 juick-commands/src/main/java/com/juick/command/Command.java create mode 100644 juick-commands/src/main/java/com/juick/command/MultiArgsCommand.java create mode 100644 juick-commands/src/main/java/com/juick/command/Ping.java create mode 100644 juick-commands/src/main/java/com/juick/command/PrivateMessage.java create mode 100644 juick-commands/src/main/java/com/juick/command/Processor.java (limited to 'juick-commands/src/main/java/com') 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 commands; + @Setter + private ProtocolListener protocolListener; + + @Autowired + public Processor(Set 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 ""; + } +} -- cgit v1.2.3