package com.juick.components.s2s; import com.juick.User; import com.juick.components.XMPPServer; import com.juick.server.MessagesQueries; import com.juick.server.SubscriptionsQueries; import com.juick.server.UserQueries; import com.juick.xmpp.JID; import com.juick.xmpp.Message; import com.juick.xmpp.extensions.JuickMessage; import com.juick.xmpp.extensions.Nickname; import com.juick.xmpp.extensions.XOOB; import com.juick.xmpp.utils.XmlUtils; import net.jodah.failsafe.Execution; import net.jodah.failsafe.RetryPolicy; import org.apache.commons.codec.digest.DigestUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.InetSocketAddress; import java.nio.channels.AsynchronousSocketChannel; import java.nio.channels.Channels; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; /** * * @author ugnich */ public class ConnectionRouter extends Connection implements Runnable { private static final Logger logger = LoggerFactory.getLogger(ConnectionRouter.class); private String componentName; private int componentPort; private String password; public ConnectionRouter(XMPPServer s2s, String componentName, int componentPort, String password) throws Exception { super(s2s); this.componentName = componentName; this.componentPort = componentPort; this.password = password; } @Override public void run() { logger.info("STREAM ROUTER START"); @SuppressWarnings("unchecked") RetryPolicy retryPolicy = new RetryPolicy() .withBackoff(1, 30, TimeUnit.SECONDS) .withJitter(0.1) .retryOn(IOException.class, XmlPullParserException.class); Execution execution = new Execution(retryPolicy); xmpp.service.submit(() -> { while (!execution.isComplete()) { try { AsynchronousSocketChannel socket = AsynchronousSocketChannel.open(); socket.connect(new InetSocketAddress("localhost", componentPort)).get(); parser.setInput(new InputStreamReader(Channels.newInputStream(socket))); parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); writer = new OutputStreamWriter(Channels.newOutputStream(socket)); String msg = String.format("", componentName); writer.write(msg); writer.flush(); parser.next(); // stream:stream streamID = parser.getAttributeValue(null, "id"); if (streamID == null || streamID.isEmpty()) { throw new Exception("FAIL ON FIRST PACKET"); } msg = "" + DigestUtils.sha1Hex(streamID + password) + ""; writer.write(msg); writer.flush(); parser.next(); if (!parser.getName().equals("handshake")) { throw new Exception("NO HANDSHAKE"); } XmlUtils.skip(parser); logger.info("STREAM ROUTER OPEN"); while (parser.next() != XmlPullParser.END_DOCUMENT) { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } String tag = parser.getName(); String to = parser.getAttributeValue(null, "to"); if (to != null && (tag.equals("message") || tag.equals("presence") || tag.equals("iq"))) { JID jid = new JID(to); if (jid.Host != null) { if (jid.Host.equals(componentName)) { if (tag.equals("message")) { Message xmsg = Message.parse(parser, xmpp.childParsers); logger.info("STREAM ROUTER (PROCESS): " + xmsg.toString()); JuickMessage jmsg = (JuickMessage) xmsg.getChild(JuickMessage.XMLNS); if (jmsg != null) { if (jid.Username != null && jid.Username.equals("recomm")) { sendJuickRecommendation(jmsg); } else { if (jmsg.getRid() > 0) { sendJuickComment(jmsg); } else if (jmsg.getMid() > 0) { sendJuickMessage(jmsg); } } } } } else if (jid.Host.endsWith(xmpp.HOSTNAME) && (jid.Host.equals(xmpp.HOSTNAME) || jid.Host.endsWith("." + xmpp.HOSTNAME))) { String xml = XmlUtils.parseToString(parser, true); logger.info("STREAM ROUTER: " + xml); } else { String xml = XmlUtils.parseToString(parser, true); logger.info("STREAM ROUTER (OUT): " + xml); xmpp.sendOut(jid.Host, xml); } } else { logger.info("STREAM ROUTER (NO TO): " + XmlUtils.parseToString(parser, true)); } } else { logger.info("STREAM ROUTER: " + XmlUtils.parseToString(parser, true)); } } logger.warn("STREAM ROUTER FINISHED"); } catch (Exception e) { logger.warn("router error, reconnection " + e.toString()); } } }); } public void sendJuickMessage(JuickMessage jmsg) { List jids = new ArrayList<>(); if (jmsg.FriendsOnly) { jids = SubscriptionsQueries.getJIDSubscribedToUser(xmpp.jdbc, jmsg.getUser().getUid(), jmsg.FriendsOnly); } else { List users = SubscriptionsQueries.getSubscribedUsers(xmpp.jdbc, jmsg.getUser().getUid(), jmsg.getMid()); for (User user : users) { for (String jid : UserQueries.getJIDsbyUID(xmpp.jdbc, user.getUid())) { jids.add(jid); } } } String txt = "@" + jmsg.getUser().getName() + ":" + jmsg.getTagsString() + "\n"; String attachment = jmsg.getAttachmentURL(); if (attachment != null) { txt += attachment + "\n"; } txt += jmsg.getText() + "\n\n"; txt += "#" + jmsg.getMid() + " http://juick.com/" + jmsg.getMid(); Nickname nick = new Nickname(); nick.Nickname = "@" + jmsg.getUser().getName(); com.juick.xmpp.Message msg = new com.juick.xmpp.Message(); msg.from = xmpp.bot.getJid(); msg.body = txt; msg.type = Message.Type.chat; msg.thread = "juick-" + jmsg.getMid(); msg.addChild(jmsg); msg.addChild(nick); if (attachment != null) { XOOB oob = new XOOB(); oob.URL = attachment; msg.addChild(oob); } for (String jid : jids) { msg.to = new JID(jid); xmpp.sendOut(msg); } } public void sendJuickComment(JuickMessage jmsg) { List users; String replyQuote; String replyTo; users = SubscriptionsQueries.getUsersSubscribedToComments(xmpp.jdbc, jmsg.getMid(), jmsg.getUser().getUid()); com.juick.Message replyMessage = jmsg.getReplyto() > 0 ? MessagesQueries.getReply(xmpp.jdbc, jmsg.getMid(), jmsg.getReplyto()) : MessagesQueries.getMessage(xmpp.jdbc, jmsg.getMid()); replyTo = replyMessage.getUser().getName(); com.juick.Message fullReply = MessagesQueries.getReply(xmpp.jdbc, jmsg.getMid(), jmsg.getRid()); replyQuote = fullReply.getReplyQuote(); String txt = "Reply by @" + jmsg.getUser().getName() + ":\n" + replyQuote + "\n@" + replyTo + " "; String attachment = jmsg.getAttachmentURL(); if (attachment != null) { txt += attachment + "\n"; } txt += jmsg.getText() + "\n\n" + "#" + jmsg.getMid() + "/" + jmsg.getRid() + " http://juick.com/" + jmsg.getMid() + "#" + jmsg.getRid(); com.juick.xmpp.Message msg = new com.juick.xmpp.Message(); msg.from = xmpp.bot.getJid(); msg.body = txt; msg.type = Message.Type.chat; msg.addChild(jmsg); for (User user : users) { for (String jid : UserQueries.getJIDsbyUID(xmpp.jdbc, user.getUid())) { msg.to = new JID(jid); xmpp.sendOut(msg); } } } public void sendJuickRecommendation(JuickMessage recomm) { List users; JuickMessage jmsg; jmsg = new JuickMessage(MessagesQueries.getMessage(xmpp.jdbc, recomm.getMid())); users = SubscriptionsQueries.getUsersSubscribedToUserRecommendations(xmpp.jdbc, recomm.getUser().getUid(), recomm.getMid(), jmsg.getUser().getUid()); String txt = "Recommended by @" + recomm.getUser().getName() + ":\n"; txt += "@" + jmsg.getUser().getName() + ":" + jmsg.getTagsString() + "\n"; String attachment = jmsg.getAttachmentURL(); if (attachment != null) { txt += attachment + "\n"; } txt += jmsg.getText() + "\n\n"; txt += "#" + jmsg.getMid(); if (jmsg.Replies > 0) { if (jmsg.Replies % 10 == 1 && jmsg.Replies % 100 != 11) { txt += " (" + jmsg.Replies + " reply)"; } else { txt += " (" + jmsg.Replies + " replies)"; } } txt += " http://juick.com/" + jmsg.getMid(); Nickname nick = new Nickname(); nick.Nickname = "@" + jmsg.getUser().getName(); com.juick.xmpp.Message msg = new com.juick.xmpp.Message(); msg.from = xmpp.bot.getJid(); msg.body = txt; msg.type = Message.Type.chat; msg.thread = "juick-" + jmsg.getMid(); msg.addChild(jmsg); msg.addChild(nick); if (attachment != null) { XOOB oob = new XOOB(); oob.URL = attachment; msg.addChild(oob); } for (User user : users) { for (String jid : UserQueries.getJIDsbyUID(xmpp.jdbc, user.getUid())) { msg.to = new JID(jid); xmpp.sendOut(msg); } } } }