diff options
author | 2016-01-26 20:09:27 +0300 | |
---|---|---|
committer | 2016-01-26 20:09:27 +0300 | |
commit | 75d80dc433348435c1f8e11fafb0d1deda1ca793 (patch) | |
tree | c06a67e2bcf6e10bf12f7caabcd43280df2d91ab /src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java | |
parent | bace08c34252bffd3a575ca86b2b2f420165a81b (diff) |
import s2s component
Diffstat (limited to 'src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java')
-rw-r--r-- | src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java | 256 |
1 files changed, 256 insertions, 0 deletions
diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java new file mode 100644 index 00000000..d8ce0daf --- /dev/null +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java @@ -0,0 +1,256 @@ +package com.juick.xmpp.s2s; + +import com.juick.server.MessagesQueries; +import com.juick.server.SubscriptionsQueries; +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.SHA1; +import com.juick.xmpp.utils.XmlUtils; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.Socket; +import java.util.ArrayList; +import java.util.List; + +import org.xmlpull.v1.XmlPullParser; + +/** + * + * @author ugnich + */ +public class ConnectionRouter extends Connection implements Runnable { + + @Override + public void run() { + System.out.println("STREAM ROUTER START"); + + try { + socket = new Socket("localhost", 5347); + parser.setInput(new InputStreamReader(socket.getInputStream())); + parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); + writer = new OutputStreamWriter(socket.getOutputStream()); + + String msg = "<stream:stream xmlns='jabber:component:accept' xmlns:stream='http://etherx.jabber.org/streams' to='s2s'>"; + 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 = "<handshake>" + SHA1.encode(streamID + "secret") + "</handshake>"; + writer.write(msg); + writer.flush(); + + parser.next(); + if (!parser.getName().equals("handshake")) { + throw new Exception("NO HANDSHAKE"); + } + XmlUtils.skip(parser); + System.out.println("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(XMPPComponent.COMPONENTNAME)) { + if (tag.equals("message")) { + Message xmsg = Message.parse(parser, XMPPComponent.childParsers); + System.out.println("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(XMPPComponent.HOSTNAME) && (jid.Host.equals(XMPPComponent.HOSTNAME) || jid.Host.endsWith("." + XMPPComponent.HOSTNAME))) { + String xml = XmlUtils.parseToString(parser, true); + System.out.println("STREAM ROUTER: " + xml); + } else { + String xml = XmlUtils.parseToString(parser, true); + System.out.println("STREAM ROUTER (OUT): " + xml); + XMPPComponent.sendOut(jid.Host, xml); + } + } else { + System.out.println("STREAM ROUTER (NO TO): " + XmlUtils.parseToString(parser, true)); + } + } else { + System.out.println("STREAM ROUTER: " + XmlUtils.parseToString(parser, true)); + } + } + + System.err.println("STREAM ROUTER FINISHED"); + } catch (Exception e) { + System.err.println("STREAM ROUTER PARSE ERROR: " + e.toString()); + } + System.exit(0); + } + + @Override + synchronized public void sendStanza(String xml) { + try { + writer.write(xml); + writer.flush(); + } catch (IOException e) { + System.err.println("STREAM ROUTER ERROR: " + xml); + System.err.println("STREAM ROUTER ERROR: " + e.toString()); + System.exit(0); + } + } + + public void sendJuickMessage(JuickMessage jmsg) { + List<String> jids; + + synchronized (XMPPComponent.sqlSync) { + if (jmsg.FriendsOnly) { + jids = SubscriptionsQueries.getJIDSubscribedToUser(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.FriendsOnly); + } else { + jids = SubscriptionsQueries.getJIDSubscribedToUserAndTags(XMPPComponent.sql, jmsg.getUser().getUID(), jmsg.getMID()); + } + } + + String txt = "@" + jmsg.getUser().getUName() + ":" + 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().getUName(); + + com.juick.xmpp.Message msg = new com.juick.xmpp.Message(); + msg.from = JuickBot.JuickJID; + 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 (int i = 0; i < jids.size(); i++) { + msg.to = new JID(jids.get(i)); + XMPPComponent.sendOut(msg); + } + } + + public void sendJuickComment(JuickMessage jmsg) { + List<String> jids; + String replyQuote; + + synchronized (XMPPComponent.sqlSync) { + jids = SubscriptionsQueries.getJIDSubscribedToComments(XMPPComponent.sql, jmsg.getMID(), jmsg.getUser().getUID()); + replyQuote = getReplyQuote(XMPPComponent.sql, jmsg.getMID(), jmsg.ReplyTo); + } + + String txt = "Reply by @" + jmsg.getUser().getUName() + ":\n" + replyQuote + "\n"; + 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 = JuickBot.JuickJID; + msg.body = txt; + msg.type = Message.Type.chat; + msg.addChild(jmsg); + for (int i = 0; i < jids.size(); i++) { + msg.to = new JID(jids.get(i)); + XMPPComponent.sendOut(msg); + } + } + + private String getReplyQuote(java.sql.Connection sql, int MID, int ReplyTo) { + String quote = ""; + if (ReplyTo > 0) { + com.juick.Message q = MessagesQueries.getReply(sql, MID, ReplyTo); + if (q != null) { + quote = q.getText(); + } + } else { + com.juick.Message q = MessagesQueries.getMessage(sql, MID); + if (q != null) { + quote = q.getText(); + } + } + if (quote.length() > 50) { + quote = ">" + quote.substring(0, 47).replace('\n', ' ') + "...\n"; + } else if (quote.length() > 0) { + quote = ">" + quote.replace('\n', ' ') + "\n"; + } + return quote; + } + + public void sendJuickRecommendation(JuickMessage recomm) { + List<String> jids; + JuickMessage jmsg; + synchronized (XMPPComponent.sqlSync) { + jmsg = new JuickMessage(MessagesQueries.getMessage(XMPPComponent.sql, recomm.getMID())); + jids = SubscriptionsQueries.getJIDSubscribedToUserRecommendations(XMPPComponent.sql, + recomm.getUser().getUID(), recomm.getMID(), jmsg.getUser().getUID()); + } + + String txt = "Recommended by @" + recomm.getUser().getUName() + ":\n"; + txt += "@" + jmsg.getUser().getUName() + ":" + 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().getUName(); + + com.juick.xmpp.Message msg = new com.juick.xmpp.Message(); + msg.from = JuickBot.JuickJID; + 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 (int i = 0; i < jids.size(); i++) { + msg.to = new JID(jids.get(i)); + XMPPComponent.sendOut(msg); + } + } +} |