diff options
Diffstat (limited to 'juick-xmpp/src/main/java/com/juick/components/XMPPConnection.java')
-rw-r--r-- | juick-xmpp/src/main/java/com/juick/components/XMPPConnection.java | 124 |
1 files changed, 76 insertions, 48 deletions
diff --git a/juick-xmpp/src/main/java/com/juick/components/XMPPConnection.java b/juick-xmpp/src/main/java/com/juick/components/XMPPConnection.java index a124c461..1bfaf429 100644 --- a/juick-xmpp/src/main/java/com/juick/components/XMPPConnection.java +++ b/juick-xmpp/src/main/java/com/juick/components/XMPPConnection.java @@ -18,14 +18,19 @@ package com.juick.components; import com.juick.User; +import com.juick.components.s2s.BasicXmppSession; import com.juick.server.helpers.UserInfo; +import com.juick.service.MessagesService; +import com.juick.service.SubscriptionService; +import com.juick.service.UserService; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; -import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.core.env.Environment; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.DependsOn; +import org.springframework.stereotype.Component; import rocks.xmpp.addr.Jid; import rocks.xmpp.core.XmppException; import rocks.xmpp.core.stanza.AbstractIQHandler; @@ -41,7 +46,6 @@ 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.util.XmppUtils; @@ -61,27 +65,44 @@ import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; /** * @author ugnich */ +@Component public class XMPPConnection implements AutoCloseable { private static final Logger logger = LoggerFactory.getLogger(XMPPConnection.class); private ExternalComponent router; @Inject - private XMPPServer xmpp; + private JuickBot bot; + + @Value("${componentname:localhost}") + private String componentName; + @Value("${component_port:5347}") + private int componentPort; + @Value("${xmpp_password:secret}") + private String password; + @Value("${upload_tmp_dir:/tmp}") + private String tmpDir; + + @Inject + public MessagesService messagesService; + @Inject + public UserService userService; @Inject - private Environment env; + public SubscriptionService subscriptionService; + @Inject + private BasicXmppSession session; + @Inject + private ExecutorService service; @PostConstruct public void init() { - logger.info("stream router start"); - String componentName = env.getProperty("componentname"); - int componentPort = NumberUtils.toInt(env.getProperty("component_port"), 5347); - String password = env.getProperty("xmpp_password"); - router = ExternalComponent.create(componentName, password, xmpp.getSession().getConfiguration(), "localhost", componentPort); + logger.info("stream router start connecting to {}", componentPort); + router = ExternalComponent.create(componentName, password, session.getConfiguration(), "localhost", componentPort); PingManager pingManager = router.getManager(PingManager.class); pingManager.setEnabled(true); router.disableFeature(EntityCapabilities.NAMESPACE); @@ -98,13 +119,13 @@ public class XMPPConnection implements AutoCloseable { router.addIQHandler(VCard.class, new AbstractIQHandler(IQ.Type.GET) { @Override protected IQ processRequest(IQ iq) { - if (iq.getTo().equals(xmpp.getJid()) || iq.getTo().asBareJid().equals(xmpp.getJid().asBareJid()) - || iq.getTo().asBareJid().toEscapedString().equals(xmpp.getJid().getDomain())) { + if (iq.getTo().equals(bot.getJid()) || iq.getTo().asBareJid().equals(bot.getJid().asBareJid()) + || iq.getTo().asBareJid().toEscapedString().equals(bot.getJid().getDomain())) { return iq.createResult(vCard); } - User user = xmpp.userService.getUserByName(iq.getTo().getLocal()); + User user = userService.getUserByName(iq.getTo().getLocal()); if (user.getUid() > 0) { - UserInfo info = xmpp.userService.getUserInfo(user); + UserInfo info = userService.getUserInfo(user); VCard userVCard = new VCard(); userVCard.setFormattedName(info.getFullName()); userVCard.setNickname(user.getName()); @@ -124,7 +145,7 @@ public class XMPPConnection implements AutoCloseable { router.addInboundMessageListener(e -> { Message message = e.getMessage(); Jid jid = message.getTo(); - if (jid.getDomain().equals(Jid.of(componentName).getDomain())) { + if (jid.getDomain().equals(router.getDomain().toEscapedString())) { com.juick.Message jmsg = message.getExtension(com.juick.Message.class); if (jmsg != null) { if (jid.getLocal().equals("recomm")) { @@ -137,8 +158,8 @@ public class XMPPConnection implements AutoCloseable { } } } - } else if (jid.getDomain().endsWith(xmpp.HOSTNAME) && (jid.getDomain().equals(xmpp.HOSTNAME) - || jid.getDomain().endsWith("." + xmpp.HOSTNAME))) { + } else if (jid.getDomain().endsWith(bot.getJid().getDomain()) && (jid.getDomain().equals(bot.getJid().getDomain()) + || jid.getDomain().endsWith("." + bot.getJid().getDomain()))) { if (logger.isInfoEnabled()) { try { logger.info("unhandled message: {}", stanzaToString(message)); @@ -147,17 +168,16 @@ public class XMPPConnection implements AutoCloseable { } } } else { - route(jid.getDomain(), ClientMessage.from(message)); + route(ClientMessage.from(message)); } }); router.addInboundIQListener(e -> { IQ iq = e.getIQ(); Jid jid = iq.getTo(); - if (!jid.getDomain().equals(xmpp.HOSTNAME)) { - route(jid.getDomain(), iq); + if (!jid.getDomain().equals(bot.getJid().getDomain())) { + route(iq); } }); - String tmpDir = env.getProperty("upload_tmp_dir", "/tmp"); FileTransferManager fileTransferManager = router.getManager(FileTransferManager.class); fileTransferManager.addFileTransferOfferListener(e -> { try { @@ -179,7 +199,7 @@ public class XMPPConnection implements AutoCloseable { Message msg = new Message(); msg.setType(Message.Type.CHAT); msg.setFrom(e.getInitiator()); - msg.setTo(xmpp.getJid()); + msg.setTo(bot.getJid()); msg.setBody(e.getDescription()); try { String attachmentUrl = String.format("juick://%s", targetFilename); @@ -192,7 +212,7 @@ public class XMPPConnection implements AutoCloseable { logger.info("transfer failed", ft.getException()); Message msg = new Message(); msg.setType(Message.Type.CHAT); - msg.setFrom(xmpp.getJid()); + msg.setFrom(bot.getJid()); msg.setTo(e.getInitiator()); msg.setBody("File transfer failed, please report to us"); router.sendMessage(msg); @@ -210,11 +230,19 @@ public class XMPPConnection implements AutoCloseable { logger.error("ft error", e1); } }); - try { - router.connect(); - } catch (XmppException e) { - logger.warn("xmpp exception", e); - } + router.addConnectionListener(event -> { + if (event.getType().equals(rocks.xmpp.core.session.ConnectionEvent.Type.RECONNECTION_SUCCEEDED)) { + logger.info("component connected"); + } + }); + service.submit(() -> { + try { + Thread.sleep(3000); + router.connect(); + } catch (InterruptedException | XmppException e) { + logger.warn("xmpp exception", e); + } + }); } String stanzaToString(Stanza stanza) throws XMLStreamException, JAXBException { @@ -227,11 +255,11 @@ public class XMPPConnection implements AutoCloseable { return stanzaWriter.toString(); } - void route(String domain, Stanza stanza) { + void route(Stanza stanza) { try { String xml = stanzaToString(stanza); logger.info("stream router (out): {}", xml); - xmpp.sendOut(domain, xml); + bot.sendNotification(stanza); } catch (XMLStreamException | JAXBException e) { logger.error("JAXB exception", e); } @@ -248,16 +276,16 @@ public class XMPPConnection implements AutoCloseable { List<String> jids = new ArrayList<>(); if (jmsg.FriendsOnly) { - jids = xmpp.subscriptionService.getJIDSubscribedToUser(jmsg.getUser().getUid(), jmsg.FriendsOnly); + jids = subscriptionService.getJIDSubscribedToUser(jmsg.getUser().getUid(), jmsg.FriendsOnly); } else { - List<User> users = xmpp.subscriptionService.getSubscribedUsers(jmsg.getUser().getUid(), jmsg.getMid()); + List<User> users = subscriptionService.getSubscribedUsers(jmsg.getUser().getUid(), jmsg.getMid()); for (User user : users) { - for (String jid : xmpp.userService.getJIDsbyUID(user.getUid())) { + for (String jid : userService.getJIDsbyUID(user.getUid())) { jids.add(jid); } } } - com.juick.Message fullMsg = xmpp.messagesService.getMessage(jmsg.getMid()); + com.juick.Message fullMsg = messagesService.getMessage(jmsg.getMid()); String txt = "@" + jmsg.getUser().getName() + ":" + fullMsg.getTagsString() + "\n"; String attachment = fullMsg.getAttachmentURL(); if (attachment != null) { @@ -269,7 +297,7 @@ public class XMPPConnection implements AutoCloseable { Nickname nick = new Nickname("@" + jmsg.getUser().getName()); Message msg = new Message(); - msg.setFrom(xmpp.getJid()); + msg.setFrom(bot.getJid()); msg.setBody(txt); msg.setType(Message.Type.CHAT); msg.setThread("juick-" + jmsg.getMid()); @@ -285,7 +313,7 @@ public class XMPPConnection implements AutoCloseable { } for (String jid : jids) { msg.setTo(Jid.of(jid)); - route(msg.getTo().getDomain(), ClientMessage.from(msg)); + route(ClientMessage.from(msg)); } } @@ -294,11 +322,11 @@ public class XMPPConnection implements AutoCloseable { String replyQuote; String replyTo; - users = xmpp.subscriptionService.getUsersSubscribedToComments(jmsg.getMid(), jmsg.getUser().getUid()); - com.juick.Message replyMessage = jmsg.getReplyto() > 0 ? xmpp.messagesService.getReply(jmsg.getMid(), jmsg.getReplyto()) - : xmpp.messagesService.getMessage(jmsg.getMid()); + users = subscriptionService.getUsersSubscribedToComments(jmsg.getMid(), jmsg.getUser().getUid()); + com.juick.Message replyMessage = jmsg.getReplyto() > 0 ? messagesService.getReply(jmsg.getMid(), jmsg.getReplyto()) + : messagesService.getMessage(jmsg.getMid()); replyTo = replyMessage.getUser().getName(); - com.juick.Message fullReply = xmpp.messagesService.getReply(jmsg.getMid(), jmsg.getRid()); + com.juick.Message fullReply = messagesService.getReply(jmsg.getMid(), jmsg.getRid()); replyQuote = fullReply.getReplyQuote(); String txt = "Reply by @" + jmsg.getUser().getName() + ":\n" + replyQuote + "\n@" + replyTo + " "; @@ -309,22 +337,22 @@ public class XMPPConnection implements AutoCloseable { txt += jmsg.getText() + "\n\n" + "#" + jmsg.getMid() + "/" + jmsg.getRid() + " http://juick.com/" + jmsg.getMid() + "#" + jmsg.getRid(); Message msg = new Message(); - msg.setFrom(xmpp.getJid()); + msg.setFrom(bot.getJid()); msg.setBody(txt); msg.setType(Message.Type.CHAT); msg.addExtension(jmsg); for (User user : users) { - for (String jid : xmpp.userService.getJIDsbyUID(user.getUid())) { + for (String jid : userService.getJIDsbyUID(user.getUid())) { msg.setTo(Jid.of(jid)); - route(msg.getTo().getDomain(), ClientMessage.from(msg)); + route(ClientMessage.from(msg)); } } } public void sendJuickRecommendation(com.juick.Message recomm) { List<User> users; - com.juick.Message jmsg = xmpp.messagesService.getMessage(recomm.getMid()); - users = xmpp.subscriptionService.getUsersSubscribedToUserRecommendations(recomm.getUser().getUid(), + com.juick.Message jmsg = messagesService.getMessage(recomm.getMid()); + users = subscriptionService.getUsersSubscribedToUserRecommendations(recomm.getUser().getUid(), recomm.getMid(), jmsg.getUser().getUid()); String txt = "Recommended by @" + recomm.getUser().getName() + ":\n"; @@ -347,7 +375,7 @@ public class XMPPConnection implements AutoCloseable { Nickname nick = new Nickname("@" + jmsg.getUser().getName()); Message msg = new Message(); - msg.setFrom(xmpp.getJid()); + msg.setFrom(bot.getJid()); msg.setBody(txt); msg.setType(Message.Type.CHAT); msg.setThread("juick-" + jmsg.getMid()); @@ -363,9 +391,9 @@ public class XMPPConnection implements AutoCloseable { } for (User user : users) { - for (String jid : xmpp.userService.getJIDsbyUID(user.getUid())) { + for (String jid : userService.getJIDsbyUID(user.getUid())) { msg.setTo(Jid.of(jid)); - route(msg.getTo().getDomain(), ClientMessage.from(msg)); + route(ClientMessage.from(msg)); } } } |