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
|
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(componentName), 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);
}
}
|