aboutsummaryrefslogtreecommitdiff
path: root/src/com/juick/jabber/ws/XMPPConnection.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/juick/jabber/ws/XMPPConnection.java')
-rw-r--r--src/com/juick/jabber/ws/XMPPConnection.java179
1 files changed, 65 insertions, 114 deletions
diff --git a/src/com/juick/jabber/ws/XMPPConnection.java b/src/com/juick/jabber/ws/XMPPConnection.java
index 91a8387b..24329992 100644
--- a/src/com/juick/jabber/ws/XMPPConnection.java
+++ b/src/com/juick/jabber/ws/XMPPConnection.java
@@ -1,5 +1,7 @@
package com.juick.jabber.ws;
+import com.juick.server.MessagesQueries;
+import com.juick.server.Utils;
import com.juick.xmpp.JID;
import com.juick.xmpp.Message;
import com.juick.xmpp.Stream;
@@ -13,6 +15,8 @@ import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Iterator;
/**
*
@@ -33,7 +37,7 @@ public class XMPPConnection implements Runnable, Stream.StreamListener, Message.
public void run() {
try {
Socket socket = new Socket("localhost", 5347);
- xmpp = new StreamComponent(new JID("", "ws.juick.com", ""), socket.getInputStream(), socket.getOutputStream(), xmppPassword);
+ xmpp = new StreamComponent(new JID("", "ws2.juick.com", ""), socket.getInputStream(), socket.getOutputStream(), xmppPassword);
xmpp.addChildParser(new JuickMessage());
xmpp.addListener((Stream.StreamListener) this);
xmpp.addListener((Message.MessageListener) this);
@@ -67,35 +71,24 @@ public class XMPPConnection implements Runnable, Stream.StreamListener, Message.
}
private void onJuickMessagePost(com.juick.Message jmsg) {
- String json = "{"
- + "\"mid\":" + jmsg.MID + ","
- + "\"user\":{" + "\"uid\":" + jmsg.User.UID + "," + "\"uname\":\"" + encloseJSON(jmsg.User.UName) + "\"},"
- + "\"timestamp\":\"" + jmsg.TimestampString + "\","
- + "\"body\":\"" + encloseJSON(jmsg.Text) + "\"";
- if (jmsg.Tags.size() > 0) {
- json += ",\"tags\":[";
- for (int i = 0; i < jmsg.Tags.size(); i++) {
- if (i > 0) {
- json += ",";
- }
- json += "\"" + encloseJSON((String) jmsg.Tags.get(i)) + "\"";
- }
- json += "]";
+ String json = com.juick.json.Message.toJSON(jmsg).toString();
+ ByteBuffer jsonbytes = Charset.forName("UTF-8").encode(json);
+ ByteBuffer bbMsg = ByteBuffer.allocate(10240);
+ bbMsg.put((byte) 0x81);
+ if (jsonbytes.limit() <= 125) {
+ bbMsg.put((byte) jsonbytes.limit());
+ } else {
+ bbMsg.put((byte) 126);
+ bbMsg.putShort((short) jsonbytes.limit());
}
- json += "}";
-
- ByteBuffer out = ByteBuffer.allocate(10240);
- out.put((byte) 0x00);
- out.put(Charset.forName("UTF-8").encode(json));
- out.put((byte) 0xFF);
- out.flip();
-
+ bbMsg.put(jsonbytes);
+ bbMsg.flip();
+ ArrayList<Integer> uids = new ArrayList<Integer>();
String query = "SELECT suser_id FROM subscr_users WHERE user_id=" + jmsg.User.UID + " AND suser_id NOT IN (SELECT user_id FROM bl_tags INNER JOIN messages_tags USING(tag_id) WHERE message_id=" + jmsg.MID + ")";
if (jmsg.Privacy < 0) {
query += " AND suser_id IN (SELECT wl_user_id FROM wl_users WHERE user_id=" + jmsg.User.UID + ")";
}
-
Statement stmt = null;
ResultSet rs = null;
try {
@@ -103,115 +96,73 @@ public class XMPPConnection implements Runnable, Stream.StreamListener, Message.
rs = stmt.executeQuery(query);
rs.beforeFirst();
while (rs.next()) {
- int UID = rs.getInt(1);
-
- for (int i = Main.sockMessages.size() - 1; i >= 0; i--) {
- SocketSubscribed ss = Main.sockMessages.get(i);
- if (ss.UID == UID) {
- try {
- out.rewind();
- ss.sock.write(out);
- } catch (IOException e) {
- Main.sockMessages.remove(i);
- try {
- ss.sock.close();
- } catch (IOException ex) {
- }
- }
- }
- }
-
- if (jmsg.Privacy <= 0) {
- for (int i = Main.sockAll.size() - 1; i >= 0; i--) {
- SocketSubscribed ss = Main.sockAll.get(i);
- if (ss.UID == UID) {
- try {
- out.rewind();
- ss.sock.write(out);
- } catch (IOException e) {
- Main.sockAll.remove(i);
- try {
- ss.sock.close();
- } catch (IOException ex) {
- }
- }
- }
- }
- }
-
+ uids.add(rs.getInt(1));
}
} catch (SQLException e) {
- System.err.println(e);
+ System.err.println("onJuickMessagePost: " + e);
} finally {
- if (rs != null) {
- try {
- rs.close();
- } catch (SQLException e) {
- }
- }
- if (stmt != null) {
- try {
- stmt.close();
- } catch (SQLException e) {
- }
- }
+ Utils.finishSQL(rs, stmt);
}
- // Send to all
- if (jmsg.Privacy > 0) {
- for (int i = Main.sockAll.size() - 1; i >= 0; i--) {
- SocketSubscribed ss = Main.sockAll.get(i);
- try {
- out.rewind();
- ss.sock.write(out);
- System.err.println(" --->" + ss.sock.socket().getRemoteSocketAddress());
- } catch (IOException e) {
- Main.sockAll.remove(i);
+ synchronized (Main.clients) {
+ for (Iterator<SocketSubscribed> i = Main.clients.iterator(); i.hasNext();) {
+ SocketSubscribed s = i.next();
+ if ((jmsg.Privacy >= 0 && (s.allMessages || s.UID == jmsg.User.UID)) || uids.contains(s.VUID)) {
+ bbMsg.rewind();
try {
- ss.sock.close();
- } catch (IOException ex) {
+ s.sock.write(bbMsg);
+ } catch (Exception e) {
+ try {
+ s.sock.socket().close();
+ } catch (Exception ex) {
+ }
+ try {
+ s.sock.close();
+ } catch (Exception ex) {
+ }
+ i.remove();
}
}
}
}
-
}
private void onJuickMessageReply(com.juick.Message jmsg) {
- String json = "{"
- + "\"mid\":" + jmsg.MID + ","
- + "\"rid\":" + jmsg.RID + ","
- + "\"replyto\":" + jmsg.ReplyTo + ","
- + "\"user\":{" + "\"uid\":" + jmsg.User.UID + "," + "\"uname\":\"" + encloseJSON(jmsg.User.UName) + "\"},"
- + "\"timestamp\":\"" + jmsg.TimestampString + "\","
- + "\"body\":\"" + encloseJSON(jmsg.Text) + "\""
- + "}";
+ String json = com.juick.json.Message.toJSON(jmsg).toString();
+ ByteBuffer jsonbytes = Charset.forName("UTF-8").encode(json);
+ ByteBuffer bbMsg = ByteBuffer.allocate(10240);
+ bbMsg.put((byte) 0x81);
+ if (jsonbytes.limit() <= 125) {
+ bbMsg.put((byte) jsonbytes.limit());
+ } else {
+ bbMsg.put((byte) 126);
+ bbMsg.putShort((short) jsonbytes.limit());
+ }
+ bbMsg.put(jsonbytes);
+ bbMsg.flip();
- ByteBuffer out = ByteBuffer.allocate(10240);
- out.put((byte) 0x00);
- out.put(Charset.forName("UTF-8").encode(json));
- out.put((byte) 0xFF);
- out.flip();
+ int privacy = MessagesQueries.getMessagePrivacy(sql, jmsg.MID);
- for (int i = Main.sockReplies.size() - 1; i >= 0; i--) {
- SocketSubscribed ss = Main.sockReplies.get(i);
- if (ss.MID == 0 || ss.MID == jmsg.MID) {
- try {
- out.rewind();
- ss.sock.write(out);
- System.err.println(" --->" + ss.sock.socket().getRemoteSocketAddress());
- } catch (IOException e) {
- Main.sockReplies.remove(i);
+ synchronized (Main.clients) {
+ for (Iterator<SocketSubscribed> i = Main.clients.iterator(); i.hasNext();) {
+ SocketSubscribed s = i.next();
+ if ((privacy >= 0 && s.allReplies) || s.MID == jmsg.MID) {
+ bbMsg.rewind();
try {
- ss.sock.close();
- } catch (IOException ex) {
+ s.sock.write(bbMsg);
+ } catch (Exception e) {
+ try {
+ s.sock.socket().close();
+ } catch (Exception ex) {
+ }
+ try {
+ s.sock.close();
+ } catch (Exception ex) {
+ }
+ i.remove();
}
}
}
}
}
-
- public static String encloseJSON(String str) {
- return str.replace("\"", "&quot;").replace("\\", "\\\\").replace("\n", "\\n");
- }
}