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.java65
1 files changed, 58 insertions, 7 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
index e41b0037..069bc367 100644
--- a/juick-commands/src/main/java/com/juick/command/PrivateMessage.java
+++ b/juick-commands/src/main/java/com/juick/command/PrivateMessage.java
@@ -6,8 +6,12 @@ import com.juick.service.PMQueriesService;
import com.juick.service.UserService;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
+import rocks.xmpp.addr.Jid;
+import rocks.xmpp.core.stanza.model.Message;
+import java.util.List;
import java.util.regex.Pattern;
/**
@@ -22,6 +26,9 @@ public class PrivateMessage extends MultiArgsCommand {
private final UserService userService;
private final PMQueriesService pmQueriesService;
+ @Value("${xmppbot_jid}")
+ private Jid jid;
+
@Autowired
public PrivateMessage(UserService userService, PMQueriesService pmQueriesService) {
this.userService = userService;
@@ -42,17 +49,61 @@ public class PrivateMessage extends MultiArgsCommand {
protected String execute(User sender, ProtocolListener protocolListener, String... arguments) {
String addressee = arguments[0];
String body = arguments[1];
+ int ret;
+
+ int addresseeUid;
+ List<String> jids_to = null;
+ boolean haveInRoster;
- User toUser = getUserService().getUserByName(addressee);
+ addresseeUid = addressee.indexOf('@') > 0 ? getUserService().getUIDbyJID(addressee) : getUserService().getUIDbyName(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";
+ if (addresseeUid > 0) {
+ if (!getUserService().isInBLAny(addresseeUid, sender.getUid())) {
+ if (getPmQueriesService().createPM(sender.getUid(), addresseeUid, body)) {
+ jids_to = getUserService().getJIDsbyUID(addresseeUid);
+ ret = 200;
+ } else {
+ ret = 500;
}
+ } else {
+ ret = 403;
}
+ } else {
+ ret = 404;
}
- return "Error";
+
+ if (ret == 200) {
+ Message msg = new Message();
+ msg.setFrom(getJid().asBareJid());
+ msg.setTo(Jid.of(Integer.toString(addresseeUid), "push.juick.com", null));
+ com.juick.Message jmsg = new com.juick.Message();
+ jmsg.setUser(sender);
+ jmsg.setText(body);
+ msg.addExtension(jmsg);
+ //TODO fix external dependency
+ //router.sendStanza(msg);
+
+ msg.setTo(Jid.of(Integer.toString(addresseeUid), "ws.juick.com", null));
+ //TODO fix external dependency
+ //router.sendStanza(msg);
+
+ for (String userJid : jids_to) {
+ Message mm = new Message();
+ mm.setTo(Jid.of(userJid));
+ mm.setType(Message.Type.CHAT);
+ haveInRoster = getPmQueriesService().havePMinRoster(sender.getUid(), userJid);
+ if (haveInRoster) {
+ mm.setFrom(Jid.of(sender.getName(), "juick.com", "Juick"));
+ mm.setBody(body);
+ } else {
+ mm.setFrom(getJid());
+ mm.setBody("Private message from @" + sender.getName() + ":\n" + body);
+ }
+ //TODO fix external dependency
+ //xmpp.sendOut(ClientMessage.from(mm));
+ }
+ }
+
+ return ret == 200 ? "Private message sent" : "Error " + ret;
}
}