aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
m---------deps/com.juick10
m---------deps/com.juick.json10
m---------deps/com.juick.server6
-rw-r--r--src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java227
4 files changed, 237 insertions, 16 deletions
diff --git a/deps/com.juick b/deps/com.juick
-Subproject 39ec74abe77cdab5aa8f50c2524e6f71c2dbbd0
+Subproject 1df19b47f7f463e6cafa7dcf877cb42967fff16
diff --git a/deps/com.juick.json b/deps/com.juick.json
-Subproject 9dfad84fa487960fde4cc4b0ac6760c8dd5f4ac
+Subproject ee0e13e7243abecf242236c224551774195524a
diff --git a/deps/com.juick.server b/deps/com.juick.server
-Subproject a0d038f5e6aa28ef67162c591abe64d057a19fa
+Subproject 27346668c2f984c3aab66c1f2716af814305be9
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..a3604b8b
--- /dev/null
+++ b/src/main/java/com/juick/xmpp/s2s/ConnectionRouter.java
@@ -0,0 +1,227 @@
+package com.juick.xmpp.s2s;
+
+import com.juick.server.MessagesQueries;
+import com.juick.server.SubscriptionsQueries;
+import com.juick.xmpp.*;
+import com.juick.xmpp.extensions.JuickMessage;
+import com.juick.xmpp.extensions.Nickname;
+import com.juick.xmpp.extensions.XOOB;
+
+import java.io.IOException;
+import java.net.Socket;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * @author ugnich
+ */
+public class ConnectionRouter implements Stream.StreamListener,
+ Message.MessageListener, Iq.IqListener, Presence.PresenceListener {
+ private static final Logger logger = Logger.getLogger(ConnectionRouter.class.getName());
+
+ private String componentName;
+ Stream router;
+ Socket socket;
+
+ ConnectionRouter(String componentName, String password) {
+ this.componentName = componentName;
+ logger.info("STREAM ROUTER START");
+ try {
+ socket = new Socket("localhost", 5347);
+ router = new StreamComponent(new JID("s2s"), socket.getInputStream(), socket.getOutputStream(), password);
+ router.addChildParser(new JuickMessage());
+ router.addListener((Stream.StreamListener) this);
+ router.addListener((Message.MessageListener) this);
+ router.addListener((Iq.IqListener) this);
+ router.startParsing();
+ } catch (IOException e) {
+ logger.log(Level.SEVERE, "router failed", e);
+ }
+ }
+ public void closeConnection() throws IOException {
+ router.logoff();
+ socket.close();
+ }
+
+ 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 (String jid : jids) {
+ msg.to = new JID(jid);
+ XMPPComponent.sendOut(msg);
+ }
+ }
+
+ public void sendJuickComment(JuickMessage jmsg) {
+ List<String> jids;
+ String replyQuote;
+ String replyTo;
+
+ synchronized (XMPPComponent.sqlSync) {
+ jids = SubscriptionsQueries.getJIDSubscribedToComments(XMPPComponent.sql, jmsg.getMID(), jmsg.getUser().getUID());
+ com.juick.Message replyMessage = jmsg.ReplyTo > 0 ? MessagesQueries.getReply(XMPPComponent.sql, jmsg.getMID(), jmsg.ReplyTo)
+ : MessagesQueries.getMessage(XMPPComponent.sql, jmsg.getMID());
+ replyTo = replyMessage.getUser().getUName();
+ replyQuote = getReplyQuote(replyMessage);
+ }
+
+ String txt = "Reply by @" + jmsg.getUser().getUName() + ":\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 = JuickBot.JuickJID;
+ msg.body = txt;
+ msg.type = Message.Type.chat;
+ msg.addChild(jmsg);
+ for (String jid : jids) {
+ msg.to = new JID(jid);
+ XMPPComponent.sendOut(msg);
+ }
+ }
+
+ private String getReplyQuote(com.juick.Message q) {
+ String 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 (String jid : jids) {
+ msg.to = new JID(jid);
+ XMPPComponent.sendOut(msg);
+ }
+ }
+
+ @Override
+ public boolean onIq(Iq iq) {
+ JID jid = iq.to;
+ if (!jid.Host.equals(componentName)) {
+ logger.info("STREAM ROUTER (IQ): " + iq.toString());
+ XMPPComponent.sendOut(iq);
+ }
+ return false;
+ }
+
+ @Override
+ public void onMessage(Message xmsg) {
+ logger.info("STREAM ROUTER (PROCESS): " + xmsg.toString());
+ JuickMessage jmsg = (JuickMessage) xmsg.getChild(JuickMessage.XMLNS);
+ JID jid = xmsg.to;
+ if (jid.Host.equals(componentName)) {
+ 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 {
+ XMPPComponent.sendOut(xmsg);
+ }
+ }
+
+ @Override
+ public void onPresence(Presence presence) {
+ JID jid = presence.to;
+ if (!jid.Host.equals(componentName)) {
+ logger.info("STREAM ROUTER (PRESENCE): " + presence.toString());
+ XMPPComponent.sendOut(presence);
+ }
+ }
+
+ @Override
+ public void onStreamReady() {
+ logger.info("STREAM ROUTER (READY)");
+ }
+
+ @Override
+ public void onStreamFail(Exception ex) {
+ logger.log(Level.SEVERE, "STREAM ROUTER (FAIL)", ex);
+ }
+}