1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
package com.juick.components.s2s;
import com.juick.User;
import com.juick.components.XMPPServer;
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 rocks.xmpp.addr.Jid;
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, AutoCloseable {
private static final Logger logger = LoggerFactory.getLogger(ConnectionRouter.class);
private String componentName;
private int componentPort;
private String password;
private Execution execution;
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 = 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("<stream:stream xmlns='jabber:component:accept' " +
"xmlns:stream='http://etherx.jabber.org/streams' to='%s'>", 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 = "<handshake>" + DigestUtils.sha1Hex(streamID + password) + "</handshake>";
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 = Jid.of(to);
if (jid.getDomain() != null) {
if (jid.getDomain().equals(componentName)) {
if (tag.equals("message")) {
Message xmsg = Message.parse(parser, xmpp.childParsers);
logger.info("stream router (process): {}", xmsg);
JuickMessage jmsg = (JuickMessage) xmsg.getChild(JuickMessage.XMLNS);
if (jmsg != null) {
if (jid.getLocal() != null && jid.getLocal().equals("recomm")) {
sendJuickRecommendation(jmsg);
} else {
if (jmsg.getRid() > 0) {
sendJuickComment(jmsg);
} else if (jmsg.getMid() > 0) {
sendJuickMessage(jmsg);
}
}
}
} else if (tag.equals("iq") || tag.equals("presence")) {
String xml = XmlUtils.parseToString(parser, true);
logger.info("stream router (stanza): {}", xml);
}
} else if (jid.getDomain().endsWith(xmpp.HOSTNAME) && (jid.getDomain().equals(xmpp.HOSTNAME)
|| jid.getDomain().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.getDomain(), xml);
}
} else {
String invalidStanza = XmlUtils.parseToString(parser, true);
logger.info("stream router (no to): {}", invalidStanza);
}
} else {
String unknownStanza = XmlUtils.parseToString(parser, true);
logger.info("stream router (unknown): {}", unknownStanza);
}
}
logger.warn("stream router finished");
} catch (InterruptedException ex) {
logger.info("shutting down");
execution.complete();
} catch (Exception e) {
logger.warn("router error, reconnection ", e);
execution.recordFailure(e);
}
}
});
}
public void sendJuickMessage(JuickMessage jmsg) {
List<String> jids = new ArrayList<>();
if (jmsg.FriendsOnly) {
jids = xmpp.subscriptionService.getJIDSubscribedToUser(jmsg.getUser().getUid(), jmsg.FriendsOnly);
} else {
List<User> users = xmpp.subscriptionService.getSubscribedUsers(jmsg.getUser().getUid(), jmsg.getMid());
for (User user : users) {
for (String jid : xmpp.userService.getJIDsbyUID(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.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 = Jid.of(jid);
xmpp.sendOut(msg);
}
}
public void sendJuickComment(JuickMessage jmsg) {
List<User> users;
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());
replyTo = replyMessage.getUser().getName();
com.juick.Message fullReply = xmpp.messagesService.getReply(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.getJid();
msg.body = txt;
msg.type = Message.Type.chat;
msg.addChild(jmsg);
for (User user : users) {
for (String jid : xmpp.userService.getJIDsbyUID(user.getUid())) {
msg.to = Jid.of(jid);
xmpp.sendOut(msg);
}
}
}
public void sendJuickRecommendation(JuickMessage recomm) {
List<User> users;
JuickMessage jmsg;
jmsg = new JuickMessage(xmpp.messagesService.getMessage(recomm.getMid()));
users = xmpp.subscriptionService.getUsersSubscribedToUserRecommendations(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.getReplies() > 0) {
if (jmsg.getReplies() % 10 == 1 && jmsg.getReplies() % 100 != 11) {
txt += " (" + jmsg.getReplies() + " reply)";
} else {
txt += " (" + jmsg.getReplies() + " 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.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 : xmpp.userService.getJIDsbyUID(user.getUid())) {
msg.to = Jid.of(jid);
xmpp.sendOut(msg);
}
}
}
@Override
public void close() throws Exception {
execution.complete();
}
}
|