aboutsummaryrefslogtreecommitdiff
path: root/juick-commands/src/main/java/com/juick/command/PrivateMessage.java
diff options
context:
space:
mode:
Diffstat (limited to 'juick-commands/src/main/java/com/juick/command/PrivateMessage.java')
-rw-r--r--juick-commands/src/main/java/com/juick/command/PrivateMessage.java53
1 files changed, 53 insertions, 0 deletions
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";
+ }
+}