aboutsummaryrefslogtreecommitdiff
path: root/juick-server-xmpp/src/main/java/com/juick/server
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-04-03 17:55:46 +0300
committerGravatar Vitaly Takmazov2018-04-03 17:55:54 +0300
commita5f045a3780ef8530afa7493cf090771b8fdcfe1 (patch)
tree56e4cbc6949dc9bd39672ccf379324338f0eef76 /juick-server-xmpp/src/main/java/com/juick/server
parentc53675e5eb19a1a05ae5edcd0322075d51628b96 (diff)
refactoring: all messages are processed with XMPPConnection
Diffstat (limited to 'juick-server-xmpp/src/main/java/com/juick/server')
-rw-r--r--juick-server-xmpp/src/main/java/com/juick/server/CommandsManager.java4
-rw-r--r--juick-server-xmpp/src/main/java/com/juick/server/XMPPConnection.java152
-rw-r--r--juick-server-xmpp/src/main/java/com/juick/server/xmpp/router/XMPPRouter.java4
3 files changed, 48 insertions, 112 deletions
diff --git a/juick-server-xmpp/src/main/java/com/juick/server/CommandsManager.java b/juick-server-xmpp/src/main/java/com/juick/server/CommandsManager.java
index 6458382f..26dafa69 100644
--- a/juick-server-xmpp/src/main/java/com/juick/server/CommandsManager.java
+++ b/juick-server-xmpp/src/main/java/com/juick/server/CommandsManager.java
@@ -44,9 +44,7 @@ import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
-import java.net.MalformedURLException;
import java.net.URI;
-import java.nio.file.Paths;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -470,7 +468,7 @@ public class CommandsManager {
int newrid = messagesService.createReply(mid, rid, user.getUid(), txt, attachmentType);
if (StringUtils.isNotEmpty(attachmentType)) {
String attachmentFName = attachment.getScheme().equals("juick") ? attachment.getHost()
- : HttpUtils.downloadImage(attachment.toURL(), tmpDir);
+ : HttpUtils.downloadImage(attachment.toURL(), tmpDir).getHost();
String fname = String.format("%d-%d.%s", mid, newrid, attachmentType);
ImageUtils.saveImageWithPreviews(attachmentFName, fname, tmpDir, imgDir);
}
diff --git a/juick-server-xmpp/src/main/java/com/juick/server/XMPPConnection.java b/juick-server-xmpp/src/main/java/com/juick/server/XMPPConnection.java
index 04c3e2ab..40209fad 100644
--- a/juick-server-xmpp/src/main/java/com/juick/server/XMPPConnection.java
+++ b/juick-server-xmpp/src/main/java/com/juick/server/XMPPConnection.java
@@ -57,6 +57,7 @@ import rocks.xmpp.extensions.filetransfer.FileTransferManager;
import rocks.xmpp.extensions.nick.model.Nickname;
import rocks.xmpp.extensions.oob.model.x.OobX;
import rocks.xmpp.extensions.ping.PingManager;
+import rocks.xmpp.extensions.receipts.MessageDeliveryReceiptsManager;
import rocks.xmpp.extensions.vcard.temp.model.VCard;
import rocks.xmpp.extensions.version.SoftwareVersionManager;
import rocks.xmpp.extensions.version.model.SoftwareVersion;
@@ -178,7 +179,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
router.addInboundMessageListener(e -> {
Message message = e.getMessage();
Jid jid = message.getTo();
- if (jid.getDomain().equals(router.getDomain().toEscapedString()) || jid.equals(jid)) {
+ if (jid.getDomain().equals(router.getDomain().toEscapedString()) || jid.equals(this.jid)) {
com.juick.Message jmsg = message.getExtension(com.juick.Message.class);
if (jmsg != null) {
if (jid.getLocal().equals("recomm")) {
@@ -199,13 +200,21 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
}
}
} else {
- String attachment = StringUtils.EMPTY;
+ URI attachment = URI.create("");
OobX oobX = message.getExtension(OobX.class);
if (oobX != null) {
- attachment = oobX.getUri().toString();
+ attachment = oobX.getUri();
}
try {
- processMessage(userService.getUserByUID(NumberUtils.toInt(message.getFrom().getLocal(), 0)).orElse(new User()), message.getBody(), attachment);
+ Jid from = message.getFrom();
+ if (from.getDomain().equals("uid.juick.com")) {
+ User fromUser = userService.getUserByUID(NumberUtils.toInt(from.getLocal(),
+ 0)).orElse(new User());
+ incomingMessageJuick(fromUser, from, message.getBody(), attachment);
+ } else {
+ incomingMessageJuick(userService.getUserByJID(from.asBareJid().toEscapedString()), from,
+ message.getBody(), attachment);
+ }
} catch (Exception e1) {
logger.warn("message exception", e1);
}
@@ -220,14 +229,14 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
}
}
} else {
- s2s(ClientMessage.from(message));
+ router.send(ClientMessage.from(message));
}
});
router.addInboundIQListener(e -> {
IQ iq = e.getIQ();
Jid jid = iq.getTo();
if (!jid.getDomain().equals(this.jid.getDomain())) {
- s2s(iq);
+ router.send(iq);
}
});
FileTransferManager fileTransferManager = router.getManager(FileTransferManager.class);
@@ -249,7 +258,8 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
if (st.getStatus().equals(FileTransfer.Status.COMPLETED)) {
logger.info("transfer completed");
try {
- processMessage(userService.getUserByJID(e.getInitiator().toEscapedString()), e.getDescription(), targetFilename);
+ Jid initiator = e.getInitiator();
+ incomingMessageJuick(userService.getUserByJID(initiator.toEscapedString()), initiator, e.getDescription(), URI.create(String.format("juick://%s", targetFilename)));
} catch (Exception e1) {
logger.error("ft error", e1);
}
@@ -301,19 +311,6 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
return stanzaWriter.toString();
}
- private void s2s(Stanza stanza) {
- try {
- String xml = stanzaToString(stanza);
- logger.info("stream router (out): {}", xml);
- xmpp.sendOut(stanza);
- } catch (XMLStreamException | JAXBException e) {
- logger.error("JAXB exception", e);
- }
-
- }
-
-
-
private void sendJuickMessage(com.juick.Message jmsg) {
List<String> jids = new ArrayList<>();
@@ -353,7 +350,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
}
for (String jid : jids) {
msg.setTo(Jid.of(jid));
- s2s(ClientMessage.from(msg));
+ router.send(ClientMessage.from(msg));
}
}
@@ -385,7 +382,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
for (User user : users) {
for (String jid : userService.getJIDsbyUID(user.getUid())) {
msg.setTo(Jid.of(jid));
- s2s(ClientMessage.from(msg));
+ router.send(ClientMessage.from(msg));
}
}
}
@@ -409,7 +406,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
mm.setFrom(jid);
mm.setBody("Private message from @" + msg.getUser().getName() + ":\n" + msg.getText());
}
- s2s(ClientMessage.from(mm));
+ router.send(ClientMessage.from(mm));
});
} else if (isReply) {
sendJuickComment(msg);
@@ -427,7 +424,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
reply.setTo(jidTo);
reply.setType(Message.Type.CHAT);
reply.setBody(txt);
- s2s(ClientMessage.from(reply));
+ router.send(ClientMessage.from(reply));
}
@Override
@@ -481,7 +478,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
for (User user : users) {
for (String jid : userService.getJIDsbyUID(user.getUid())) {
msg.setTo(Jid.of(jid));
- s2s(ClientMessage.from(msg));
+ router.send(ClientMessage.from(msg));
}
}
}
@@ -493,7 +490,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
Presence p = new Presence(Jid.of(userJid));
p.setFrom(jid);
p.setPriority((byte) 10);
- s2s(ClientPresence.from(p));
+ router.send(ClientPresence.from(p));
});
}
@@ -506,7 +503,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
reply.setFrom(p.getTo().asBareJid());
reply.setTo(p.getFrom().asBareJid());
reply.setType(Presence.Type.UNSUBSCRIBE);
- s2s(ClientPresence.from(reply));
+ router.send(ClientPresence.from(reply));
} else if (p.getType().equals(Presence.Type.PROBE)) {
int uid_to = 0;
if (!toJuick) {
@@ -521,7 +518,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
if (!userService.getActiveJIDs().contains(p.getFrom().asBareJid().toEscapedString())) {
reply.setStatus("Send ON to enable notifications");
}
- s2s(ClientPresence.from(reply));
+ router.send(ClientPresence.from(reply));
} else {
Presence reply = new Presence();
reply.setFrom(p.getTo());
@@ -529,7 +526,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
reply.setType(Presence.Type.ERROR);
reply.setId(p.getId());
reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.ITEM_NOT_FOUND));
- s2s(ClientPresence.from(reply));
+ router.send(ClientPresence.from(reply));
}
} else if (p.getType().equals(Presence.Type.SUBSCRIBE)) {
boolean canSubscribe = false;
@@ -547,12 +544,12 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
reply.setFrom(p.getTo());
reply.setTo(p.getFrom());
reply.setType(Presence.Type.SUBSCRIBED);
- s2s(ClientPresence.from(reply));
+ router.send(ClientPresence.from(reply));
reply.setFrom(reply.getFrom().withResource(jid.getResource()));
reply.setPriority((byte) 10);
reply.setType(null);
- s2s(ClientPresence.from(reply));
+ router.send(ClientPresence.from(reply));
} else {
Presence reply = new Presence();
reply.setFrom(p.getTo());
@@ -560,7 +557,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
reply.setType(Presence.Type.ERROR);
reply.setId(p.getId());
reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.ITEM_NOT_FOUND));
- s2s(ClientPresence.from(reply));
+ router.send(ClientPresence.from(reply));
}
} else if (p.getType().equals(Presence.Type.UNSUBSCRIBE)) {
if (!toJuick) {
@@ -574,11 +571,11 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
reply.setFrom(p.getTo());
reply.setTo(p.getFrom());
reply.setType(Presence.Type.UNSUBSCRIBED);
- s2s(ClientPresence.from(reply));
+ router.send(ClientPresence.from(reply));
}
}
- public void incomingMessage(Message msg) {
+ public void incomingMessage(Message msg) throws Exception {
if (msg.getType() != null && msg.getType().equals(Message.Type.ERROR)) {
StanzaError error = msg.getError();
if (error != null && error.getCondition().equals(Condition.RESOURCE_CONSTRAINT)) {
@@ -596,73 +593,21 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
String username = msg.getTo().getLocal();
User user_from;
- String signuphash = StringUtils.EMPTY;
user_from = userService.getUserByJID(msg.getFrom().asBareJid().toEscapedString());
- if (user_from == null) {
- signuphash = userService.getSignUpHashByJID(msg.getFrom().asBareJid().toEscapedString());
+ URI attachmentURI = URI.create("");
+ OobX oobX = msg.getExtension(OobX.class);
+ if (oobX != null) {
+ attachmentURI = oobX.getUri();
}
-
+ incomingMessageJuick(user_from, msg.getFrom(), msg.getBody(), attachmentURI);
+ }
+ public com.juick.Message incomingMessageJuick(User user_from, Jid from, String command, URI attachment) throws Exception {
if (user_from == null) {
- Message reply = new Message();
- reply.setFrom(msg.getTo());
- reply.setTo(msg.getFrom());
- reply.setType(Message.Type.CHAT);
- reply.setBody("Для того, чтобы начать пользоваться сервисом, пожалуйста пройдите быструю регистрацию: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nЕсли у вас уже есть учетная запись на Juick, вы сможете присоединить этот JabberID к ней.\n\nTo start using Juick, please sign up: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nIf you already have an account on Juick, you will be proposed to attach this JabberID to your existing account.");
- s2s(ClientMessage.from(reply));
- return;
+ String signuphash = userService.getSignUpHashByJID(from.asBareJid().toEscapedString());
+ sendReply(from, "Для того, чтобы начать пользоваться сервисом, пожалуйста пройдите быструю регистрацию: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nЕсли у вас уже есть учетная запись на Juick, вы сможете присоединить этот JabberID к ней.\n\nTo start using Juick, please sign up: http://juick.com/signup?type=xmpp&hash=" + signuphash + "\nIf you already have an account on Juick, you will be proposed to attach this JabberID to your existing account.");
+ return null;
}
- if (username.equals(jid.getLocal())) {
- try {
- OobX oobX = msg.getExtension(OobX.class);
- if (oobX != null) {
- incomingMessageJuick(user_from, msg.getFrom(), msg.getBody().trim(), oobX.getUri());
- } else {
- incomingMessageJuick(user_from, msg.getFrom(), msg.getBody().trim(), null);
- }
- } catch (Exception e) {
- logger.info("xmpp exception", e);
- }
- return;
- }
-
- int uid_to = userService.getUIDbyName(username);
-
- if (uid_to == 0) {
- Message reply = new Message();
- reply.setFrom(msg.getTo());
- reply.setTo(msg.getFrom());
- reply.setType(Message.Type.ERROR);
- reply.setId(msg.getId());
- reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.ITEM_NOT_FOUND));
- s2s(ClientMessage.from(reply));
- return;
- }
-
- boolean success = false;
- if (!userService.isInBLAny(uid_to, user_from.getUid())) {
- success = pmQueriesService.createPM(user_from.getUid(), uid_to, msg.getBody());
- }
-
- if (success) {
- com.juick.Message jmsg = new com.juick.Message();
- jmsg.setUser(user_from);
- jmsg.setTo(userService.getUserByUID(uid_to).get());
- jmsg.setText(msg.getBody());
- applicationEventPublisher.publishEvent(new MessageEvent(this, jmsg));
- } else {
- Message reply = new Message();
- reply.setFrom(msg.getTo());
- reply.setTo(msg.getFrom());
- reply.setType(Message.Type.ERROR);
- reply.setId(msg.getId());
- reply.setError(new StanzaError(StanzaError.Type.CANCEL, Condition.NOT_ALLOWED));
- s2s(ClientMessage.from(reply));
- }
-
- return;
- }
- public com.juick.Message incomingMessageJuick(User user_from, Jid from, String command, URI attachment) throws Exception {
int commandlen = command.length();
// COMPATIBILITY
@@ -683,7 +628,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
subscriptionService.subscribeMessage(mid, user_from.getUid());
if (StringUtils.isNotEmpty(attachmentType)) {
String attachmentFName = attachment.getScheme().equals("juick") ? attachment.getHost()
- : HttpUtils.downloadImage(attachment.toURL(), tmpDir);
+ : HttpUtils.downloadImage(attachment.toURL(), tmpDir).getHost();
String fname = String.format("%d.%s", mid, attachmentType);
ImageUtils.saveImageWithPreviews(attachmentFName, fname, tmpDir, imgDir);
}
@@ -692,15 +637,6 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
return msg;
}
}
- public com.juick.Message processMessage(User visitor, String body, String attachmentName) throws Exception {
- if (StringUtils.isNotEmpty(attachmentName)) {
- URI httpUri = URI.create(attachmentName);
- if (!httpUri.isAbsolute()) {
- attachmentName = String.format("juick://%s", attachmentName);
- }
- }
- return incomingMessageJuick(visitor, Jid.of(String.valueOf(visitor.getUid()), "uid.juick.com", "perl"), body, URI.create(attachmentName));
- }
@Override
public void stanzaReceived(Stanza xmlValue) {
@@ -711,7 +647,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
}
} else if (xmlValue instanceof Message) {
Message msg = (Message) xmlValue;
- incomingMessage(msg);
+ router.send(msg);
} else if (xmlValue instanceof IQ) {
IQ iq = (IQ) xmlValue;
router.send(iq);
@@ -727,7 +663,7 @@ public class XMPPConnection implements StanzaListener, NotificationListener {
userService.getActiveJIDs().forEach(j -> {
try {
presence.setTo(Jid.of(j));
- s2s(ClientPresence.from(presence));
+ router.send(ClientPresence.from(presence));
} catch (IllegalArgumentException ex) {
logger.warn("Invalid jid: {}", j, ex);
}
diff --git a/juick-server-xmpp/src/main/java/com/juick/server/xmpp/router/XMPPRouter.java b/juick-server-xmpp/src/main/java/com/juick/server/xmpp/router/XMPPRouter.java
index e70eb168..6edecf05 100644
--- a/juick-server-xmpp/src/main/java/com/juick/server/xmpp/router/XMPPRouter.java
+++ b/juick-server-xmpp/src/main/java/com/juick/server/xmpp/router/XMPPRouter.java
@@ -10,9 +10,11 @@ import org.xmlpull.v1.XmlPullParserException;
import rocks.xmpp.addr.Jid;
import rocks.xmpp.core.stanza.model.IQ;
import rocks.xmpp.core.stanza.model.Message;
+import rocks.xmpp.core.stanza.model.Presence;
import rocks.xmpp.core.stanza.model.Stanza;
import rocks.xmpp.core.stanza.model.server.ServerIQ;
import rocks.xmpp.core.stanza.model.server.ServerMessage;
+import rocks.xmpp.core.stanza.model.server.ServerPresence;
import rocks.xmpp.util.XmppUtils;
import javax.annotation.PostConstruct;
@@ -166,7 +168,7 @@ public class XMPPRouter implements StreamHandler {
} else if (input instanceof IQ) {
sendOut(ServerIQ.from((IQ)input));
} else {
- logger.warn("unhandled stanza: {}", stanza);
+ sendOut(ServerPresence.from((Presence) input));
}
}